174 lines
4.2 KiB
GDScript
174 lines
4.2 KiB
GDScript
extends StaticBody3D
|
|
class_name Tower
|
|
|
|
# DANGER NONE Should always be first
|
|
enum TYPES { NONE, PIERRE, ALINE, MAXENCE, VICTORIA, EVAN, ALEX, GERALDINE }
|
|
|
|
var bullet : PackedScene = preload("res://Bullets/bullet.tscn")
|
|
@export var tower_name : String = "Pierre"
|
|
@export var tower_type : TYPES
|
|
@export var icone : Texture2D
|
|
@export var bio : String
|
|
|
|
@export var price : int = 100
|
|
|
|
@export_group("Attack")
|
|
@export var bullet_damage : int = 1
|
|
@export var action_cooldown : float = 1.5:
|
|
set(value):
|
|
action_cooldown = clamp(value, 0.3, 999)
|
|
$Cooldown.wait_time = action_cooldown
|
|
|
|
@export_group("Energy")
|
|
@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
|
|
|
|
@export_group("Button")
|
|
@export var buttonTooltip : String
|
|
|
|
|
|
@onready var energy_bar : EnergyBar3D = $EnergyBar3D
|
|
@onready var sprite : Sprite3D = $Sprite3D
|
|
|
|
var energy : float = max_energy:
|
|
set(value):
|
|
energy = clampf(value, 0.0, max_energy)
|
|
energy_bar.update(value)
|
|
is_exhausted = energy < energy_cost
|
|
EventBus.energy_has_changed.emit(self)
|
|
|
|
var current_targets : Array = []
|
|
var current : CharacterBody3D
|
|
var can_shoot : bool = true
|
|
var is_exhausted : bool = false
|
|
var builded : bool = false
|
|
|
|
var is_rest : bool = true:
|
|
set(value):
|
|
is_rest = value
|
|
if value:
|
|
resting()
|
|
else:
|
|
in_action()
|
|
|
|
@export_category("Upgrades")
|
|
var tower_upgrades : Array[TowerUpgrade]
|
|
@export var tower_shop : Array[TowerUpgrade]
|
|
|
|
|
|
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
|
|
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")
|
|
|
|
func _ready() -> void:
|
|
EventBus.tower_upgraded.connect(_on_EventBus_tower_upgraded)
|
|
energy_bar.set_up(energy, max_energy)
|
|
energy_bar.set_max_value(max_energy)
|
|
$Cooldown.wait_time = action_cooldown
|
|
|
|
func _process(delta: float) -> void:
|
|
if is_rest:
|
|
return
|
|
|
|
if is_instance_valid(current):
|
|
if can_shoot && not is_exhausted:
|
|
shoot()
|
|
can_shoot = false
|
|
$Cooldown.start()
|
|
else:
|
|
for i in $BulletContainer.get_child_count():
|
|
$BulletContainer.get_child(i).queue_free()
|
|
|
|
|
|
func shoot() -> void:
|
|
energy -= energy_cost
|
|
|
|
var temp_bullet : Bullet = bullet.instantiate()
|
|
temp_bullet.target = current
|
|
temp_bullet.bullet_damage = bullet_damage
|
|
temp_bullet.start_position = $Aim.global_position
|
|
EventBus.bullet_shooted.emit(temp_bullet)
|
|
|
|
|
|
func resting() -> void:
|
|
energy += energy_regen
|
|
visible = false
|
|
set_collision_layer_value(3,false)
|
|
set_collision_mask_value(3, false)
|
|
$EnergyCooldown.start()
|
|
|
|
|
|
func in_action() -> void:
|
|
visible = true
|
|
set_collision_layer_value(3, true)
|
|
set_collision_mask_value(3, true)
|
|
$EnergyCooldown.stop()
|
|
|
|
|
|
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 show_price_tag()-> void:
|
|
$PriceTag.text = str(price) +" €"
|
|
visible = true
|
|
|
|
|
|
func hide_price_tag()-> void:
|
|
visible = false
|
|
|
|
|
|
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()
|
|
|
|
|
|
func _on_EventBus_tower_upgraded(tower_to_upgrade : String, upgrade : TowerUpgrade) -> void:
|
|
if tower_to_upgrade == tower_name:
|
|
apply_upgrade(upgrade)
|