mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2025-12-16 13:30:07 +01:00
Merge pull request #536 from aaronfranke/dtc-public
Change Dodge the Creeps to be more consistent with the docs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
extends Node
|
||||
|
||||
export(PackedScene) var _mob_scene
|
||||
export(PackedScene) var mob_scene
|
||||
var score
|
||||
|
||||
func _ready():
|
||||
@@ -31,21 +31,22 @@ func _on_MobTimer_timeout():
|
||||
mob_spawn_location.offset = randi()
|
||||
|
||||
# Create a Mob instance and add it to the scene.
|
||||
var mob_instance = _mob_scene.instance()
|
||||
add_child(mob_instance)
|
||||
var mob = mob_scene.instance()
|
||||
add_child(mob)
|
||||
|
||||
# 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.
|
||||
mob_instance.position = mob_spawn_location.position
|
||||
mob.position = mob_spawn_location.position
|
||||
|
||||
# Add some randomness to the direction.
|
||||
direction += rand_range(-TAU / 8, TAU / 8)
|
||||
mob_instance.rotation = direction
|
||||
direction += rand_range(-PI / 4, PI / 4)
|
||||
mob.rotation = direction
|
||||
|
||||
# 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():
|
||||
|
||||
@@ -14,7 +14,7 @@ _data = {
|
||||
|
||||
[node name="Main" type="Node"]
|
||||
script = ExtResource( 1 )
|
||||
_mob_scene = ExtResource( 2 )
|
||||
mob_scene = ExtResource( 2 )
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="."]
|
||||
anchor_right = 1.0
|
||||
|
||||
@@ -2,30 +2,34 @@ extends Area2D
|
||||
|
||||
signal hit
|
||||
|
||||
# These only need to be accessed in this script, so we can make them private.
|
||||
# Private variables in GDScript have their name starting with an underscore.
|
||||
export var _speed = 400 # How fast the player will move (pixels/sec).
|
||||
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():
|
||||
_screen_size = get_viewport_rect().size
|
||||
screen_size = get_viewport_rect().size
|
||||
hide()
|
||||
|
||||
|
||||
func _process(delta):
|
||||
var velocity = Vector2() # The player's movement vector.
|
||||
velocity.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
||||
velocity.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
|
||||
var velocity = Vector2.ZERO # The player's movement vector.
|
||||
if Input.is_action_pressed("move_right"):
|
||||
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:
|
||||
velocity = velocity.normalized() * _speed
|
||||
velocity = velocity.normalized() * speed
|
||||
$AnimatedSprite.play()
|
||||
else:
|
||||
$AnimatedSprite.stop()
|
||||
|
||||
position += velocity * delta
|
||||
position.x = clamp(position.x, 0, _screen_size.x)
|
||||
position.y = clamp(position.y, 0, _screen_size.y)
|
||||
position.x = clamp(position.x, 0, screen_size.x)
|
||||
position.y = clamp(position.y, 0, screen_size.y)
|
||||
|
||||
if velocity.x != 0:
|
||||
$AnimatedSprite.animation = "right"
|
||||
@@ -39,8 +43,7 @@ func _process(delta):
|
||||
func start(pos):
|
||||
position = pos
|
||||
show()
|
||||
# Must be deferred as we can't change physics properties on a physics callback.
|
||||
$CollisionShape2D.set_deferred("disabled", false)
|
||||
$CollisionShape2D.disabled = false
|
||||
|
||||
|
||||
func _on_Player_body_entered(_body):
|
||||
|
||||
@@ -5,10 +5,10 @@ public class Main : Node
|
||||
#pragma warning disable 649
|
||||
// We assign this in the editor, so we don't need the warning about not being assigned.
|
||||
[Export]
|
||||
private PackedScene _mobScene;
|
||||
public PackedScene mobScene;
|
||||
#pragma warning restore 649
|
||||
|
||||
private int _score;
|
||||
public int score;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -31,7 +31,7 @@ public class Main : Node
|
||||
// Note that for calling Godot-provided methods with strings,
|
||||
// we have to use the original Godot snake_case name.
|
||||
GetTree().CallGroup("mobs", "queue_free");
|
||||
_score = 0;
|
||||
score = 0;
|
||||
|
||||
var player = GetNode<Player>("Player");
|
||||
var startPosition = GetNode<Position2D>("StartPosition");
|
||||
@@ -40,7 +40,7 @@ public class Main : Node
|
||||
GetNode<Timer>("StartTimer").Start();
|
||||
|
||||
var hud = GetNode<HUD>("HUD");
|
||||
hud.UpdateScore(_score);
|
||||
hud.UpdateScore(score);
|
||||
hud.ShowMessage("Get Ready!");
|
||||
|
||||
GetNode<AudioStreamPlayer>("Music").Play();
|
||||
@@ -54,9 +54,9 @@ public class Main : Node
|
||||
|
||||
public void OnScoreTimerTimeout()
|
||||
{
|
||||
_score++;
|
||||
score++;
|
||||
|
||||
GetNode<HUD>("HUD").UpdateScore(_score);
|
||||
GetNode<HUD>("HUD").UpdateScore(score);
|
||||
}
|
||||
|
||||
public void OnMobTimerTimeout()
|
||||
@@ -70,20 +70,21 @@ public class Main : Node
|
||||
mobSpawnLocation.Offset = GD.Randi();
|
||||
|
||||
// Create a Mob instance and add it to the scene.
|
||||
var mobInstance = (Mob)_mobScene.Instance();
|
||||
AddChild(mobInstance);
|
||||
var mob = (Mob)mobScene.Instance();
|
||||
AddChild(mob);
|
||||
|
||||
// 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.
|
||||
mobInstance.Position = mobSpawnLocation.Position;
|
||||
mob.Position = mobSpawnLocation.Position;
|
||||
|
||||
// Add some randomness to the direction.
|
||||
direction += (float)GD.RandRange(-Mathf.Tau / 8, Mathf.Tau / 8);
|
||||
mobInstance.Rotation = direction;
|
||||
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
|
||||
mob.Rotation = direction;
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ _data = {
|
||||
|
||||
[node name="Main" type="Node"]
|
||||
script = ExtResource( 1 )
|
||||
_mobScene = ExtResource( 2 )
|
||||
mobScene = ExtResource( 2 )
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="."]
|
||||
anchor_right = 1.0
|
||||
|
||||
@@ -5,31 +5,46 @@ public class Player : Area2D
|
||||
[Signal]
|
||||
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]
|
||||
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()
|
||||
{
|
||||
_screenSize = GetViewportRect().Size;
|
||||
screenSize = GetViewportRect().Size;
|
||||
Hide();
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
Vector2 velocity; // 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");
|
||||
var velocity = Vector2.Zero; // The player's movement vector.
|
||||
|
||||
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");
|
||||
|
||||
if (velocity.Length() > 0)
|
||||
{
|
||||
velocity = velocity.Normalized() * _speed;
|
||||
velocity = velocity.Normalized() * speed;
|
||||
animatedSprite.Play();
|
||||
}
|
||||
else
|
||||
@@ -39,8 +54,8 @@ public class Player : Area2D
|
||||
|
||||
Position += velocity * delta;
|
||||
Position = new Vector2(
|
||||
x: Mathf.Clamp(Position.x, 0, _screenSize.x),
|
||||
y: Mathf.Clamp(Position.y, 0, _screenSize.y)
|
||||
x: Mathf.Clamp(Position.x, 0, screenSize.x),
|
||||
y: Mathf.Clamp(Position.y, 0, screenSize.y)
|
||||
);
|
||||
|
||||
if (velocity.x != 0)
|
||||
@@ -61,8 +76,7 @@ public class Player : Area2D
|
||||
{
|
||||
Position = pos;
|
||||
Show();
|
||||
// Must be deferred as we can't change physics properties on a physics callback.
|
||||
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", false);
|
||||
GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
|
||||
}
|
||||
|
||||
public void OnPlayerBodyEntered(PhysicsBody2D body)
|
||||
|
||||
Reference in New Issue
Block a user