From 29a7d9b884c20520c362f6432ed09fd0871908fc Mon Sep 17 00:00:00 2001 From: James Date: Thu, 5 Jun 2025 15:35:19 +0200 Subject: [PATCH] Starting working on GUI : Cube Icon + integrity bar + money counter + money buy tower --- Assets/Icones/cube.svg | 1 + Assets/Icones/cube.svg.import | 37 +++++++++++++++ Bullets/arrow.tscn | 2 + Globals/EventBus.gd | 12 ++++- Tiles/cube.gd | 21 +++++++-- Tiles/cube.tscn | 2 + Towers/tower.gd | 55 ++++++++++++++++++++++- Towers/tower.tscn | 12 ++++- UI/cube_integrity.gd | 9 ++++ UI/cube_integrity.gd.uid | 1 + UI/energy_bar_2d.gd | 9 ++++ UI/energy_bar_2d.gd.uid | 1 + UI/energy_bar_2d.tscn | 29 ++++++++++++ UI/energy_bar_3d.gd | 15 +++++++ UI/energy_bar_3d.gd.uid | 1 + UI/energy_bar_3d.tscn | 16 +++++++ UI/gui.tscn | 76 +++++++++++++++++++++++++++++++ UI/label_money.gd | 9 ++++ UI/label_money.gd.uid | 1 + UI/shop_panel.tscn | 23 ++++++---- UI/tower_button.gd | 9 +++- enemies/enemy-a.tscn | 4 ++ project.godot | 1 + world.gd | 84 +++++++++++++++++++++++++++-------- world.tscn | 19 +++----- 25 files changed, 401 insertions(+), 48 deletions(-) create mode 100644 Assets/Icones/cube.svg create mode 100644 Assets/Icones/cube.svg.import create mode 100644 UI/cube_integrity.gd create mode 100644 UI/cube_integrity.gd.uid create mode 100644 UI/energy_bar_2d.gd create mode 100644 UI/energy_bar_2d.gd.uid create mode 100644 UI/energy_bar_2d.tscn create mode 100644 UI/energy_bar_3d.gd create mode 100644 UI/energy_bar_3d.gd.uid create mode 100644 UI/energy_bar_3d.tscn create mode 100644 UI/gui.tscn create mode 100644 UI/label_money.gd create mode 100644 UI/label_money.gd.uid diff --git a/Assets/Icones/cube.svg b/Assets/Icones/cube.svg new file mode 100644 index 0000000..9c8e4f1 --- /dev/null +++ b/Assets/Icones/cube.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Assets/Icones/cube.svg.import b/Assets/Icones/cube.svg.import new file mode 100644 index 0000000..573565f --- /dev/null +++ b/Assets/Icones/cube.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://nh6qxoglb4g3" +path="res://.godot/imported/cube.svg-c0871abfea5e01c560421978cffeb532.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/Icones/cube.svg" +dest_files=["res://.godot/imported/cube.svg-c0871abfea5e01c560421978cffeb532.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/Bullets/arrow.tscn b/Bullets/arrow.tscn index 2c2b89c..33dd1c8 100644 --- a/Bullets/arrow.tscn +++ b/Bullets/arrow.tscn @@ -23,6 +23,8 @@ billboard = 2 texture = ExtResource("2_r5o86") [node name="Impact" type="Area3D" parent="."] +collision_layer = 4 +collision_mask = 4 [node name="CollisionShape3D2" type="CollisionShape3D" parent="Impact"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0714111, 0) diff --git a/Globals/EventBus.gd b/Globals/EventBus.gd index 117e181..8b3ca7f 100644 --- a/Globals/EventBus.gd +++ b/Globals/EventBus.gd @@ -3,4 +3,14 @@ extends Node #Emitter : TowerButton.gd #Subscriber : World.gd -signal tower_selected(tower : PackedScene) +signal tower_selected(tower : PackedScene, tower_button : TowerButton) + + +#Emitter : cube.gd +#Subscriber : progress_bar_cube_integrity.gd +signal cube_integrity_changed(_value : int, _max_value : int) + + +#Emitter : cube.gd +#Subscriber : label_money.gd +signal money_changed(_value : int) diff --git a/Tiles/cube.gd b/Tiles/cube.gd index 2317329..e0ed877 100644 --- a/Tiles/cube.gd +++ b/Tiles/cube.gd @@ -1,17 +1,32 @@ extends StaticBody3D class_name TheCube -@export var max_life : int = 50 -var health : int = max_life : +@export var max_health : int = 50 + +var health : int = max_health : set(value): health = value + EventBus.cube_integrity_changed.emit(health, max_health) if health <= 0: death() +@export var money : int = 100 : + set(value): + money = value + EventBus.money_changed.emit(money) + + +func _ready() -> void: + call_deferred("display_money_on_first_call") + +#Because I can't connect properly on first frame and send money info +func display_money_on_first_call() -> void: + EventBus.money_changed.emit(money) + + func take_damage(damage : int) -> void: health -= damage - print(health) func death(): diff --git a/Tiles/cube.tscn b/Tiles/cube.tscn index 7a053e4..4682d92 100644 --- a/Tiles/cube.tscn +++ b/Tiles/cube.tscn @@ -8,6 +8,8 @@ albedo_color = Color(0.051971, 0.051971, 0.051971, 1) [node name="TheCube" type="StaticBody3D"] +collision_layer = 8 +collision_mask = 8 script = ExtResource("1_wrmut") [node name="CollisionShape3D" type="CollisionShape3D" parent="."] diff --git a/Towers/tower.gd b/Towers/tower.gd index 563fe49..94e8ab3 100644 --- a/Towers/tower.gd +++ b/Towers/tower.gd @@ -4,15 +4,48 @@ class_name Tower var bullet : PackedScene = preload("res://Bullets/arrow.tscn") @export var tower_name : String = "Pierre" @export var bullet_damage : int = 1 -@export var energy : float = 100.0 #Next thing to do +@export var price : int = 100 + +@export_group("Energy") +@export var max_energy : float = 100.0 #Next thing to do +@export var energy_regen : float = 10.0 +@export var energy_cost : float = 50.0 +@onready var energy_bar : EnergyBar3D = $EnergyBar3D + +var tower_button : TowerButton = null + +var energy : float = max_energy: + set(value): + energy = clampf(value, 0.0, max_energy) + energy_bar.update(value) + if energy < energy_cost: + is_exhausted = true + else: + is_exhausted = false + var current_targets : Array = [] var current : CharacterBody3D var can_shoot : bool = true +var is_exhausted : bool = false +var is_rest : bool = false: + set(value): + is_rest = value + if value: + resting() + else: + in_action() + + +func _ready() -> void: + energy_bar.set_up(energy, max_energy) func _process(delta: float) -> void: + if is_rest: + return + if is_instance_valid(current): - if can_shoot: + if can_shoot && not is_exhausted: shoot() can_shoot = false $Cooldown.start() @@ -22,6 +55,8 @@ func _process(delta: float) -> void: func shoot() -> void: + energy -= energy_cost + var temp_bullet : Bullet = bullet.instantiate() temp_bullet.target = current temp_bullet.bullet_damage = bullet_damage @@ -29,6 +64,17 @@ func shoot() -> void: temp_bullet.global_position = $Aim.global_position +func resting() -> void: + energy += energy_regen + visible = false + $EnergyCooldown.start() + + +func in_action() -> void: + visible = true + $EnergyCooldown.stop() + + func choose_target(_current_targets : Array) -> void: var temp_array : Array = _current_targets var current_target : CharacterBody3D = null @@ -41,6 +87,7 @@ func choose_target(_current_targets : Array) -> void: current = current_target + func _on_range_body_entered(body: Node3D) -> void: if body is Enemy: current_targets.append(body) @@ -55,3 +102,7 @@ func _on_range_body_exited(body: Node3D) -> void: func _on_cooldown_timeout() -> void: can_shoot = true + + +func _on_energy_cooldown_timeout() -> void: + resting() diff --git a/Towers/tower.tscn b/Towers/tower.tscn index 774c851..35c7bcb 100644 --- a/Towers/tower.tscn +++ b/Towers/tower.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=5 format=3 uid="uid://trg7ag3dqr2l"] +[gd_scene load_steps=6 format=3 uid="uid://trg7ag3dqr2l"] [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="PackedScene" uid="uid://bm303ot1ck82l" path="res://UI/energy_bar_3d.tscn" id="3_pajr1"] [sub_resource type="BoxShape3D" id="BoxShape3D_egfuc"] size = Vector3(1, 0.1, 1) @@ -20,6 +21,8 @@ shape = SubResource("BoxShape3D_egfuc") [node name="Range" type="Area3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.923584, 0) +collision_layer = 4 +collision_mask = 4 [node name="CollisionShape3D" type="CollisionShape3D" parent="Range"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.475708, 0) @@ -33,6 +36,9 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.02026, 0) [node name="Cooldown" type="Timer" parent="."] wait_time = 1.5 +[node name="EnergyCooldown" type="Timer" parent="."] +wait_time = 0.5 + [node name="Sprite3D" type="Sprite3D" parent="."] transform = Transform3D(3.5, 0, 0, 0, 3.5, 0, 0, 0, 3.5, 0, 0.515442, 0) billboard = 2 @@ -41,6 +47,10 @@ hframes = 8 vframes = 12 frame = 75 +[node name="EnergyBar3D" parent="." instance=ExtResource("3_pajr1")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.44473, 0) + [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="timeout" from="Cooldown" to="." method="_on_cooldown_timeout"] +[connection signal="timeout" from="EnergyCooldown" to="." method="_on_energy_cooldown_timeout"] diff --git a/UI/cube_integrity.gd b/UI/cube_integrity.gd new file mode 100644 index 0000000..e8f7766 --- /dev/null +++ b/UI/cube_integrity.gd @@ -0,0 +1,9 @@ +extends ProgressBar + +func _ready() -> void: + EventBus.cube_integrity_changed.connect(_on_EventBus_cube_integrity_changed) + value = 100 + + +func _on_EventBus_cube_integrity_changed(_value, _max_value) -> void: + set_value(float(_value)/float(_max_value) * 100.0) diff --git a/UI/cube_integrity.gd.uid b/UI/cube_integrity.gd.uid new file mode 100644 index 0000000..c9014fb --- /dev/null +++ b/UI/cube_integrity.gd.uid @@ -0,0 +1 @@ +uid://dmcgoepvytnn diff --git a/UI/energy_bar_2d.gd b/UI/energy_bar_2d.gd new file mode 100644 index 0000000..ce55c2e --- /dev/null +++ b/UI/energy_bar_2d.gd @@ -0,0 +1,9 @@ +extends ProgressBar + +func setup_bar(amount : int, max_amount : int) -> void: + max_value = max_amount + value = amount + + +func update_bar(amount : int) -> void: + value = amount diff --git a/UI/energy_bar_2d.gd.uid b/UI/energy_bar_2d.gd.uid new file mode 100644 index 0000000..0fe060a --- /dev/null +++ b/UI/energy_bar_2d.gd.uid @@ -0,0 +1 @@ +uid://ckd6msqvmhtos diff --git a/UI/energy_bar_2d.tscn b/UI/energy_bar_2d.tscn new file mode 100644 index 0000000..56466e2 --- /dev/null +++ b/UI/energy_bar_2d.tscn @@ -0,0 +1,29 @@ +[gd_scene load_steps=4 format=3 uid="uid://xlhufcqxk113"] + +[ext_resource type="Script" uid="uid://ckd6msqvmhtos" path="res://UI/energy_bar_2d.gd" id="1_4beyc"] + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_qpj2g"] +bg_color = Color(0.341176, 0.0823529, 0.0941176, 0) +border_width_left = 2 +border_width_top = 2 +border_width_right = 2 +border_width_bottom = 2 +expand_margin_left = 4.0 +expand_margin_top = 4.0 +expand_margin_right = 4.0 +expand_margin_bottom = 4.0 + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_xcn5x"] +bg_color = Color(0.921569, 0.890196, 0.415686, 1) + +[node name="HealthBar2D" type="ProgressBar"] +custom_minimum_size = Vector2(100, 10) +offset_left = 4.0 +offset_top = 4.0 +offset_right = 104.0 +offset_bottom = 14.0 +theme_override_styles/background = SubResource("StyleBoxFlat_qpj2g") +theme_override_styles/fill = SubResource("StyleBoxFlat_xcn5x") +value = 50.0 +show_percentage = false +script = ExtResource("1_4beyc") diff --git a/UI/energy_bar_3d.gd b/UI/energy_bar_3d.gd new file mode 100644 index 0000000..aa5c9da --- /dev/null +++ b/UI/energy_bar_3d.gd @@ -0,0 +1,15 @@ +extends Sprite3D +class_name EnergyBar3D + +@onready var bar : ProgressBar = $SubViewport/EnergyBar2D + +func _ready() -> void: + texture = $SubViewport.get_texture() + + +func set_up(value : int, max_amount : int) -> void: + bar.setup_bar(value, max_amount) + + +func update(value : int) -> void: + bar.update_bar(value) diff --git a/UI/energy_bar_3d.gd.uid b/UI/energy_bar_3d.gd.uid new file mode 100644 index 0000000..ac94276 --- /dev/null +++ b/UI/energy_bar_3d.gd.uid @@ -0,0 +1 @@ +uid://d0hr28ug7324 diff --git a/UI/energy_bar_3d.tscn b/UI/energy_bar_3d.tscn new file mode 100644 index 0000000..140d8c1 --- /dev/null +++ b/UI/energy_bar_3d.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=4 format=3 uid="uid://bm303ot1ck82l"] + +[ext_resource type="Texture2D" uid="uid://bd7ed8dvc7m0v" path="res://icon.svg" id="1_d48o1"] +[ext_resource type="Script" uid="uid://d0hr28ug7324" path="res://UI/energy_bar_3d.gd" id="2_d48o1"] +[ext_resource type="PackedScene" uid="uid://xlhufcqxk113" path="res://UI/energy_bar_2d.tscn" id="3_8lue3"] + +[node name="EnergyBar3D" type="Sprite3D"] +billboard = 2 +texture = ExtResource("1_d48o1") +script = ExtResource("2_d48o1") + +[node name="SubViewport" type="SubViewport" parent="."] +size = Vector2i(108, 18) +size_2d_override_stretch = true + +[node name="EnergyBar2D" parent="SubViewport" instance=ExtResource("3_8lue3")] diff --git a/UI/gui.tscn b/UI/gui.tscn new file mode 100644 index 0000000..ebb419b --- /dev/null +++ b/UI/gui.tscn @@ -0,0 +1,76 @@ +[gd_scene load_steps=6 format=3 uid="uid://p6a6rb7sgeqd"] + +[ext_resource type="Script" uid="uid://d2x8oy4os7ysn" path="res://UI/label_money.gd" id="1_cwd3r"] +[ext_resource type="Texture2D" uid="uid://nh6qxoglb4g3" path="res://Assets/Icones/cube.svg" id="1_epgl5"] +[ext_resource type="Script" uid="uid://dmcgoepvytnn" path="res://UI/cube_integrity.gd" id="2_sj6ny"] + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_nxmp4"] +bg_color = Color(0.180392, 0.180392, 0.180392, 0) +border_color = Color(0.099986, 0.099986, 0.0999859, 1) + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_epgl5"] +bg_color = Color(0.727498, 0.222072, 0.249347, 1) + +[node name="GUI" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="VBox" type="VBoxContainer" parent="."] +layout_mode = 1 +anchors_preset = 12 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_top = -32.0 +grow_horizontal = 2 +grow_vertical = 0 + +[node name="HBox" type="HBoxContainer" parent="VBox"] +layout_mode = 2 +alignment = 1 + +[node name="LabelMoney" type="Label" parent="VBox/HBox"] +layout_mode = 2 +size_flags_vertical = 1 +text = "Money :" +vertical_alignment = 2 +script = ExtResource("1_cwd3r") + +[node name="Margin" type="MarginContainer" parent="VBox/HBox"] +layout_mode = 2 +size_flags_horizontal = 3 + +[node name="CubeIcon" type="TextureButton" parent="VBox/HBox"] +custom_minimum_size = Vector2(50, 50) +layout_mode = 2 +size_flags_horizontal = 4 +texture_normal = ExtResource("1_epgl5") +texture_pressed = ExtResource("1_epgl5") +texture_hover = ExtResource("1_epgl5") +texture_disabled = ExtResource("1_epgl5") +texture_focused = ExtResource("1_epgl5") +ignore_texture_size = true +stretch_mode = 0 + +[node name="Margin2" type="MarginContainer" parent="VBox/HBox"] +layout_mode = 2 +size_flags_horizontal = 3 + +[node name="LabelTeam" type="Label" parent="VBox/HBox"] +layout_mode = 2 +size_flags_vertical = 1 +text = "actif/total" +vertical_alignment = 2 + +[node name="CubeIntegrity" type="ProgressBar" parent="VBox"] +custom_minimum_size = Vector2(0, 5) +layout_mode = 2 +theme_override_styles/background = SubResource("StyleBoxFlat_nxmp4") +theme_override_styles/fill = SubResource("StyleBoxFlat_epgl5") +value = 50.0 +show_percentage = false +script = ExtResource("2_sj6ny") diff --git a/UI/label_money.gd b/UI/label_money.gd new file mode 100644 index 0000000..ae55525 --- /dev/null +++ b/UI/label_money.gd @@ -0,0 +1,9 @@ +extends Label + +func _ready() -> void: + EventBus.money_changed.connect(_on_EventBus_money_changed) + + +func _on_EventBus_money_changed(_value) -> void: + print("money to change : ", _value) + set_text("Money = " + str(_value)) diff --git a/UI/label_money.gd.uid b/UI/label_money.gd.uid new file mode 100644 index 0000000..586a185 --- /dev/null +++ b/UI/label_money.gd.uid @@ -0,0 +1 @@ +uid://d2x8oy4os7ysn diff --git a/UI/shop_panel.tscn b/UI/shop_panel.tscn index e67a5f3..b3789d7 100644 --- a/UI/shop_panel.tscn +++ b/UI/shop_panel.tscn @@ -1,7 +1,9 @@ [gd_scene load_steps=3 format=3 uid="uid://he31g7f3ru1c"] [ext_resource type="PackedScene" uid="uid://bqpeu50pgsdfk" path="res://UI/tower_button.tscn" id="1_txsyf"] -[ext_resource type="PackedScene" uid="uid://dp846r4jscm5p" path="res://Towers/tower-b.tscn" id="2_oxe32"] + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_oxe32"] +bg_color = Color(0.921569, 0.890196, 0.415686, 1) [node name="ShopPanel" type="Panel"] custom_minimum_size = Vector2(300, 0) @@ -23,11 +25,18 @@ grow_vertical = 2 [node name="PierreButton" parent="VBoxContainer" instance=ExtResource("1_txsyf")] layout_mode = 2 -[node name="AlineButton" parent="VBoxContainer" instance=ExtResource("1_txsyf")] -layout_mode = 2 -text = "Aline -350 Trucs" -tower = ExtResource("2_oxe32") +[node name="EnergyBar" type="ProgressBar" parent="VBoxContainer/PierreButton"] +custom_minimum_size = Vector2(0, 5) +layout_mode = 1 +anchors_preset = 12 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_top = -4.0 +grow_horizontal = 2 +grow_vertical = 0 +theme_override_styles/fill = SubResource("StyleBoxFlat_oxe32") +show_percentage = false [node name="MarginContainer" type="MarginContainer" parent="VBoxContainer"] layout_mode = 2 @@ -38,5 +47,3 @@ custom_minimum_size = Vector2(0, 50) layout_mode = 2 text = "Cancel " - -[editable path="VBoxContainer/AlineButton"] diff --git a/UI/tower_button.gd b/UI/tower_button.gd index da17eb9..84b7a75 100644 --- a/UI/tower_button.gd +++ b/UI/tower_button.gd @@ -1,7 +1,12 @@ extends Button +class_name TowerButton @export var tower : PackedScene - +var tower_in_game : Tower func _on_pressed() -> void: - EventBus.tower_selected.emit(tower) + EventBus.tower_selected.emit(tower, self) + +func _process(delta: float) -> void: + if tower_in_game: + $EnergyBar.value = tower_in_game.energy/tower_in_game.max_energy * 100.0 diff --git a/enemies/enemy-a.tscn b/enemies/enemy-a.tscn index 83560a2..c174a90 100644 --- a/enemies/enemy-a.tscn +++ b/enemies/enemy-a.tscn @@ -12,6 +12,8 @@ radius = 1.0 [node name="PathFollow3D" type="PathFollow3D"] [node name="EnemyA" type="CharacterBody3D" parent="."] +collision_layer = 4 +collision_mask = 4 script = ExtResource("1_l7e03") speed = 1 @@ -30,6 +32,8 @@ vframes = 12 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.6, 0) [node name="AttackRange" type="Area3D" parent="EnemyA"] +collision_layer = 8 +collision_mask = 8 [node name="CollisionShape3D" type="CollisionShape3D" parent="EnemyA/AttackRange"] shape = SubResource("SphereShape3D_cmo2f") diff --git a/project.godot b/project.godot index 93cb60f..774dea0 100644 --- a/project.godot +++ b/project.godot @@ -51,3 +51,4 @@ rest={ 3d_physics/layer_1="floor" 3d_physics/layer_2="tower" 3d_physics/layer_3="baddy" +3d_physics/layer_4="cube" diff --git a/world.gd b/world.gd index f93c4e9..ac6acdb 100644 --- a/world.gd +++ b/world.gd @@ -1,9 +1,11 @@ extends Node3D @onready var tower_blueprint = preload("res://Towers/tower.tscn") +@onready var selected_tower_button : TowerButton = $CanvasLayer/UI/ShopPanel/VBoxContainer/PierreButton @onready var cam : Camera3D = $Camera3D @onready var selector : Node3D = $Selector +@onready var the_cube : TheCube = %TheCube var team_in_action : Array[Tower] @@ -11,6 +13,8 @@ var team_in_rest : Array[Tower] var target_tower : Tower var in_build_menu : bool = false +var selected_collider : CollisionObject3D + func _ready() -> void: $Spawner.spawn_next_wave() $CanvasLayer/UI/ShopPanel.visible = in_build_menu @@ -20,9 +24,20 @@ func _ready() -> void: func _process(delta: float) -> void: handle_player_controls() if Input.is_action_just_pressed("build"): - build_tower(tower_blueprint) - if Input.is_action_just_pressed("rest") && target_tower: - send_to_rest(target_tower) + if not selected_collider: + return + + if selected_collider.collision_layer == 1: + build_tower(tower_blueprint) + if selected_collider is TheCube: + open_shop() + else: + close_shop() + + if Input.is_action_just_pressed("rest"): + close_shop() + if target_tower: + send_to_rest(target_tower) func handle_player_controls() -> void: @@ -38,38 +53,57 @@ func handle_player_controls() -> void: if ray_result.is_empty(): selector.visible = false + selected_collider = null return - selector.visible = true var collider : CollisionObject3D = ray_result.get("collider") + selected_collider = collider + + selector.visible = true selector.global_position = collider.global_position selector.global_position += Vector3(0.0,0.2,0.0) - if collider is Tower: + if selected_collider is Tower: target_tower = collider else: target_tower = null - +# Refactoriser ce code func build_tower(scene : PackedScene) -> void: if !selector.visible: return + var temp_tower : Tower = scene.instantiate() + + #Move existing tower for tower : Tower in team_in_action: if tower.tower_name == temp_tower.tower_name: move_tower(tower) return + #Send to action a resting tower for tower : Tower in team_in_rest: if tower.tower_name == temp_tower.tower_name: move_tower(tower) send_to_action(tower) return - add_child(temp_tower) - team_in_action.append(temp_tower) - temp_tower.global_position = selector.global_position + #Build Tower + if temp_tower.price < the_cube.money: + #Can't build + print("Too costly") + return + else : + #Can build + the_cube.money -= temp_tower.price + temp_tower.tower_button = selected_tower_button + add_child(temp_tower) + selected_tower_button.tower_in_game = temp_tower + + send_to_action(temp_tower) + move_tower(temp_tower) + func move_tower(tower : Tower): @@ -79,19 +113,33 @@ func move_tower(tower : Tower): func send_to_rest(tower : Tower): team_in_action.erase(tower) team_in_rest.append(tower) - tower.visible = false + tower.is_rest = true func send_to_action(tower : Tower): team_in_rest.erase(tower) team_in_action.append(tower) - tower.visible = true + tower.is_rest = false - -func _on_EventBus_tower_selected(tower : PackedScene): - tower_blueprint = tower - - -func _on_recrutement_button_pressed() -> void: - in_build_menu = !in_build_menu +func open_shop(): + if in_build_menu: + close_shop() + return + + in_build_menu = true $CanvasLayer/UI/ShopPanel.visible = in_build_menu + + + +func close_shop(): + in_build_menu = false + $CanvasLayer/UI/ShopPanel.visible = in_build_menu + + +func _on_EventBus_tower_selected(tower : PackedScene, tower_button : TowerButton): + tower_blueprint = tower + selected_tower_button = tower_button + + +func _on_cube_icon_pressed() -> void: + open_shop() diff --git a/world.tscn b/world.tscn index 4baad2f..1b8cce0 100644 --- a/world.tscn +++ b/world.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=13 format=3 uid="uid://ky0uewndeuwv"] +[gd_scene load_steps=14 format=3 uid="uid://ky0uewndeuwv"] [ext_resource type="Script" uid="uid://qqid42jkpkmv" path="res://world.gd" id="1_fj7yv"] [ext_resource type="PackedScene" uid="uid://d1f6m15niwgt" path="res://Tiles/tile.tscn" id="3_aqk2v"] @@ -10,6 +10,7 @@ [ext_resource type="Resource" uid="uid://dkbuy7fetnihs" path="res://Waves/wave_1.tres" id="7_ioo17"] [ext_resource type="Resource" uid="uid://dvw6vm3vbpmdl" path="res://Waves/wave_2.tres" id="8_wse8f"] [ext_resource type="PackedScene" uid="uid://dbx8ow3v45k4k" path="res://UI/ui.tscn" id="9_ioo17"] +[ext_resource type="PackedScene" uid="uid://p6a6rb7sgeqd" path="res://UI/gui.tscn" id="11_ic0uy"] [sub_resource type="Curve3D" id="Curve3D_aqk2v"] _data = { @@ -140,6 +141,7 @@ transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 9.5, 0 transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 9.5, 0, 0.5) [node name="TheCube" parent="Map" instance=ExtResource("5_wse8f")] +unique_name_in_owner = true transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 10.1496, 0.657819, 0.497613) [node name="Spawner" type="Path3D" parent="."] @@ -161,20 +163,11 @@ light_color = Color(0.823308, 0.863845, 0.742102, 1) [node name="UI" parent="CanvasLayer" instance=ExtResource("9_ioo17")] -[node name="RecrutementButton" type="Button" parent="CanvasLayer/UI"] -custom_minimum_size = Vector2(200, 50) +[node name="GUI" parent="CanvasLayer/UI" instance=ExtResource("11_ic0uy")] layout_mode = 1 -anchors_preset = 2 -anchor_top = 1.0 -anchor_bottom = 1.0 -offset_left = 29.0 -offset_top = -77.0 -offset_right = 229.0 -offset_bottom = -27.0 -grow_vertical = 0 -text = "Recrutement" [connection signal="timeout" from="Spawner/SpawnTimer" to="Spawner" method="_on_spawn_timer_timeout"] -[connection signal="pressed" from="CanvasLayer/UI/RecrutementButton" to="." method="_on_recrutement_button_pressed"] +[connection signal="pressed" from="CanvasLayer/UI/GUI/VBox/HBox/CubeIcon" to="." method="_on_cube_icon_pressed"] [editable path="CanvasLayer/UI"] +[editable path="CanvasLayer/UI/GUI"]