diff options
Diffstat (limited to 'scripts/tower_manager.gd')
-rw-r--r-- | scripts/tower_manager.gd | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/scripts/tower_manager.gd b/scripts/tower_manager.gd new file mode 100644 index 0000000..250b66b --- /dev/null +++ b/scripts/tower_manager.gd @@ -0,0 +1,30 @@ +extends Node + +@onready var _tiles : TileMapLayer = get_node("../Map/TileMapLayer") +var _towers : Dictionary + +func _ready(): + _towers = {} + +func place_tower(tower : Dictionary, tile_position : Vector2i): + var tower_cost = tower["cost"] + if is_valid_placement_tile(tile_position) && GameData.get_life() >= tower_cost: + var tmp = tower["scene"].instantiate() + var path = get_tree().get_root().get_node("Main/Defenders") + path.add_child(tmp) + tmp.global_position = tile_position + occupy_tile(tile_position) + GameData.subtract_life(tower_cost) + +func is_valid_placement_tile(tile_position : Vector2i): + var tile_coords = _tiles.get_cell_atlas_coords(tile_position) + return (tile_coords == (Vector2i(-1, -1))) && (!is_tile_occupied(tile_position)) + +func is_tile_occupied(tile_position : Vector2i): + return _towers.has(tile_position) + +func occupy_tile(tile_position): + _towers[tile_position] = true + +func free_tile(tile_position): + _towers.erase(tile_position) |