Brick.gd 795 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. class_name Brick
  2. extends StaticBody2D
  3. signal kace_broken
  4. signal kace_damaged
  5. var life: int
  6. func setup(x: int, y: int, max_life: int):
  7. position.x = x * 40
  8. position.y = y * 20
  9. life = randi() % max_life + 1
  10. _refresh_look()
  11. func damage():
  12. life = life - 1
  13. emit_signal("kace_damaged")
  14. if life < 1:
  15. blow_up()
  16. else:
  17. _refresh_look()
  18. func blow_up():
  19. emit_signal("kace_broken")
  20. queue_free()
  21. func impact(pos: Vector2):
  22. damage()
  23. static func color_from_life(n: int) -> Color:
  24. var x = float(n % 256) / 255
  25. if n < 256:
  26. return Color(x, 1.0, 1.0 - x)
  27. elif n < 512:
  28. return Color(1.0, 1.0 - x, x)
  29. elif n < 768:
  30. return Color(1.0, 0.0, 1.0 - x)
  31. else:
  32. return Color(1.0, 0.0, 0.0)
  33. func _refresh_look() -> void:
  34. modulate = color_from_life(life)
  35. update() # For custom drawing