summaryrefslogtreecommitdiff
path: root/scripts/player.gd
blob: b26beaca952a9450050d2f849ce906b9832353d1 (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
extends Node2D

var game_stats_config = preload("res://resources/game_stats_config.tres")
@onready var map_config = preload("res://resources/map_generator_resource.tres")
@onready var _tm = get_node("../Defenders")

var _max_x : int
var _max_y : int

var dirs = {
	"move_right": Vector2.RIGHT,
	"move_left": Vector2.LEFT,
	"move_up": Vector2.UP,
	"move_down": Vector2.DOWN
}

var tower_ids = {
	"tower_action_one": game_stats_config.defenders_index.WARLOCK,
	"tower_action_two": game_stats_config.defenders_index.WYVERN,
	"tower_action_three": game_stats_config.defenders_index.WARLOCK,
	"tower_action_four": game_stats_config.defenders_index.WARLOCK,
}

signal curr_tile(tile_state)

func _ready():
	GameData.reset()
	_max_x = map_config.grid_width * map_config.tile_size
	_max_y = map_config.grid_height * map_config.tile_size
	position = position.snapped(Vector2.ONE * map_config.tile_size)
	position += Vector2.ONE * map_config.tile_size / 2

func _unhandled_input(event):
	for dir in dirs.keys():
		if event.is_action_pressed(dir):
			handle_move(dirs[dir])
			return
	for id in tower_ids.keys():
		if event.is_action_pressed(id):
			_tm.handle_tower_key(tower_ids[id], global_position)
	emit_signal("curr_tile", _get_tile_state())

func handle_move(dir):
	position += dir * map_config.tile_size
	position.x = max(position.x, map_config.tile_size / 2)
	position.y = max(position.y, map_config.tile_size / 2)
	position.x = min(position.x, _max_x - (map_config.tile_size / 2))
	position.y = min(position.y, _max_y - (map_config.tile_size / 2))

# this method of getting uniform menu for the panel
# was specifically designed that way in game stats config
# it may be inefficient and is somewhat messy
func _get_tile_state():
	var tile_info = null
	if _tm.is_tile_occupied(position):
		tile_info = _tm.get_upgrades_at(position)
	elif _tm.is_valid_placement_tile(position):
		tile_info = game_stats_config.defenders
	return tile_info