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

76 lines
1.7 KiB
GDScript3
Raw Normal View History

2023-10-06 11:40:11 +02:00
extends Node2D
@onready var rayCast = $RayCast2D
@onready var timer = $Timer
2023-10-07 18:17:45 +02:00
var bullet: PackedScene = null
2023-10-06 11:40:11 +02:00
2023-10-07 18:13:45 +02:00
var config = null
var tower_id: int = 0
var health: int = 0
var damage: int = 0
var speed: int = 0
var shoot_timer: float = 0
var durability_reduction: int = 0
2023-10-06 11:40:11 +02:00
var is_shoot: bool = true
var enemies = []
2023-10-07 18:13:45 +02:00
func set_config(config_path):
config = load(config_path).new()
func set_tower(id: int):
tower_id = id
2023-10-07 18:17:45 +02:00
bullet = config.tower[tower_id]['bullet']
2023-10-07 18:13:45 +02:00
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
2023-10-06 11:40:11 +02:00
func _update_look_at(enemy: Vector2):
look_at(enemy)
func _physics_process(_delta):
2023-10-07 18:13:45 +02:00
update_healthbar()
if health < 1:
queue_free()
2023-10-06 11:40:11 +02:00
if enemies.is_empty():
$AnimatedSprite2D.stop()
else:
_update_look_at(enemies[0].global_position)
if timer.is_stopped():
$AnimatedSprite2D.play("default")
_shoot()
func _shoot():
if bullet:
var bullet_instance: Node2D = bullet.instantiate()
get_tree().current_scene.add_child(bullet_instance)
2023-10-07 18:13:45 +02:00
bullet_instance.speed = speed
bullet_instance.damage = damage
2023-10-06 11:40:11 +02:00
bullet_instance.global_position = global_position
bullet_instance.global_rotation = rayCast.global_rotation + 0.1
2023-10-07 18:13:45 +02:00
health -= durability_reduction
2023-10-06 11:40:11 +02:00
timer.start()
2023-10-07 18:13:45 +02:00
func update_healthbar():
$HealthBar.value = health
if health >= 100:
$HealthBar.visible = false
else:
$HealthBar.visible = true
2023-10-06 11:40:11 +02:00
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())