2018-10-09 16:58:51 +02:00
|
|
|
using Godot;
|
|
|
|
|
|
|
|
public class Player : Area2D
|
|
|
|
{
|
|
|
|
[Signal]
|
|
|
|
public delegate void Hit();
|
|
|
|
|
2020-10-06 11:28:27 +02:00
|
|
|
// 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".
|
2018-10-09 16:58:51 +02:00
|
|
|
[Export]
|
2020-10-06 11:28:27 +02:00
|
|
|
private int _speed = 400; // How fast the player will move (pixels/sec).
|
2018-10-09 16:58:51 +02:00
|
|
|
|
|
|
|
private Vector2 _screenSize; // Size of the game window.
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
2020-10-06 11:28:27 +02:00
|
|
|
_screenSize = GetViewportRect().Size;
|
2018-10-09 16:58:51 +02:00
|
|
|
Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
2020-01-31 08:43:50 +01:00
|
|
|
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");
|
2018-10-09 16:58:51 +02:00
|
|
|
|
|
|
|
var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
|
|
|
|
|
|
|
|
if (velocity.Length() > 0)
|
|
|
|
{
|
2020-10-06 11:28:27 +02:00
|
|
|
velocity = velocity.Normalized() * _speed;
|
2018-10-09 16:58:51 +02:00
|
|
|
animatedSprite.Play();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
animatedSprite.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Position += velocity * delta;
|
|
|
|
Position = new Vector2(
|
|
|
|
x: Mathf.Clamp(Position.x, 0, _screenSize.x),
|
|
|
|
y: Mathf.Clamp(Position.y, 0, _screenSize.y)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (velocity.x != 0)
|
|
|
|
{
|
|
|
|
animatedSprite.Animation = "right";
|
2020-01-31 08:43:50 +01:00
|
|
|
// See the note below about boolean assignment.
|
2018-10-09 16:58:51 +02:00
|
|
|
animatedSprite.FlipH = velocity.x < 0;
|
|
|
|
animatedSprite.FlipV = false;
|
|
|
|
}
|
2020-01-31 08:43:50 +01:00
|
|
|
else if (velocity.y != 0)
|
2018-10-09 16:58:51 +02:00
|
|
|
{
|
|
|
|
animatedSprite.Animation = "up";
|
|
|
|
animatedSprite.FlipV = velocity.y > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(Vector2 pos)
|
|
|
|
{
|
|
|
|
Position = pos;
|
|
|
|
Show();
|
2020-01-31 08:43:50 +01:00
|
|
|
// Must be deferred as we can't change physics properties on a physics callback.
|
2020-10-06 11:28:27 +02:00
|
|
|
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", false);
|
2018-10-09 16:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void OnPlayerBodyEntered(PhysicsBody2D body)
|
|
|
|
{
|
|
|
|
Hide(); // Player disappears after being hit.
|
2020-10-06 11:28:27 +02:00
|
|
|
EmitSignal(nameof(Hit));
|
2020-01-31 08:43:50 +01:00
|
|
|
// Must be deferred as we can't change physics properties on a physics callback.
|
2020-10-06 11:28:27 +02:00
|
|
|
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
|
2018-10-09 16:58:51 +02:00
|
|
|
}
|
|
|
|
}
|