Browse Source

Merge pull request #632 from aaronfranke/fsm-bullets

Fix bullets in the Finite State Machine demo
Aaron Franke 4 years ago
parent
commit
ef069d49ae

+ 38 - 3
2d/finite_state_machine/Demo.tscn

@@ -1,15 +1,50 @@
-[gd_scene load_steps=5 format=2]
+[gd_scene load_steps=6 format=2]
 
 [ext_resource path="res://player/Player.tscn" type="PackedScene" id=1]
-[ext_resource path="res://debug/Explanations.tscn" type="PackedScene" id=2]
+[ext_resource path="res://fonts/source_code_pro_explanations.tres" type="DynamicFont" id=2]
 [ext_resource path="res://debug/StatesStackDiplayer.tscn" type="PackedScene" id=3]
 [ext_resource path="res://debug/ControlsPanel.tscn" type="PackedScene" id=4]
+[ext_resource path="res://fonts/source_code_pro_explanations_bold.tres" type="DynamicFont" id=5]
 
 [node name="Demo" type="Node"]
 
 [node name="Player" parent="." instance=ExtResource( 1 )]
+position = Vector2( 640, 400 )
 
-[node name="Explanations" parent="." instance=ExtResource( 2 )]
+[node name="Explanations" type="RichTextLabel" parent="."]
+anchor_right = 1.0
+anchor_bottom = 1.0
+margin_left = 10.0
+margin_top = -370.0
+margin_right = -10.0
+margin_bottom = -730.0
+rect_clip_content = false
+mouse_filter = 2
+size_flags_vertical = 4
+custom_fonts/bold_font = ExtResource( 5 )
+custom_fonts/normal_font = ExtResource( 2 )
+bbcode_enabled = true
+bbcode_text = "This example shows how to apply the State programming pattern in GDScript, including Hierarchical States, and a pushdown automaton.
+
+States are common in games. You can use the pattern to:
+
+1. Separate each behavior and transitions between behaviors, thus make scripts shorter and easier to manage
+2. Respect the Single Responsibility Principle. Each State object represents [b]one[/b] action
+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.
+
+You can read more about States in the excellent [url=http://gameprogrammingpatterns.com/state.html]Game Programming Patterns ebook[/url]."
+text = "This example shows how to apply the State programming pattern in GDScript, including Hierarchical States, and a pushdown automaton.
+
+States are common in games. You can use the pattern to:
+
+1. Separate each behavior and transitions between behaviors, thus make scripts shorter and easier to manage
+2. Respect the Single Responsibility Principle. Each State object represents one action
+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.
+
+You can read more about States in the excellent Game Programming Patterns ebook."
+__meta__ = {
+"_edit_lock_": true
+}
 
 [node name="Control" type="Control" parent="."]
 anchor_right = 1.0

+ 7 - 5
2d/finite_state_machine/player/Player.tscn

@@ -41,7 +41,6 @@ use_filter = true
 font_data = ExtResource( 14 )
 
 [node name="Player" type="KinematicBody2D"]
-position = Vector2( 628.826, 391.266 )
 script = ExtResource( 1 )
 __meta__ = {
 "_edit_horizontal_guides_": [  ]
@@ -81,11 +80,11 @@ texture = ExtResource( 9 )
 [node name="BodyPivot" type="Position2D" parent="."]
 
 [node name="Body" type="Sprite" parent="BodyPivot"]
-position = Vector2( 0, -58.8242 )
+position = Vector2( 0, -58 )
 texture = ExtResource( 10 )
 
 [node name="BulletSpawn" type="Node2D" parent="BodyPivot"]
-position = Vector2( 1.17401, -61.266 )
+position = Vector2( 0, -58 )
 script = ExtResource( 11 )
 
 [node name="CooldownTimer" type="Timer" parent="BodyPivot/BulletSpawn"]
@@ -93,7 +92,7 @@ wait_time = 0.2
 one_shot = true
 
 [node name="WeaponPivot" type="Position2D" parent="BodyPivot"]
-position = Vector2( 1.17401, -61.266 )
+position = Vector2( 0, -58 )
 script = ExtResource( 12 )
 
 [node name="Offset" type="Position2D" parent="BodyPivot/WeaponPivot"]
@@ -110,11 +109,14 @@ margin_top = -172.0
 margin_right = 110.0
 margin_bottom = -138.0
 custom_fonts/font = SubResource( 4 )
-text = "Test"
+text = "Idle"
 align = 1
 valign = 1
 uppercase = true
 script = ExtResource( 15 )
+__meta__ = {
+"_edit_use_anchors_": false
+}
 
 [connection signal="state_changed" from="StateMachine" to="BodyPivot/WeaponPivot/Offset/Sword" method="_on_StateMachine_state_changed"]
 [connection signal="state_changed" from="StateMachine" to="StateNameDisplayer" method="_on_StateMachine_state_changed"]

+ 4 - 3
2d/finite_state_machine/player/bullet/bullet_spawner.gd

@@ -4,14 +4,15 @@ var bullet = preload("Bullet.tscn")
 
 func _unhandled_input(event):
 	if event.is_action_pressed("fire"):
-		fire(owner.look_direction)
+		fire()
 
 
-func fire(direction):
+func fire():
 	if not $CooldownTimer.is_stopped():
 		return
 
 	$CooldownTimer.start()
 	var new_bullet = bullet.instance()
-	new_bullet.direction = direction
 	add_child(new_bullet)
+	new_bullet.position = global_position
+	new_bullet.direction = owner.look_direction