feat: refactoring && walls
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
public struct CanCollideComponent : IComponent
|
||||
{
|
||||
public bool CanCollide;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using Raylib_cs;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Components.Common;
|
||||
|
||||
public struct ColorComponent : IComponent
|
||||
{
|
||||
public Color Color { get; set; }
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Numerics;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Components.Common;
|
||||
|
||||
public struct PositionComponent : IComponent
|
||||
{
|
||||
public Vector3 Position;
|
||||
@@ -1,7 +1,8 @@
|
||||
using System.Numerics;
|
||||
using Raylib_cs;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Components.Environment;
|
||||
|
||||
public struct BlockComponent : IComponent
|
||||
{
|
||||
public Vector3 Size;
|
||||
@@ -1,7 +1,9 @@
|
||||
using Raylib_cs;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Components.Player;
|
||||
|
||||
public struct CameraComponent : IComponent
|
||||
{
|
||||
public Camera2D Camera;
|
||||
public Camera3D Camera;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Components.Player;
|
||||
|
||||
public struct HealthComponent : IComponent
|
||||
{
|
||||
public int HealthPoints;
|
||||
@@ -1,6 +1,8 @@
|
||||
using BakeryGame.Models;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Components.Player;
|
||||
|
||||
public struct MovementComponent : IComponent
|
||||
{
|
||||
public Direction Direction;
|
||||
@@ -1,7 +1,8 @@
|
||||
using System.Numerics;
|
||||
using Raylib_cs;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Components.Player;
|
||||
|
||||
public struct PlayerComponent : IComponent
|
||||
{
|
||||
public Vector3 Size;
|
||||
46
Entities/BlockFactory.cs
Normal file
46
Entities/BlockFactory.cs
Normal file
@@ -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<Entity> 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;
|
||||
}
|
||||
38
Entities/PlayerFactory.cs
Normal file
38
Entities/PlayerFactory.cs
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using Raylib_cs;
|
||||
|
||||
namespace BakeryGame.Models;
|
||||
|
||||
public struct EnvItem
|
||||
{
|
||||
public EnvItem(Rectangle rect, Color color, bool canCollide)
|
||||
|
||||
88
Program.cs
88
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);
|
||||
|
||||
46
SystemRegistrations.cs
Normal file
46
SystemRegistrations.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
@@ -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))
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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; }
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user