This commit is contained in:
maxwes08
2025-09-15 10:48:45 +02:00
parent 50f9d4c0c8
commit 40dd5c3a9e
4 changed files with 90 additions and 31 deletions

View File

@@ -1,4 +1,5 @@
using OpenTK.Mathematics;
using OpenTK.Windowing.GraphicsLibraryFramework;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -12,15 +13,29 @@ namespace Voxel
public Vector3 Position;
private World _world;
public double lastClick = 0;
public readonly float mouseCooldown = 0.2f;
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(), 10);
if (!success) return;
x += normal.X;
y += normal.Y;
z += normal.Z;
_world.SetBlock(x, y, z, Blocks.OakPlanks);
}
public void BreakBlock()
{
var (success, hit, x, y, z) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 10);
var (success, hit, x, y, z, normal) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 10);
if (!success) return;
_world.SetBlock(x, y, z, Blocks.Air);
@@ -30,8 +45,28 @@ namespace Voxel
{
Camera.Update(deltaTime);
if (Input.GetKey(OpenTK.Windowing.GraphicsLibraryFramework.Keys.Space))
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();
}
}