1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- extends Node2D
- var living_cells = {}
- var default_font
- const label_offset = Vector2(5, 15)
- const cell_size = Vector2(40, 20)
- const rect_pad = Vector2(3, 3)
- func spawn_brick(x, y, maximum_life):
- living_cells[Vector2(x, y)] = randi() % maximum_life + 1
- update()
- func may_die_by_taking_this(pos, damage):
- var has_died = false
- if damage > 0 and living_cells.has(pos):
- living_cells[pos] -= damage
- update()
- has_died = living_cells[pos] <= 0
- if has_died:
- living_cells.erase(pos)
- return has_died
- func create_font():
- var label = Label.new()
- default_font = label.get_font("font")
- func color_from_life(n):
- var x = float(n % 256) / 255
- if (n < 256):
- return Color(x, 1.0, 1.0 - x)
- elif (n < 512):
- return Color(1.0, 1.0 - x, x)
- elif (n < 768):
- return Color(1.0, 0.0, 1.0 - x)
- else:
- return Color(1.0, 0.0, 0.0)
- func _ready():
- create_font()
- func _draw():
- var amount
- var pos
- var color
- for cell in living_cells:
- pos = cell * cell_size
- amount = living_cells[cell]
- color = color_from_life(amount)
- draw_rect(Rect2(pos + rect_pad, cell_size - 2 * rect_pad), color, true)
- draw_string(default_font, pos + label_offset, str(amount))
|