2024-11-11 19:47:09 +01:00
|
|
|
namespace Theater_ECS;
|
2024-11-14 15:35:33 +01:00
|
|
|
using Theater_ECS.Containers;
|
|
|
|
using Theater_ECS.Example;
|
2024-11-11 19:47:09 +01:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
abstract class System
|
|
|
|
{
|
2024-11-14 15:35:33 +01:00
|
|
|
private List<Component> _Components = new .(10) ~ delete _;
|
2024-11-11 19:47:09 +01:00
|
|
|
|
2024-11-14 15:35:33 +01:00
|
|
|
///Get the compoent id and cache it inside of this system
|
|
|
|
public void RegisterComponent<T>(ECS ecs) where T : struct => _Components.Add(ecs.RegisterComponent<T>());
|
2024-11-11 19:47:09 +01:00
|
|
|
|
2024-11-14 15:35:33 +01:00
|
|
|
public abstract void RegisterSystem(ECS ecs);
|
2024-11-11 19:47:09 +01:00
|
|
|
|
2024-11-14 15:35:33 +01:00
|
|
|
public virtual void Run(void* p1) { };
|
|
|
|
public virtual void Run(void* p1, void* p2) { };
|
|
|
|
public virtual void Run(void* p1, void* p2, void* p3) { };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void RunSystem(ECS ecs)
|
|
|
|
{
|
|
|
|
int_cosize count = int_cosize.MaxValue;
|
|
|
|
Component comp = _Components[0];
|
|
|
|
|
|
|
|
for(var c in _Components)
|
|
|
|
if(ecs.GetComponentCount(c) < count)
|
|
|
|
{
|
|
|
|
count = ecs.GetComponentCount(c);
|
|
|
|
comp = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
//We now have the list of entities to loop through
|
|
|
|
Span<Entity> entities = ecs.GetComponentEntities(comp);
|
|
|
|
UList main = ecs.[Friend]_compRegistry.Components[comp].[Friend]_packedEntities;
|
|
|
|
|
|
|
|
switch(_Components.Count)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
if(comp == _Components[0])
|
|
|
|
{
|
|
|
|
var cun = entities.Length-1;
|
|
|
|
for(int ii < cun)
|
|
|
|
{
|
|
|
|
this.Run(
|
|
|
|
main[(.)entities[ii].Index],
|
|
|
|
ecs.GetComponentData(entities[ii], _Components[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
var cun = entities.Length-1;
|
|
|
|
for(int ii < cun)
|
|
|
|
{
|
|
|
|
this.Run(
|
|
|
|
ecs.GetComponentData(entities[ii], _Components[0]),
|
|
|
|
ecs.GetComponentData(entities[ii], _Components[1]),
|
|
|
|
ecs.GetComponentData(entities[ii], _Components[2]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MovementSystem : System
|
|
|
|
{
|
|
|
|
public override void RegisterSystem(ECS ecs)
|
|
|
|
{
|
|
|
|
RegisterComponent<Position>(ecs);
|
|
|
|
//RegisterComponent<waste>(ecs);
|
|
|
|
RegisterComponent<Velocity>(ecs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Run(void* pos, void* vel)
|
|
|
|
{
|
|
|
|
((Position*)pos).x += ((Velocity*)vel).x;
|
|
|
|
((Position*)pos).y += ((Velocity*)vel).y;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Run(void* pos, void* waste, void* vel)
|
|
|
|
{
|
|
|
|
((Position*)pos).x += ((Velocity*)vel).x;
|
|
|
|
((Position*)pos).y += ((Velocity*)vel).y;
|
|
|
|
|
|
|
|
}
|
2024-11-11 19:47:09 +01:00
|
|
|
}
|