Merge pull request #536 from aaronfranke/dtc-public

Change Dodge the Creeps to be more consistent with the docs
This commit is contained in:
Aaron Franke
2020-10-21 01:50:46 -04:00
committed by GitHub
6 changed files with 69 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
extends Node extends Node
export(PackedScene) var _mob_scene export(PackedScene) var mob_scene
var score var score
func _ready(): func _ready():
@@ -31,21 +31,22 @@ func _on_MobTimer_timeout():
mob_spawn_location.offset = randi() mob_spawn_location.offset = randi()
# Create a Mob instance and add it to the scene. # Create a Mob instance and add it to the scene.
var mob_instance = _mob_scene.instance() var mob = mob_scene.instance()
add_child(mob_instance) add_child(mob)
# Set the mob's direction perpendicular to the path direction. # Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + TAU / 4 var direction = mob_spawn_location.rotation + PI / 2
# Set the mob's position to a random location. # Set the mob's position to a random location.
mob_instance.position = mob_spawn_location.position mob.position = mob_spawn_location.position
# Add some randomness to the direction. # Add some randomness to the direction.
direction += rand_range(-TAU / 8, TAU / 8) direction += rand_range(-PI / 4, PI / 4)
mob_instance.rotation = direction mob.rotation = direction
# Choose the velocity. # Choose the velocity.
mob_instance.linear_velocity = Vector2(rand_range(mob_instance.min_speed, mob_instance.max_speed), 0).rotated(direction) var velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
mob.linear_velocity = velocity.rotated(direction)
func _on_ScoreTimer_timeout(): func _on_ScoreTimer_timeout():

View File

@@ -14,7 +14,7 @@ _data = {
[node name="Main" type="Node"] [node name="Main" type="Node"]
script = ExtResource( 1 ) script = ExtResource( 1 )
_mob_scene = ExtResource( 2 ) mob_scene = ExtResource( 2 )
[node name="ColorRect" type="ColorRect" parent="."] [node name="ColorRect" type="ColorRect" parent="."]
anchor_right = 1.0 anchor_right = 1.0

View File

@@ -2,30 +2,34 @@ extends Area2D
signal hit signal hit
# These only need to be accessed in this script, so we can make them private. export var speed = 400 # How fast the player will move (pixels/sec).
# Private variables in GDScript have their name starting with an underscore. var screen_size # Size of the game window.
export var _speed = 400 # How fast the player will move (pixels/sec).
var _screen_size # Size of the game window.
func _ready(): func _ready():
_screen_size = get_viewport_rect().size screen_size = get_viewport_rect().size
hide() hide()
func _process(delta): func _process(delta):
var velocity = Vector2() # The player's movement vector. var velocity = Vector2.ZERO # The player's movement vector.
velocity.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left") if Input.is_action_pressed("move_right"):
velocity.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up") velocity.x += 1
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_pressed("move_up"):
velocity.y -= 1
if velocity.length() > 0: if velocity.length() > 0:
velocity = velocity.normalized() * _speed velocity = velocity.normalized() * speed
$AnimatedSprite.play() $AnimatedSprite.play()
else: else:
$AnimatedSprite.stop() $AnimatedSprite.stop()
position += velocity * delta position += velocity * delta
position.x = clamp(position.x, 0, _screen_size.x) position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, _screen_size.y) position.y = clamp(position.y, 0, screen_size.y)
if velocity.x != 0: if velocity.x != 0:
$AnimatedSprite.animation = "right" $AnimatedSprite.animation = "right"
@@ -39,8 +43,7 @@ func _process(delta):
func start(pos): func start(pos):
position = pos position = pos
show() show()
# Must be deferred as we can't change physics properties on a physics callback. $CollisionShape2D.disabled = false
$CollisionShape2D.set_deferred("disabled", false)
func _on_Player_body_entered(_body): func _on_Player_body_entered(_body):

View File

@@ -5,10 +5,10 @@ public class Main : Node
#pragma warning disable 649 #pragma warning disable 649
// We assign this in the editor, so we don't need the warning about not being assigned. // We assign this in the editor, so we don't need the warning about not being assigned.
[Export] [Export]
private PackedScene _mobScene; public PackedScene mobScene;
#pragma warning restore 649 #pragma warning restore 649
private int _score; public int score;
public override void _Ready() public override void _Ready()
{ {
@@ -31,7 +31,7 @@ public class Main : Node
// Note that for calling Godot-provided methods with strings, // Note that for calling Godot-provided methods with strings,
// we have to use the original Godot snake_case name. // we have to use the original Godot snake_case name.
GetTree().CallGroup("mobs", "queue_free"); GetTree().CallGroup("mobs", "queue_free");
_score = 0; score = 0;
var player = GetNode<Player>("Player"); var player = GetNode<Player>("Player");
var startPosition = GetNode<Position2D>("StartPosition"); var startPosition = GetNode<Position2D>("StartPosition");
@@ -40,7 +40,7 @@ public class Main : Node
GetNode<Timer>("StartTimer").Start(); GetNode<Timer>("StartTimer").Start();
var hud = GetNode<HUD>("HUD"); var hud = GetNode<HUD>("HUD");
hud.UpdateScore(_score); hud.UpdateScore(score);
hud.ShowMessage("Get Ready!"); hud.ShowMessage("Get Ready!");
GetNode<AudioStreamPlayer>("Music").Play(); GetNode<AudioStreamPlayer>("Music").Play();
@@ -54,9 +54,9 @@ public class Main : Node
public void OnScoreTimerTimeout() public void OnScoreTimerTimeout()
{ {
_score++; score++;
GetNode<HUD>("HUD").UpdateScore(_score); GetNode<HUD>("HUD").UpdateScore(score);
} }
public void OnMobTimerTimeout() public void OnMobTimerTimeout()
@@ -70,20 +70,21 @@ public class Main : Node
mobSpawnLocation.Offset = GD.Randi(); mobSpawnLocation.Offset = GD.Randi();
// Create a Mob instance and add it to the scene. // Create a Mob instance and add it to the scene.
var mobInstance = (Mob)_mobScene.Instance(); var mob = (Mob)mobScene.Instance();
AddChild(mobInstance); AddChild(mob);
// Set the mob's direction perpendicular to the path direction. // Set the mob's direction perpendicular to the path direction.
float direction = mobSpawnLocation.Rotation + Mathf.Tau / 4; float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
// Set the mob's position to a random location. // Set the mob's position to a random location.
mobInstance.Position = mobSpawnLocation.Position; mob.Position = mobSpawnLocation.Position;
// Add some randomness to the direction. // Add some randomness to the direction.
direction += (float)GD.RandRange(-Mathf.Tau / 8, Mathf.Tau / 8); direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mobInstance.Rotation = direction; mob.Rotation = direction;
// Choose the velocity. // Choose the velocity.
mobInstance.LinearVelocity = new Vector2((float)GD.RandRange(mobInstance.minSpeed, mobInstance.maxSpeed), 0).Rotated(direction); var velocity = new Vector2((float)GD.RandRange(mob.minSpeed, mob.maxSpeed), 0);
mob.LinearVelocity = velocity.Rotated(direction);
} }
} }

View File

@@ -14,7 +14,7 @@ _data = {
[node name="Main" type="Node"] [node name="Main" type="Node"]
script = ExtResource( 1 ) script = ExtResource( 1 )
_mobScene = ExtResource( 2 ) mobScene = ExtResource( 2 )
[node name="ColorRect" type="ColorRect" parent="."] [node name="ColorRect" type="ColorRect" parent="."]
anchor_right = 1.0 anchor_right = 1.0

View File

@@ -5,31 +5,46 @@ public class Player : Area2D
[Signal] [Signal]
public delegate void Hit(); public delegate void Hit();
// These only need to be accessed in this script, so we can make them private.
// Private variables in C# in Godot have their name starting with an
// underscore and also have the "private" keyword instead of "public".
[Export] [Export]
private int _speed = 400; // How fast the player will move (pixels/sec). public int speed = 400; // How fast the player will move (pixels/sec).
private Vector2 _screenSize; // Size of the game window. public Vector2 screenSize; // Size of the game window.
public override void _Ready() public override void _Ready()
{ {
_screenSize = GetViewportRect().Size; screenSize = GetViewportRect().Size;
Hide(); Hide();
} }
public override void _Process(float delta) public override void _Process(float delta)
{ {
Vector2 velocity; // The player's movement vector. var velocity = Vector2.Zero; // The player's movement vector.
velocity.x = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");
velocity.y = Input.GetActionStrength("move_down") - Input.GetActionStrength("move_up"); if (Input.IsActionPressed("move_right"))
{
velocity.x += 1;
}
if (Input.IsActionPressed("move_left"))
{
velocity.x -= 1;
}
if (Input.IsActionPressed("move_down"))
{
velocity.y += 1;
}
if (Input.IsActionPressed("move_up"))
{
velocity.y -= 1;
}
var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite"); var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
if (velocity.Length() > 0) if (velocity.Length() > 0)
{ {
velocity = velocity.Normalized() * _speed; velocity = velocity.Normalized() * speed;
animatedSprite.Play(); animatedSprite.Play();
} }
else else
@@ -39,8 +54,8 @@ public class Player : Area2D
Position += velocity * delta; Position += velocity * delta;
Position = new Vector2( Position = new Vector2(
x: Mathf.Clamp(Position.x, 0, _screenSize.x), x: Mathf.Clamp(Position.x, 0, screenSize.x),
y: Mathf.Clamp(Position.y, 0, _screenSize.y) y: Mathf.Clamp(Position.y, 0, screenSize.y)
); );
if (velocity.x != 0) if (velocity.x != 0)
@@ -61,8 +76,7 @@ public class Player : Area2D
{ {
Position = pos; Position = pos;
Show(); Show();
// Must be deferred as we can't change physics properties on a physics callback. GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", false);
} }
public void OnPlayerBodyEntered(PhysicsBody2D body) public void OnPlayerBodyEntered(PhysicsBody2D body)