summaryrefslogtreecommitdiff
path: root/scripts/tower_manager.gd
blob: 250b66b57fcd674bbbc153e6aa44e4a071691151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)