From 450e2b686e9b5343826f6f1e8679de4706735b8a Mon Sep 17 00:00:00 2001 From: Moe Poi ~ Date: Mon, 9 Oct 2023 08:01:58 +0700 Subject: [PATCH] Fix error to rebuild the tower --- scripts/stages/1/game_manager.gd | 10 +++++++--- scripts/towers/tower.gd | 7 ++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/scripts/stages/1/game_manager.gd b/scripts/stages/1/game_manager.gd index 19e8b2c..ad2d340 100644 --- a/scripts/stages/1/game_manager.gd +++ b/scripts/stages/1/game_manager.gd @@ -19,7 +19,7 @@ func _ready(): $base.connect("enemy_entered_base", on_enemy_entered_base) var placements = $Placements.get_children() for placement in placements: - placement.connect("on_placement", on_game_placement) + placement.connect("on_placement", on_tower_placement) $CanvasLayer/TowerMenu.connect("build_tower", on_build_tower) func on_game_paused(): @@ -32,7 +32,7 @@ func on_game_resume(): get_tree().paused = data['paused'] $CanvasLayer/PauseMenu.hide_menu() -func on_game_placement(pos, tower_placement_id): +func on_tower_placement(pos, tower_placement_id): var tower_id = null var disabled = false var disabled_2 = false @@ -68,15 +68,19 @@ func on_build_tower(pos, tower_id, tower_placement_id): var tower = config.tower[tower_id]['asset'].instantiate() get_tree().current_scene.add_child(tower) tower.set_config(config_path) - tower.set_tower(tower_id) + tower.set_tower(tower_id, tower_placement_id) tower.set_timer() tower.global_position = pos + tower.connect("on_destroy", on_tower_destroy) data['tower'][str(tower_placement_id)]['id'] = tower_id data['tower'][str(tower_placement_id)]['name'] = tower.name data['resource'] -= config.menu[tower_id]['price'] $CanvasLayer/GameStats.set_resource(data['resource']) +func on_tower_destroy(tower_placement_id): + data['tower'].erase(str(tower_placement_id)) + func on_enemy_entered_base(enemy): data['health'] -= enemy.damage $CanvasLayer/GameStats.set_damage(enemy.damage) diff --git a/scripts/towers/tower.gd b/scripts/towers/tower.gd index c6ef356..7578411 100644 --- a/scripts/towers/tower.gd +++ b/scripts/towers/tower.gd @@ -1,5 +1,7 @@ extends Node2D +signal on_destroy(tower_placement_id) + @onready var rayCast = $RayCast2D @onready var timer = $Timer @@ -7,6 +9,7 @@ var bullet: PackedScene = null var config = null var tower_id: int = 0 +var tower_placement_id = null var health: int = 0 var damage: int = 0 var speed: int = 0 @@ -20,8 +23,9 @@ var enemies = [] func set_config(config_path): config = load(config_path).new() -func set_tower(id: int): +func set_tower(id: int, placement_id): tower_id = id + tower_placement_id = placement_id bullet = config.tower[tower_id]['bullet'] health = config.tower[tower_id]['health'] damage = config.tower[tower_id]['damage'] @@ -38,6 +42,7 @@ func _update_look_at(enemy: Vector2): func _physics_process(_delta): update_healthbar() if health < 1: + on_destroy.emit(tower_placement_id) queue_free() if enemies.is_empty():