chore: add confirm popup

This commit is contained in:
Varylios 2025-09-06 23:26:37 +02:00
parent 0f3ca8137a
commit d68f496ad1
10 changed files with 122 additions and 23 deletions

View file

@ -37,6 +37,9 @@ func _ready() -> void:
func _input(event: InputEvent) -> void:
if Engine.is_editor_hint():
return
if event.is_action_pressed("pause_game"):
get_tree().paused = not get_tree().paused

View file

@ -1,6 +1,10 @@
@tool
class_name Helper
const confirmPopupScene : PackedScene = preload("res://UI/confirm_popup.tscn")
const SCENE_DIR_PATTERN : String = "res://\\w+/(Scenes|Map \\d+)/$"
const SCENE_FILE_PATTERN : String = "([^/]*)\\.tscn$"
const RESOURCE_FILE_PATTERN : String = "([^/]*)\\.tres$"
@ -63,3 +67,16 @@ static func getFileFromDir(path : String, regexPattern : String, pathRegexPatter
files.append(path + file)
return files
static func showConfirmPopup(
text : String,
nodeToAppend : Node,
confirmCallback : Callable,
cancelCallback : Callable = func(): null
) -> void:
var confirmPopup : ConfirmPopup = confirmPopupScene.instantiate()
nodeToAppend.add_child(confirmPopup)
confirmPopup.label.text = text
confirmPopup.confirmed.connect(confirmCallback)
confirmPopup.canceled.connect(cancelCallback)

View file

@ -29,8 +29,8 @@ baseCost = 20
value = 10.0
maxLevel = 3
upgradeDirection = 1
upgradeScaleType = 1
costScaleType = 1
upgradeScaleType = 0
costScaleType = 0
costModificator = 30.0
metadata/_custom_type_script = "uid://bg0x4egeu17qb"
@ -43,8 +43,8 @@ baseCost = 50
value = 2.0
maxLevel = 3
upgradeDirection = 1
upgradeScaleType = 1
costScaleType = 1
upgradeScaleType = 0
costScaleType = 0
costModificator = 50.0
metadata/_custom_type_script = "uid://bg0x4egeu17qb"
@ -57,8 +57,8 @@ baseCost = 50
value = 2.0
maxLevel = 3
upgradeDirection = 1
upgradeScaleType = 1
costScaleType = 1
upgradeScaleType = 0
costScaleType = 0
costModificator = 50.0
[sub_resource type="Resource" id="Resource_wnw3f"]
@ -71,7 +71,7 @@ value = 0.2
maxLevel = 2
upgradeDirection = 1
upgradeScaleType = 0
costScaleType = 1
costScaleType = 0
costModificator = 0.0
[sub_resource type="ViewportTexture" id="ViewportTexture_r52mr"]
@ -94,3 +94,4 @@ texture = SubResource("ViewportTexture_r52mr")
[node name="EnergyBar2D" parent="EnergyBar3D/SubViewport" index="0"]
max_value = 25.0
value = 25.0

View file

@ -146,11 +146,12 @@ func onBodyExited(body: Node3D) -> void:
func toggleConnection(activate : bool) -> void:
if activate:
if activate && not $Range.body_entered.is_connected(onBodyEntered):
$Range.body_entered.connect(onBodyEntered)
$Range.body_exited.connect(onBodyExited)
else:
$Range.body_entered.disconnect(onBodyEntered)
$Range.body_exited.disconnect(onBodyExited)
if $Range.body_entered.is_connected(onBodyEntered):
$Range.body_entered.disconnect(onBodyEntered)
$Range.body_exited.disconnect(onBodyExited)
availableTargets.clear()
target = null

18
UI/ConfirmPopup.gd Normal file
View file

@ -0,0 +1,18 @@
extends PanelContainer
class_name ConfirmPopup
signal confirmed
signal canceled
@onready var label : Label = $MarginContainer/ConfirmPrompt/Label
func _ready() -> void:
%CancelBtn.pressed.connect(emitPressedButton.bind(canceled))
%ConfirmBtn.pressed.connect(emitPressedButton.bind(confirmed))
func emitPressedButton(state : Signal) -> void:
state.emit()
queue_free()

1
UI/ConfirmPopup.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://8c1ej1k0vbmn

51
UI/confirm_popup.tscn Normal file
View file

@ -0,0 +1,51 @@
[gd_scene load_steps=3 format=3 uid="uid://cxn3x8dq8vawa"]
[ext_resource type="Script" uid="uid://8c1ej1k0vbmn" path="res://UI/ConfirmPopup.gd" id="1_8vkfp"]
[ext_resource type="Theme" uid="uid://bbp4qcbwkeg7x" path="res://default_theme.tres" id="1_x7bkm"]
[node name="Panel" type="PanelContainer"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -194.0
offset_right = 194.0
offset_bottom = 128.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3
theme = ExtResource("1_x7bkm")
script = ExtResource("1_8vkfp")
[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 2
theme_override_constants/margin_left = 50
theme_override_constants/margin_top = 50
theme_override_constants/margin_right = 50
theme_override_constants/margin_bottom = 25
[node name="ConfirmPrompt" type="VBoxContainer" parent="MarginContainer"]
layout_mode = 2
theme_override_constants/separation = 50
[node name="Label" type="Label" parent="MarginContainer/ConfirmPrompt"]
layout_mode = 2
size_flags_vertical = 6
horizontal_alignment = 1
[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer/ConfirmPrompt"]
layout_mode = 2
theme_override_constants/separation = 50
[node name="ConfirmBtn" type="Button" parent="MarginContainer/ConfirmPrompt/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "Confirm"
[node name="CancelBtn" type="Button" parent="MarginContainer/ConfirmPrompt/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "Cancel
"

View file

@ -21,8 +21,8 @@ func _ready() -> void:
EventBus.wave_has_change.connect(onWaveChange)
%NextWaveBtn.pressed.connect(EventBus.lauch_next_wave.emit)
$WaveCooldown/Timer.timeout.connect(updateWaveCooldownLabel)
%QuitLevelBtn.pressed.connect(Game.quitLevel)
%QuitGameBtn.pressed.connect(Game.quitGame)
%QuitLevelBtn.pressed.connect(Helper.showConfirmPopup.bind("Quit level ?", self, Game.quitLevel))
%QuitGameBtn.pressed.connect(Helper.showConfirmPopup.bind("Quit game ?", self, Game.quitGame))
$TowerInfoPanel.visible = false
if Engine.is_editor_hint():

View file

@ -18,17 +18,22 @@ func towerLinked(value) -> void:
$LeFond/Label.text = "%d" % tower.price
toggle_mode = true
add_theme_stylebox_override("pressed", GameStyleBoxFlat.createWithColor(GameColor.COLOR.SELECTED))
tower.changed.connect(towerChanged)
tower.changed.connect(onTowerChanged)
tower.energy_changed.connect(func(): $EnergyBar.value = tower.energy)
Game.money_changed.connect(onMoneyChanged)
EventBus.tower_selected.connect(func(_type): set_pressed_no_signal(_type == tower.type))
toggled.connect(func(state): EventBus.tower_selected.emit(tower.type if state else Tower.TYPE.NONE))
tooltip_text = tower.name
texture = tower.icone
towerChanged()
onTowerChanged()
func towerChanged() -> void :
func onTowerChanged() -> void :
disabled = not tower.builded && Game.money < tower.price
$EnergyBar.max_value = tower.max_energy
$EnergyBar.visible = tower.builded
$LeFond.visible = not tower.builded
func onMoneyChanged() -> void:
disabled = not tower.builded && Game.money < tower.price

View file

@ -2,7 +2,7 @@ extends Resource
class_name Upgrade
enum SCALE_TYPE { NONE, LINEAR, EXP, LOG }
enum SCALE_TYPE { LINEAR, EXP, LOG }
enum UPGRADE_DIRECTION { INCREASE = 1, DECREASE = -1 }
@ -17,7 +17,7 @@ enum UPGRADE_DIRECTION { INCREASE = 1, DECREASE = -1 }
@export_group("Upgrade Scaling")
@export var upgradeDirection : UPGRADE_DIRECTION = UPGRADE_DIRECTION.INCREASE
@export var upgradeScaleType : SCALE_TYPE = SCALE_TYPE.NONE
@export var upgradeScaleType : SCALE_TYPE = SCALE_TYPE.LINEAR
@export var costScaleType : SCALE_TYPE = SCALE_TYPE.LINEAR
@export var costModificator : float
@ -29,20 +29,22 @@ var isLevelMax : bool :
func upgradeProperty(subject: Variant, property: String) -> void:
subject.set(property, getNextValue(subject.get(property)))
subject.set(property, getNextValue(subject.get(property), value, upgradeScaleType))
func upgradeUpgrade() -> void:
level += 1
Game.money -= cost
cost += costModificator
cost = getNextValue(cost, costModificator, costScaleType)
func canUpgrade():
return not isLevelMax && Game.money >= cost
func getNextValue(oldValue):
match upgradeScaleType:
SCALE_TYPE.NONE: return oldValue + value
SCALE_TYPE.LINEAR: return oldValue + (value * upgradeDirection * level)
func getNextValue(oldValue, baseValue, scaleType : SCALE_TYPE):
match scaleType:
SCALE_TYPE.LINEAR: return oldValue + value
_:
push_warning("Upgrade scale type not defined !")
return oldValue