Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Capture mouse and lcok vertical rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Jan 21, 2024
1 parent 256c49f commit e7f8c04
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
12 changes: 8 additions & 4 deletions Client/Rendering/World/ChunkRenderSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public void Reload() {
}

public class ChunkMesh : IDisposable {
public static readonly object BufLock = new();

public readonly VoxelClient Client;
public readonly RenderSystem RenderSystem;

Expand All @@ -136,10 +138,12 @@ public ChunkMesh(VoxelClient client, Span<BasicVertex.Packed> packedVertices, ui
Client = client;
RenderSystem = Client.RenderSystem;

Buffer = RenderSystem.ResourceFactory.CreateBuffer(new() {
SizeInBytes = (uint)Marshal.SizeOf<BasicVertex.Packed>() * (uint)packedVertices.Length, Usage = BufferUsage.VertexBuffer
});
RenderSystem.GraphicsDevice.UpdateBuffer(Buffer, 0, packedVertices);
lock (BufLock) {
Buffer = RenderSystem.ResourceFactory.CreateBuffer(new() {
SizeInBytes = (uint)Marshal.SizeOf<BasicVertex.Packed>() * (uint)packedVertices.Length, Usage = BufferUsage.VertexBuffer
});
RenderSystem.GraphicsDevice.UpdateBuffer(Buffer, 0, packedVertices);
}
IndexCount = indexCount;

Position = position;
Expand Down
25 changes: 21 additions & 4 deletions Client/VoxelClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Runtime.InteropServices;
using GlmSharp;
using Veldrid.Sdl2;
using Voxel.Client.Keybinding;
using Voxel.Client.Network;
using Voxel.Client.Rendering;
Expand All @@ -15,6 +17,9 @@ namespace Voxel.Client;
public class VoxelClient : Game {
public static VoxelClient Instance { get; private set; }

public static bool isMouseCapruted;
public static bool justCapturedMouse;

public GameRenderer GameRenderer { get; set; }

/// <summary>
Expand Down Expand Up @@ -77,11 +82,15 @@ public void SetupWorld() {

public override void OnFrame(double delta, double tickAccumulator) {
Keybinds.Poll();
justCapturedMouse = false;

if (Keybinds.Pause.justPressed)
CaptureMouse(false);
if (Keybinds.Attack.justPressed)
CaptureMouse(true);

if (Keybinds.Pause.justPressed) {
useMSAA = !useMSAA;
GameRenderer.SetMSAA(useMSAA ? 1u : 8u);
}
if (isMouseCapruted)
NativeWindow.SetMousePosition(new(NativeWindow.Width/2, NativeWindow.Height/2));

PlayerEntity?.Update(delta);

Expand All @@ -107,4 +116,12 @@ public override void Dispose() {
GameRenderer.Dispose();
base.Dispose();
}

private static void CaptureMouse(bool captured) {
if (Sdl2Native.SDL_SetRelativeMouseMode(captured) == -1)
return;
if (captured && !isMouseCapruted)
justCapturedMouse = true;
isMouseCapruted = captured;
}
}
9 changes: 8 additions & 1 deletion Client/World/Entity/ControlledClientPlayerEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public void Update(double delta) {
// movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength;

rotation += new dvec2((float)(looking.y * delta) * 1, (float)(looking.x * delta) * 1);
if (VoxelClient.isMouseCapruted)
rotation += VoxelClient.Instance.InputManager.MouseDelta.swizzle.yx * -1 / 192;

if (rotation.x < -MathF.PI/2)
rotation.x = -MathF.PI/2;
if (rotation.x > MathF.PI/2)
rotation.x = MathF.PI/2;

movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d * 4;
var localVel = dvec2.Lerp(velocity.xz, movement3d.xz, 0.9);
Expand All @@ -68,7 +75,7 @@ public void Update(double delta) {
VoxelClient.Instance.connection!.SendPacket(transformUpdate);


if (Keybinds.Attack.justPressed)
if (Keybinds.Attack.justPressed && !VoxelClient.justCapturedMouse)
BreakBlock();

if (Keybinds.Use.justPressed)
Expand Down
9 changes: 0 additions & 9 deletions Common/Collision/WorldRaycast.cs

This file was deleted.

0 comments on commit e7f8c04

Please sign in to comment.