From 36af656e0d9a95aaa8bead8d9db1d72a5674d7e9 Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Thu, 18 Apr 2024 16:04:53 +0700 Subject: [PATCH] feat: refactoring && walls --- Components/CanCollideComponent.cs | 6 -- Components/{ => Common}/ColorComponent.cs | 2 + Components/{ => Common}/PositionComponent.cs | 2 + .../{ => Environment}/BlockComponent.cs | 3 +- Components/{ => Player}/CameraComponent.cs | 4 +- Components/{ => Player}/HealthComponent.cs | 2 + Components/{ => Player}/MovementComponent.cs | 2 + Components/{ => Player}/PlayerComponent.cs | 3 +- Entities/BlockFactory.cs | 46 ++++++++++ Entities/PlayerFactory.cs | 38 ++++++++ Models/EnvItem.cs | 2 + Program.cs | 92 ++++--------------- SystemRegistrations.cs | 46 ++++++++++ Systems/{ => Common}/CollisionSystem.cs | 5 +- Systems/{ => Player}/CameraSystem.cs | 21 +++-- Systems/{ => Player}/HealthSystem.cs | 3 + Systems/{ => Player}/MovementSystem.cs | 4 + .../PlayerRenderSystem.cs} | 7 +- Systems/{ => Rendering}/BlockRenderSystem.cs | 4 + Systems/{ => Rendering}/HPRenderSystem.cs | 3 + 20 files changed, 202 insertions(+), 93 deletions(-) delete mode 100644 Components/CanCollideComponent.cs rename Components/{ => Common}/ColorComponent.cs (75%) rename Components/{ => Common}/PositionComponent.cs (75%) rename Components/{ => Environment}/BlockComponent.cs (71%) rename Components/{ => Player}/CameraComponent.cs (55%) rename Components/{ => Player}/HealthComponent.cs (70%) rename Components/{ => Player}/MovementComponent.cs (78%) rename Components/{ => Player}/PlayerComponent.cs (74%) create mode 100644 Entities/BlockFactory.cs create mode 100644 Entities/PlayerFactory.cs create mode 100644 SystemRegistrations.cs rename Systems/{ => Common}/CollisionSystem.cs (95%) rename Systems/{ => Player}/CameraSystem.cs (69%) rename Systems/{ => Player}/HealthSystem.cs (88%) rename Systems/{ => Player}/MovementSystem.cs (93%) rename Systems/{BallRenderSystem.cs => Player/PlayerRenderSystem.cs} (76%) rename Systems/{ => Rendering}/BlockRenderSystem.cs (87%) rename Systems/{ => Rendering}/HPRenderSystem.cs (88%) diff --git a/Components/CanCollideComponent.cs b/Components/CanCollideComponent.cs deleted file mode 100644 index 6eb4ed2..0000000 --- a/Components/CanCollideComponent.cs +++ /dev/null @@ -1,6 +0,0 @@ -using Scellecs.Morpeh; - -public struct CanCollideComponent : IComponent -{ - public bool CanCollide; -} \ No newline at end of file diff --git a/Components/ColorComponent.cs b/Components/Common/ColorComponent.cs similarity index 75% rename from Components/ColorComponent.cs rename to Components/Common/ColorComponent.cs index af01a7f..94609cf 100644 --- a/Components/ColorComponent.cs +++ b/Components/Common/ColorComponent.cs @@ -1,6 +1,8 @@ using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Components.Common; + public struct ColorComponent : IComponent { public Color Color { get; set; } diff --git a/Components/PositionComponent.cs b/Components/Common/PositionComponent.cs similarity index 75% rename from Components/PositionComponent.cs rename to Components/Common/PositionComponent.cs index 221fde5..831458f 100644 --- a/Components/PositionComponent.cs +++ b/Components/Common/PositionComponent.cs @@ -1,6 +1,8 @@ using System.Numerics; using Scellecs.Morpeh; +namespace BakeryGame.Components.Common; + public struct PositionComponent : IComponent { public Vector3 Position; diff --git a/Components/BlockComponent.cs b/Components/Environment/BlockComponent.cs similarity index 71% rename from Components/BlockComponent.cs rename to Components/Environment/BlockComponent.cs index c1b7729..28d94ce 100644 --- a/Components/BlockComponent.cs +++ b/Components/Environment/BlockComponent.cs @@ -1,7 +1,8 @@ using System.Numerics; -using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Components.Environment; + public struct BlockComponent : IComponent { public Vector3 Size; diff --git a/Components/CameraComponent.cs b/Components/Player/CameraComponent.cs similarity index 55% rename from Components/CameraComponent.cs rename to Components/Player/CameraComponent.cs index a97984b..de98267 100644 --- a/Components/CameraComponent.cs +++ b/Components/Player/CameraComponent.cs @@ -1,7 +1,9 @@ using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Components.Player; + public struct CameraComponent : IComponent { - public Camera2D Camera; + public Camera3D Camera; } \ No newline at end of file diff --git a/Components/HealthComponent.cs b/Components/Player/HealthComponent.cs similarity index 70% rename from Components/HealthComponent.cs rename to Components/Player/HealthComponent.cs index 1f963a6..cf2dd28 100644 --- a/Components/HealthComponent.cs +++ b/Components/Player/HealthComponent.cs @@ -1,5 +1,7 @@ using Scellecs.Morpeh; +namespace BakeryGame.Components.Player; + public struct HealthComponent : IComponent { public int HealthPoints; diff --git a/Components/MovementComponent.cs b/Components/Player/MovementComponent.cs similarity index 78% rename from Components/MovementComponent.cs rename to Components/Player/MovementComponent.cs index 646da8b..2117c72 100644 --- a/Components/MovementComponent.cs +++ b/Components/Player/MovementComponent.cs @@ -1,6 +1,8 @@ using BakeryGame.Models; using Scellecs.Morpeh; +namespace BakeryGame.Components.Player; + public struct MovementComponent : IComponent { public Direction Direction; diff --git a/Components/PlayerComponent.cs b/Components/Player/PlayerComponent.cs similarity index 74% rename from Components/PlayerComponent.cs rename to Components/Player/PlayerComponent.cs index 516bbaa..3f11c3d 100644 --- a/Components/PlayerComponent.cs +++ b/Components/Player/PlayerComponent.cs @@ -1,7 +1,8 @@ using System.Numerics; -using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Components.Player; + public struct PlayerComponent : IComponent { public Vector3 Size; diff --git a/Entities/BlockFactory.cs b/Entities/BlockFactory.cs new file mode 100644 index 0000000..46bd329 --- /dev/null +++ b/Entities/BlockFactory.cs @@ -0,0 +1,46 @@ +using System.Numerics; +using BakeryGame.Components.Common; +using BakeryGame.Components.Environment; +using Raylib_cs; +using Scellecs.Morpeh; + +namespace BakeryGame.Entities; + +public class BlockFactory +{ + private readonly World _world; + + public BlockFactory(World world) + { + _world = world; + } + + public Entity CreateBlock(float x, float z) + { + var position = new Vector3(x, 1.0f, z); + var size = new Vector3(BlockSize, BlockSize, BlockSize); + + var block = _world.CreateEntity(); + block.SetComponent(new BlockComponent { Size = size }); + block.SetComponent(new ColorComponent { Color = Color.Gray }); + block.SetComponent(new PositionComponent(){ Position = position }); + + return block; + } + + public IEnumerable GenerateMapOfBlocks() + { + for (int x = -16 / 2; x <= 16 / 2; x++) { + for (int z = -16 / 2; z <= 16 / 2; z++) { + if (x == -16 / 2 || x == 16 / 2 || z == -16 / 2 || z == 16 / 2) + { + yield return CreateBlock(x, z); + } + } + } + + + } + + public const float BlockSize = 1; +} \ No newline at end of file diff --git a/Entities/PlayerFactory.cs b/Entities/PlayerFactory.cs new file mode 100644 index 0000000..7c24b40 --- /dev/null +++ b/Entities/PlayerFactory.cs @@ -0,0 +1,38 @@ +using System.Numerics; +using BakeryGame.Components.Common; +using BakeryGame.Components.Player; +using Raylib_cs; +using Scellecs.Morpeh; + +namespace BakeryGame.Entities; + +public class PlayerFactory +{ + private readonly World _world; + + public PlayerFactory(World world) + { + _world = world; + } + + public Entity CreatePlayer(out CameraComponent camera) + { + var player = _world.CreateEntity(); + player.SetComponent(new HealthComponent { HealthPoints = 100 }); + player.SetComponent(new PositionComponent { Position = new Vector3(0.0f, 1.0f, 2.0f) }); + player.SetComponent(new MovementComponent() { Speed = 0.1f }); + camera = new CameraComponent() + { + Camera = new Camera3D(new(0.0f, 10.0f, 10.0f), new(0.0f, 0.0f, 0.0f), new(0.0f, 1.0f, 0.0f), 45.0f, 0) + }; + player.SetComponent(camera); + + player.SetComponent(new PlayerComponent + { + Size = new Vector3(1.0f, 2.0f, 1.0f ), + }); + + return player; + } + +} \ No newline at end of file diff --git a/Models/EnvItem.cs b/Models/EnvItem.cs index b4430d6..49aacdc 100644 --- a/Models/EnvItem.cs +++ b/Models/EnvItem.cs @@ -1,5 +1,7 @@ using Raylib_cs; +namespace BakeryGame.Models; + public struct EnvItem { public EnvItem(Rectangle rect, Color color, bool canCollide) diff --git a/Program.cs b/Program.cs index 14c2552..129b909 100644 --- a/Program.cs +++ b/Program.cs @@ -1,10 +1,20 @@ // See https://aka.ms/new-console-template for more information using System.Numerics; +using BakeryGame.Components.Common; +using BakeryGame.Components.Environment; +using BakeryGame.Components.Player; +using BakeryGame.Entities; +using BakeryGame.Models; using BakeryGame.Systems; +using BakeryGame.Systems.Common; +using BakeryGame.Systems.Player; +using BakeryGame.Systems.Rendering; using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame; + internal class Program { private static World _world; @@ -18,92 +28,30 @@ internal class Program new EnvItem(new Rectangle(650, 300, 100, 10), Color.Gray, true) }; - private static Entity CreateBlock() - { - var position = new Vector3( -4.0f, 1.0f, 2.0f ); - var size = new Vector3( 2.0f, 2.0f, 2.0f ); - - var block = _world.CreateEntity(); - block.SetComponent(new BlockComponent { Size = size }); - block.SetComponent(new ColorComponent { Color = Color.Gray }); - block.SetComponent(new PositionComponent(){ Position = position }); - - return block; - } - - private static Entity CreatePlayer() - { - var player = _world.CreateEntity(); - player.SetComponent(new HealthComponent { HealthPoints = 100 }); - player.SetComponent(new PositionComponent { Position = new Vector3(0.0f, 1.0f, 2.0f) }); - player.SetComponent(new MovementComponent() { Speed = 0.1f }); - // ball.SetComponent(new CameraComponent - // { - // Camera = new Cam2qera2D - // { - // Target = new Vector2(800 / 2 + 10.0f, 480 / 2 + 10.0f), - // Offset = new Vector2(800 / 2, 480 / 2), - // Rotation = 0.0f, - // Zoom = 1.0f - // } - // }); - player.SetComponent(new PlayerComponent - { - Size = new Vector3(1.0f, 2.0f, 1.0f ), - }); - - return player; - } + private static void Main(string[] args) { _world = World.Create(); - + var playerFactory = new PlayerFactory(_world); + var blockFactory = new BlockFactory(_world); + Raylib.InitWindow(800, 480, "Hello World"); - var player = CreatePlayer(); - var block = CreateBlock(); - var camera = new Camera3D(new ( 0.0f, 10.0f, 10.0f ), new( 0.0f, 0.0f, 0.0f ), new ( 0.0f, 1.0f, 0.0f ), 45.0f, 0); + var player = playerFactory.CreatePlayer(out var camera); + var block = blockFactory.GenerateMapOfBlocks().ToList(); - var healthSystem = new HealthSystem { World = _world }; - var movementSystem = new MovementSystem { World = _world }; - var collisionSystem = new CollisionSystem(_world); - var systemsGroup = _world.CreateSystemsGroup(); - systemsGroup.AddSystem(healthSystem); - systemsGroup.AddSystem(movementSystem); - systemsGroup.AddSystem(collisionSystem); - - systemsGroup.EnableSystem(movementSystem); - systemsGroup.EnableSystem(healthSystem); - systemsGroup.EnableSystem(collisionSystem); - _world.AddSystemsGroup(0, systemsGroup); - - var renderSystemsGroup = _world.CreateSystemsGroup(); - var hpRenreSystem = new HPRenderSystem { World = _world }; - var ballRenderSystem = new BallRenderSystem { World = _world }; - //var cameraSystem = new CameraSystem(_world); - var blockRenderSystem = new BlockRenderSystem(_world); - - renderSystemsGroup.AddSystem(blockRenderSystem); - renderSystemsGroup.AddSystem(ballRenderSystem); - renderSystemsGroup.AddSystem(hpRenreSystem); - //renderSystemsGroup.AddSystem(cameraSystem); - - renderSystemsGroup.EnableSystem(blockRenderSystem); - renderSystemsGroup.EnableSystem(ballRenderSystem); - renderSystemsGroup.EnableSystem(hpRenreSystem); - //renderSystemsGroup.EnableSystem(cameraSystem); - - _world.AddSystemsGroup(1, renderSystemsGroup); + SystemRegistrations.RegisterLogicGroup(_world); + SystemRegistrations.RegisterGraphicsGroup(_world); Raylib.SetTargetFPS(60); while (!Raylib.WindowShouldClose()) { var deltaTime = Raylib.GetFrameTime(); - + //Raylib.UpdateCamera(ref camera.Camera, CameraMode.Free); Raylib.BeginDrawing(); Raylib.ClearBackground(Color.White); - Raylib.BeginMode3D(camera); + Raylib.BeginMode3D(camera.Camera); _world.Update(deltaTime); Raylib.DrawGrid(10, 1.0f); _world.CleanupUpdate(deltaTime); diff --git a/SystemRegistrations.cs b/SystemRegistrations.cs new file mode 100644 index 0000000..acd54d2 --- /dev/null +++ b/SystemRegistrations.cs @@ -0,0 +1,46 @@ +using BakeryGame.Systems.Common; +using BakeryGame.Systems.Player; +using BakeryGame.Systems.Rendering; +using Scellecs.Morpeh; + +namespace BakeryGame; + +public static class SystemRegistrations +{ + public static void RegisterLogicGroup(World world) + { + var healthSystem = new HealthSystem { World = world }; + var movementSystem = new MovementSystem { World = world }; + var collisionSystem = new CollisionSystem(world); + var systemsGroup = world.CreateSystemsGroup(); + systemsGroup.AddSystem(healthSystem); + systemsGroup.AddSystem(movementSystem); + systemsGroup.AddSystem(collisionSystem); + + systemsGroup.EnableSystem(movementSystem); + systemsGroup.EnableSystem(healthSystem); + systemsGroup.EnableSystem(collisionSystem); + world.AddSystemsGroup(0, systemsGroup); + } + + public static void RegisterGraphicsGroup(World world) + { + var renderSystemsGroup = world.CreateSystemsGroup(); + var hpRenreSystem = new HPRenderSystem { World = world }; + var ballRenderSystem = new PlayerRenderSystem { World = world }; + var cameraSystem = new CameraSystem(world); + var blockRenderSystem = new BlockRenderSystem(world); + + renderSystemsGroup.AddSystem(blockRenderSystem); + renderSystemsGroup.AddSystem(ballRenderSystem); + renderSystemsGroup.AddSystem(hpRenreSystem); + renderSystemsGroup.AddSystem(cameraSystem); + + renderSystemsGroup.EnableSystem(blockRenderSystem); + renderSystemsGroup.EnableSystem(ballRenderSystem); + renderSystemsGroup.EnableSystem(hpRenreSystem); + renderSystemsGroup.EnableSystem(cameraSystem); + + world.AddSystemsGroup(1, renderSystemsGroup); + } +} \ No newline at end of file diff --git a/Systems/CollisionSystem.cs b/Systems/Common/CollisionSystem.cs similarity index 95% rename from Systems/CollisionSystem.cs rename to Systems/Common/CollisionSystem.cs index 40e5118..c20f681 100644 --- a/Systems/CollisionSystem.cs +++ b/Systems/Common/CollisionSystem.cs @@ -1,9 +1,12 @@ using System.Numerics; +using BakeryGame.Components.Common; +using BakeryGame.Components.Environment; +using BakeryGame.Components.Player; using BakeryGame.Models; using Raylib_cs; using Scellecs.Morpeh; -namespace BakeryGame.Systems; +namespace BakeryGame.Systems.Common; public class CollisionSystem: ISystem { diff --git a/Systems/CameraSystem.cs b/Systems/Player/CameraSystem.cs similarity index 69% rename from Systems/CameraSystem.cs rename to Systems/Player/CameraSystem.cs index 0f12fef..e950528 100644 --- a/Systems/CameraSystem.cs +++ b/Systems/Player/CameraSystem.cs @@ -1,7 +1,9 @@ -using System.Numerics; -using Raylib_cs; +using BakeryGame.Components.Common; +using BakeryGame.Components.Player; using Scellecs.Morpeh; +namespace BakeryGame.Systems.Player; + public class CameraSystem : ISystem { private Filter _filter; @@ -28,21 +30,22 @@ public class CameraSystem : ISystem ref var camera = ref cameraComponent.Camera; // Camera target follows player - camera.Target = new Vector2(position.X + 20, position.Y + 20); + //camera.Target.X = position.X; + //camera.Target.Z = position.Z; // // Camera rotation controls // if (IsKeyDown(KEY_A)) camera.rotation--; // else if (IsKeyDown(KEY_S)) camera.rotation++; // Limit camera rotation to 80 degrees (-40 to 40) - if (camera.Rotation > 40) camera.Rotation = 40; - else if (camera.Rotation < -40) camera.Rotation = -40; + // if (camera.Rotation > 40) camera.Rotation = 40; + // else if (camera.Rotation < -40) camera.Rotation = -40; // Camera zoom controls - camera.Zoom += Raylib.GetMouseWheelMove() * 0.05f; - - if (camera.Zoom > 3.0f) camera.Zoom = 3.0f; - else if (camera.Zoom < 0.1f) camera.Zoom = 0.1f; + // camera.Zoom += Raylib.GetMouseWheelMove() * 0.05f; + // + // if (camera.Zoom > 3.0f) camera.Zoom = 3.0f; + // else if (camera.Zoom < 0.1f) camera.Zoom = 0.1f; // // Camera reset (zoom and rotation) // if (IsKeyPressed(KEY_R)) diff --git a/Systems/HealthSystem.cs b/Systems/Player/HealthSystem.cs similarity index 88% rename from Systems/HealthSystem.cs rename to Systems/Player/HealthSystem.cs index fd96d4b..9c358a9 100644 --- a/Systems/HealthSystem.cs +++ b/Systems/Player/HealthSystem.cs @@ -1,6 +1,9 @@ +using BakeryGame.Components.Player; using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Systems.Player; + public sealed class HealthSystem : ISystem { private Filter filter; diff --git a/Systems/MovementSystem.cs b/Systems/Player/MovementSystem.cs similarity index 93% rename from Systems/MovementSystem.cs rename to Systems/Player/MovementSystem.cs index 30f49ff..3eed383 100644 --- a/Systems/MovementSystem.cs +++ b/Systems/Player/MovementSystem.cs @@ -1,7 +1,11 @@ +using BakeryGame.Components.Common; +using BakeryGame.Components.Player; using BakeryGame.Models; using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Systems.Player; + public sealed class MovementSystem : ISystem { private Filter _filter; diff --git a/Systems/BallRenderSystem.cs b/Systems/Player/PlayerRenderSystem.cs similarity index 76% rename from Systems/BallRenderSystem.cs rename to Systems/Player/PlayerRenderSystem.cs index a0f75d4..239c90a 100644 --- a/Systems/BallRenderSystem.cs +++ b/Systems/Player/PlayerRenderSystem.cs @@ -1,8 +1,11 @@ -using System.Numerics; +using BakeryGame.Components.Common; +using BakeryGame.Components.Player; using Raylib_cs; using Scellecs.Morpeh; -public class BallRenderSystem : ISystem +namespace BakeryGame.Systems.Player; + +public class PlayerRenderSystem : ISystem { private Filter _filter; public World World { get; set; } diff --git a/Systems/BlockRenderSystem.cs b/Systems/Rendering/BlockRenderSystem.cs similarity index 87% rename from Systems/BlockRenderSystem.cs rename to Systems/Rendering/BlockRenderSystem.cs index eca55e3..1364ce9 100644 --- a/Systems/BlockRenderSystem.cs +++ b/Systems/Rendering/BlockRenderSystem.cs @@ -1,6 +1,10 @@ +using BakeryGame.Components.Common; +using BakeryGame.Components.Environment; using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Systems.Rendering; + public class BlockRenderSystem : ISystem { private Filter _filter; diff --git a/Systems/HPRenderSystem.cs b/Systems/Rendering/HPRenderSystem.cs similarity index 88% rename from Systems/HPRenderSystem.cs rename to Systems/Rendering/HPRenderSystem.cs index ef5935a..0ce81a5 100644 --- a/Systems/HPRenderSystem.cs +++ b/Systems/Rendering/HPRenderSystem.cs @@ -1,6 +1,9 @@ +using BakeryGame.Components.Player; using Raylib_cs; using Scellecs.Morpeh; +namespace BakeryGame.Systems.Rendering; + public class HPRenderSystem : ILateSystem { private Filter _filter;