1
0
Fork 0

Add archetype and change usage to fit api

This commit is contained in:
Booklordofthedings 2024-11-18 10:28:36 +01:00
parent 265ad04473
commit e63aab283b
4 changed files with 31 additions and 22 deletions

13
src/MovingBall.bf Normal file
View file

@ -0,0 +1,13 @@
namespace Theater_ECS_Example;
using Theater_ECS;
struct MovingBall : IArchetypeable
{
public void Instantiate(ECS ecs, Entity e)
{
ecs.Components.AddComponentToEntity(e, ecs.Components.GetId(typeof(Position).GetFullName(.. scope .())), &Position());
ecs.Components.AddComponentToEntity(e, ecs.Components.GetId(typeof(Velocity).GetFullName(.. scope .())), &Velocity());
ecs.Components.AddComponentToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite());
}
}

View file

@ -12,51 +12,47 @@ class Program
{ {
Raylib.InitWindow(1280, 720, "Theater-ECS-Example"); Raylib.InitWindow(1280, 720, "Theater-ECS-Example");
ECS ecs = new .(); ECS ecs = new .();
defer delete ecs; defer { DeleteECSAndItems!(ecs); }
RendererSystem rendererSystem = new .(); ecs.Systems.RegisterSystem(
defer delete rendererSystem; new RendererSystem(),
rendererSystem.RegisterSystem(ecs); "RendererSystem");
MovementSystem movementSystem = new .(); ecs.Systems.RegisterSystem(
defer delete movementSystem; new MovementSystem(),
movementSystem.RegisterSystem(ecs); "MovementSystem");
uint64 entityCounter = 0; uint64 entityCounter = 0;
for(int32 i < 50000) for(int32 i < 50000)
{ {
entityCounter++; entityCounter++;
var entity = ecs.Entity_Create(); ecs.Entities.Create(MovingBall());
ecs.[Friend]_compRegistry.Components[rendererSystem.[Friend]_Components[0]].Add(entity, &Position());
ecs.[Friend]_compRegistry.Components[rendererSystem.[Friend]_Components[1]].Add(entity, &Sprite());
ecs.[Friend]_compRegistry.Components[movementSystem.[Friend]_Components[1]].Add(entity, &Velocity());
} }
while(!Raylib.WindowShouldClose()) while(!Raylib.WindowShouldClose())
{ {
movementSystem.RunSystem(ecs); ecs.Systems.RunSystem("MovementSystem", ecs)
.IgnoreError();
if(Raylib.IsMouseButtonDown((.)MouseButton.MOUSE_BUTTON_LEFT)) if(Raylib.IsMouseButtonDown((.)MouseButton.MOUSE_BUTTON_LEFT))
{ {
entityCounter++; entityCounter++;
var entity = ecs.Entity_Create(); ecs.Entities.Create(MovingBall());
ecs.[Friend]_compRegistry.Components[rendererSystem.[Friend]_Components[0]].Add(entity, &Position());
ecs.[Friend]_compRegistry.Components[rendererSystem.[Friend]_Components[1]].Add(entity, &Sprite());
ecs.[Friend]_compRegistry.Components[movementSystem.[Friend]_Components[1]].Add(entity, &Velocity());
} }
Raylib.BeginDrawing(); Raylib.BeginDrawing();
Raylib.ClearBackground(Raylib.RAYWHITE); Raylib.ClearBackground(Raylib.RAYWHITE);
rendererSystem.RunSystem(ecs); ecs.Systems.RunSystem("RendererSystem", ecs)
.IgnoreError();
Raylib.DrawFPS(0,0); Raylib.DrawFPS(0,0);
Raylib.DrawText(entityCounter.ToString(.. scope .()), 0, 20, 20, Raylib.BLACK); Raylib.DrawText(entityCounter.ToString(.. scope .()), 0, 20, 20, Raylib.BLACK);
Raylib.EndDrawing(); Raylib.EndDrawing();
} }
} }
} }

View file

@ -15,6 +15,6 @@ class RendererSystem : System
public override void Run(void* pos, void* sprite) public override void Run(void* pos, void* sprite)
{ {
var toDraw = (Position*)pos; var toDraw = (Position*)pos;
//Raylib.DrawTexture(((Sprite*)sprite).sprite, (.)toDraw.x, (.)toDraw.y, Raylib.WHITE); Raylib.DrawTexture(((Sprite*)sprite).sprite, (.)toDraw.x, (.)toDraw.y, Raylib.WHITE);
} }
} }

View file

@ -4,6 +4,6 @@ using System;
struct Velocity struct Velocity
{ {
public float x = (.)gRand.NextI32() % 30; public float x = (.)gRand.NextI32() % 60;
public float y = (.)gRand.NextI32() % 30; public float y = (.)gRand.NextI32() % 60;
} }