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

96 lines
2.4 KiB
GDScript3
Raw Normal View History

2023-10-06 11:40:11 +02:00
extends Node2D
2023-10-09 03:01:58 +02:00
signal on_destroy(tower_placement_id)
2023-10-06 11:40:11 +02:00
@onready var rayCast = $RayCast2D
@onready var timer = $Timer
2023-10-21 17:45:51 +02:00
@onready var shoot = $Shoot
2023-10-06 11:40:11 +02:00
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
2023-10-09 03:01:58 +02:00
var tower_placement_id = null
2023-10-07 18:13:45 +02:00
var health: int = 0
var damage: int = 0
var speed: int = 0
var shoot_timer: float = 0
var durability_reduction: int = 0
2023-10-20 19:59:00 +02:00
var area_damage: bool = false
2023-10-20 20:52:37 +02:00
var slow_effect: bool = false
2023-10-06 11:40:11 +02:00
var enemies = []
2023-10-07 18:13:45 +02:00
func set_config(config_path):
config = load(config_path).new()
2023-10-09 03:01:58 +02:00
func set_tower(id: int, placement_id):
2023-10-07 18:13:45 +02:00
tower_id = id
2023-10-09 03:01:58 +02:00
tower_placement_id = placement_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']
2023-10-20 19:59:00 +02:00
area_damage = config.tower[tower_id]['area_damage']
2023-10-20 20:52:37 +02:00
slow_effect = config.tower[tower_id]['slow_effect']
2023-10-07 18:13:45 +02:00
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:
2023-10-09 03:01:58 +02:00
on_destroy.emit(tower_placement_id)
2023-10-07 18:13:45 +02:00
queue_free()
2023-10-06 11:40:11 +02:00
if enemies.is_empty():
$AnimatedSprite2D.stop()
else:
2023-11-19 11:49:32 +01:00
if !enemies[0].is_dead:
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()
else:
enemies.erase(enemies[0])
2023-10-06 11:40:11 +02:00
func _shoot():
if bullet:
2023-11-14 16:02:05 +01:00
# Check SFX Status
if Global.sfx_status:
shoot.play()
2023-10-06 11:40:11 +02:00
var bullet_instance: Node2D = bullet.instantiate()
2023-10-16 19:12:44 +02:00
bullet_instance.dir = rotation
bullet_instance.rotation = rotation
bullet_instance.global_position = global_position
2023-10-07 18:13:45 +02:00
bullet_instance.speed = speed
bullet_instance.damage = damage
2023-10-20 19:59:00 +02:00
bullet_instance.area_damage = area_damage
bullet_instance.area_damage_target = enemies
2023-10-20 20:52:37 +02:00
bullet_instance.slow_effect = slow_effect
2023-10-16 19:12:44 +02:00
get_tree().current_scene.add_child(bullet_instance)
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())