Add archetype and change usage to fit api
This commit is contained in:
parent
265ad04473
commit
e63aab283b
4 changed files with 31 additions and 22 deletions
13
src/MovingBall.bf
Normal file
13
src/MovingBall.bf
Normal 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());
|
||||
}
|
||||
}
|
|
@ -12,51 +12,47 @@ class Program
|
|||
{
|
||||
Raylib.InitWindow(1280, 720, "Theater-ECS-Example");
|
||||
|
||||
|
||||
ECS ecs = new .();
|
||||
defer delete ecs;
|
||||
defer { DeleteECSAndItems!(ecs); }
|
||||
|
||||
RendererSystem rendererSystem = new .();
|
||||
defer delete rendererSystem;
|
||||
rendererSystem.RegisterSystem(ecs);
|
||||
ecs.Systems.RegisterSystem(
|
||||
new RendererSystem(),
|
||||
"RendererSystem");
|
||||
|
||||
MovementSystem movementSystem = new .();
|
||||
defer delete movementSystem;
|
||||
movementSystem.RegisterSystem(ecs);
|
||||
ecs.Systems.RegisterSystem(
|
||||
new MovementSystem(),
|
||||
"MovementSystem");
|
||||
|
||||
uint64 entityCounter = 0;
|
||||
|
||||
for(int32 i < 50000)
|
||||
{
|
||||
entityCounter++;
|
||||
var entity = ecs.Entity_Create();
|
||||
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());
|
||||
ecs.Entities.Create(MovingBall());
|
||||
}
|
||||
|
||||
|
||||
|
||||
while(!Raylib.WindowShouldClose())
|
||||
{
|
||||
movementSystem.RunSystem(ecs);
|
||||
ecs.Systems.RunSystem("MovementSystem", ecs)
|
||||
.IgnoreError();
|
||||
|
||||
if(Raylib.IsMouseButtonDown((.)MouseButton.MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
entityCounter++;
|
||||
var entity = ecs.Entity_Create();
|
||||
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());
|
||||
ecs.Entities.Create(MovingBall());
|
||||
}
|
||||
|
||||
Raylib.BeginDrawing();
|
||||
Raylib.ClearBackground(Raylib.RAYWHITE);
|
||||
|
||||
rendererSystem.RunSystem(ecs);
|
||||
ecs.Systems.RunSystem("RendererSystem", ecs)
|
||||
.IgnoreError();
|
||||
|
||||
Raylib.DrawFPS(0,0);
|
||||
Raylib.DrawText(entityCounter.ToString(.. scope .()), 0, 20, 20, Raylib.BLACK);
|
||||
Raylib.EndDrawing();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -15,6 +15,6 @@ class RendererSystem : System
|
|||
public override void Run(void* pos, void* sprite)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -4,6 +4,6 @@ using System;
|
|||
|
||||
struct Velocity
|
||||
{
|
||||
public float x = (.)gRand.NextI32() % 30;
|
||||
public float y = (.)gRand.NextI32() % 30;
|
||||
public float x = (.)gRand.NextI32() % 60;
|
||||
public float y = (.)gRand.NextI32() % 60;
|
||||
}
|
Loading…
Add table
Reference in a new issue