player added

This commit is contained in:
maxwes08
2025-09-30 10:58:12 +02:00
parent 38dccf0a84
commit 11f76ca429
11 changed files with 259 additions and 48 deletions

View File

@@ -3,24 +3,23 @@ using OpenTK.Windowing.GraphicsLibraryFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Voxel
{
public class Player
public class Player : Entity
{
public Vector3 Position;
private World _world;
public double lastClick = 0;
public readonly float mouseCooldown = 0.2f;
private int _blockIndex = 0;
private Blocks _selectedBlock = Blocks.Dirt;
public Player(World world, Vector3 startPos)
public Player(Vector3 startPos, World world) : base(startPos, 0.5f, 1.8f, world)
{
_world = world;
}
public void PlaceBlock()
@@ -45,7 +44,8 @@ namespace Voxel
public void Update(float deltaTime)
{
Camera.Update(deltaTime);
Camera.Position = Position + Vector3.UnitY * 0.5f;
Rotation = Camera.Yaw;
if (lastClick > 0)
{
@@ -71,6 +71,33 @@ namespace Voxel
BreakBlock();
}
if (Input.GetKey(Keys.W))
ApplyLocalVelocity(0, -5 * deltaTime);
if (Input.GetKey(Keys.Space) && OnGround)
{
Velocity = new Vector3(Velocity.X, 0.5f, Velocity.Z);
OnGround = false;
Console.WriteLine("Jump");
}
}
public void ApplyLocalVelocity(float x, float z)
{
Vector3 localVelocity = new Vector3(x, 0, z);
float yaw = MathHelper.DegreesToRadians(Rotation);
float cos = MathF.Cos(yaw); // yaw in radians
float sin = MathF.Sin(yaw);
Vector3 worldVelocity = new Vector3(
localVelocity.X * cos - localVelocity.Z * sin,
Velocity.Y,
localVelocity.X * sin + localVelocity.Z * cos
);
Velocity = worldVelocity;
}
public void SwitchBlock(bool inverted)