You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.8 KiB
80 lines
1.8 KiB
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
|
|
|
public partial class Player3D : CharacterBody3D
|
|
{
|
|
[Export] private float _speed = 1.0f;
|
|
[Export] private Camera3D _camera;
|
|
[Export] private Sprite3D _frontSprite;
|
|
[Export] private Sprite3D _sideSprite;
|
|
|
|
private bool _sideFlipped;
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
var inputDir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
|
|
if (inputDir == Vector2.Zero)
|
|
return;
|
|
|
|
MoveOnInput(inputDir, delta);
|
|
SwitchSprites(inputDir);
|
|
}
|
|
|
|
private void MoveOnInput(Vector2 inputDir, double delta)
|
|
{
|
|
inputDir = inputDir.Rotated(-_camera.GlobalRotation.Y);
|
|
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
|
|
if (direction != Vector3.Zero)
|
|
Velocity = new Vector3(direction.X * _speed * (float) delta * Scale.X, Velocity.Y, direction.Z * _speed * (float) delta * Scale.Z);
|
|
else
|
|
Velocity = Velocity.MoveToward(new Vector3(0, 0, 0), _speed);
|
|
|
|
MoveAndSlide();
|
|
}
|
|
|
|
private void SwitchSprites(Vector2 inputDir)
|
|
{
|
|
float X = inputDir.X;
|
|
float Y = inputDir.Y;
|
|
|
|
if (X == 0.0 && Y == 0.0)
|
|
{
|
|
ActivateFrontSprite(true);
|
|
return;
|
|
}
|
|
|
|
if (X != 0)
|
|
{
|
|
ActivateFrontSprite(false);
|
|
|
|
if (X > 0.0f)
|
|
{
|
|
_sideFlipped = false;
|
|
_sideSprite.FlipH = _sideFlipped;
|
|
}
|
|
|
|
if (X < 0.0f)
|
|
{
|
|
_sideFlipped = true;
|
|
_sideSprite.FlipH = _sideFlipped;
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (Y != 0)
|
|
{
|
|
ActivateFrontSprite(true);
|
|
}
|
|
|
|
}
|
|
|
|
private void ActivateFrontSprite(bool activate)
|
|
{
|
|
_frontSprite.Visible = activate;
|
|
_sideSprite.Visible = !activate;
|
|
}
|
|
|
|
|
|
}
|