Skip to content

Instantly share code, notes, and snippets.

@profan
Last active September 7, 2018 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save profan/0729b518e02fc0cde644e65441b17d98 to your computer and use it in GitHub Desktop.
Save profan/0729b518e02fc0cde644e65441b17d98 to your computer and use it in GitHub Desktop.
flickering rectangle thing
extends Node2D
export(float) var width
export(float) var height
var glow_speed = 0.00125 # per second
export(float) var glow_min_target = 0.425
export(float) var glow_max_target = 0.515
export(float) var glow_mul = 0.05
export(float) var flicker_min = 0.5
export(float) var flicker_max = 2.5
var flicker_timer = 0.0
var flicker_next = 1.0
var cur_flickering
var flicker_steps_min = 1
var flicker_steps_max = 5
var flicker_step_min = 0.1
var flicker_step_max = 0.3
var cur_colour
func _ready():
randomize()
func _draw():
draw_rect(Rect2(0, 0, width, height), modulate, true)
func _single_flicker():
self_modulate.r += 0.75
self_modulate.g += 0.75
self_modulate.b += 0.75
func _flicker():
var num_flickers = int(rand_range(flicker_steps_min, flicker_steps_max))
for i in num_flickers:
var flick_wait = rand_range(flicker_step_min, flicker_step_max)
while flick_wait >= 0.0:
var time_passed = yield()
# printt(flick_wait, time_passed)
flick_wait -= time_passed
_single_flicker()
cur_flickering = null
func _process(delta):
if !get_parent().visible: set_process(false)
self_modulate.r = glow_min_target + ((sin(OS.get_ticks_msec() * glow_speed) + 0.5) * glow_mul)
self_modulate.g = glow_min_target + ((sin(OS.get_ticks_msec() * glow_speed) + 0.5) * glow_mul)
self_modulate.b = glow_min_target + ((sin(OS.get_ticks_msec() * glow_speed) + 0.5) * glow_mul)
if cur_flickering != null and cur_flickering.is_valid():
cur_flickering = cur_flickering.resume(delta)
flicker_timer += delta
if flicker_timer >= flicker_next:
if cur_flickering == null: cur_flickering = _flicker()
flicker_next = rand_range(flicker_min, flicker_max)
flicker_timer = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment