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.

79 lines
2.2KB

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