Fix error to rebuild the tower

This commit is contained in:
Moe Poi ~ 2023-10-09 08:01:58 +07:00
parent ffa52c30ca
commit 450e2b686e
2 changed files with 13 additions and 4 deletions

View file

@ -19,7 +19,7 @@ func _ready():
$base.connect("enemy_entered_base", on_enemy_entered_base) $base.connect("enemy_entered_base", on_enemy_entered_base)
var placements = $Placements.get_children() var placements = $Placements.get_children()
for placement in placements: 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) $CanvasLayer/TowerMenu.connect("build_tower", on_build_tower)
func on_game_paused(): func on_game_paused():
@ -32,7 +32,7 @@ func on_game_resume():
get_tree().paused = data['paused'] get_tree().paused = data['paused']
$CanvasLayer/PauseMenu.hide_menu() $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 tower_id = null
var disabled = false var disabled = false
var disabled_2 = 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() var tower = config.tower[tower_id]['asset'].instantiate()
get_tree().current_scene.add_child(tower) get_tree().current_scene.add_child(tower)
tower.set_config(config_path) tower.set_config(config_path)
tower.set_tower(tower_id) tower.set_tower(tower_id, tower_placement_id)
tower.set_timer() tower.set_timer()
tower.global_position = pos 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)]['id'] = tower_id
data['tower'][str(tower_placement_id)]['name'] = tower.name data['tower'][str(tower_placement_id)]['name'] = tower.name
data['resource'] -= config.menu[tower_id]['price'] data['resource'] -= config.menu[tower_id]['price']
$CanvasLayer/GameStats.set_resource(data['resource']) $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): func on_enemy_entered_base(enemy):
data['health'] -= enemy.damage data['health'] -= enemy.damage
$CanvasLayer/GameStats.set_damage(enemy.damage) $CanvasLayer/GameStats.set_damage(enemy.damage)

View file

@ -1,5 +1,7 @@
extends Node2D extends Node2D
signal on_destroy(tower_placement_id)
@onready var rayCast = $RayCast2D @onready var rayCast = $RayCast2D
@onready var timer = $Timer @onready var timer = $Timer
@ -7,6 +9,7 @@ var bullet: PackedScene = null
var config = null var config = null
var tower_id: int = 0 var tower_id: int = 0
var tower_placement_id = null
var health: int = 0 var health: int = 0
var damage: int = 0 var damage: int = 0
var speed: int = 0 var speed: int = 0
@ -20,8 +23,9 @@ var enemies = []
func set_config(config_path): func set_config(config_path):
config = load(config_path).new() config = load(config_path).new()
func set_tower(id: int): func set_tower(id: int, placement_id):
tower_id = id tower_id = id
tower_placement_id = placement_id
bullet = config.tower[tower_id]['bullet'] bullet = config.tower[tower_id]['bullet']
health = config.tower[tower_id]['health'] health = config.tower[tower_id]['health']
damage = config.tower[tower_id]['damage'] damage = config.tower[tower_id]['damage']
@ -38,6 +42,7 @@ func _update_look_at(enemy: Vector2):
func _physics_process(_delta): func _physics_process(_delta):
update_healthbar() update_healthbar()
if health < 1: if health < 1:
on_destroy.emit(tower_placement_id)
queue_free() queue_free()
if enemies.is_empty(): if enemies.is_empty():