Refactor
This commit is contained in:
@@ -2,9 +2,10 @@ using Godot;
|
|||||||
|
|
||||||
public partial class Player : CharacterBody3D
|
public partial class Player : CharacterBody3D
|
||||||
{
|
{
|
||||||
public const float Speed = 5.0f;
|
[Export]
|
||||||
public const float JumpVelocity = 4.5f;
|
private float _speed = 5.0f;
|
||||||
|
[Export]
|
||||||
|
private float _jumpVelocity = 4.5f;
|
||||||
[Export]
|
[Export]
|
||||||
private float _sensitivityHorizontal = 0.05f;
|
private float _sensitivityHorizontal = 0.05f;
|
||||||
[Export]
|
[Export]
|
||||||
@@ -33,30 +34,35 @@ public partial class Player : CharacterBody3D
|
|||||||
|
|
||||||
public override void _PhysicsProcess(double delta)
|
public override void _PhysicsProcess(double delta)
|
||||||
{
|
{
|
||||||
Vector3 velocity = Velocity;
|
var gravityDelta = _gravity * (float)delta;
|
||||||
|
var velocity = CalculateMovement(Velocity, Transform, gravityDelta);
|
||||||
if (!IsOnFloor())
|
|
||||||
velocity.Y -= _gravity * (float)delta;
|
|
||||||
|
|
||||||
if (Input.IsActionJustPressed("jump") && IsOnFloor())
|
|
||||||
velocity.Y = JumpVelocity;
|
|
||||||
|
|
||||||
Vector2 inputDir = Input.GetVector("right", "left", "back", "forward");
|
|
||||||
Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
|
|
||||||
if (direction != Vector3.Zero)
|
|
||||||
{
|
|
||||||
velocity.X = direction.X * Speed;
|
|
||||||
velocity.Z = direction.Z * Speed;
|
|
||||||
direction = direction.Normalized();
|
|
||||||
GetNode<Node3D>("Pivot").LookAt(Position + direction, Vector3.Up);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
|
|
||||||
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, Speed);
|
|
||||||
}
|
|
||||||
|
|
||||||
Velocity = velocity;
|
Velocity = velocity;
|
||||||
MoveAndSlide();
|
MoveAndSlide();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Vector3 CalculateMovement(Vector3 velocity, Transform3D transform, float gravityDelta)
|
||||||
|
{
|
||||||
|
if (!IsOnFloor())
|
||||||
|
velocity.Y -= gravityDelta;
|
||||||
|
|
||||||
|
if (Input.IsActionJustPressed("jump") && IsOnFloor())
|
||||||
|
velocity.Y = _jumpVelocity;
|
||||||
|
|
||||||
|
Vector2 inputDir = Input.GetVector("right", "left", "back", "forward");
|
||||||
|
Vector3 direction = (transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
|
||||||
|
if (direction != Vector3.Zero)
|
||||||
|
{
|
||||||
|
velocity.X = direction.X * _speed;
|
||||||
|
velocity.Z = direction.Z * _speed;
|
||||||
|
GetNode<Node3D>("Pivot").LookAt(Position + direction.Normalized(), Vector3.Up);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
|
||||||
|
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
return velocity;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user