gd,refacto: added state chart addon and namespace cleanup

This commit is contained in:
2025-06-05 14:47:51 +02:00
parent 8818e77d23
commit 5c36765a36
239 changed files with 10430 additions and 115 deletions

View File

@ -0,0 +1,59 @@
extends Node2D
@onready var _add_coal_to_drill_button:Button = %AddCoalToDrillButton
@onready var _coal_available_label:Label = %CoalAvailableLabel
@onready var _coal_in_drill_label:Label = %CoalInDrillLabel
@onready var _state_chart:StateChart = %StateChart
var _coal_available:int = 0:
set(value):
_coal_available = value
# update the UI when this changes
_coal_available_label.text = str(_coal_available)
_add_coal_to_drill_button.disabled = _coal_available == 0
var _coal_in_drill:int = 0:
set(value):
_coal_in_drill = value
# update the UI when this changes
_coal_in_drill_label.text = str(_coal_in_drill)
if _coal_in_drill == 0:
# if there is no more coal in the drill send the
# coal_depleted event
_state_chart.send_event("coal_depleted")
else:
# otherwise send the coal_available event
_state_chart.send_event("coal_available")
func _ready():
_coal_available = 1 # we start with 1 coal
func _on_add_coal_to_drill_button_pressed():
# take one coal from the pile and put it into the generator
_coal_available -= 1
_coal_in_drill += 1
func _on_drill_has_coal_state_stepped():
# when we are in this state, we produce 2 coal and consume one of the coal in
# the drill
_coal_available += 2
_coal_in_drill -= 1
func _on_drill_has_no_coal_state_stepped():
# when we are in this state, the drill has no coal so we just flash
# the label red.
_coal_in_drill_label.modulate = Color.RED
create_tween().tween_property(_coal_in_drill_label, "modulate", Color.WHITE, 0.5)
func _on_next_round_button_pressed():
# when the next round button is pressed we handle all currently active states
_state_chart.step()

View File

@ -0,0 +1 @@
uid://dctc1yeiwh0wx

View File

@ -0,0 +1,104 @@
[gd_scene load_steps=8 format=3 uid="uid://iewxecs0uwss"]
[ext_resource type="Script" path="res://godot_state_charts_examples/stepping/stepping.gd" id="1_8d8ax"]
[ext_resource type="PackedScene" uid="uid://bcwkugn6v3oy7" path="res://addons/godot_state_charts/utilities/state_chart_debugger.tscn" id="1_u4aev"]
[ext_resource type="Script" path="res://addons/godot_state_charts/state_chart.gd" id="2_7ehwm"]
[ext_resource type="Script" path="res://addons/godot_state_charts/parallel_state.gd" id="4_ohpt6"]
[ext_resource type="Script" path="res://addons/godot_state_charts/compound_state.gd" id="5_xbwwa"]
[ext_resource type="Script" path="res://addons/godot_state_charts/atomic_state.gd" id="6_n6kwq"]
[ext_resource type="Script" path="res://addons/godot_state_charts/transition.gd" id="7_78ppn"]
[node name="SteppingExample" type="Node2D"]
script = ExtResource("1_8d8ax")
[node name="StateChartDebugger" parent="." instance=ExtResource("1_u4aev")]
offset_left = 353.0
offset_top = 5.0
offset_right = 635.0
offset_bottom = 326.0
initial_node_to_watch = NodePath("../StateChart")
[node name="NextRoundButton" type="Button" parent="."]
offset_left = 500.0
offset_top = 421.0
offset_right = 611.0
offset_bottom = 454.0
text = "Next Round"
[node name="AddCoalToDrillButton" type="Button" parent="."]
unique_name_in_owner = true
offset_left = 371.0
offset_top = 422.0
offset_right = 482.0
offset_bottom = 455.0
text = "Add Coal"
[node name="StateChart" type="Node" parent="."]
unique_name_in_owner = true
script = ExtResource("2_7ehwm")
track_in_editor = true
[node name="Root" type="Node" parent="StateChart"]
script = ExtResource("4_ohpt6")
[node name="Drill" type="Node" parent="StateChart/Root"]
script = ExtResource("5_xbwwa")
initial_state = NodePath("Drill Has No Coal")
[node name="Drill Has Coal" type="Node" parent="StateChart/Root/Drill"]
editor_description = "When in this state, the drill has coal and will produce two more coal per round."
script = ExtResource("6_n6kwq")
[node name="On Coal Depleted" type="Node" parent="StateChart/Root/Drill/Drill Has Coal"]
script = ExtResource("7_78ppn")
to = NodePath("../../Drill Has No Coal")
event = &"coal_depleted"
[node name="Drill Has No Coal" type="Node" parent="StateChart/Root/Drill"]
editor_description = "When in this state, the drill will do nothing."
script = ExtResource("6_n6kwq")
[node name="On Coal Available" type="Node" parent="StateChart/Root/Drill/Drill Has No Coal"]
script = ExtResource("7_78ppn")
to = NodePath("../../Drill Has Coal")
event = &"coal_available"
[node name="GridContainer" type="GridContainer" parent="."]
offset_left = 366.0
offset_top = 333.0
offset_right = 518.0
offset_bottom = 389.0
columns = 2
[node name="Label" type="Label" parent="GridContainer"]
layout_mode = 2
text = "Coal mined: "
[node name="CoalAvailableLabel" type="Label" parent="GridContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "0"
[node name="Label2" type="Label" parent="GridContainer"]
layout_mode = 2
text = "Coal in drill:"
[node name="CoalInDrillLabel" type="Label" parent="GridContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "0"
[node name="Info" type="Label" parent="."]
offset_left = 13.0
offset_top = 22.0
offset_right = 322.0
offset_bottom = 254.0
text = "This is a super-simple example on how to use the \"step\" function to use a state chart in a turn-based game.
This simulates a coal-driven mining drill which consumes one coal per round and produces two coal per round when running. You can press the \"Add Coal\" button to add coal into the mining drill. Press \"Next Round\" to simulate the next round. This will call the \"step\" method and then execute code depending on which state the mining drill is currently in (e.g. either mine 2 coal or do nothing if the drill is off because it has no more coal)."
autowrap_mode = 2
[connection signal="pressed" from="NextRoundButton" to="." method="_on_next_round_button_pressed"]
[connection signal="pressed" from="AddCoalToDrillButton" to="." method="_on_add_coal_to_drill_button_pressed"]
[connection signal="state_stepped" from="StateChart/Root/Drill/Drill Has Coal" to="." method="_on_drill_has_coal_state_stepped"]
[connection signal="state_stepped" from="StateChart/Root/Drill/Drill Has No Coal" to="." method="_on_drill_has_no_coal_state_stepped"]