Skip to content

Instantly share code, notes, and snippets.

@YannickFricke
Created May 24, 2024 14:20
Show Gist options
  • Save YannickFricke/f9d964ca84e7f71a5e1b6212563d5516 to your computer and use it in GitHub Desktop.
Save YannickFricke/f9d964ca84e7f71a5e1b6212563d5516 to your computer and use it in GitHub Desktop.
Light2D flickering effect
extends Light2D
@export
var one_cycle_in_milliseconds = 1500
@export var min_alpha_number = 0
@export var max_alpha_number = 1
var intermediate_value = 0
func _process(delta: float):
var delta_in_milliseconds = delta * 1000
intermediate_value += delta_in_milliseconds
if intermediate_value >= one_cycle_in_milliseconds:
intermediate_value -= one_cycle_in_milliseconds
var percent_of_current_run = 100.0 / one_cycle_in_milliseconds * intermediate_value
var alpha_value = _get_alpha_value(percent_of_current_run)
self.color = Color(self.color.r, self.color.g, self.color.b, alpha_value)
func _get_alpha_value(percent):
if percent >= 0 and percent < 10.1:
return min_alpha_number
if percent >= 10.2 and percent < 20.1:
return min_alpha_number
if percent >= 20.6 and percent < 30.1:
return min_alpha_number
if percent >= 30.6 and percent < 45.1:
return min_alpha_number
if percent >= 55.1 and percent < 57.1:
return min_alpha_number
if percent >= 60.1 and percent < 65.1:
return min_alpha_number
if percent >= 75.1 and percent < 77.1:
return min_alpha_number
if percent >= 85.1 and percent < 86.1:
return min_alpha_number
else:
return max_alpha_number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment