Brick.gd 926 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. class_name Brick
  2. extends StaticBody2D
  3. signal kace_broken
  4. signal kace_damaged
  5. signal life_changed
  6. signal impact
  7. const MAX_LIFE := 1999
  8. const Explosion = preload("res://effect/particle/explosion/Explosion.tscn")
  9. const EXPLOSION_OFFSET := Vector2(10, 5)
  10. var life: int
  11. func setup(x: int, y: int, max_life: int) -> void:
  12. position.x = x * 40
  13. position.y = y * 20
  14. life = randi() % max_life + 1
  15. life = min(life, MAX_LIFE)
  16. emit_signal("life_changed", life)
  17. func damage() -> void:
  18. life = life - 1
  19. emit_signal("kace_damaged")
  20. if life < 1:
  21. blow_up()
  22. else:
  23. emit_signal("life_changed", life)
  24. func blow_up() -> void:
  25. emit_signal("kace_broken")
  26. var explosion = Explosion.instance()
  27. explosion.position = to_global(EXPLOSION_OFFSET)
  28. get_parent().get_parent().add_child(explosion)
  29. queue_free()
  30. func impact(pos: Vector2) -> void:
  31. emit_signal("impact", to_local(pos))
  32. func bounce(pos: Vector2) -> void:
  33. damage()