2018-10-09 16:58:51 +02:00
|
|
|
using Godot;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
public class Player : Area2D
|
|
|
|
{
|
|
|
|
[Signal]
|
|
|
|
public delegate void Hit();
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public int Speed; // How fast the player will move (pixels/sec).
|
|
|
|
|
|
|
|
private Vector2 _screenSize; // Size of the game window.
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
_screenSize = GetViewport().GetSize();
|
|
|
|
|
|
|
|
Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
|
|
|
var velocity = new Vector2(); // The player's movement vector.
|
|
|
|
|
|
|
|
if (Input.IsActionPressed("ui_right"))
|
|
|
|
{
|
|
|
|
velocity.x += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionPressed("ui_left"))
|
|
|
|
{
|
|
|
|
velocity.x -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionPressed("ui_down"))
|
|
|
|
{
|
|
|
|
velocity.y += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.IsActionPressed("ui_up"))
|
|
|
|
{
|
|
|
|
velocity.y -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
|
|
|
|
|
|
|
|
if (velocity.Length() > 0)
|
|
|
|
{
|
|
|
|
velocity = velocity.Normalized() * Speed;
|
|
|
|
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";
|
|
|
|
// See the note below about boolean assignment
|
|
|
|
animatedSprite.FlipH = velocity.x < 0;
|
|
|
|
animatedSprite.FlipV = false;
|
|
|
|
}
|
|
|
|
else if(velocity.y != 0)
|
|
|
|
{
|
|
|
|
animatedSprite.Animation = "up";
|
|
|
|
animatedSprite.FlipV = velocity.y > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(Vector2 pos)
|
|
|
|
{
|
|
|
|
Position = pos;
|
|
|
|
Show();
|
2019-02-26 14:50:18 +01:00
|
|
|
// Must be deferred as we can't change physics properties on a physics callback
|
|
|
|
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.
|
|
|
|
EmitSignal("Hit");
|
2019-02-26 14:50:18 +01:00
|
|
|
// Must be deferred as we can't change physics properties on a physics callback
|
|
|
|
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("Disabled", true);
|
2018-10-09 16:58:51 +02:00
|
|
|
}
|
|
|
|
}
|