From 40dd5c3a9e6598f094cd307929d03158719b32ea Mon Sep 17 00:00:00 2001 From: maxwes08 Date: Mon, 15 Sep 2025 10:48:45 +0200 Subject: [PATCH] peak --- Input.cs | 11 +++++++++++ Player.cs | 39 +++++++++++++++++++++++++++++++++++++-- Window.cs | 23 ++++++++++++++++++++++- World.cs | 48 ++++++++++++++++++++---------------------------- 4 files changed, 90 insertions(+), 31 deletions(-) diff --git a/Input.cs b/Input.cs index 68640c4..cd1c85b 100644 --- a/Input.cs +++ b/Input.cs @@ -5,6 +5,17 @@ namespace Voxel public static class Input { private static Dictionary _keystates = new Dictionary(); + private static Dictionary _mouseButtonStates = new Dictionary(); + + public static bool GetMouseButton(MouseButton button) + { + return _mouseButtonStates.TryGetValue(button, out bool pressed) && pressed; + } + + public static void SetMouseButton(MouseButton button, bool pressed) + { + _mouseButtonStates[button] = pressed; + } public static bool GetKey(Keys key) { diff --git a/Player.cs b/Player.cs index d64f9a5..0885e1a 100644 --- a/Player.cs +++ b/Player.cs @@ -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(); } } diff --git a/Window.cs b/Window.cs index dc3b408..f345f63 100644 --- a/Window.cs +++ b/Window.cs @@ -2,6 +2,7 @@ using OpenTK.Windowing.Common; using OpenTK.Windowing.Common.Input; using OpenTK.Windowing.Desktop; +using OpenTK.Windowing.GraphicsLibraryFramework; namespace Voxel { @@ -17,11 +18,19 @@ namespace Voxel { base.OnUpdateFrame(e); - if (Input.GetKey(OpenTK.Windowing.GraphicsLibraryFramework.Keys.Escape)) + if (Input.GetKey(Keys.Escape)) { Close(); } + if (Input.GetKey(Keys.F11)) + { + if (!IsFullscreen) + WindowState = WindowState.Fullscreen; + else + WindowState = WindowState.Normal; + } + if (Player != null) { Player.Update((float)e.Time); @@ -93,5 +102,17 @@ namespace Voxel base.OnKeyDown(e); Input.SetKey(e.Key, true); } + + protected override void OnMouseDown(MouseButtonEventArgs e) + { + base.OnMouseDown(e); + Input.SetMouseButton(e.Button, true); + } + + protected override void OnMouseUp(MouseButtonEventArgs e) + { + base.OnMouseUp(e); + Input.SetMouseButton(e.Button, false); + } } } diff --git a/World.cs b/World.cs index a2e1aef..94d61a9 100644 --- a/World.cs +++ b/World.cs @@ -59,7 +59,7 @@ namespace Voxel chunk.SetBlock(localX, worldY, localZ, block); } - public (bool success, Blocks block, int x, int y, int z) Raycast(Vector3 origin, Vector3 direction, float maxDistance) + public (bool success, Blocks block, int x, int y, int z, Vector3i normal) Raycast(Vector3 origin, Vector3 direction, float maxDistance) { int x = (int)MathF.Floor(origin.X); int y = (int)MathF.Floor(origin.Y); @@ -84,49 +84,41 @@ namespace Voxel : (origin.Z - MathF.Floor(origin.Z)) * tDeltaZ; float distance = 0f; + Vector3i normal = Vector3i.Zero; while (distance <= maxDistance) { Blocks block = GetBlock(x, y, z); if (block != Blocks.Air) { - return (true, block, x, y, z); + return (true, block, x, y, z, normal); } // step to next voxel - if (tMaxX < tMaxY) + if (tMaxX < tMaxY && tMaxX < tMaxZ) { - if (tMaxX < tMaxZ) - { - x += stepX; - distance = tMaxX; - tMaxX += tDeltaX; - } - else - { - z += stepZ; - distance = tMaxZ; - tMaxZ += tDeltaZ; - } + x += stepX; + distance = tMaxX; + tMaxX += tDeltaX; + normal = new Vector3i(stepX > 0 ? -1 : 1, 0, 0); + } + else if (tMaxY < tMaxZ) + { + y += stepY; + distance = tMaxY; + tMaxY += tDeltaY; + normal = new Vector3i(0, stepY > 0 ? -1 : 1, 0); } else { - if (tMaxY < tMaxZ) - { - y += stepY; - distance = tMaxY; - tMaxY += tDeltaY; - } - else - { - z += stepZ; - distance = tMaxZ; - tMaxZ += tDeltaZ; - } + z += stepZ; + distance = tMaxZ; + tMaxZ += tDeltaZ; + normal = new Vector3i(0, 0, stepZ > 0 ? -1 : 1); } } - return (false, Blocks.Air, 0, 0, 0); + return (false, Blocks.Air, 0, 0, 0, normal); } public void Update()