player and entity physics improvements
This commit is contained in:
81
Player.cs
81
Player.cs
@@ -11,11 +11,15 @@ namespace Voxel
|
||||
{
|
||||
public class Player : Entity
|
||||
{
|
||||
private World _world;
|
||||
public double lastClick = 0;
|
||||
public readonly float mouseCooldown = 0.2f;
|
||||
|
||||
private int _blockIndex = 0;
|
||||
private Blocks _selectedBlock = Blocks.Dirt;
|
||||
private double _tickTime = 0;
|
||||
private bool _isJumping = false;
|
||||
|
||||
private Blocks _selectedBlock = Blocks.OakPlanks;
|
||||
private Vector3 previousPosition;
|
||||
|
||||
public Player(Vector3 startPos, World world) : base(startPos, 0.5f, 1.8f, world)
|
||||
{
|
||||
@@ -42,20 +46,26 @@ namespace Voxel
|
||||
_world.SetBlock(x, y, z, Blocks.Air);
|
||||
}
|
||||
|
||||
public void Update(float deltaTime)
|
||||
public void Tick()
|
||||
{
|
||||
Camera.Position = Position + Vector3.UnitY * 0.5f;
|
||||
Rotation = Camera.Yaw;
|
||||
previousPosition = Position;
|
||||
|
||||
if (lastClick > 0)
|
||||
{
|
||||
lastClick -= deltaTime;
|
||||
if (lastClick < 0) lastClick = 0;
|
||||
}
|
||||
bool sprinting = Input.GetKey(Keys.LeftControl);
|
||||
|
||||
if (!Input.GetMouseButton(MouseButton.Right) && !Input.GetMouseButton(MouseButton.Left))
|
||||
if (Input.GetKey(Keys.W))
|
||||
ApplyWalkSpeed(0, -1, sprinting);
|
||||
if (Input.GetKey(Keys.S))
|
||||
ApplyWalkSpeed(0, 1, sprinting);
|
||||
if (Input.GetKey(Keys.A))
|
||||
ApplyWalkSpeed(-1, 0, false);
|
||||
if (Input.GetKey(Keys.D))
|
||||
ApplyWalkSpeed(1, 0, false);
|
||||
|
||||
if (Input.GetKey(Keys.Space) && OnGround)
|
||||
{
|
||||
lastClick = 0;
|
||||
Console.WriteLine("Jump");
|
||||
Velocity = new Vector3(Velocity.X, 0.42f, Velocity.Z);
|
||||
OnGround = false;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButton(MouseButton.Right) && lastClick == 0)
|
||||
@@ -72,32 +82,47 @@ namespace Voxel
|
||||
BreakBlock();
|
||||
}
|
||||
|
||||
if (Input.GetKey(Keys.W))
|
||||
ApplyLocalVelocity(0, -5 * deltaTime);
|
||||
base.Tick();
|
||||
|
||||
if (Input.GetKey(Keys.Space) && OnGround)
|
||||
Console.WriteLine(Velocity.X.ToString() + ", " + Velocity.Y.ToString() + ", " + Velocity.Z.ToString());
|
||||
}
|
||||
|
||||
public void Update(float deltaTime, float alpha)
|
||||
{
|
||||
Camera.Position = Vector3.Lerp(previousPosition, Position, alpha) + Vector3.UnitY * 0.62f;
|
||||
Rotation = Camera.Yaw;
|
||||
|
||||
if (lastClick > 0)
|
||||
{
|
||||
Velocity = new Vector3(Velocity.X, 0.5f, Velocity.Z);
|
||||
OnGround = false;
|
||||
Console.WriteLine("Jump");
|
||||
lastClick -= deltaTime;
|
||||
if (lastClick < 0) lastClick = 0;
|
||||
}
|
||||
|
||||
if (!Input.GetMouseButton(MouseButton.Right) && !Input.GetMouseButton(MouseButton.Left))
|
||||
{
|
||||
lastClick = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyLocalVelocity(float x, float z)
|
||||
public void ApplyWalkSpeed(float x, float z, bool sprinting)
|
||||
{
|
||||
Vector3 localVelocity = new Vector3(x, 0, z);
|
||||
Vector3 inputDir = new Vector3(x, 0, z);
|
||||
if (inputDir.LengthSquared > 0)
|
||||
inputDir = Vector3.Normalize(inputDir);
|
||||
|
||||
float yaw = MathHelper.DegreesToRadians(Rotation);
|
||||
float cos = MathF.Cos(yaw); // yaw in radians
|
||||
float sin = MathF.Sin(yaw);
|
||||
float yawRad = MathHelper.DegreesToRadians(Rotation);
|
||||
float cos = MathF.Cos(yawRad);
|
||||
float sin = MathF.Sin(yawRad);
|
||||
|
||||
Vector3 worldVelocity = new Vector3(
|
||||
localVelocity.X * cos - localVelocity.Z * sin,
|
||||
Velocity.Y,
|
||||
localVelocity.X * sin + localVelocity.Z * cos
|
||||
Vector3 worldDir = new Vector3(
|
||||
inputDir.X * cos - inputDir.Z * sin,
|
||||
0,
|
||||
inputDir.X * sin + inputDir.Z * cos
|
||||
);
|
||||
|
||||
Velocity = worldVelocity;
|
||||
float speed = sprinting ? 0.13f : 0.10f;
|
||||
|
||||
Velocity += worldDir * speed;
|
||||
}
|
||||
|
||||
public void SwitchBlock(bool inverted)
|
||||
|
||||
Reference in New Issue
Block a user