52 lines
1.1 KiB
C#
52 lines
1.1 KiB
C#
using Godot;
|
|
|
|
public partial class Player : CharacterBody3D
|
|
{
|
|
[Export]
|
|
private int _speed { get; set; } = 14;
|
|
|
|
[Signal]
|
|
public delegate Vector3 MoveEventHandler();
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
Velocity = ProcessMovement();
|
|
MoveAndSlide();
|
|
EmitSignal(SignalName.Move, Position);
|
|
}
|
|
|
|
private Vector3 ProcessMovement()
|
|
{
|
|
var direction = Vector3.Zero;
|
|
var targetVelocity = Vector3.Zero;
|
|
|
|
if (Input.IsActionPressed("move_right"))
|
|
{
|
|
direction.X += 1.0f;
|
|
}
|
|
if (Input.IsActionPressed("move_left"))
|
|
{
|
|
direction.X -= 1.0f;
|
|
}
|
|
if (Input.IsActionPressed("move_back"))
|
|
{
|
|
direction.Z += 1.0f;
|
|
}
|
|
if (Input.IsActionPressed("move_forward"))
|
|
{
|
|
direction.Z -= 1.0f;
|
|
}
|
|
|
|
if (direction != Vector3.Zero)
|
|
{
|
|
direction = direction.Normalized();
|
|
GetNode<Node3D>("Pivot").LookAt(Position + direction, Vector3.Up);
|
|
}
|
|
|
|
// Ground velocity
|
|
targetVelocity.X = direction.X * _speed;
|
|
targetVelocity.Z = direction.Z * _speed;
|
|
return targetVelocity;
|
|
}
|
|
}
|