A game created for the Godot Wild Jam #21
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.2KB

  1. tool
  2. extends Sprite
  3. const TRANS = Tween.TRANS_LINEAR
  4. const EASE = Tween.EASE_IN_OUT
  5. export var pulse_color:Color setget _set_pulse_color, _get_pulse_color
  6. export var pulse_in_time:float = 0.5
  7. export var pulse_out_time:float = 0.5
  8. export var pulse_in:float = 0.1
  9. export var pulse_out:float = 0.9
  10. export var pulse_offset:float = 0.1 setget _set_pulse_offset, _get_pulse_offset
  11. var _running = false
  12. func _camp(v, o_min, o_max):
  13. # Reduces v (clamped to 0 and 1) to the values between o_min and o_max
  14. return o_min + ((o_max - o_min) * clamp(v, 0.0, 1.0))
  15. func _ramp(v, f_min, f_max):
  16. # Expands v, spreading the values between f_min and f_max to be between 0 and 1.
  17. if f_min == f_max:
  18. return f_min
  19. return clamp((v - f_min) / (f_max - f_min), 0.0, 1.0)
  20. func _set_pulse_offset(v):
  21. material.set_shader_param("fade_offset", _camp(v, 0.1, 0.5))
  22. func _get_pulse_offset():
  23. return _ramp(material.get_shader_param("fade_offset"), 0.1, 0.5)
  24. func _set_pulse_color(c):
  25. material.set_shader_param("primary_color", c)
  26. func _get_pulse_color():
  27. return material.get_shader_param("primary_color")
  28. func _ready():
  29. material.set_shader_param("primary_color", pulse_color)
  30. func start():
  31. _running = true
  32. _on_PulseOut_timeout()
  33. func stop():
  34. $PulseOut.stop()
  35. $PulseIn.stop()
  36. _running = false
  37. func _interpolate_to(v, t):
  38. $Pulse.interpolate_property(self, "pulse_offset", self.pulse_offset, v, t, TRANS, EASE)
  39. $Pulse.start()
  40. func _on_PulseIn_timeout():
  41. if _running:
  42. #print(self.get_name(), " Pulse Out Time: ", $PulseOut.wait_time)
  43. $PulseIn.stop()
  44. if _get_pulse_offset() == pulse_in:
  45. $PulseOut.wait_time = pulse_out_time
  46. else:
  47. $PulseOut.wait_time = (1.0 - ((_get_pulse_offset() - pulse_in) / (pulse_out / pulse_in))) * pulse_out_time
  48. _interpolate_to(pulse_out, $PulseOut.wait_time)
  49. $PulseOut.start()
  50. func _on_PulseOut_timeout():
  51. if _running:
  52. #print(self.get_name(), " Pulse In Time: ", $PulseOut.wait_time)
  53. $PulseOut.stop()
  54. if _get_pulse_offset() == pulse_out:
  55. $PulseIn.wait_time = pulse_in_time
  56. else:
  57. # Adjust time due to current offset.
  58. $PulseIn.wait_time = (_get_pulse_offset() - pulse_in) / (pulse_out / pulse_in) * pulse_in_time
  59. _interpolate_to(pulse_in, $PulseIn.wait_time)
  60. $PulseIn.start()