refactor: clean code + simplification

This commit is contained in:
Varylios 2025-09-04 02:54:37 +02:00
parent 6785e5ae87
commit a2698da4a6
14 changed files with 151 additions and 204 deletions

View file

@ -17,19 +17,14 @@ var health : int = 0 :
if health >= max_life and is_alive: if health >= max_life and is_alive:
death() death()
@onready var Path : PathFollow3D = get_parent() @onready var path : PathFollow3D = get_parent()
@onready var healthBar : ProgressBar = $HealthBar3D/SubViewport/HealthBar2D @onready var healthBar : ProgressBar = $HealthBar3D/SubViewport/HealthBar2D
@onready var progress : float :
get(): return $"..".progress
var attack_target : Node3D : var attack_target : Node3D
set(value):
attack_target = value
if value:
movement_stop = true
else:
movement_stop = false
var is_alive : bool = true var is_alive : bool = true
var movement_stop : bool = false
func _ready() -> void: func _ready() -> void:
healthBar.value = health healthBar.value = health
@ -40,10 +35,10 @@ func _physics_process(delta: float) -> void:
if !is_alive: if !is_alive:
return return
if not movement_stop:
Path.set_progress(Path.get_progress() + speed * delta)
if attack_target: if attack_target:
attack() attack()
else:
path.progress += speed * delta
func take_damage(damage : int) -> void: func take_damage(damage : int) -> void:
@ -51,6 +46,7 @@ func take_damage(damage : int) -> void:
healthBar.visible = true healthBar.visible = true
healthBar.value = health healthBar.value = health
func attack(): func attack():
if $AttackCooldown.is_stopped(): if $AttackCooldown.is_stopped():
$AttackCooldown.start() $AttackCooldown.start()
@ -60,13 +56,13 @@ func attack():
func death() -> void: func death() -> void:
is_alive = false is_alive = false
died.emit() died.emit()
EventBus.money_received.emit(money) TheCube.money += money
$Sprite3D.visible = false $Sprite3D.visible = false
var death_vfx : CPUParticles3D = death_vfx_packed.instantiate() var death_vfx : CPUParticles3D = death_vfx_packed.instantiate()
get_tree().current_scene.add_child(death_vfx) get_tree().current_scene.add_child(death_vfx)
death_vfx.global_position = global_position death_vfx.global_position = global_position
Path.queue_free() path.queue_free()
func _on_attack_range_body_entered(body: Node3D) -> void: func _on_attack_range_body_entered(body: Node3D) -> void:

View file

@ -2,8 +2,8 @@
extends Node extends Node
@warning_ignore_start("unused_signal") @warning_ignore_start("unused_signal")
## [b]Emitter[/b] : [method GuiButton._ready][br] ## [b]Emitter[/b] : [method GuiButton][br]
## [b]Subscriber[/b] : [method TowerManager._init] ## [b]Subscriber[/b] : [method TowerManager]
signal tower_selected(towerType : Tower.TYPE) signal tower_selected(towerType : Tower.TYPE)
@ -52,16 +52,6 @@ signal team_in_rest_changed(count : int)
signal projectile_shooted(projectile: Projectile, startPosition: Vector3) signal projectile_shooted(projectile: Projectile, startPosition: Vector3)
## [b]Emitter[/b] : [Enemy][br]
## [b]Subscriber[/b] : [TheCube]
signal money_received(amount : int)
## [b]Emitter[/b] : [TowerManager][br]
## [b]Subscriber[/b] : [TheCube]
signal money_spent(amount : int)
## [b]Emitter[/b] : [WorldManager][br] ## [b]Emitter[/b] : [WorldManager][br]
## [b]Subscriber[/b] : [TheCube] ## [b]Subscriber[/b] : [TheCube]
signal player_has_won() signal player_has_won()

View file

@ -51,8 +51,9 @@ troops = Array[ExtResource("3_ub8wd")]([SubResource("Resource_qfjse"), SubResour
[resource] [resource]
script = ExtResource("1_ftl6b") script = ExtResource("1_ftl6b")
waves = Array[ExtResource("2_457yt")]([SubResource("Resource_457yt"), SubResource("Resource_qeljc")])
auto_start = false auto_start = false
allowedTowers = Array[int]([1, 2, 3, 4]) allowedTowers = Array[int]([1, 2, 3, 4])
laneCount = 1 laneCount = 1
startingMoney = 999
waves = Array[ExtResource("2_457yt")]([SubResource("Resource_457yt"), SubResource("Resource_qeljc")])
metadata/_custom_type_script = "uid://cuhq6u67cbbqm" metadata/_custom_type_script = "uid://cuhq6u67cbbqm"

View file

@ -2,9 +2,11 @@
extends EnhancedResource extends EnhancedResource
class_name Level class_name Level
@export var waves : Array[Wave] :
set(value):
waves = arrayValueChanged(value, Wave.new)
@export var auto_start : bool = false @export var auto_start : bool = false
@export var allowedTowers : Array[Tower.TYPE] @export var allowedTowers : Array[Tower.TYPE]
@export var laneCount : int = 1 @export var laneCount : int = 1
@export var startingMoney : int = 0
@export var waves : Array[Wave] :
set(value):
waves = arrayValueChanged(value, Wave.new)

View file

@ -106,5 +106,6 @@ func addMap(mapScene : PackedScene) -> void:
add_child(map) add_child(map)
level = map.level level = map.level
paths = map.paths.get_children() paths = map.paths.get_children()
TheCube.money = level.startingMoney
EventBus.allowedTowerHasChange.emit(level.allowedTowers) EventBus.allowedTowerHasChange.emit(level.allowedTowers)
spawnNextWave() spawnNextWave()

View file

@ -11,21 +11,18 @@ var health : int = max_health :
death() death()
@export var money : int = 1000 : static var money : int :
set(value): set(value):
money = value money = value
EventBus.money_changed.emit(money) EventBus.money_changed.emit(money)
func _ready() -> void: func _ready() -> void:
EventBus.money_received.connect(_on_EventBus_money_received) EventBus.player_has_won.connect(win)
EventBus.money_spent.connect(_on_EventBus_money_spent)
EventBus.player_has_won.connect(_on_EventBus_player_has_won)
EventBus.money_changed.emit.call_deferred(money)
EventBus.cube_ready.emit.call_deferred(self) EventBus.cube_ready.emit.call_deferred(self)
func take_damage(damage : int) -> void: func take_damage(damage: int) -> void:
health -= damage health -= damage
@ -33,17 +30,13 @@ func win():
Transition.goto("res://UI/win_screen.tscn") Transition.goto("res://UI/win_screen.tscn")
func death(): static func spendMoney(amount : int) -> bool:
if money > amount:
money -= amount
return true
return false
static func death():
EventBus.player_defeated.emit() EventBus.player_defeated.emit()
Transition.goto("res://UI/defeat_screen.tscn") Transition.goto("res://UI/defeat_screen.tscn")
func _on_EventBus_money_received(money_received : int):
money += money_received
func _on_EventBus_money_spent(money_spent : int):
money -= money_spent
func _on_EventBus_player_has_won():
win()

View file

@ -1,18 +1,17 @@
[gd_scene load_steps=3 format=3 uid="uid://do7gm1gjhu1t5"] [gd_scene load_steps=4 format=3 uid="uid://do7gm1gjhu1t5"]
[ext_resource type="Script" uid="uid://ypfplijax7cd" path="res://Tiles/Tile.gd" id="1_j1l7f"]
[ext_resource type="PackedScene" uid="uid://cphq1crga6rix" path="res://Assets/GLB format/tile-straight.glb" id="1_m0ea6"] [ext_resource type="PackedScene" uid="uid://cphq1crga6rix" path="res://Assets/GLB format/tile-straight.glb" id="1_m0ea6"]
[sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_71op4"] [sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_71op4"]
data = PackedVector3Array(-0.5, 0, 0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0.2, 0.5, 0.5, 0.2, -0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0, 0.5, 0.5, 0.2, 0.5, 0.5, 0.2, -0.5, 0.5, 0.2, 0.5, 0.5, 0, -0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0.2, 0.5, 0.5, 0.2, 0.5, 0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0.2, 0.5, 0.5, 0.2, 0.5, 0.5, 0, 0.5, 0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0.2, 0.5, -0.5, 0, 0.5, 0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0, -0.5, -0.5, 0, 0.5, -0.5, 0.2, -0.5, -0.5, 0.2, 0.5, -0.5, 0.2, -0.5, -0.5, 0, 0.5, -0.5, 0.2, -0.5, -0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0.2, -0.5, -0.5, 0.2, -0.5, -0.5, 0, -0.5, -0.5, 0.2, -0.5, 0.5, 0, -0.5, 0.5, 0.2, -0.5, 0.5, 0, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, -0.5, 0.5, 0.2, -0.5, 0.5, 0.2, 0.5, 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0.2, 0.5, -0.5, 0.2, 0.5, -0.5, 0.2, -0.5, 0.5, 0.2, -0.5, -0.5, 0.2, -0.5, 0.5, 0.2, 0.5, -0.5, 0.2, 0.5, 0.5, 0.2, 0.5, -0.5, 0.2, -0.5)
[node name="Road" type="Node3D"] [node name="StaticBody3D" type="StaticBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 0, 0.5)
[node name="StaticBody3D" type="StaticBody3D" parent="."]
collision_layer = 0 collision_layer = 0
collision_mask = 0 collision_mask = 0
script = ExtResource("1_j1l7f")
[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("ConcavePolygonShape3D_71op4") shape = SubResource("ConcavePolygonShape3D_71op4")
[node name="tile-straight2" parent="." instance=ExtResource("1_m0ea6")] [node name="tile-straight2" parent="." instance=ExtResource("1_m0ea6")]

View file

@ -1,18 +1,17 @@
[gd_scene load_steps=3 format=3 uid="uid://n0jw4n4qh502"] [gd_scene load_steps=4 format=3 uid="uid://n0jw4n4qh502"]
[ext_resource type="Script" uid="uid://ypfplijax7cd" path="res://Tiles/Tile.gd" id="1_lplsu"]
[ext_resource type="PackedScene" uid="uid://bmtm2qt72ve6o" path="res://Assets/GLB format/tile-corner-square.glb" id="1_t7lde"] [ext_resource type="PackedScene" uid="uid://bmtm2qt72ve6o" path="res://Assets/GLB format/tile-corner-square.glb" id="1_t7lde"]
[sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_71op4"] [sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_71op4"]
data = PackedVector3Array(-0.5, 0, 0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0.2, 0.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5, 0.2, -0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0, 0.5, 0.5, 0.2, 0.5, 0.5, 0.2, -0.5, 0.5, 0.2, 0.5, 0.5, 0, -0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0.2, 0.5, 0.5, 0.2, 0.5, 0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0.2, 0.5, 0.5, 0.2, 0.5, 0.5, 0, 0.5, 0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0.2, 0.5, -0.5, 0, 0.5, 0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0, -0.5, -0.5, 0, 0.5, -0.5, 0.2, -0.5, -0.5, 0.2, 0.5, -0.5, 0.2, -0.5, -0.5, 0, 0.5, -0.5, 0.2, -0.5, -0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0.2, -0.5, -0.5, 0.2, -0.5, -0.5, 0, -0.5, -0.5, 0.2, -0.5, 0.5, 0, -0.5, 0.5, 0.2, -0.5, 0.5, 0, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, -0.5, 0.5, 0.2, -0.5, 0.5, 0.2, 0.5, 0.5, 0.2, 0.5, -0.5, 0.2, 0.5, -0.5, 0.2, -0.5, 0.5, 0.2, -0.5, -0.5, 0.2, -0.5, 0.5, 0.2, 0.5, -0.5, 0.2, 0.5, 0.5, 0.2, 0.5, -0.5, 0.2, -0.5)
[node name="Road" type="Node3D"] [node name="StaticBody3D" type="StaticBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 0, 0.5)
[node name="StaticBody3D" type="StaticBody3D" parent="."]
collision_layer = 0 collision_layer = 0
collision_mask = 0 collision_mask = 0
script = ExtResource("1_lplsu")
[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("ConcavePolygonShape3D_71op4") shape = SubResource("ConcavePolygonShape3D_71op4")
[node name="tile-corner-square2" parent="." instance=ExtResource("1_t7lde")] [node name="tile-corner-square2" parent="." instance=ExtResource("1_t7lde")]

View file

@ -1,2 +1,6 @@
extends StaticBody3D extends StaticBody3D
class_name BuildTile class_name GameTile
enum TYPE { PATH, TOWER, SCENE }
@export var type : TYPE

View file

@ -1,21 +1,16 @@
[gd_scene load_steps=4 format=3 uid="uid://d1f6m15niwgt"] [gd_scene load_steps=4 format=3 uid="uid://d1f6m15niwgt"]
[ext_resource type="Script" uid="uid://ypfplijax7cd" path="res://Tiles/tile.gd" id="1_2ljfi"] [ext_resource type="Script" uid="uid://ypfplijax7cd" path="res://Tiles/Tile.gd" id="1_2ljfi"]
[ext_resource type="PackedScene" uid="uid://p67ggbkjfwm1" path="res://Assets/GLB format/tile.glb" id="1_71op4"] [ext_resource type="PackedScene" uid="uid://p67ggbkjfwm1" path="res://Assets/GLB format/tile.glb" id="1_71op4"]
[sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_71op4"] [sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_71op4"]
data = PackedVector3Array(-0.5, 0, 0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0.2, 0.5, 0.5, 0.2, -0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0, 0.5, 0.5, 0.2, 0.5, 0.5, 0.2, -0.5, 0.5, 0.2, 0.5, 0.5, 0, -0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0.2, 0.5, 0.5, 0.2, 0.5, 0.5, 0, 0.5, -0.5, 0, 0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.5, 0, 0.5, -0.5, 0.2, 0.5, 0.5, 0.2, 0.5, 0.5, 0, 0.5, 0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0.2, 0.5, -0.5, 0, 0.5, 0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, 0.5, -0.5, 0, 0.5, -0.5, 0, -0.5, -0.5, 0, 0.5, -0.5, 0.2, -0.5, -0.5, 0.2, 0.5, -0.5, 0.2, -0.5, -0.5, 0, 0.5, -0.5, 0.2, -0.5, -0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0, -0.5, 0.5, 0.2, -0.5, -0.5, 0.2, -0.5, -0.5, 0, -0.5, -0.5, 0.2, -0.5, 0.5, 0, -0.5, 0.5, 0.2, -0.5, 0.5, 0, -0.5, -0.5, 0.2, -0.5, -0.5, 0.2, -0.5, 0.5, 0.2, -0.5, 0.5, 0.2, 0.5, 0.5, 0.2, 0.5, -0.5, 0.2, 0.5, -0.5, 0.2, -0.5, 0.5, 0.2, -0.5, -0.5, 0.2, -0.5, 0.5, 0.2, 0.5, -0.5, 0.2, 0.5, 0.5, 0.2, 0.5, -0.5, 0.2, -0.5)
[node name="Tile" type="Node3D"] [node name="Tile" type="StaticBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 0, 0.5)
[node name="tile2" parent="." instance=ExtResource("1_71op4")]
[node name="StaticBody3D" type="StaticBody3D" parent="."]
transform = Transform3D(1, 0, 0, 0, -1, 8.74228e-08, 0, -8.74228e-08, -1, 0, 0.2, 0)
script = ExtResource("1_2ljfi") script = ExtResource("1_2ljfi")
type = 1
[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("ConcavePolygonShape3D_71op4") shape = SubResource("ConcavePolygonShape3D_71op4")
[editable path="tile2"] [node name="tile2" parent="." instance=ExtResource("1_71op4")]

View file

@ -31,30 +31,25 @@ var projectileScene : PackedScene = preload("res://Towers/Projectiles/projectile
@export var buttonTooltip : String @export var buttonTooltip : String
@onready var energy_bar : ProgressBar = $EnergyBar3D/SubViewport/EnergyBar2D @onready var energyBar : ProgressBar = $EnergyBar3D/SubViewport/EnergyBar2D
@onready var sprite : Sprite3D = $Sprite3D @onready var sprite : Sprite3D = $Sprite3D
@onready var energyRecoveryCooldown : Timer = $EnergyRecoveryCooldown
var energy : float = 0: var energy : float = 0:
set(value): set(value):
energy_bar.value = value energyBar.value = value
energy = clampf(value, 0.0, max_energy) energy = clampf(value, 0.0, max_energy)
is_exhausted = energy < energy_cost is_exhausted = energy < energy_cost
EventBus.energy_has_changed.emit(self) EventBus.energy_has_changed.emit(self)
var current_targets : Array = [] var availableTargets : Array[Enemy]
var current : CharacterBody3D var target : Enemy
var can_shoot : bool = false
var is_exhausted : bool = false var is_exhausted : bool = false
var is_rest : bool :
get(): return not energyRecoveryCooldown.is_stopped()
var builded : 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") @export_category("Upgrades")
@export var upgrades : Array[TowerUpgrade] @export var upgrades : Array[TowerUpgrade]
@ -63,25 +58,20 @@ var is_rest : bool = true:
func _ready() -> void: func _ready() -> void:
EventBus.tower_upgraded.connect(apply_upgrade) EventBus.tower_upgraded.connect(apply_upgrade)
EventBus.energy_has_changed.emit(self) EventBus.energy_has_changed.emit(self)
energyRecoveryCooldown.timeout.connect(func(): energy += energy_regen)
# WARNING : Prevent .tscn file to be modified by the load of the scene in editor # WARNING : Prevent .tscn file to be modified by the load of the scene in editor
if not Engine.is_editor_hint(): if not Engine.is_editor_hint():
$PriceTag.text = str(price) +"" collision_layer = 0
$PriceTag.text = str(price) + ""
energy = max_energy energy = max_energy
$Range/Range.shape = towerRange $Range/Range.shape = towerRange
energy_bar.value = energy energyBar.value = energy
energy_bar.max_value = max_energy energyBar.max_value = max_energy
$Cooldown.wait_time = action_cooldown
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
if is_rest: if visible && $AttackCooldown.is_stopped() && builded && is_instance_valid(target):
return shoot()
if is_instance_valid(current):
if can_shoot && not is_exhausted:
shoot()
can_shoot = false
$Cooldown.start()
func get_available_upgrades() -> Array[TowerUpgrade]: func get_available_upgrades() -> Array[TowerUpgrade]:
@ -89,72 +79,57 @@ func get_available_upgrades() -> Array[TowerUpgrade]:
func apply_upgrade(towerType : Tower.TYPE, upgrade : TowerUpgrade): func apply_upgrade(towerType : Tower.TYPE, upgrade : TowerUpgrade):
## TODO DANGER connect cube money if towerType == tower_type && upgrade.canUpgrade():
if towerType == tower_type && upgrade.canUpgrade(100):
upgrade.upgrade(self) upgrade.upgrade(self)
func shoot() -> void: func shoot() -> void:
energy -= energy_cost energy -= energy_cost
var projectile : Projectile = projectileScene.instantiate() var projectile : Projectile = projectileScene.instantiate()
projectile.loadProjectile(projectileRessource, current) projectile.loadProjectile(projectileRessource, target)
EventBus.projectile_shooted.emit(projectile, $Aim.global_position) EventBus.projectile_shooted.emit(projectile, $Aim.global_position)
$AttackCooldown.start(action_cooldown)
func resting() -> void: func resting() -> void:
energy += energy_regen
visible = false visible = false
set_collision_layer_value(3,false) collision_layer = 0
set_collision_mask_value(3, false) collision_mask = 0
$EnergyCooldown.start() energyRecoveryCooldown.start()
func in_action() -> void: func in_action() -> void:
visible = true visible = true
set_collision_layer_value(3, true) collision_layer = 0b100
set_collision_mask_value(3, true) collision_mask = 0b100
$EnergyCooldown.stop() energyRecoveryCooldown.stop()
func choose_target(_current_targets : Array) -> void: func choose_target() -> void:
var temp_array : Array = _current_targets target = null
var current_target : CharacterBody3D = null for enemy in availableTargets:
for i in temp_array: if not target || enemy.progress > target.progress:
if current_target == null : target = enemy
current_target = i
else:
if i.get_parent().get_progress() > current_target.get_parent().get_progress():
current_target = i
current = current_target
func build() -> void: func build() -> bool:
EventBus.money_spent.emit(price) if builded || not TheCube.spendMoney(price):
can_shoot = true return false
collision_layer = 3
sprite.modulate = "ffffffff" sprite.modulate = "ffffffff"
builded = true builded = true
$PriceTag.visible = false $PriceTag.visible = false
EventBus.tower_builded.emit(self) EventBus.tower_builded.emit(self)
return true
func _on_range_body_entered(body: Node3D) -> void: func _on_range_body_entered(body: Node3D) -> void:
if body is Enemy: if body is Enemy:
current_targets.append(body) availableTargets.append(body)
choose_target(current_targets) choose_target()
func _on_range_body_exited(body: Node3D) -> void: func _on_range_body_exited(body: Node3D) -> void:
if body is Enemy: if body is Enemy:
current_targets.erase(body) availableTargets.erase(body)
choose_target(current_targets) choose_target()
func _on_cooldown_timeout() -> void:
can_shoot = true
func _on_energy_cooldown_timeout() -> void:
resting()

View file

@ -2,46 +2,45 @@ extends Node3D
class_name TowerManager class_name TowerManager
enum STATE { IDLE, PLACING }
var _state : int = STATE.IDLE
@onready var cam : Camera3D = $"../Camera3D" @onready var cam : Camera3D = $"../Camera3D"
@onready var selection_icon : Sprite3D = $Sprite3DSelection @onready var selection_icon : Sprite3D = $Sprite3DSelection
var towers : Dictionary var towers : Dictionary
var selected_collider : CollisionObject3D var usedLocations : Dictionary
var selected_tower : Tower = null var selectedTile : Vector3
var the_cube : TheCube var selected_tower : Tower
func _ready() -> void: func _ready() -> void:
EventBus.tower_selected.connect(selectTower) EventBus.tower_selected.connect(selectTower)
EventBus.cube_ready.connect(func(value): the_cube = value)
#$AnimationPlayer.play("arrow_bobbing") #$AnimationPlayer.play("arrow_bobbing")
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
handle_player_controls() var collider : CollisionObject3D = handle_player_controls()
var tower : Tower
if collider is GameTile:
tower = usedLocations.get(collider.global_position.round())
if Input.is_action_just_pressed("build"): if Input.is_action_just_pressed("build"):
if not selected_collider: if not collider is GameTile:
return return
if selected_collider is Tower && selected_collider != selected_tower: if isTileFree(collider):
EventBus.tower_selected.emit(selected_collider.tower_type) placeTower()
elif selected_collider.collision_layer == 1: elif tower && selected_tower.builded:
build_tower() EventBus.tower_selected.emit(tower.tower_type)
if Input.is_action_just_pressed("rest"): if Input.is_action_just_pressed("rest"):
if _state == STATE.PLACING: if tower:
quit_placing() moveTower(tower, Vector3.INF)
else:
EventBus.tower_selected.emit(Tower.TYPE.NONE)
if selected_collider is Tower:
selected_collider.is_rest = true
selected_collider.global_position = Vector3.ZERO
emitTeamChanges()
func handle_player_controls() -> void: func handle_player_controls() -> Node3D:
var space_state : PhysicsDirectSpaceState3D = get_world_3d().direct_space_state var space_state : PhysicsDirectSpaceState3D = get_world_3d().direct_space_state
var mouse_pos : Vector2 = get_viewport().get_mouse_position() var mouse_pos : Vector2 = get_viewport().get_mouse_position()
@ -51,48 +50,53 @@ func handle_player_controls() -> void:
ray.collide_with_bodies = true ray.collide_with_bodies = true
var ray_result : Dictionary = space_state.intersect_ray(ray) var ray_result : Dictionary = space_state.intersect_ray(ray)
if ray_result.is_empty(): if ray_result.is_empty():
visible = false visible = false
selected_collider = null return null
return
selected_collider = ray_result.get("collider") var collider : Node3D = ray_result.get("collider")
visible = true visible = true
selection_icon.visible = true selection_icon.visible = true
global_position = selected_collider.global_position global_position = collider.global_position + Vector3(0.0, 0.21, 0.0)
global_position += Vector3(0.0,0.02,0.0)
if _state == STATE.PLACING: if selected_tower && not selected_tower.builded:
selected_tower.sprite.modulate = "ff4545c8" #If the tower can't be placed he is red selected_tower.sprite.modulate = "ff4545c8" # If the tower can't be placed he is red
selection_icon.visible = false selection_icon.visible = false # If we are placing a tower, hide the selector model
if selected_collider is BuildTile: #if we are placing a tower, hide the selector model if collider is GameTile && isTileFree(collider):
selected_tower.sprite.modulate = "61ff45c8" #If the tower can be placed he is green selected_tower.sprite.modulate = "61ff45c8" # If the tower can be placed he is green
return collider
func quit_placing() -> void: func placeTower() -> void:
_state = STATE.IDLE if not selected_tower:
# Refactoriser ce code
func build_tower() -> void:
if !visible || !selected_tower:
return return
if not selected_tower.builded && selected_tower.price > the_cube.money: if not selected_tower.builded:
#Can't build if not selected_tower.build():
print("Too costly") return
return remove_child(selected_tower)
$"../Towers".add_child(selected_tower)
if selected_tower.is_rest: moveTower(selected_tower, global_position)
if not selected_tower.builded:
#Build Tower
selected_tower.build() func isTileFree(tile: GameTile) -> bool:
remove_child(selected_tower) return not usedLocations.has(tile.global_position.round()) \
$"../Towers".add_child(selected_tower) && tile.type == GameTile.TYPE.TOWER
quit_placing()
selected_tower.is_rest = false
## Set [param toPosition] with [Vector3.INF] to make the tower rest
func moveTower(tower: Tower, toPosition: Vector3) -> void:
usedLocations.erase(tower.global_position.round())
if toPosition == Vector3.INF:
tower.resting()
else:
usedLocations.set(global_position.round(), tower)
tower.global_position = toPosition
tower.in_action()
selected_tower.global_position = global_position
emitTeamChanges() emitTeamChanges()
@ -101,17 +105,14 @@ func selectTower(towerType: Tower.TYPE):
if selected_tower && not selected_tower.builded: if selected_tower && not selected_tower.builded:
selected_tower.visible = false selected_tower.visible = false
_state = STATE.IDLE
if selected_tower && selected_tower.tower_type == towerType || towerType == Tower.TYPE.NONE: if selected_tower && selected_tower.tower_type == towerType || towerType == Tower.TYPE.NONE:
selected_tower = null selected_tower = null
elif towers.has(towerType): elif towers.has(towerType):
selected_tower = towers.get(towerType) selected_tower = towers.get(towerType)
selected_tower.visible = true selected_tower.visible = true
else: else:
_state = STATE.PLACING
selected_tower = Game.towers.get(towerType).instantiate() selected_tower = Game.towers.get(towerType).instantiate()
towers.set(towerType, selected_tower) towers.set(towerType, selected_tower)
selected_tower.collision_layer = 0
add_child(selected_tower) add_child(selected_tower)

View file

@ -1,15 +1,10 @@
[gd_scene load_steps=11 format=3 uid="uid://trg7ag3dqr2l"] [gd_scene load_steps=8 format=3 uid="uid://trg7ag3dqr2l"]
[ext_resource type="Script" uid="uid://8kpvuurr5h5n" path="res://Towers/Tower.gd" id="1_egfuc"] [ext_resource type="Script" uid="uid://8kpvuurr5h5n" path="res://Towers/Tower.gd" id="1_egfuc"]
[ext_resource type="Texture2D" uid="uid://bn6ikwol6x8r0" path="res://Assets/Characters/Male1.png" id="2_egfuc"] [ext_resource type="Texture2D" uid="uid://bn6ikwol6x8r0" path="res://Assets/Characters/Male1.png" id="2_egfuc"]
[ext_resource type="Texture2D" uid="uid://uptdcefxlv4c" path="res://Assets/Icones/ppdf_bio_image_placeholder_2.png" id="2_mnaic"] [ext_resource type="Texture2D" uid="uid://uptdcefxlv4c" path="res://Assets/Icones/ppdf_bio_image_placeholder_2.png" id="2_mnaic"]
[ext_resource type="Script" uid="uid://bg0x4egeu17qb" path="res://Upgrades/Tower/TowerUpgrade.gd" id="3_5dr1v"]
[ext_resource type="Resource" uid="uid://c6844q5o2gjhd" path="res://Upgrades/Tower/Resources/TowerMaxEnergyUpgrade.tres" id="3_jv31o"]
[ext_resource type="Script" uid="uid://blnmjxmusrsa7" path="res://UI/GameStyleBoxFlat.gd" id="8_5dr1v"] [ext_resource type="Script" uid="uid://blnmjxmusrsa7" path="res://UI/GameStyleBoxFlat.gd" id="8_5dr1v"]
[sub_resource type="BoxShape3D" id="BoxShape3D_egfuc"]
size = Vector3(1, 0.1, 1)
[sub_resource type="ViewportTexture" id="ViewportTexture_jv31o"] [sub_resource type="ViewportTexture" id="ViewportTexture_jv31o"]
viewport_path = NodePath("EnergyBar3D/SubViewport") viewport_path = NodePath("EnergyBar3D/SubViewport")
@ -31,11 +26,8 @@ collision_mask = 4
script = ExtResource("1_egfuc") script = ExtResource("1_egfuc")
icone = ExtResource("2_mnaic") icone = ExtResource("2_mnaic")
bio = "Aime se promener dans l'herbe et manger des framboises. Sa petite bouille la rend trop mignonne." bio = "Aime se promener dans l'herbe et manger des framboises. Sa petite bouille la rend trop mignonne."
tower_shop = Array[ExtResource("3_5dr1v")]([ExtResource("3_jv31o")])
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.00914001, 0)
shape = SubResource("BoxShape3D_egfuc")
[node name="Range" type="Area3D" parent="."] [node name="Range" type="Area3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.4, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.4, 0)
@ -48,10 +40,11 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.475708, 0)
[node name="Aim" type="Marker3D" parent="."] [node name="Aim" type="Marker3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.572046, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.572046, 0)
[node name="Cooldown" type="Timer" parent="."] [node name="AttackCooldown" type="Timer" parent="."]
wait_time = 1.5 wait_time = 1.5
one_shot = true
[node name="EnergyCooldown" type="Timer" parent="."] [node name="EnergyRecoveryCooldown" type="Timer" parent="."]
wait_time = 0.5 wait_time = 0.5
[node name="Sprite3D" type="Sprite3D" parent="."] [node name="Sprite3D" type="Sprite3D" parent="."]
@ -93,8 +86,6 @@ metadata/_custom_type_script = "uid://blnmjxmusrsa7"
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.2, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.2, 0)
billboard = 2 billboard = 2
[connection signal="input_event" from="." to="." method="_on_input_event"]
[connection signal="body_entered" from="Range" to="." method="_on_range_body_entered"] [connection signal="body_entered" from="Range" to="." method="_on_range_body_entered"]
[connection signal="body_exited" from="Range" to="." method="_on_range_body_exited"] [connection signal="body_exited" from="Range" to="." method="_on_range_body_exited"]
[connection signal="timeout" from="Cooldown" to="." method="_on_cooldown_timeout"] [connection signal="timeout" from="AttackCooldown" to="." method="_on_cooldown_timeout"]
[connection signal="timeout" from="EnergyCooldown" to="." method="_on_energy_cooldown_timeout"]

View file

@ -33,8 +33,8 @@ func upgradeUpgrade() -> void:
cost += baseCost cost += baseCost
func canUpgrade(money : int): func canUpgrade():
return isLevelMax && money > cost return not isLevelMax && TheCube.money > cost
func getNextValue(oldValue): func getNextValue(oldValue):