Frontier-of-Hell/scripts/towers/tower.gd

82 lines
2 KiB
GDScript

extends Node2D
signal on_destroy(tower_placement_id)
@onready var rayCast = $RayCast2D
@onready var timer = $Timer
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
var shoot_timer: float = 0
var durability_reduction: int = 0
var is_shoot: bool = true
var enemies = []
func set_config(config_path):
config = load(config_path).new()
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']
speed = config.tower[tower_id]['speed']
shoot_timer = config.tower[tower_id]['timer']
durability_reduction = config.tower[tower_id]['durability_reduction']
func set_timer():
timer.wait_time = shoot_timer
func _update_look_at(enemy: Vector2):
look_at(enemy)
func _physics_process(_delta):
update_healthbar()
if health < 1:
on_destroy.emit(tower_placement_id)
queue_free()
if enemies.is_empty():
$AnimatedSprite2D.stop()
else:
var distance = global_position.distance_to(enemies[0].global_position)
_update_look_at(enemies[0].global_position + enemies[0].enemy_vol * (distance / speed))
if timer.is_stopped():
$AnimatedSprite2D.play("default")
_shoot()
func _shoot():
if bullet:
var bullet_instance: Node2D = bullet.instantiate()
bullet_instance.dir = rotation
bullet_instance.rotation = rotation
bullet_instance.global_position = global_position
bullet_instance.speed = speed
bullet_instance.damage = damage
get_tree().current_scene.add_child(bullet_instance)
health -= durability_reduction
timer.start()
func update_healthbar():
$HealthBar.value = health
if health >= 100:
$HealthBar.visible = false
else:
$HealthBar.visible = true
func _on_body_entered(body):
if body.is_in_group("Enemy"):
enemies.append(body.get_parent())
func _on_body_exited(body):
enemies.erase(body.get_parent())