94 lines
2.4 KiB
C#
94 lines
2.4 KiB
C#
using OpenTK.Mathematics;
|
|
using OpenTK.Windowing.GraphicsLibraryFramework;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Voxel
|
|
{
|
|
public class Player
|
|
{
|
|
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)
|
|
{
|
|
_world = world;
|
|
}
|
|
|
|
public void PlaceBlock()
|
|
{
|
|
var (success, hit, x, y, z, normal) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 8);
|
|
if (!success) return;
|
|
|
|
x += normal.X;
|
|
y += normal.Y;
|
|
z += normal.Z;
|
|
|
|
_world.SetBlock(x, y, z, _selectedBlock);
|
|
}
|
|
|
|
public void BreakBlock()
|
|
{
|
|
var (success, hit, x, y, z, normal) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 8);
|
|
if (!success) return;
|
|
|
|
_world.SetBlock(x, y, z, Blocks.Air);
|
|
}
|
|
|
|
public void Update(float deltaTime)
|
|
{
|
|
Camera.Update(deltaTime);
|
|
|
|
if (lastClick > 0)
|
|
{
|
|
lastClick -= deltaTime;
|
|
if (lastClick < 0) lastClick = 0;
|
|
}
|
|
|
|
if (!Input.GetMouseButton(MouseButton.Right) && !Input.GetMouseButton(MouseButton.Left))
|
|
{
|
|
lastClick = 0;
|
|
}
|
|
|
|
if (Input.GetMouseButton(MouseButton.Right) && lastClick == 0)
|
|
{
|
|
lastClick = mouseCooldown;
|
|
|
|
PlaceBlock();
|
|
}
|
|
|
|
if (Input.GetMouseButton(MouseButton.Left) && lastClick == 0)
|
|
{
|
|
lastClick = mouseCooldown;
|
|
|
|
BreakBlock();
|
|
}
|
|
}
|
|
|
|
public void SwitchBlock(bool inverted)
|
|
{
|
|
var keys = BlockDefinitions.Blocks.Keys.ToList();
|
|
|
|
if (inverted)
|
|
if (_blockIndex == 0)
|
|
_blockIndex = keys.Count -1;
|
|
else
|
|
_blockIndex -= 1;
|
|
else
|
|
_blockIndex += 1;
|
|
|
|
_blockIndex = _blockIndex % keys.Count;
|
|
_selectedBlock = keys[_blockIndex];
|
|
Console.WriteLine(_selectedBlock);
|
|
}
|
|
}
|
|
}
|