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

Binary file not shown.

After

Width:  |  Height:  |  Size: 635 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://p8vbysdruo4o"
path="res://.godot/imported/break.png-76e3da8567a2ce2ef66cf2a31e3edc5e.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://godot_state_charts_examples/platformer/breaking_box/break.png"
dest_files=["res://.godot/imported/break.png-76e3da8567a2ce2ef66cf2a31e3edc5e.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

View File

@ -0,0 +1,54 @@
extends StaticBody2D
signal clicked(node)
@export var health:int = 3
@onready var _state_chart:StateChart = %StateChart
@onready var _label:Label = %Label
@onready var _detection_shape = %CollisionShape2D
@onready var _animation_player = %AnimationPlayer
func _ready():
_label.text = str(health)
func _on_idle_state_entered():
# When we enter idle state we play the idle animation
_animation_player.play("Idle")
func _on_detection_area_body_entered(_body):
# When someone enters the area, reduce the health
# and notify the state chart.
health = max(0, health-1)
_label.text = str(health)
_state_chart.set_expression_property("health", health)
_state_chart.send_event("health_changed")
func _on_blinking_state_entered():
# when we enter blinking state, play the hit animation
_animation_player.play("Hit")
func _on_dying_state_entered():
# When we enter dying state, play the final death animation
_animation_player.play("Break")
func _on_dead_state_entered():
# When we enter dead state, we're done and can free the node.
queue_free()
func _on_animation_player_animation_finished(_anim_name):
# Forward animation_finished events to the state chart
_state_chart.send_event("animation_finished")
# This is to make the box clickable. Clicking it will show it in the debugger.
func _on_input_event(_viewport, event, _shape_idx):
# if the left mouse button is up emit the clicked signal
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed() == false:
clicked.emit(self)

View File

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

View File

@ -0,0 +1,425 @@
[gd_scene load_steps=19 format=3 uid="uid://h0af1dot82p3"]
[ext_resource type="Script" path="res://godot_state_charts_examples/platformer/breaking_box/breaking_box.gd" id="1_c2rh2"]
[ext_resource type="Texture2D" uid="uid://c5dogybcf6yfo" path="res://godot_state_charts_examples/platformer/breaking_box/idle.png" id="1_dixph"]
[ext_resource type="Script" path="res://addons/godot_state_charts/state_chart.gd" id="2_atynw"]
[ext_resource type="Texture2D" uid="uid://dq0ww6xvgeap3" path="res://godot_state_charts_examples/platformer/breaking_box/hit.png" id="3_ngvxc"]
[ext_resource type="Texture2D" uid="uid://p8vbysdruo4o" path="res://godot_state_charts_examples/platformer/breaking_box/break.png" id="4_jb7vf"]
[ext_resource type="Script" path="res://addons/godot_state_charts/compound_state.gd" id="5_wouwd"]
[ext_resource type="Script" path="res://addons/godot_state_charts/atomic_state.gd" id="6_p4jwe"]
[ext_resource type="Script" path="res://addons/godot_state_charts/transition.gd" id="7_1ecad"]
[ext_resource type="Script" path="res://addons/godot_state_charts/expression_guard.gd" id="8_8riiw"]
[sub_resource type="Animation" id="Animation_toibu"]
resource_name = "Break"
length = 0.3
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("BoxSprite:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("4_jb7vf")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("BoxSprite:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [4]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("BoxSprite:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.2),
"transitions": PackedFloat32Array(16.5643, 1),
"update": 0,
"values": [0, 3]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Label:visible")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/4/type = "value"
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/path = NodePath("BoxCollider:disabled")
tracks/4/interp = 1
tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/5/type = "value"
tracks/5/imported = false
tracks/5/enabled = true
tracks/5/path = NodePath("BoxSprite:position")
tracks/5/interp = 1
tracks/5/loop_wrap = true
tracks/5/keys = {
"times": PackedFloat32Array(0, 0.2),
"transitions": PackedFloat32Array(1, 8.87654),
"update": 0,
"values": [Vector2(0, -10), Vector2(0, 50)]
}
tracks/6/type = "value"
tracks/6/imported = false
tracks/6/enabled = true
tracks/6/path = NodePath("BoxSprite:modulate:a")
tracks/6/interp = 1
tracks/6/loop_wrap = true
tracks/6/keys = {
"times": PackedFloat32Array(0, 0.2),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [1.0, 0.0]
}
[sub_resource type="Animation" id="Animation_rc67x"]
resource_name = "Hit"
length = 0.2
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("BoxSprite:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("3_ngvxc")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("BoxSprite:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [2]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("BoxSprite:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.1),
"transitions": PackedFloat32Array(1, 1),
"update": 1,
"values": [0, 1]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Label:visible")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
[sub_resource type="Animation" id="Animation_yu346"]
resource_name = "Idle"
length = 0.1
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("BoxSprite:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_dixph")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("BoxSprite:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [1]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("BoxSprite:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [0]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Label:visible")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/4/type = "value"
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/path = NodePath("BoxCollider:disabled")
tracks/4/interp = 1
tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
[sub_resource type="Animation" id="Animation_a7q8m"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("BoxSprite:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_dixph")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("BoxSprite:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [1]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("BoxSprite:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [0]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Label:visible")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/4/type = "value"
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/path = NodePath("BoxCollider:disabled")
tracks/4/interp = 1
tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/5/type = "value"
tracks/5/imported = false
tracks/5/enabled = true
tracks/5/path = NodePath("BoxSprite:position")
tracks/5/interp = 1
tracks/5/loop_wrap = true
tracks/5/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, -10)]
}
tracks/6/type = "value"
tracks/6/imported = false
tracks/6/enabled = true
tracks/6/path = NodePath("BoxSprite:modulate")
tracks/6/interp = 1
tracks/6/loop_wrap = true
tracks/6/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Color(1, 1, 1, 1)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_t7nrw"]
_data = {
"Break": SubResource("Animation_toibu"),
"Hit": SubResource("Animation_rc67x"),
"Idle": SubResource("Animation_yu346"),
"RESET": SubResource("Animation_a7q8m")
}
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8vd2b"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jbwph"]
size = Vector2(14.5, 2)
[sub_resource type="Resource" id="Resource_v858x"]
script = ExtResource("8_8riiw")
expression = "health > 0"
[sub_resource type="Resource" id="Resource_j86i4"]
script = ExtResource("8_8riiw")
expression = "health <= 0"
[node name="BreakingBox" type="StaticBody2D"]
input_pickable = true
script = ExtResource("1_c2rh2")
[node name="BoxSprite" type="Sprite2D" parent="."]
texture_filter = 1
position = Vector2(0, -10)
texture = ExtResource("1_dixph")
[node name="Label" type="Label" parent="."]
unique_name_in_owner = true
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -20.0
offset_top = -23.0
offset_right = 20.0
offset_bottom = 3.0
grow_horizontal = 2
grow_vertical = 2
theme_override_font_sizes/font_size = 10
text = "3"
horizontal_alignment = 1
vertical_alignment = 1
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
unique_name_in_owner = true
editor_description = "The animation player will send an \"animation_finished\" event back to the state chart. This will then take the appropriate transitions (e.g. switch back to the idle animation)."
libraries = {
"": SubResource("AnimationLibrary_t7nrw")
}
[node name="BoxCollider" type="CollisionShape2D" parent="."]
position = Vector2(0, -10)
shape = SubResource("RectangleShape2D_8vd2b")
[node name="DetectionArea" type="Area2D" parent="."]
position = Vector2(0, -3)
collision_layer = 0
[node name="CollisionShape2D" type="CollisionShape2D" parent="DetectionArea"]
unique_name_in_owner = true
position = Vector2(0, -19)
shape = SubResource("RectangleShape2D_jbwph")
[node name="StateChart" type="Node" parent="."]
unique_name_in_owner = true
script = ExtResource("2_atynw")
track_in_editor = true
[node name="Root" type="Node" parent="StateChart"]
script = ExtResource("5_wouwd")
initial_state = NodePath("Idle")
[node name="Idle" type="Node" parent="StateChart/Root"]
script = ExtResource("6_p4jwe")
[node name="On Damage" type="Node" parent="StateChart/Root/Idle"]
script = ExtResource("7_1ecad")
to = NodePath("../../Blinking")
event = &"health_changed"
guard = SubResource("Resource_v858x")
delay_in_seconds = "0.0"
[node name="On Break" type="Node" parent="StateChart/Root/Idle"]
script = ExtResource("7_1ecad")
to = NodePath("../../Dying")
event = &"health_changed"
guard = SubResource("Resource_j86i4")
delay_in_seconds = "0.0"
[node name="Blinking" type="Node" parent="StateChart/Root"]
script = ExtResource("6_p4jwe")
[node name="Animation Finished" type="Node" parent="StateChart/Root/Blinking"]
script = ExtResource("7_1ecad")
to = NodePath("../../Idle")
event = &"animation_finished"
delay_in_seconds = "0.0"
[node name="Dying" type="Node" parent="StateChart/Root"]
script = ExtResource("6_p4jwe")
[node name="Animation Finished" type="Node" parent="StateChart/Root/Dying"]
script = ExtResource("7_1ecad")
to = NodePath("../../Dead")
event = &"animation_finished"
delay_in_seconds = "0.0"
[node name="Dead" type="Node" parent="StateChart/Root"]
script = ExtResource("6_p4jwe")
[connection signal="input_event" from="." to="." method="_on_input_event"]
[connection signal="animation_finished" from="AnimationPlayer" to="." method="_on_animation_player_animation_finished"]
[connection signal="body_entered" from="DetectionArea" to="." method="_on_detection_area_body_entered"]
[connection signal="state_entered" from="StateChart/Root/Idle" to="." method="_on_idle_state_entered"]
[connection signal="state_entered" from="StateChart/Root/Blinking" to="." method="_on_blinking_state_entered"]
[connection signal="state_entered" from="StateChart/Root/Dying" to="." method="_on_dying_state_entered"]
[connection signal="state_entered" from="StateChart/Root/Dead" to="." method="_on_dead_state_entered"]

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dq0ww6xvgeap3"
path="res://.godot/imported/hit.png-1c36fd330c418d67632dbaced016a36f.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://godot_state_charts_examples/platformer/breaking_box/hit.png"
dest_files=["res://.godot/imported/hit.png-1c36fd330c418d67632dbaced016a36f.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

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 B

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://c5dogybcf6yfo"
path="res://.godot/imported/idle.png-ceff55276331d87b08aa0aee62d3e6af.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://godot_state_charts_examples/platformer/breaking_box/idle.png"
dest_files=["res://.godot/imported/idle.png-ceff55276331d87b08aa0aee62d3e6af.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