From 8c5c4863aeacb4afcf70f339df6d601e2df1a7a6 Mon Sep 17 00:00:00 2001 From: bd Date: Thu, 22 May 2025 13:50:49 -0400 Subject: Reorganized files into scenes and scripts --- scripts/cursor.gd | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripts/cursor.gd (limited to 'scripts/cursor.gd') diff --git a/scripts/cursor.gd b/scripts/cursor.gd new file mode 100644 index 0000000..025679b --- /dev/null +++ b/scripts/cursor.gd @@ -0,0 +1,61 @@ +extends Node2D + +@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, + "move_up": Vector2.UP, + "move_down": Vector2.DOWN} +var towers : Dictionary = { + "place_warlock": { + "scene": preload("res://scenes/warlock.tscn"), + "cost": 10 + }, + "place_wyvern": { + "scene": preload("res://scenes/wyvern.tscn"), + "cost": 15 + } +} + +func _ready(): + tile_size = grid_manager.tile_size + max_x = 20 * tile_size + max_y = 15 * tile_size + position = position.snapped(Vector2.ONE * tile_size) + position += Vector2(max_x / 2, max_y / 2) + Vector2(1, 0) * 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 + grid_manager.occupy_tile(position) + GameData.subtract_life(tower_cost) + +func handle_move(dir): + position += dir * tile_size + position.x = max(position.x, tile_size / 2) + position.y = max(position.y, tile_size + 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)) -- cgit v1.2.3