TowerDefense/Towers/tower.gd

56 lines
1.4 KiB
GDScript3
Raw Normal View History

2025-03-26 18:55:43 +01:00
extends StaticBody3D
class_name Tower
var bullet : PackedScene = preload("res://Bullets/arrow.tscn")
@export var bullet_damage : int = 1
var current_targets : Array = []
var current : CharacterBody3D
var can_shoot : bool = true
func _process(delta: float) -> void:
if is_instance_valid(current):
if can_shoot:
shoot()
can_shoot = false
$Cooldown.start()
else:
for i in $BulletContainer.get_child_count():
$BulletContainer.get_child(i).queue_free()
func shoot() -> void:
var temp_bullet : Bullet = bullet.instantiate()
temp_bullet.target = current
temp_bullet.bullet_damage = bullet_damage
$BulletContainer.add_child(temp_bullet)
temp_bullet.global_position = $Aim.global_position
func choose_target(_current_targets : Array) -> void:
var temp_array : Array = _current_targets
var current_target : CharacterBody3D = null
for i in temp_array:
if current_target == null :
current_target = i
else:
if i.get_parent().get_progress() > current_target.get_parent().get_progress():
current_target = i
current = current_target
func _on_range_body_entered(body: Node3D) -> void:
if body is Enemy:
current_targets.append(body)
choose_target(current_targets)
func _on_range_body_exited(body: Node3D) -> void:
if body is Enemy:
current_targets.erase(body)
choose_target(current_targets)
func _on_cooldown_timeout() -> void:
can_shoot = true