Skip to content

Commit 704ebee

Browse files
authored
Merge pull request #400 from aaronfranke/2d-physics-pl
Update 2D physics platformer
2 parents 60f515e + 3eeba85 commit 704ebee

File tree

17 files changed

+143
-156
lines changed

17 files changed

+143
-156
lines changed

2d/particles/particles.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ scale = 0.5
190190
scale_random = 1.0
191191
color = Color( 0.214844, 1, 0.392731, 1 )
192192

193-
[node name="Node" type="Node"]
193+
[node name="Particles" type="Node"]
194194

195195
[node name="Fire" type="Particles2D" parent="."]
196196
material = SubResource( 1 )

2d/physics_platformer/Stage.tscn

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

33
[ext_resource path="res://Tileset.tres" type="TileSet" id=1]
44
[ext_resource path="res://coin/Coin.tscn" type="PackedScene" id=2]
@@ -8,14 +8,7 @@
88
[ext_resource path="res://player/Player.tscn" type="PackedScene" id=6]
99
[ext_resource path="res://enemy/Enemy.tscn" type="PackedScene" id=7]
1010
[ext_resource path="res://background/ParallaxBg.tscn" type="PackedScene" id=8]
11-
12-
13-
14-
15-
16-
17-
18-
11+
[ext_resource path="res://audio/music.ogg" type="AudioStream" id=9]
1912

2013
[sub_resource type="PhysicsMaterial" id=1]
2114
friction = 0.0
@@ -53,7 +46,7 @@ friction = 0.0
5346
[sub_resource type="PhysicsMaterial" id=12]
5447
friction = 0.0
5548

56-
[node name="Stage" type="Node"]
49+
[node name="Stage" type="Node2D"]
5750

5851
[node name="TileMap" type="TileMap" parent="."]
5952
tile_set = ExtResource( 1 )
@@ -64,7 +57,8 @@ __meta__ = {
6457
"_edit_lock_": true
6558
}
6659

67-
[node name="Coins" type="Node" parent="."]
60+
[node name="Coins" type="Node2D" parent="."]
61+
editor/display_folded = true
6862

6963
[node name="Coin" parent="Coins" instance=ExtResource( 2 )]
7064
position = Vector2( 672, 1179 )
@@ -192,7 +186,8 @@ position = Vector2( 4236.75, 541.058 )
192186
[node name="Coin42" parent="Coins" instance=ExtResource( 2 )]
193187
position = Vector2( 4172.75, 541.058 )
194188

195-
[node name="Props" type="Node" parent="."]
189+
[node name="Props" type="Node2D" parent="."]
190+
editor/display_folded = true
196191

197192
[node name="MovingPlatform" parent="Props" instance=ExtResource( 3 )]
198193
position = Vector2( 1451.86, 742.969 )
@@ -219,7 +214,8 @@ position = Vector2( 927.698, 1120.81 )
219214
position = Vector2( 251.684, 1045.6 )
220215
physics_material_override = SubResource( 1 )
221216

222-
[node name="Enemies" type="Node" parent="."]
217+
[node name="Enemies" type="Node2D" parent="."]
218+
editor/display_folded = true
223219

224220
[node name="Enemy1" parent="Enemies" instance=ExtResource( 7 )]
225221
position = Vector2( 834.664, 1309.6 )
@@ -277,3 +273,6 @@ size_flags_vertical = 0
277273
text = "This is a simple demo on how to make a platformer game with Godot.\"This version uses physics and the 2D physics engine for motion and collision.\"\"The demo also shows the benefits of using the scene system, where coins,\"enemies and the player are edited separatedly and instanced in the stage.\"\"To edit the base tiles for the tileset, open the tileset_edit.tscn file and follow \"instructions.\""
278274
autowrap = true
279275

276+
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
277+
stream = ExtResource( 9 )
278+
autoplay = true

2d/physics_platformer/Stage2.tscn

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
[ext_resource path="res://player/Player.tscn" type="PackedScene" id=2]
55
[ext_resource path="res://background/ParallaxBg.tscn" type="PackedScene" id=3]
66

7-
8-
9-
10-
[node name="Stage" type="Node"]
7+
[node name="Stage2" type="Node2D"]
118

129
[node name="TileMap" type="TileMap" parent="."]
1310
tile_set = ExtResource( 1 )
@@ -32,4 +29,3 @@ size_flags_horizontal = 2
3229
size_flags_vertical = 0
3330
text = "This is a simple demo on how to make a platformer game with Godot.\"This version uses physics and the 2D physics engine for motion and collision.\"\"The demo also shows the benefits of using the scene system, where coins,\"enemies and the player are edited separatedly and instanced in the stage.\"\"To edit the base tiles for the tileset, open the tileset_edit.tscn file and follow \"instructions.\""
3431
autowrap = true
35-

2d/physics_platformer/TilesetEdit.tscn

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[ext_resource path="res://tiles_demo.png" type="Texture" id=1]
44

5-
[node name="Node" type="Node"]
5+
[node name="TilesetEdit" type="Node2D"]
66

77
[node name="Floor" type="Sprite" parent="."]
88
texture = ExtResource( 1 )
@@ -156,4 +156,3 @@ text = "This scene serves as a tool for editing the tileset.
156156
This will save a tileset. Saving over it will merge your changes.
157157
158158
Finally, the saved tileset resource (tileset.tres in this case), can be opened to be used into a TileMap node for editing a tile map."
159-

2d/physics_platformer/background/ParallaxBg.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,3 @@ position = Vector2( 0, 225 )
7171
texture = ExtResource( 6 )
7272
centered = false
7373
region_rect = Rect2( 0, 0, 800, 256 )
74-

2d/physics_platformer/coin/Coin.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ emission_sphere_radius = 20.0
105105
flag_disable_z = true
106106
gravity = Vector3( 0, 0, 0 )
107107
initial_velocity = 1.0
108+
angular_velocity = 0.0191222
108109
orbit_velocity = 0.0
109110
orbit_velocity_random = 0.0
110111
scale = 0.3
@@ -117,7 +118,7 @@ script = ExtResource( 1 )
117118
texture = ExtResource( 2 )
118119
hframes = 4
119120

120-
[node name="Anim" type="AnimationPlayer" parent="."]
121+
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
121122
autoplay = "spin"
122123
anims/spin = SubResource( 1 )
123124
anims/taken = SubResource( 2 )
@@ -139,5 +140,4 @@ texture = ExtResource( 4 )
139140

140141
[node name="Enabler" type="VisibilityEnabler2D" parent="."]
141142
pause_particles = false
142-
143143
[connection signal="body_entered" from="." to="." method="_on_body_enter"]

2d/physics_platformer/coin/coin.gd

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
extends Area2D
2-
31
class_name Coin
2+
extends Area2D
43

5-
# Member variables
64
var taken = false
75

8-
9-
func _on_body_enter( body ):
6+
func _on_body_enter(body):
107
if not taken and body is Player:
11-
($Anim as AnimationPlayer).play("taken")
8+
($AnimationPlayer as AnimationPlayer).play("taken")

2d/physics_platformer/enemy/Enemy.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ flag_disable_z = true
117117
spread = 180.0
118118
gravity = Vector3( 0, 98, 0 )
119119
initial_velocity = 322.73
120+
angular_velocity = 1.5
120121
orbit_velocity = 0.0
121122
orbit_velocity_random = 0.0
122123
scale = 2.4
@@ -133,7 +134,7 @@ position = Vector2( 16.2569, 11.0034 )
133134
scale = Vector2( 23.5056, 10.8629 )
134135
pause_particles = false
135136

136-
[node name="Anim" type="AnimationPlayer" parent="."]
137+
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
137138
anims/explode = SubResource( 2 )
138139
anims/idle = SubResource( 3 )
139140
anims/walk = SubResource( 4 )
@@ -179,4 +180,3 @@ stream = ExtResource( 4 )
179180

180181
[node name="SoundExplode" type="AudioStreamPlayer2D" parent="."]
181182
stream = ExtResource( 5 )
182-
Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,30 @@
1-
extends RigidBody2D
2-
31
class_name Enemy
2+
extends RigidBody2D
43

5-
# Member variables
64
const WALK_SPEED = 50
7-
const STATE_WALKING = 0
8-
const STATE_DYING = 1
95

10-
# state machine
11-
var state = STATE_WALKING
6+
enum State {
7+
WALKING,
8+
DYING
9+
}
10+
11+
var state = State.WALKING
1212

1313
var direction = -1
1414
var anim = ""
1515

16-
onready var rc_left = $RaycastLeft
17-
onready var rc_right = $RaycastRight
18-
1916
var Bullet = preload("res://player/bullet.gd")
2017

21-
22-
func _die():
23-
queue_free()
24-
25-
func _pre_explode():
26-
#make sure nothing collides against this
27-
$Shape1.queue_free()
28-
$Shape2.queue_free()
29-
$Shape3.queue_free()
30-
31-
# Stay there
32-
mode = MODE_STATIC
33-
($SoundExplode as AudioStreamPlayer2D).play()
34-
35-
func _bullet_collider(cc, s, dp):
36-
mode = MODE_RIGID
37-
state = STATE_DYING
38-
39-
s.set_angular_velocity(sign(dp.x) * 33.0)
40-
set_friction(1)
41-
cc.disable()
42-
($SoundHit as AudioStreamPlayer2D).play()
18+
onready var rc_left = $RaycastLeft
19+
onready var rc_right = $RaycastRight
4320

4421
func _integrate_forces(s):
4522
var lv = s.get_linear_velocity()
4623
var new_anim = anim
4724

48-
if state == STATE_DYING:
25+
if state == State.DYING:
4926
new_anim = "explode"
50-
elif state == STATE_WALKING:
27+
elif state == State.WALKING:
5128
new_anim = "walk"
5229

5330
var wall_side = 0.0
@@ -81,6 +58,31 @@ func _integrate_forces(s):
8158

8259
if anim != new_anim:
8360
anim = new_anim
84-
($Anim as AnimationPlayer).play(anim)
61+
($AnimationPlayer as AnimationPlayer).play(anim)
8562

8663
s.set_linear_velocity(lv)
64+
65+
66+
func _die():
67+
queue_free()
68+
69+
70+
func _pre_explode():
71+
#make sure nothing collides against this
72+
$Shape1.queue_free()
73+
$Shape2.queue_free()
74+
$Shape3.queue_free()
75+
76+
# Stay there
77+
mode = MODE_STATIC
78+
($SoundExplode as AudioStreamPlayer2D).play()
79+
80+
81+
func _bullet_collider(cc, s, dp):
82+
mode = MODE_RIGID
83+
state = State.DYING
84+
85+
s.set_angular_velocity(sign(dp.x) * 33.0)
86+
set_friction(1)
87+
cc.disable()
88+
($SoundHit as AudioStreamPlayer2D).play()

2d/physics_platformer/platform/MovingPlatform.tscn

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[ext_resource path="res://platform/moving_platform.gd" type="Script" id=1]
44
[ext_resource path="res://platform/moving_platform.png" type="Texture" id=2]
55

6-
[node name="Moving_platform" type="Node2D"]
6+
[node name="MovingPlatform" type="Node2D"]
77
script = ExtResource( 1 )
88

99
[node name="Platform" type="RigidBody2D" parent="."]
@@ -14,4 +14,3 @@ texture = ExtResource( 2 )
1414

1515
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Platform"]
1616
polygon = PoolVector2Array( -88, -24, 88, -24, 88, 24, -88, 24 )
17-

0 commit comments

Comments
 (0)