TowerDefense/Towers/tower.gd

171 lines
4.1 KiB
GDScript3
Raw Normal View History

2025-03-26 18:55:43 +01:00
extends StaticBody3D
class_name Tower
2025-08-29 12:11:51 +02:00
# DANGER "NONE" Should always be first
enum TYPE { NONE, PIERRE, ALINE, MAXENCE, VICTORIA, EVAN, ALEX, GERALDINE }
2025-08-31 23:07:55 +02:00
var projectileScene : PackedScene = preload("res://Projectiles/projectile.tscn")
@export var tower_name : String = "None"
@export var tower_type : TYPE
2025-08-25 23:42:09 +02:00
@export var icone : Texture2D
@export var bio : String
@export var price : int = 100
2025-06-18 12:19:43 +02:00
@export_group("Attack")
2025-08-31 23:07:55 +02:00
@export var projectileRessource : ProjectileResource
2025-06-18 12:19:43 +02:00
@export var action_cooldown : float = 1.5:
set(value):
action_cooldown = clamp(value, 0.3, 999)
$Cooldown.wait_time = action_cooldown
@export_group("Energy")
2025-06-18 12:19:43 +02:00
@export var max_energy : float = 100.0:
set(value):
max_energy = value
@export var energy_regen : float = 10.0
@export var energy_cost : float = 50.0
2025-08-29 20:07:36 +02:00
@export_group("Button")
@export var buttonTooltip : String
@onready var energy_bar : EnergyBar3D = $EnergyBar3D
2025-08-27 13:27:32 +02:00
@onready var sprite : Sprite3D = $Sprite3D
var energy : float = max_energy:
set(value):
energy = clampf(value, 0.0, max_energy)
energy_bar.update(value)
2025-08-30 15:46:36 +02:00
is_exhausted = energy < energy_cost
2025-08-27 14:37:26 +02:00
EventBus.energy_has_changed.emit(self)
2025-03-26 18:55:43 +01:00
var current_targets : Array = []
var current : CharacterBody3D
var can_shoot : bool = true
var is_exhausted : bool = false
2025-08-29 12:11:51 +02:00
var builded : bool = false
2025-08-29 12:11:51 +02:00
var is_rest : bool = true:
set(value):
is_rest = value
if value:
resting()
else:
in_action()
2025-08-25 23:23:03 +02:00
@export_category("Upgrades")
2025-06-18 12:19:43 +02:00
var tower_upgrades : Array[TowerUpgrade]
2025-08-25 23:23:03 +02:00
@export var tower_shop : Array[TowerUpgrade]
2025-06-18 12:19:43 +02:00
2025-08-25 23:23:03 +02:00
func get_available_upgrades() -> Array[TowerUpgrade]:
var available_upgrades : Array[TowerUpgrade]
#Check every upgrades in shop
for shop_upgrade in tower_shop:
#If they have no conditions, they are available
if shop_upgrade.condition == null:
available_upgrades.append(shop_upgrade)
continue
else:
# If they have a condition, check for the previously bought upgrades
for owned_upgrade in tower_upgrades: #Check
2025-08-25 23:23:03 +02:00
if shop_upgrade.condition == owned_upgrade:
available_upgrades.append(shop_upgrade)
return available_upgrades
func apply_upgrade(upgrade : TowerUpgrade):
upgrade.apply_upgrade(self)
if tower_shop.has(upgrade):
tower_shop.erase(upgrade)
tower_upgrades.append(upgrade)
else:
printerr("Upgrade already bought")
2025-06-18 12:19:43 +02:00
func _ready() -> void:
2025-06-18 12:19:43 +02:00
EventBus.tower_upgraded.connect(_on_EventBus_tower_upgraded)
energy_bar.set_up(energy, max_energy)
2025-08-27 13:27:32 +02:00
energy_bar.set_max_value(max_energy)
2025-06-18 12:19:43 +02:00
$Cooldown.wait_time = action_cooldown
2025-03-26 18:55:43 +01:00
func _process(delta: float) -> void:
if is_rest:
return
2025-03-26 18:55:43 +01:00
if is_instance_valid(current):
if can_shoot && not is_exhausted:
2025-03-26 18:55:43 +01:00
shoot()
can_shoot = false
$Cooldown.start()
func shoot() -> void:
energy -= energy_cost
2025-08-31 23:07:55 +02:00
var projectile : Projectile = projectileScene.instantiate()
projectile.loadProjectile(projectileRessource, $Aim.global_position, current)
EventBus.projectile_shooted.emit(projectile)
2025-03-26 18:55:43 +01:00
func resting() -> void:
energy += energy_regen
visible = false
2025-06-18 12:19:43 +02:00
set_collision_layer_value(3,false)
set_collision_mask_value(3, false)
$EnergyCooldown.start()
func in_action() -> void:
visible = true
2025-06-18 12:19:43 +02:00
set_collision_layer_value(3, true)
set_collision_mask_value(3, true)
$EnergyCooldown.stop()
2025-03-26 18:55:43 +01:00
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
2025-03-26 18:55:43 +01:00
current = current_target
2025-08-27 13:27:32 +02:00
func show_price_tag()-> void:
$PriceTag.text = str(price) +""
visible = true
func hide_price_tag()-> void:
visible = false
2025-03-26 18:55:43 +01:00
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
func _on_energy_cooldown_timeout() -> void:
resting()
2025-06-18 12:19:43 +02:00
func _on_EventBus_tower_upgraded(tower_to_upgrade : String, upgrade : TowerUpgrade) -> void:
if tower_to_upgrade == tower_name:
2025-08-25 23:23:03 +02:00
apply_upgrade(upgrade)