From 8c5c5260c2289072cbc4053c66a8709da7e09848 Mon Sep 17 00:00:00 2001 From: copygirl Date: Fri, 16 Sep 2022 23:26:29 +0200 Subject: [PATCH] Split CameraModule into Components and Controller --- src/Immersion/Program.cs | 7 +++- .../Modules/CameraComponents.cs | 34 +++++++++++++++++ .../{CameraModule.cs => CameraController.cs} | 38 ++++--------------- src/gaemstone.Client/Modules/Renderer.cs | 2 +- 4 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 src/gaemstone.Client/Modules/CameraComponents.cs rename src/gaemstone.Client/Modules/{CameraModule.cs => CameraController.cs} (63%) diff --git a/src/Immersion/Program.cs b/src/Immersion/Program.cs index e6615e2..db08a8d 100644 --- a/src/Immersion/Program.cs +++ b/src/Immersion/Program.cs @@ -8,7 +8,8 @@ using Silk.NET.Maths; using Silk.NET.OpenGL; using Silk.NET.Windowing; using static flecs_hub.flecs; -using static gaemstone.Client.CameraModule; +using static gaemstone.Client.CameraComponents; +using static gaemstone.Client.FreeCameraController; using static gaemstone.Client.Input; using static gaemstone.Client.Windowing; @@ -36,8 +37,10 @@ universe.RegisterComponent(); universe.RegisterComponent(); universe.RegisterComponent(); +universe.RegisterModule(); +universe.RegisterModule(); + universe.RegisterModule(); -universe.RegisterModule(); universe.RegisterModule(); game.Set(new RawInput()); diff --git a/src/gaemstone.Client/Modules/CameraComponents.cs b/src/gaemstone.Client/Modules/CameraComponents.cs new file mode 100644 index 0000000..f564913 --- /dev/null +++ b/src/gaemstone.Client/Modules/CameraComponents.cs @@ -0,0 +1,34 @@ +using gaemstone.ECS; +using Silk.NET.Maths; + +namespace gaemstone.Client; + +[Module] +[DependsOn(typeof(Input))] +public class CameraComponents +{ + [Component] + public struct Camera + { + public static readonly Camera Default2D = Create2D(); + public static readonly Camera Default3D = Create3D(80.0F); + + public static Camera Create2D(float nearPlane = -100.0F, float farPlane = 100.0F) + => new() { NearPlane = nearPlane, FarPlane = farPlane }; + public static Camera Create3D(float fieldOfView, float nearPlane = 0.1F, float farPlane = 200.0F) + => new() { FieldOfView = fieldOfView, NearPlane = nearPlane, FarPlane = farPlane }; + + public float FieldOfView { get; set; } + public float NearPlane { get; set; } + public float FarPlane { get; set; } + + public bool IsOrthographic => (FieldOfView == 0.0F); + } + + [Component] + public struct CameraViewport + { + public Vector4D ClearColor { get; set; } + public Rectangle Viewport { get; set; } + } +} diff --git a/src/gaemstone.Client/Modules/CameraModule.cs b/src/gaemstone.Client/Modules/CameraController.cs similarity index 63% rename from src/gaemstone.Client/Modules/CameraModule.cs rename to src/gaemstone.Client/Modules/CameraController.cs index 522851b..cad0629 100644 --- a/src/gaemstone.Client/Modules/CameraModule.cs +++ b/src/gaemstone.Client/Modules/CameraController.cs @@ -2,39 +2,16 @@ using System; using gaemstone.ECS; using Silk.NET.Input; using Silk.NET.Maths; +using static gaemstone.Client.CameraComponents; using static gaemstone.Client.Input; namespace gaemstone.Client; [Module] +[DependsOn(typeof(CameraComponents))] [DependsOn(typeof(Input))] -public class CameraModule +public class FreeCameraController { - [Component] - public struct Camera - { - public static readonly Camera Default2D = Create2D(); - public static readonly Camera Default3D = Create3D(80.0F); - - public static Camera Create2D(float nearPlane = -100.0F, float farPlane = 100.0F) - => new() { NearPlane = nearPlane, FarPlane = farPlane }; - public static Camera Create3D(float fieldOfView, float nearPlane = 0.1F, float farPlane = 200.0F) - => new() { FieldOfView = fieldOfView, NearPlane = nearPlane, FarPlane = farPlane }; - - public float FieldOfView { get; set; } - public float NearPlane { get; set; } - public float FarPlane { get; set; } - - public bool IsOrthographic => (FieldOfView == 0.0F); - } - - [Component] - public struct CameraViewport - { - public Vector4D ClearColor { get; set; } - public Rectangle Viewport { get; set; } - } - [Component] public struct CameraController { @@ -54,10 +31,11 @@ public class CameraModule else controller.MouseGrabbedAt = null; } - if (controller.MouseGrabbedAt is not Vector2D pos) return; - - var mouseMoved = input.MousePosition - pos; - input.Context!.Mice[0].Position = pos.ToSystem(); + var mouseMoved = Vector2D.Zero; + if (controller.MouseGrabbedAt is Vector2D pos) { + mouseMoved = input.MousePosition - pos; + input.Context!.Mice[0].Position = pos.ToSystem(); + } var dt = (float)delta.TotalSeconds; var xMovement = mouseMoved.X * dt * controller.MouseSensitivity; diff --git a/src/gaemstone.Client/Modules/Renderer.cs b/src/gaemstone.Client/Modules/Renderer.cs index 8d8c533..944641d 100644 --- a/src/gaemstone.Client/Modules/Renderer.cs +++ b/src/gaemstone.Client/Modules/Renderer.cs @@ -4,7 +4,7 @@ using System.Runtime.InteropServices; using gaemstone.ECS; using Silk.NET.Maths; using Silk.NET.OpenGL; -using static gaemstone.Client.CameraModule; +using static gaemstone.Client.CameraComponents; using static gaemstone.Client.Windowing; namespace gaemstone.Client;