Skip to content

Commit 17e8007

Browse files
authored
Merge pull request #318 from cbscribe/dodge_31_updates
Update Dodge demo to match 3.1 docs
2 parents 1a5517a + 961c71d commit 17e8007

25 files changed

+187
-169
lines changed

2d/dodge_the_creeps/HUD.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ func show_message(text):
1010
func show_game_over():
1111
show_message("Game Over")
1212
yield($MessageTimer, "timeout")
13-
$StartButton.show()
14-
$MessageLabel.text = "Dodge the\nCreeps!"
13+
$MessageLabel.text = "Dodge the\nCreeps"
1514
$MessageLabel.show()
15+
yield(get_tree().create_timer(1), 'timeout')
16+
$StartButton.show()
1617

1718
func update_score(score):
1819
$ScoreLabel.text = str(score)

2d/dodge_the_creeps/HUD.tscn

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
1-
[gd_scene load_steps=6 format=2]
1+
[gd_scene load_steps=7 format=2]
22

33
[ext_resource path="res://HUD.gd" type="Script" id=1]
44
[ext_resource path="res://fonts/Xolonium-Regular.ttf" type="DynamicFontData" id=2]
55

66
[sub_resource type="DynamicFont" id=1]
7+
78
size = 64
89
font_data = ExtResource( 2 )
910

1011
[sub_resource type="DynamicFont" id=2]
11-
size = 72
12-
font_data = ExtResource( 2 )
1312

14-
[sub_resource type="DynamicFont" id=3]
15-
size = 48
13+
size = 64
1614
font_data = ExtResource( 2 )
1715

16+
[sub_resource type="InputEventAction" id=3]
17+
18+
action = "ui_select"
19+
20+
[sub_resource type="ShortCut" id=4]
21+
22+
shortcut = SubResource( 3 )
23+
1824
[node name="HUD" type="CanvasLayer"]
1925
script = ExtResource( 1 )
2026

2127
[node name="ScoreLabel" type="Label" parent="."]
22-
anchor_left = 0.5
23-
anchor_right = 0.5
24-
margin_left = -25.0
25-
margin_right = 25.0
26-
margin_bottom = 100.0
28+
anchor_right = 1.0
29+
margin_bottom = 78.0
2730
custom_fonts/font = SubResource( 1 )
2831
text = "0"
2932
align = 1
3033

3134
[node name="MessageLabel" type="Label" parent="."]
32-
anchor_left = 0.5
3335
anchor_top = 0.5
34-
anchor_right = 0.5
36+
anchor_right = 1.0
3537
anchor_bottom = 0.5
36-
margin_left = -200.0
37-
margin_top = -150.0
38-
margin_right = 200.0
39-
margin_bottom = 50.0
40-
custom_fonts/font = SubResource( 2 )
38+
margin_top = -79.5
39+
margin_bottom = 79.5
40+
custom_fonts/font = SubResource( 1 )
4141
text = "Dodge the
42-
Creeps!"
42+
Creeps"
4343
align = 1
44-
valign = 1
45-
46-
[node name="MessageTimer" type="Timer" parent="."]
47-
wait_time = 2.0
48-
one_shot = true
4944

5045
[node name="StartButton" type="Button" parent="."]
5146
anchor_left = 0.5
5247
anchor_top = 1.0
5348
anchor_right = 0.5
5449
anchor_bottom = 1.0
55-
margin_left = -100.0
50+
margin_left = -90.0
5651
margin_top = -200.0
57-
margin_right = 100.0
52+
margin_right = 90.0
5853
margin_bottom = -100.0
59-
custom_fonts/font = SubResource( 3 )
54+
custom_fonts/font = SubResource( 2 )
55+
shortcut = SubResource( 4 )
6056
text = "Start"
6157

62-
[connection signal="timeout" from="MessageTimer" to="." method="_on_MessageTimer_timeout"]
58+
[node name="MessageTimer" type="Timer" parent="."]
59+
one_shot = true
60+
6361
[connection signal="pressed" from="StartButton" to="." method="_on_StartButton_pressed"]
62+
[connection signal="timeout" from="MessageTimer" to="." method="_on_MessageTimer_timeout"]

2d/dodge_the_creeps/Main.gd

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,35 @@ var score
66
func _ready():
77
randomize()
88

9+
func game_over():
10+
$ScoreTimer.stop()
11+
$MobTimer.stop()
12+
$HUD.show_game_over()
13+
$Music.stop()
14+
$DeathSound.play()
15+
916
func new_game():
1017
score = 0
11-
$HUD.update_score(score)
1218
$Player.start($StartPosition.position)
1319
$StartTimer.start()
20+
$HUD.update_score(score)
1421
$HUD.show_message("Get Ready")
1522
$Music.play()
1623

17-
func game_over():
18-
$DeathSound.play()
19-
$Music.stop()
20-
$ScoreTimer.stop()
21-
$MobTimer.stop()
22-
$HUD.show_game_over()
23-
2424
func _on_MobTimer_timeout():
25-
# choose a random location on the Path2D
26-
$MobPath/MobSpawnLocation.set_offset(randi())
25+
$MobPath/MobSpawnLocation.offset = randi()
2726
var mob = Mob.instance()
2827
add_child(mob)
2928
var direction = $MobPath/MobSpawnLocation.rotation + PI/2
3029
mob.position = $MobPath/MobSpawnLocation.position
31-
# add some randomness to the direction
3230
direction += rand_range(-PI/4, PI/4)
3331
mob.rotation = direction
34-
mob.set_linear_velocity(Vector2(rand_range(mob.MIN_SPEED, mob.MAX_SPEED), 0).rotated(direction))
35-
36-
func _on_StartTimer_timeout():
37-
$MobTimer.start()
38-
$ScoreTimer.start()
32+
mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0).rotated(direction)
3933

4034
func _on_ScoreTimer_timeout():
4135
score += 1
4236
$HUD.update_score(score)
37+
38+
func _on_StartTimer_timeout():
39+
$MobTimer.start()
40+
$ScoreTimer.start()

2d/dodge_the_creeps/Main.tscn

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99

1010
[sub_resource type="Curve2D" id=1]
1111
_data = {
12-
"points": PoolVector2Array( 0, 0, 0, 0, 1.70602, 3.56798, 0, 0, 0, 0, 482.263, 3.26522, 0, 0, 0, 0, 480.489, 729.458, 0, 0, 0, 0, 0.794434, 732.118, 0, 0, 0, 0, 1.97066, 3.83263 )
12+
"points": PoolVector2Array( 0, 0, 0, 0, -0.901337, 0.225891, 0, 0, 0, 0, 480.262, 1.29041, 0, 0, 0, 0, 481.327, 700.681, 0, 0, 0, 0, 0.163177, 698.552, 0, 0, 0, 0, -0.901337, 0.225891 )
1313
}
1414

1515
[node name="Main" type="Node"]
1616
script = ExtResource( 1 )
1717
Mob = ExtResource( 2 )
1818

1919
[node name="ColorRect" type="ColorRect" parent="."]
20-
margin_right = 486.0
21-
margin_bottom = 734.0
22-
color = Color( 0.253451, 0.425288, 0.429412, 1 )
20+
anchor_right = 1.0
21+
anchor_bottom = 1.0
22+
color = Color( 0.223529, 0.317647, 0.368627, 1 )
2323

2424
[node name="Player" parent="." instance=ExtResource( 3 )]
2525

@@ -35,22 +35,20 @@ one_shot = true
3535
[node name="StartPosition" type="Position2D" parent="."]
3636
position = Vector2( 240, 450 )
3737

38+
[node name="MobPath" type="Path2D" parent="."]
39+
curve = SubResource( 1 )
40+
41+
[node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"]
42+
position = Vector2( -0.901337, 0.225891 )
43+
rotation = 0.00221238
44+
3845
[node name="HUD" parent="." instance=ExtResource( 4 )]
3946

4047
[node name="Music" type="AudioStreamPlayer" parent="."]
4148
stream = ExtResource( 5 )
42-
volume_db = -8.0
4349

4450
[node name="DeathSound" type="AudioStreamPlayer" parent="."]
4551
stream = ExtResource( 6 )
46-
volume_db = -5.0
47-
48-
[node name="MobPath" type="Path2D" parent="."]
49-
curve = SubResource( 1 )
50-
51-
[node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"]
52-
position = Vector2( 1.70602, 3.56798 )
53-
rotation = -0.000630111
5452

5553
[connection signal="hit" from="Player" to="." method="game_over"]
5654
[connection signal="timeout" from="MobTimer" to="." method="_on_MobTimer_timeout"]

2d/dodge_the_creeps/Mob.gd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
extends RigidBody2D
22

3-
export (int) var MIN_SPEED
4-
export (int) var MAX_SPEED
3+
export var min_speed = 150
4+
export var max_speed = 250
55
var mob_types = ["walk", "swim", "fly"]
66

77
func _ready():
88
$AnimatedSprite.animation = mob_types[randi() % mob_types.size()]
99

10-
func _on_Visibility_screen_exited():
11-
queue_free()
10+
func _on_VisibilityNotifier2D_screen_exited():
11+
queue_free()

2d/dodge_the_creeps/Mob.tscn

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

33
[ext_resource path="res://Mob.gd" type="Script" id=1]
4-
[ext_resource path="res://art/enemyWalking_1.png" type="Texture" id=2]
5-
[ext_resource path="res://art/enemyWalking_2.png" type="Texture" id=3]
6-
[ext_resource path="res://art/enemyFlyingAlt_1.png" type="Texture" id=4]
7-
[ext_resource path="res://art/enemyFlyingAlt_2.png" type="Texture" id=5]
8-
[ext_resource path="res://art/enemySwimming_1.png" type="Texture" id=6]
9-
[ext_resource path="res://art/enemySwimming_2.png" type="Texture" id=7]
4+
[ext_resource path="res://art/enemySwimming_1.png" type="Texture" id=2]
5+
[ext_resource path="res://art/enemySwimming_2.png" type="Texture" id=3]
6+
[ext_resource path="res://art/enemyWalking_1.png" type="Texture" id=4]
7+
[ext_resource path="res://art/enemyWalking_2.png" type="Texture" id=5]
8+
[ext_resource path="res://art/enemyFlyingAlt_1.png" type="Texture" id=6]
9+
[ext_resource path="res://art/enemyFlyingAlt_2.png" type="Texture" id=7]
1010

1111
[sub_resource type="SpriteFrames" id=1]
12+
1213
animations = [ {
1314
"frames": [ ExtResource( 2 ), ExtResource( 3 ) ],
1415
"loop": true,
15-
"name": "walk",
16+
"name": "swim",
1617
"speed": 4.0
1718
}, {
1819
"frames": [ ExtResource( 4 ), ExtResource( 5 ) ],
1920
"loop": true,
20-
"name": "fly",
21-
"speed": 3.0
21+
"name": "walk",
22+
"speed": 4.0
2223
}, {
2324
"frames": [ ExtResource( 6 ), ExtResource( 7 ) ],
2425
"loop": true,
25-
"name": "swim",
26-
"speed": 4.0
26+
"name": "fly",
27+
"speed": 3.0
2728
} ]
2829

2930
[sub_resource type="CapsuleShape2D" id=2]
30-
radius = 34.8222
31-
height = 28.8308
31+
32+
radius = 35.2706
33+
height = 23.3281
3234

3335
[node name="Mob" type="RigidBody2D"]
34-
input_pickable = true
3536
collision_mask = 0
3637
gravity_scale = 0.0
37-
linear_damp = 0.1
38-
angular_damp = 1.0
3938
script = ExtResource( 1 )
4039
__meta__ = {
4140
"_edit_group_": true
4241
}
43-
MIN_SPEED = 150
44-
MAX_SPEED = 250
4542

4643
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
4744
scale = Vector2( 0.75, 0.75 )
4845
frames = SubResource( 1 )
49-
animation = "swim"
50-
frame = 1
46+
animation = "walk"
5147
playing = true
5248

53-
[node name="Collision" type="CollisionShape2D" parent="."]
49+
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
5450
rotation = 1.5708
5551
shape = SubResource( 2 )
5652

57-
[node name="Visibility" type="VisibilityNotifier2D" parent="."]
53+
[node name="VisibilityNotifier2D" type="VisibilityNotifier2D" parent="."]
5854

55+
[connection signal="screen_exited" from="VisibilityNotifier2D" to="." method="_on_VisibilityNotifier2D_screen_exited"]

2d/dodge_the_creeps/Player.gd

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,32 @@ extends Area2D
22

33
signal hit
44

5-
export (int) var SPEED
6-
var velocity = Vector2()
7-
var screensize
5+
export var speed = 400
6+
var extents
7+
var screen_size
88

99
func _ready():
10+
screen_size = get_viewport_rect().size
1011
hide()
11-
screensize = get_viewport_rect().size
12-
13-
func start(pos):
14-
position = pos
15-
show()
16-
# Must be deferred as we can't change physics properties on a physics callback
17-
$Collision.set_deferred("disabled", false)
1812

1913
func _process(delta):
20-
velocity = Vector2()
14+
var velocity = Vector2()
2115
if Input.is_action_pressed("ui_right"):
2216
velocity.x += 1
2317
if Input.is_action_pressed("ui_left"):
2418
velocity.x -= 1
25-
if Input.is_action_pressed("ui_down"):
26-
velocity.y += 1
2719
if Input.is_action_pressed("ui_up"):
2820
velocity.y -= 1
21+
if Input.is_action_pressed("ui_down"):
22+
velocity.y += 1
2923
if velocity.length() > 0:
30-
velocity = velocity.normalized() * SPEED
24+
velocity = velocity.normalized() * speed
3125
$AnimatedSprite.play()
32-
$Trail.emitting = true
3326
else:
3427
$AnimatedSprite.stop()
35-
$Trail.emitting = false
36-
3728
position += velocity * delta
38-
position.x = clamp(position.x, 0, screensize.x)
39-
position.y = clamp(position.y, 0, screensize.y)
29+
position.x = clamp(position.x, 0, screen_size.x)
30+
position.y = clamp(position.y, 0, screen_size.y)
4031

4132
if velocity.x != 0:
4233
$AnimatedSprite.animation = "right"
@@ -46,8 +37,12 @@ func _process(delta):
4637
$AnimatedSprite.animation = "up"
4738
$AnimatedSprite.flip_v = velocity.y > 0
4839

49-
func _on_Player_body_entered(_body):
50-
# Must be deferred as we can't change physics properties on a physics callback
51-
$Collision.set_deferred("disabled", true)
40+
func _on_Player_body_entered(body):
5241
hide()
5342
emit_signal("hit")
43+
$CollisionShape2D.set_deferred("disabled", true)
44+
45+
func start(pos):
46+
position = pos
47+
show()
48+
$CollisionShape2D.disabled = false

0 commit comments

Comments
 (0)