peak
This commit is contained in:
11
Input.cs
11
Input.cs
@@ -5,6 +5,17 @@ namespace Voxel
|
||||
public static class Input
|
||||
{
|
||||
private static Dictionary<Keys, bool> _keystates = new Dictionary<Keys, bool>();
|
||||
private static Dictionary<MouseButton, bool> _mouseButtonStates = new Dictionary<MouseButton, bool>();
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
39
Player.cs
39
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();
|
||||
}
|
||||
}
|
||||
|
||||
23
Window.cs
23
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
48
World.cs
48
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()
|
||||
|
||||
Reference in New Issue
Block a user