summaryrefslogtreecommitdiff
path: root/scripts/tower_manager.gd
blob: 9682dd98b92a3252ee845d10f0c40287df3bbdb8 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
extends Node

var game_stats_config = preload("res://resources/game_stats_config.tres")
@onready var _tiles : TileMapLayer = get_node("../Map/TileMapLayer")
var _towers : Dictionary

var _tower_actions : Array[String] = [
	"tower_action_one",
	"tower_action_two",
	"tower_action_three",
	"tower_action_four"
]

var _tower_data : Dictionary[String, Dictionary] = {
	"tower_action_one": {
		"scene": preload("res://scenes/warlock.tscn"),
		"cost": game_stats_config.defenders["warlock"]["cost"],
	},
	"tower_action_two": {
		"scene": preload("res://scenes/wyvern.tscn"),
		"cost": game_stats_config.defenders["wyvern"]["cost"],
	},
	# TODO -- space reserved for last two towers
	"tower_action_three": {
		"scene": preload("res://scenes/warlock.tscn"),
		"cost": game_stats_config.defenders["warlock"]["cost"],
	},
	"tower_action_four": {
		"scene": preload("res://scenes/warlock.tscn"),
		"cost": game_stats_config.defenders["warlock"]["cost"],
	},
}

func _ready():
	_towers = {}

func handle_tower_key(event, tile_position : Vector2i):
	for action in _tower_actions:
		if event.is_action_pressed(action):
			if is_tile_occupied(tile_position):
				_handle_upgrade(action, _towers[tile_position])
			elif _is_valid_placement_tile(tile_position):
				_handle_place_tower(_tower_data[action], tile_position)

func _handle_upgrade(action : String, tower : StaticBody2D):
	var tower_key : String = tower.get_tower_key()
	var upgrades = game_stats_config.defenders[tower_key]["upgrades"]

	print("foo: ", GameData.get_life(), " ", action, " ", tower_key, " ", upgrades)
	if upgrades.has(action):
		var upgrade_data = upgrades[action]
		var upgrade_cost = upgrade_data["cost"]
		
		if GameData.get_life() >= upgrade_cost:
			GameData.subtract_life(upgrade_cost)
			tower.apply_upgrade(upgrade_data["effects"])

func _handle_place_tower(tower : Dictionary, tile_position : Vector2i):
	var tower_cost = tower["cost"]
	if GameData.get_life() >= tower_cost:
		GameData.subtract_life(tower_cost)
		var tmp = tower["scene"].instantiate()
		add_child(tmp)
		tmp.global_position = tile_position
		_occupy_tile(tile_position, tmp)

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, tower):
	_towers[tile_position] = tower

func _free_tile(tile_position):
	_towers.erase(tile_position)