summaryrefslogtreecommitdiff
path: root/scenes/UI/cursor.gd
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-05-20 18:56:55 -0400
committerbd <bdunahu@operationnull.com>2025-05-20 18:56:55 -0400
commitffaf4b8a5ce3cbe1654760ee331a3d63f6bd3b04 (patch)
treea001c05caf8c7ed64d6c94bc5e5ea21e7f422589 /scenes/UI/cursor.gd
parent2e1a04a94ee7e132ac2b4334e37978bb2a271c21 (diff)
Enforce placing towers only on empty tiles
Diffstat (limited to 'scenes/UI/cursor.gd')
-rw-r--r--scenes/UI/cursor.gd29
1 files changed, 21 insertions, 8 deletions
diff --git a/scenes/UI/cursor.gd b/scenes/UI/cursor.gd
index 158a2a4..7f9a7ae 100644
--- a/scenes/UI/cursor.gd
+++ b/scenes/UI/cursor.gd
@@ -1,8 +1,11 @@
extends Node2D
-var tile_size = 16.0
-var max_x = 20 * tile_size
-var max_y = 15 * tile_size
+@onready var grid_manager = get_node("../GridManager")
+@onready var tilemap = get_node("../L1")
+
+var tile_size : float
+var max_x : float
+var max_y : float
var dirs = {"move_right": Vector2.RIGHT,
"move_left": Vector2.LEFT,
@@ -11,9 +14,13 @@ var dirs = {"move_right": Vector2.RIGHT,
var towers : Dictionary
func _ready():
+ tile_size = grid_manager.tile_size
+ max_x = 20 * tile_size
+ max_y = 15 * tile_size
+
towers["place_warlock"] = preload("res://scenes/defenders/warlock.tscn")
position = position.snapped(Vector2.ONE * tile_size)
- position += Vector2.ONE * tile_size/2
+ position += Vector2.ONE * tile_size / 2
func _unhandled_input(event):
for dir in dirs.keys():
@@ -26,10 +33,12 @@ func _unhandled_input(event):
return
func handle_tower(tower):
- var tmp = tower.instantiate()
- var path = get_tree().get_root().get_node("Main/Defenders")
- path.add_child(tmp)
- tmp.global_position = position
+ if is_valid_placement_tile():
+ var tmp = tower.instantiate()
+ var path = get_tree().get_root().get_node("Main/Defenders")
+ path.add_child(tmp)
+ tmp.global_position = position
+ grid_manager.occupy_tile(position)
func handle_move(dir):
position += dir * tile_size
@@ -37,3 +46,7 @@ func handle_move(dir):
position.y = max(position.y, tile_size / 2)
position.x = min(position.x, max_x - (tile_size / 2))
position.y = min(position.y, max_y - (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))) && (!grid_manager.is_tile_occupied(position))