blob: d12f9e0c932778220fb3eb939ee2e835814e0c12 (
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
|
extends Node2D
@onready var tilemap = get_node("../Map/TileMapLayer")
var game_stats_config = preload("res://resources/game_stats_config.tres")
@onready var map_config = preload("res://resources/map_generator_resource.tres")
var occupied_tiles = {}
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 towers : Dictionary[String, Dictionary] = {
"place_warlock": {
"scene": preload("res://scenes/warlock.tscn"),
"cost": game_stats_config.warlock_defender_cost
},
"place_wyvern": {
"scene": preload("res://scenes/wyvern.tscn"),
"cost": game_stats_config.wyvern_defender_cost
}
}
func _ready():
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 tower in towers.keys():
if event.is_action_pressed(tower):
handle_tower(towers[tower])
return
func handle_tower(tower):
var tower_cost = tower["cost"]
if is_valid_placement_tile() && 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 = position
occupy_tile(position)
GameData.subtract_life(tower_cost)
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))
func is_valid_placement_tile():
var tile_coords = tilemap.get_cell_atlas_coords(tilemap.local_to_map(global_position))
return (tile_coords == (Vector2i(-1, -1))) && (!is_tile_occupied(position))
func is_tile_occupied(tile_position):
return occupied_tiles.has(tile_position)
func occupy_tile(tile_position):
occupied_tiles[tile_position] = true
func free_tile(tile_position):
occupied_tiles.erase(tile_position)
|