diff --git a/img.png b/img.png index 6c0ed6f..27f7e4f 100644 Binary files a/img.png and b/img.png differ diff --git a/pellet.png b/pellet.png new file mode 100644 index 0000000..a0ec1a0 Binary files /dev/null and b/pellet.png differ diff --git a/src/MovingBall.bf b/src/Archetypes/MovingBall.bf similarity index 75% rename from src/MovingBall.bf rename to src/Archetypes/MovingBall.bf index 0bec0a9..315a26b 100644 --- a/src/MovingBall.bf +++ b/src/Archetypes/MovingBall.bf @@ -1,13 +1,17 @@ -namespace Theater_ECS_Example; +namespace Theater_ECS_Example.Archetypes; +using Theater_ECS_Example.Components; + using Theater_ECS; struct MovingBall : IArchetypeable { + + public void Instantiate(ECS ecs, Entity e) { ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Position).GetFullName(.. scope .())), &Position()); ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Velocity).GetFullName(.. scope .())), &Velocity()); - ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite()); + ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite(1)); } } \ No newline at end of file diff --git a/src/Archetypes/Player.bf b/src/Archetypes/Player.bf new file mode 100644 index 0000000..20679fb --- /dev/null +++ b/src/Archetypes/Player.bf @@ -0,0 +1,21 @@ +namespace Theater_ECS_Example.Archetypes; +using Theater_ECS_Example.Components; + +using Theater_ECS; + + +struct Player : IArchetypeable +{ + public void Instantiate(ECS ecs, Entity e) + { + ecs.Components.AddToEntity( e, ecs.Components.GetId(typeof(Position).GetFullName(.. scope .())), + &Position() + { + x = 1280/2, + y = 720/2} + ); + ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(PlayerControlable).GetFullName(.. scope .())), &PlayerControlable()); + ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Velocity).GetFullName(.. scope .())), &Velocity()); + ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite()); + } +} \ No newline at end of file diff --git a/src/Components/PlayerControlable.bf b/src/Components/PlayerControlable.bf new file mode 100644 index 0000000..245c0bb --- /dev/null +++ b/src/Components/PlayerControlable.bf @@ -0,0 +1,11 @@ +namespace Theater_ECS_Example.Components; + +using RaylibBeef; + +struct PlayerControlable +{ + public KeyboardKey Left = .KEY_A; + public KeyboardKey Right = .KEY_D; + public KeyboardKey Down = .KEY_S; + public KeyboardKey Up = .KEY_W; +} \ No newline at end of file diff --git a/src/Components/Position.bf b/src/Components/Position.bf new file mode 100644 index 0000000..2ec6256 --- /dev/null +++ b/src/Components/Position.bf @@ -0,0 +1,15 @@ +namespace Theater_ECS_Example.Components; + +using System; + +struct Position +{ + public float x; + public float y; + + public void Update(float a, float b) mut + { + x += a; + y += b; + } +} \ No newline at end of file diff --git a/src/Components/Sprite.bf b/src/Components/Sprite.bf new file mode 100644 index 0000000..77e214b --- /dev/null +++ b/src/Components/Sprite.bf @@ -0,0 +1,26 @@ +namespace Theater_ECS_Example.Components; + +using System; + +using RaylibBeef; + +struct Sprite +{ + private static Texture2D? _texture = null; + private static Texture2D? _pellet = null; + + public Texture2D sprite; + + public this(uint8 s = 0) + { + if(_texture == null) + _texture = Raylib.LoadTexture("img.png"); + if(_pellet == null) + _pellet = Raylib.LoadTexture("pellet.png"); + + if(s == 0) + sprite = (.)_texture; + else + sprite = (.)_pellet; + } +} \ No newline at end of file diff --git a/src/Components/Velocity.bf b/src/Components/Velocity.bf new file mode 100644 index 0000000..8f9c411 --- /dev/null +++ b/src/Components/Velocity.bf @@ -0,0 +1,9 @@ +namespace Theater_ECS_Example.Components; + +using System; + +struct Velocity +{ + public float x = 0; + public float y = 0; +} \ No newline at end of file diff --git a/src/Position.bf b/src/Position.bf deleted file mode 100644 index 15faf97..0000000 --- a/src/Position.bf +++ /dev/null @@ -1,15 +0,0 @@ -namespace Theater_ECS_Example; - -using System; - -struct Position -{ - public float x = (.)gRand.NextI32() % 1280; - public float y = (.)gRand.NextI32() % 720; - - public void Update(float a, float b) mut - { - x += a; - y += b; - } -} \ No newline at end of file diff --git a/src/Program.bf b/src/Program.bf index d4f4baf..79994cc 100644 --- a/src/Program.bf +++ b/src/Program.bf @@ -1,4 +1,8 @@ namespace Theater_ECS_Example; +using Theater_ECS_Example.Systems; +using Theater_ECS_Example.Components; +using Theater_ECS_Example.Archetypes; + using System; @@ -23,20 +27,21 @@ class Program new MovementSystem(), "MovementSystem"); - uint64 entityCounter = 0; + ecs.Systems.RegisterSystem( + new CharacterSystem(), + "CharacterSystem"); - for(int32 i < 50) - { - entityCounter++; - ecs.Entities.Create(MovingBall()); - } + uint64 entityCounter = 1; + ecs.Entities.Create(Player()); while(!Raylib.WindowShouldClose()) { ecs.Systems.RunSystem("MovementSystem", ecs) .IgnoreError(); + ecs.Systems.RunSystem("CharacterSystem", ecs) + .IgnoreError(); if(Raylib.IsMouseButtonDown((.)MouseButton.MOUSE_BUTTON_LEFT)) { diff --git a/src/Sprite.bf b/src/Sprite.bf deleted file mode 100644 index d342ffa..0000000 --- a/src/Sprite.bf +++ /dev/null @@ -1,19 +0,0 @@ -namespace Theater_ECS_Example; - -using System; - -using RaylibBeef; - -struct Sprite -{ - private static Texture2D? _texture = null; - - public Texture2D sprite; - - public this() - { - if(_texture == null) - _texture = Raylib.LoadTexture("img.png"); - sprite = (.)_texture; - } -} \ No newline at end of file diff --git a/src/Systems/CharacterSystem.bf b/src/Systems/CharacterSystem.bf new file mode 100644 index 0000000..a93c8f7 --- /dev/null +++ b/src/Systems/CharacterSystem.bf @@ -0,0 +1,34 @@ +namespace Theater_ECS_Example.Systems; +using Theater_ECS_Example.Components; + +using Theater_ECS; + +using RaylibBeef; + +class CharacterSystem : System +{ + public override void RegisterSystem(ECS ecs) + { + RegisterComponent(ecs); + RegisterComponent(ecs); + + Run_2 = => RunFunction; + } + + public static void RunFunction(void* p1, void* p2) + { + var vel = (Velocity*)p2; + var controls = (PlayerControlable*)p1; + + if(Raylib.IsKeyDown(controls.Right)) + vel.x += 100; + else if(Raylib.IsKeyDown(controls.Left)) + vel.x += -100; + + if(Raylib.IsKeyDown(controls.Down)) + vel.y += 100; + else if(Raylib.IsKeyDown(controls.Up)) + vel.y += -100; + //What is this normalization thing that you speak of ? + } +} \ No newline at end of file diff --git a/src/MovementSystem.bf b/src/Systems/MovementSystem.bf similarity index 65% rename from src/MovementSystem.bf rename to src/Systems/MovementSystem.bf index 209bda7..c548e4f 100644 --- a/src/MovementSystem.bf +++ b/src/Systems/MovementSystem.bf @@ -1,4 +1,7 @@ -namespace Theater_ECS_Example; +namespace Theater_ECS_Example.Systems; +using Theater_ECS_Example.Components; + +using RaylibBeef; using Theater_ECS; @@ -20,6 +23,12 @@ class MovementSystem : System ((Velocity*)vel).x * RaylibBeef.Raylib.GetFrameTime(), ((Velocity*)vel).y * RaylibBeef.Raylib.GetFrameTime() ); + ((Velocity*)vel).x = System.Math.Clamp(((Velocity*)vel).x, -50, 50); + ((Velocity*)vel).y = System.Math.Clamp(((Velocity*)vel).y, -50, 50); + + ((Velocity*)vel).y *= 0.9f; + ((Velocity*)vel).x *= 0.9f; + if(mPos.x > 1280 || mPos.x < 0) ((Velocity*)vel).x *= -1; diff --git a/src/RendererSystem.bf b/src/Systems/RendererSystem.bf similarity index 83% rename from src/RendererSystem.bf rename to src/Systems/RendererSystem.bf index 9ef1818..9596100 100644 --- a/src/RendererSystem.bf +++ b/src/Systems/RendererSystem.bf @@ -1,4 +1,6 @@ -namespace Theater_ECS_Example; +namespace Theater_ECS_Example.Systems; +using Theater_ECS_Example.Components; + using Theater_ECS; diff --git a/src/Velocity.bf b/src/Velocity.bf deleted file mode 100644 index 5dee86c..0000000 --- a/src/Velocity.bf +++ /dev/null @@ -1,9 +0,0 @@ -namespace Theater_ECS_Example; - -using System; - -struct Velocity -{ - public float x = (.)gRand.NextI32() % 60; - public float y = (.)gRand.NextI32() % 60; -} \ No newline at end of file