LifeContainer.gd 773 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. extends Node2D
  2. var living_cells = {}
  3. var default_font
  4. const print_offset = Vector2(1.0, 15.0)
  5. func spawn_brick(x, y, maximum_life):
  6. living_cells[Vector2(x, y)] = maximum_life
  7. update()
  8. func may_die_by_taking_this(pos, damage):
  9. var has_died = false
  10. if damage > 0 and living_cells.has(pos):
  11. living_cells[pos] -= damage
  12. update()
  13. has_died = living_cells[pos] <= 0
  14. if has_died:
  15. living_cells.erase(pos)
  16. return has_died
  17. func create_font():
  18. var label = Label.new()
  19. default_font = label.get_font("font")
  20. func _ready():
  21. create_font()
  22. func _draw():
  23. for p in living_cells:
  24. draw_rect(Rect2(p * Vector2(40, 20) + Vector2(2, 2), Vector2(36, 16)), Color.red, true)
  25. draw_string(default_font, p * Vector2(40, 20) + print_offset, str(living_cells[p]))