Skip to content

Commit f28a09e

Browse files
committed
Fix bullets in the Finite State Machine demo
1 parent 85c1805 commit f28a09e

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

2d/finite_state_machine/Demo.tscn

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

33
[ext_resource path="res://player/Player.tscn" type="PackedScene" id=1]
4-
[ext_resource path="res://debug/Explanations.tscn" type="PackedScene" id=2]
4+
[ext_resource path="res://fonts/source_code_pro_explanations.tres" type="DynamicFont" id=2]
55
[ext_resource path="res://debug/StatesStackDiplayer.tscn" type="PackedScene" id=3]
66
[ext_resource path="res://debug/ControlsPanel.tscn" type="PackedScene" id=4]
7+
[ext_resource path="res://fonts/source_code_pro_explanations_bold.tres" type="DynamicFont" id=5]
78

89
[node name="Demo" type="Node"]
910

1011
[node name="Player" parent="." instance=ExtResource( 1 )]
12+
position = Vector2( 640, 400 )
1113

12-
[node name="Explanations" parent="." instance=ExtResource( 2 )]
14+
[node name="Explanations" type="RichTextLabel" parent="."]
15+
anchor_right = 1.0
16+
anchor_bottom = 1.0
17+
margin_left = 10.0
18+
margin_top = -370.0
19+
margin_right = -10.0
20+
margin_bottom = -730.0
21+
rect_clip_content = false
22+
mouse_filter = 2
23+
size_flags_vertical = 4
24+
custom_fonts/bold_font = ExtResource( 5 )
25+
custom_fonts/normal_font = ExtResource( 2 )
26+
bbcode_enabled = true
27+
bbcode_text = "This example shows how to apply the State programming pattern in GDScript, including Hierarchical States, and a pushdown automaton.
28+
29+
States are common in games. You can use the pattern to:
30+
31+
1. Separate each behavior and transitions between behaviors, thus make scripts shorter and easier to manage
32+
2. Respect the Single Responsibility Principle. Each State object represents [b]one[/b] action
33+
3. Improve your code's structure. Look at the scene tree and FileSystem tab: without looking at the code, you'll know what the Player can or cannot do.
34+
35+
You can read more about States in the excellent [url=http://gameprogrammingpatterns.com/state.html]Game Programming Patterns ebook[/url]."
36+
text = "This example shows how to apply the State programming pattern in GDScript, including Hierarchical States, and a pushdown automaton.
37+
38+
States are common in games. You can use the pattern to:
39+
40+
1. Separate each behavior and transitions between behaviors, thus make scripts shorter and easier to manage
41+
2. Respect the Single Responsibility Principle. Each State object represents one action
42+
3. Improve your code's structure. Look at the scene tree and FileSystem tab: without looking at the code, you'll know what the Player can or cannot do.
43+
44+
You can read more about States in the excellent Game Programming Patterns ebook."
45+
__meta__ = {
46+
"_edit_lock_": true
47+
}
1348

1449
[node name="Control" type="Control" parent="."]
1550
anchor_right = 1.0

2d/finite_state_machine/player/Player.tscn

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use_filter = true
4141
font_data = ExtResource( 14 )
4242

4343
[node name="Player" type="KinematicBody2D"]
44-
position = Vector2( 628.826, 391.266 )
4544
script = ExtResource( 1 )
4645
__meta__ = {
4746
"_edit_horizontal_guides_": [ ]
@@ -81,19 +80,19 @@ texture = ExtResource( 9 )
8180
[node name="BodyPivot" type="Position2D" parent="."]
8281

8382
[node name="Body" type="Sprite" parent="BodyPivot"]
84-
position = Vector2( 0, -58.8242 )
83+
position = Vector2( 0, -58 )
8584
texture = ExtResource( 10 )
8685

8786
[node name="BulletSpawn" type="Node2D" parent="BodyPivot"]
88-
position = Vector2( 1.17401, -61.266 )
87+
position = Vector2( 0, -58 )
8988
script = ExtResource( 11 )
9089

9190
[node name="CooldownTimer" type="Timer" parent="BodyPivot/BulletSpawn"]
9291
wait_time = 0.2
9392
one_shot = true
9493

9594
[node name="WeaponPivot" type="Position2D" parent="BodyPivot"]
96-
position = Vector2( 1.17401, -61.266 )
95+
position = Vector2( 0, -58 )
9796
script = ExtResource( 12 )
9897

9998
[node name="Offset" type="Position2D" parent="BodyPivot/WeaponPivot"]
@@ -110,11 +109,14 @@ margin_top = -172.0
110109
margin_right = 110.0
111110
margin_bottom = -138.0
112111
custom_fonts/font = SubResource( 4 )
113-
text = "Test"
112+
text = "Idle"
114113
align = 1
115114
valign = 1
116115
uppercase = true
117116
script = ExtResource( 15 )
117+
__meta__ = {
118+
"_edit_use_anchors_": false
119+
}
118120

119121
[connection signal="state_changed" from="StateMachine" to="BodyPivot/WeaponPivot/Offset/Sword" method="_on_StateMachine_state_changed"]
120122
[connection signal="state_changed" from="StateMachine" to="StateNameDisplayer" method="_on_StateMachine_state_changed"]

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ var bullet = preload("Bullet.tscn")
44

55
func _unhandled_input(event):
66
if event.is_action_pressed("fire"):
7-
fire(owner.look_direction)
7+
fire()
88

99

10-
func fire(direction):
10+
func fire():
1111
if not $CooldownTimer.is_stopped():
1212
return
1313

1414
$CooldownTimer.start()
1515
var new_bullet = bullet.instance()
16-
new_bullet.direction = direction
1716
add_child(new_bullet)
17+
new_bullet.position = global_position
18+
new_bullet.direction = owner.look_direction

0 commit comments

Comments
 (0)