Skip to content

Commit 9975c95

Browse files
committed
Update Finite State Machine demo
1 parent ac88236 commit 9975c95

File tree

25 files changed

+163
-146
lines changed

25 files changed

+163
-146
lines changed

2d/finite_state_machine/Demo.tscn

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[gd_scene load_steps=5 format=2]
22

3-
[ext_resource path="res://debug/ControlsPanel.tscn" type="PackedScene" id=1]
3+
[ext_resource path="res://player/Player.tscn" type="PackedScene" id=1]
44
[ext_resource path="res://debug/Explanations.tscn" type="PackedScene" id=2]
55
[ext_resource path="res://debug/StatesStackDiplayer.tscn" type="PackedScene" id=3]
6-
[ext_resource path="res://player/Player.tscn" type="PackedScene" id=4]
6+
[ext_resource path="res://debug/ControlsPanel.tscn" type="PackedScene" id=4]
77

88
[node name="Demo" type="Node"]
99

10-
[node name="Player" parent="." instance=ExtResource( 4 )]
10+
[node name="Player" parent="." instance=ExtResource( 1 )]
1111

1212
[node name="Explanations" parent="." instance=ExtResource( 2 )]
1313

@@ -20,6 +20,6 @@ __meta__ = {
2020

2121
[node name="StatesStackDiplayer" parent="Control" instance=ExtResource( 3 )]
2222

23-
[node name="ControlsPanel" parent="Control" instance=ExtResource( 1 )]
23+
[node name="ControlsPanel" parent="Control" instance=ExtResource( 4 )]
2424

2525
[editable path="Player"]

2d/finite_state_machine/debug/ControlsPanel.tscn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_scene load_steps=2 format=2]
22

3-
[ext_resource path="res://fonts/source_code_pro_explanations.tres" type="DynamicFont" id=2]
3+
[ext_resource path="res://fonts/source_code_pro_explanations.tres" type="DynamicFont" id=1]
44

55
[node name="ControlsPanel" type="Panel"]
66
anchor_left = 1.0
@@ -18,7 +18,7 @@ margin_left = 10.0
1818
margin_top = 10.0
1919
margin_right = -10.0
2020
margin_bottom = -10.0
21-
custom_fonts/font = ExtResource( 2 )
21+
custom_fonts/font = ExtResource( 1 )
2222
text = "Shoot:
2323
Attack:
2424
Stagger:
@@ -32,7 +32,7 @@ margin_left = 10.0
3232
margin_top = 10.0
3333
margin_right = -10.0
3434
margin_bottom = -10.0
35-
custom_fonts/font = ExtResource( 2 )
35+
custom_fonts/font = ExtResource( 1 )
3636
text = "R
3737
F
3838
X

2d/finite_state_machine/debug/states_stack_displayer.gd

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ extends Panel
22

33
onready var fsm_node = get_node("../../Player/StateMachine")
44

5-
func _process(delta):
6-
var states_names = ''
7-
var numbers = ''
5+
func _process(_delta):
6+
var states_names = ""
7+
var numbers = ""
88
var index = 0
99
for state in fsm_node.states_stack:
1010
states_names += state.get_name() + '\n'
1111
numbers += str(index) + '\n'
1212
index += 1
13-
1413
$States.text = states_names
1514
$Numbers.text = numbers

2d/finite_state_machine/player/Player.tscn

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ __meta__ = {
4949

5050
[node name="StateMachine" type="Node" parent="."]
5151
script = ExtResource( 2 )
52-
START_STATE = NodePath("Idle")
5352

5453
[node name="Idle" type="Node" parent="StateMachine"]
5554
script = ExtResource( 3 )
@@ -116,7 +115,6 @@ align = 1
116115
valign = 1
117116
uppercase = true
118117
script = ExtResource( 15 )
119-
120118
[connection signal="state_changed" from="StateMachine" to="BodyPivot/WeaponPivot/Offset/Sword" method="_on_StateMachine_state_changed"]
121119
[connection signal="state_changed" from="StateMachine" to="StateNameDisplayer" method="_on_StateMachine_state_changed"]
122120
[connection signal="animation_finished" from="AnimationPlayer" to="StateMachine" method="_on_animation_finished"]

2d/finite_state_machine/player/bullet/Bullet.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ script = ExtResource( 1 )
1212

1313
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
1414
shape = SubResource( 1 )
15-
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
extends KinematicBody2D
22

3-
43
var direction = Vector2()
5-
export(float) var SPEED = 1000.0
6-
4+
export(float) var speed = 1000.0
75

86
func _ready():
97
set_as_toplevel(true)
@@ -13,16 +11,16 @@ func _physics_process(delta):
1311
if is_outside_view_bounds():
1412
queue_free()
1513

16-
var motion = direction * SPEED * delta
14+
var motion = direction * speed * delta
1715
var collision_info = move_and_collide(motion)
1816
if collision_info:
1917
queue_free()
2018

2119

2220
func is_outside_view_bounds():
2321
return position.x > OS.get_screen_size().x or position.x < 0.0 \
24-
or position.y > OS.get_screen_size().y or position.y < 0.0
22+
or position.y > OS.get_screen_size().y or position.y < 0.0
2523

2624

2725
func _draw():
28-
draw_circle(Vector2(), $CollisionShape2D.shape.radius, Color('#ffffff'))
26+
draw_circle(Vector2(), $CollisionShape2D.shape.radius, Color.white)

2d/finite_state_machine/player/bullet/bullet_spawner.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ func _input(event):
66
if event.is_action_pressed("fire"):
77
fire(owner.look_direction)
88

9+
910
func fire(direction):
1011
if not $CooldownTimer.is_stopped():
1112
return
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
"""
2-
The Player is a KinematicBody2D, in other words a physics-driven object.
3-
It can move, collide with the world...
4-
It HAS a state machine, but the body and the state machine are separate.
5-
"""
61
extends KinematicBody2D
2+
# The Player is a KinematicBody2D, in other words a physics-driven object.
3+
# It can move, collide with the world, etc...
4+
# The player has a state machine, but the body and the state machine are separate.
75

86
signal direction_changed(new_direction)
97

10-
var look_direction = Vector2(1, 0) setget set_look_direction
8+
var look_direction = Vector2.RIGHT setget set_look_direction
119

12-
func take_damage(attacker, amount, effect=null):
13-
if self.is_a_parent_of(attacker):
10+
func take_damage(attacker, amount, effect = null):
11+
if is_a_parent_of(attacker):
1412
return
1513
$States/Stagger.knockback_direction = (attacker.global_position - global_position).normalized()
1614
$Health.take_damage(amount, effect)
1715

16+
1817
func set_dead(value):
1918
set_process_input(not value)
2019
set_physics_process(not value)
2120
$CollisionPolygon2D.disabled = value
2221

22+
2323
func set_look_direction(value):
2424
look_direction = value
2525
emit_signal("direction_changed", value)

2d/finite_state_machine/player/player_state_machine.gd

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ func _ready():
99
"attack": $Attack,
1010
}
1111

12+
1213
func _change_state(state_name):
13-
"""
14-
The base state_machine interface this node extends does most of the work
15-
"""
14+
# The base state_machine interface this node extends does most of the work.
1615
if not _active:
1716
return
1817
if state_name in ["stagger", "jump", "attack"]:
@@ -21,11 +20,10 @@ func _change_state(state_name):
2120
$Jump.initialize($Move.speed, $Move.velocity)
2221
._change_state(state_name)
2322

23+
2424
func _input(event):
25-
"""
26-
Here we only handle input that can interrupt states, attacking in this case
27-
otherwise we let the state node handle it
28-
"""
25+
# Here we only handle input that can interrupt states, attacking in this case,
26+
# otherwise we let the state node handle it.
2927
if event.is_action_pressed("attack"):
3028
if current_state in [$Attack, $Stagger]:
3129
return

2d/finite_state_machine/player/states/combat/attack.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ extends "res://state_machine/state.gd"
33
func enter():
44
owner.get_node("AnimationPlayer").play("idle")
55

6+
67
func _on_Sword_attack_finished():
78
emit_signal("finished", "previous")

0 commit comments

Comments
 (0)