smashball/scenes/tilemaps/block_tile_layer.gd

34 lines
907 B
GDScript

class_name BlockTileLayer extends TileMapLayer
signal block_hit(tile_pos: Vector2i, hits_left: int)
signal block_destroyed(tile_pos: Vector2i)
## Atlas in which the destructable blocks are stored.
@export var max_hits: int = 4
var hit_counts: Dictionary[Vector2i, int] = {}
## Returns true if block is destroyed.
func hit_tile(coords: Vector2i) -> bool:
var source_id: int = get_cell_source_id(coords)
var current_atlas: Vector2i = get_cell_atlas_coords(coords)
var row: int = current_atlas.y
var hits: int = hit_counts.get(coords, 0) + 1
if hits >= max_hits:
# destroy
set_cell(coords, -1)
hit_counts.erase(coords)
block_destroyed.emit(coords)
return true
else:
# damage
var new_atlas: Vector2i = Vector2i(hits, row) # column=hits, row=same color
set_cell(coords, source_id, new_atlas)
hit_counts[coords] = hits
block_hit.emit(coords, max_hits - hits)
return false