Skip to content

Commit 4bba072

Browse files
authored
Merge pull request #536 from aaronfranke/dtc-public
Change Dodge the Creeps to be more consistent with the docs
2 parents ae02236 + 4af7fd5 commit 4bba072

File tree

6 files changed

+69
-50
lines changed

6 files changed

+69
-50
lines changed

2d/dodge_the_creeps/Main.gd

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extends Node
22

3-
export(PackedScene) var _mob_scene
3+
export(PackedScene) var mob_scene
44
var score
55

66
func _ready():
@@ -31,21 +31,22 @@ func _on_MobTimer_timeout():
3131
mob_spawn_location.offset = randi()
3232

3333
# Create a Mob instance and add it to the scene.
34-
var mob_instance = _mob_scene.instance()
35-
add_child(mob_instance)
34+
var mob = mob_scene.instance()
35+
add_child(mob)
3636

3737
# Set the mob's direction perpendicular to the path direction.
38-
var direction = mob_spawn_location.rotation + TAU / 4
38+
var direction = mob_spawn_location.rotation + PI / 2
3939

4040
# Set the mob's position to a random location.
41-
mob_instance.position = mob_spawn_location.position
41+
mob.position = mob_spawn_location.position
4242

4343
# Add some randomness to the direction.
44-
direction += rand_range(-TAU / 8, TAU / 8)
45-
mob_instance.rotation = direction
44+
direction += rand_range(-PI / 4, PI / 4)
45+
mob.rotation = direction
4646

4747
# Choose the velocity.
48-
mob_instance.linear_velocity = Vector2(rand_range(mob_instance.min_speed, mob_instance.max_speed), 0).rotated(direction)
48+
var velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
49+
mob.linear_velocity = velocity.rotated(direction)
4950

5051

5152
func _on_ScoreTimer_timeout():

2d/dodge_the_creeps/Main.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ _data = {
1414

1515
[node name="Main" type="Node"]
1616
script = ExtResource( 1 )
17-
_mob_scene = ExtResource( 2 )
17+
mob_scene = ExtResource( 2 )
1818

1919
[node name="ColorRect" type="ColorRect" parent="."]
2020
anchor_right = 1.0

2d/dodge_the_creeps/Player.gd

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,34 @@ extends Area2D
22

33
signal hit
44

5-
# These only need to be accessed in this script, so we can make them private.
6-
# Private variables in GDScript have their name starting with an underscore.
7-
export var _speed = 400 # How fast the player will move (pixels/sec).
8-
var _screen_size # Size of the game window.
5+
export var speed = 400 # How fast the player will move (pixels/sec).
6+
var screen_size # Size of the game window.
97

108
func _ready():
11-
_screen_size = get_viewport_rect().size
9+
screen_size = get_viewport_rect().size
1210
hide()
1311

1412

1513
func _process(delta):
16-
var velocity = Vector2() # The player's movement vector.
17-
velocity.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
18-
velocity.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
14+
var velocity = Vector2.ZERO # The player's movement vector.
15+
if Input.is_action_pressed("move_right"):
16+
velocity.x += 1
17+
if Input.is_action_pressed("move_left"):
18+
velocity.x -= 1
19+
if Input.is_action_pressed("move_down"):
20+
velocity.y += 1
21+
if Input.is_action_pressed("move_up"):
22+
velocity.y -= 1
1923

2024
if velocity.length() > 0:
21-
velocity = velocity.normalized() * _speed
25+
velocity = velocity.normalized() * speed
2226
$AnimatedSprite.play()
2327
else:
2428
$AnimatedSprite.stop()
2529

2630
position += velocity * delta
27-
position.x = clamp(position.x, 0, _screen_size.x)
28-
position.y = clamp(position.y, 0, _screen_size.y)
31+
position.x = clamp(position.x, 0, screen_size.x)
32+
position.y = clamp(position.y, 0, screen_size.y)
2933

3034
if velocity.x != 0:
3135
$AnimatedSprite.animation = "right"
@@ -39,8 +43,7 @@ func _process(delta):
3943
func start(pos):
4044
position = pos
4145
show()
42-
# Must be deferred as we can't change physics properties on a physics callback.
43-
$CollisionShape2D.set_deferred("disabled", false)
46+
$CollisionShape2D.disabled = false
4447

4548

4649
func _on_Player_body_entered(_body):

mono/dodge_the_creeps/Main.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ public class Main : Node
55
#pragma warning disable 649
66
// We assign this in the editor, so we don't need the warning about not being assigned.
77
[Export]
8-
private PackedScene _mobScene;
8+
public PackedScene mobScene;
99
#pragma warning restore 649
1010

11-
private int _score;
11+
public int score;
1212

1313
public override void _Ready()
1414
{
@@ -31,7 +31,7 @@ public void NewGame()
3131
// Note that for calling Godot-provided methods with strings,
3232
// we have to use the original Godot snake_case name.
3333
GetTree().CallGroup("mobs", "queue_free");
34-
_score = 0;
34+
score = 0;
3535

3636
var player = GetNode<Player>("Player");
3737
var startPosition = GetNode<Position2D>("StartPosition");
@@ -40,7 +40,7 @@ public void NewGame()
4040
GetNode<Timer>("StartTimer").Start();
4141

4242
var hud = GetNode<HUD>("HUD");
43-
hud.UpdateScore(_score);
43+
hud.UpdateScore(score);
4444
hud.ShowMessage("Get Ready!");
4545

4646
GetNode<AudioStreamPlayer>("Music").Play();
@@ -54,9 +54,9 @@ public void OnStartTimerTimeout()
5454

5555
public void OnScoreTimerTimeout()
5656
{
57-
_score++;
57+
score++;
5858

59-
GetNode<HUD>("HUD").UpdateScore(_score);
59+
GetNode<HUD>("HUD").UpdateScore(score);
6060
}
6161

6262
public void OnMobTimerTimeout()
@@ -70,20 +70,21 @@ public void OnMobTimerTimeout()
7070
mobSpawnLocation.Offset = GD.Randi();
7171

7272
// Create a Mob instance and add it to the scene.
73-
var mobInstance = (Mob)_mobScene.Instance();
74-
AddChild(mobInstance);
73+
var mob = (Mob)mobScene.Instance();
74+
AddChild(mob);
7575

7676
// Set the mob's direction perpendicular to the path direction.
77-
float direction = mobSpawnLocation.Rotation + Mathf.Tau / 4;
77+
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
7878

7979
// Set the mob's position to a random location.
80-
mobInstance.Position = mobSpawnLocation.Position;
80+
mob.Position = mobSpawnLocation.Position;
8181

8282
// Add some randomness to the direction.
83-
direction += (float)GD.RandRange(-Mathf.Tau / 8, Mathf.Tau / 8);
84-
mobInstance.Rotation = direction;
83+
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
84+
mob.Rotation = direction;
8585

8686
// Choose the velocity.
87-
mobInstance.LinearVelocity = new Vector2((float)GD.RandRange(mobInstance.minSpeed, mobInstance.maxSpeed), 0).Rotated(direction);
87+
var velocity = new Vector2((float)GD.RandRange(mob.minSpeed, mob.maxSpeed), 0);
88+
mob.LinearVelocity = velocity.Rotated(direction);
8889
}
8990
}

mono/dodge_the_creeps/Main.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ _data = {
1414

1515
[node name="Main" type="Node"]
1616
script = ExtResource( 1 )
17-
_mobScene = ExtResource( 2 )
17+
mobScene = ExtResource( 2 )
1818

1919
[node name="ColorRect" type="ColorRect" parent="."]
2020
anchor_right = 1.0

mono/dodge_the_creeps/Player.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,46 @@ public class Player : Area2D
55
[Signal]
66
public delegate void Hit();
77

8-
// These only need to be accessed in this script, so we can make them private.
9-
// Private variables in C# in Godot have their name starting with an
10-
// underscore and also have the "private" keyword instead of "public".
118
[Export]
12-
private int _speed = 400; // How fast the player will move (pixels/sec).
9+
public int speed = 400; // How fast the player will move (pixels/sec).
1310

14-
private Vector2 _screenSize; // Size of the game window.
11+
public Vector2 screenSize; // Size of the game window.
1512

1613
public override void _Ready()
1714
{
18-
_screenSize = GetViewportRect().Size;
15+
screenSize = GetViewportRect().Size;
1916
Hide();
2017
}
2118

2219
public override void _Process(float delta)
2320
{
24-
Vector2 velocity; // The player's movement vector.
25-
velocity.x = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");
26-
velocity.y = Input.GetActionStrength("move_down") - Input.GetActionStrength("move_up");
21+
var velocity = Vector2.Zero; // The player's movement vector.
22+
23+
if (Input.IsActionPressed("move_right"))
24+
{
25+
velocity.x += 1;
26+
}
27+
28+
if (Input.IsActionPressed("move_left"))
29+
{
30+
velocity.x -= 1;
31+
}
32+
33+
if (Input.IsActionPressed("move_down"))
34+
{
35+
velocity.y += 1;
36+
}
37+
38+
if (Input.IsActionPressed("move_up"))
39+
{
40+
velocity.y -= 1;
41+
}
2742

2843
var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
2944

3045
if (velocity.Length() > 0)
3146
{
32-
velocity = velocity.Normalized() * _speed;
47+
velocity = velocity.Normalized() * speed;
3348
animatedSprite.Play();
3449
}
3550
else
@@ -39,8 +54,8 @@ public override void _Process(float delta)
3954

4055
Position += velocity * delta;
4156
Position = new Vector2(
42-
x: Mathf.Clamp(Position.x, 0, _screenSize.x),
43-
y: Mathf.Clamp(Position.y, 0, _screenSize.y)
57+
x: Mathf.Clamp(Position.x, 0, screenSize.x),
58+
y: Mathf.Clamp(Position.y, 0, screenSize.y)
4459
);
4560

4661
if (velocity.x != 0)
@@ -61,8 +76,7 @@ public void Start(Vector2 pos)
6176
{
6277
Position = pos;
6378
Show();
64-
// Must be deferred as we can't change physics properties on a physics callback.
65-
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", false);
79+
GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
6680
}
6781

6882
public void OnPlayerBodyEntered(PhysicsBody2D body)

0 commit comments

Comments
 (0)