- Notifications
You must be signed in to change notification settings - Fork2
🧁KECS is a fast and easy C# Entity Component System framework for writing your own games.
License
MIT, Unknown licenses found
Licenses found
ludaludaed/KECS
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
KECS is a fast and easy C# Entity Component System framework for writing your own games.
Important! KECS is still in development, so its use in projects is only at your own risk.
World is a container for all entities and components. The world is created withWorlds.Create()
. The world can be setup with its initial settings using theWorldConfig
structure.
varworld=Worlds.Create(worldName,newWorldConfig(){ ...});
The world can be retrieved using its name or idWorlds.Get()
varworld=Worlds.Get(worldName);
The world can be destroyed using theDestroy ()
method of theWorld
class.
world.Destroy();
A component is a container only for users data. In KECS, component is only a value type.
publicstructMoveComponent{publicVector2Direction;publicfloatSpeed;}
An entity is a container for components. The entity has methods for adding, removing, and getting components.
Entityentity=_world.CreateEntity();refvarsettedSpeedComponent=refentity.Set(newSpeedComponent());refvargottenSpeedComponent=refentity.Get<SpeedComponent>();entity.Remove<SpeedComponent>();boolhas=entity.Has<SpeedComponent>();entity.Destroy();
Important! An entity without components will be automatically deleted.
EntityBuilder allows you to create entities according to a template you define.
varbuilder=newEntityBuilder();builder.Append(newFooComponent()).Append(newBarComponent()).Append(newBazComponent());varentity=builder.Build(World);
TheAppend()
method allows you to add a component to the entity template.TheBuild(world)
method allows you to create an entity from this template in the world.
NOTE:This way of creating an entity allows you to reduce the number of side archetypes at the initial sequential assignment of entity components.
The system processes entities matching the filter.The system must implement the abstract classSystemBase
orUpdateSystem
.
publicclassSystemTest1:UpdateSystem{publicoverridevoidInitialize(){// Will be called when the system is initialized.}publicoverridevoidOnUpdate(floatdeltaTime){_world.CreateQuery().ForEach((Entityentity,refComponentcomp)=>{comp.Counter++;});}publicoverridevoidOnDestroy(){// Will be called when the system is destroyed.}publicoverridevoidPostDestroy(){//Will be called after the system is destroyed.}}
Emitting an event for an entity is set by theentity.SetEvent<>()
method.
publicstructEventComponent{ ...}...entity.SetEvent<EventComponent>();
Receiving an event.
publicclassSystemTest1:UpdateSystem{publicoverridevoidOnUpdate(floatdeltaTime){_world.CreateQuery().ForEach((Entityentity,refEventComponentevent)=>{ ...});}}
Important! The event hangs on the entity on exactly one frame, so the event appears only on the next frame and deleted after it.
To create a query to the world you need to call theCreateQuery()
method.In order to include or exclude a set of components from iteration. You can use a method chain for the request, consisting ofWith<>()
orWithout<>()
.In order to iterate over this set, you need to call theForeach()
method from the request. Components specified indelegate arguments will automatically be considered as included in the selection.
publicclassSystemTest1:UpdateSystem{publicoverridevoidOnUpdate(floatdeltaTime){_world.CreateQuery().With<BarComponent>().Without<BazComponent>().ForEach((Entityentity,refFooComponentfooComp)=>{ ...});}}
Adding shared data to systems is done by calling theAddShared ()
method of theSystems
class.
publicclassSharedData{//Some fields}...var world=Worlds.Create();varsystems=newSystems(world);systems.Add(newSystemTest()).Add(newSystemTest1()).AddShared(newSharedData());systems.Initialize();
Shared data for the system is obtained by calling theGetShared<>()
method.
publicclassSystemTest1:UpdateSystem{publicoverridevoidOnUpdate(floatdeltaTime){varsharedData=_systems.GetShared<SharedData>();_world.CreateQuery().ForEach((Entityentity,refFooComponentfooComp)=>{ ...});}}
Systems are added using theAdd<>()
method of theSystems
class.After adding all systems, you must call theIntitalize()
method.
publicclassStartUp:MonoBehaviour{publicWorld_world;publicSystems_systems;publicvoidAwake(){_world=Worlds.Create();_systems=newSystems(_world);_systems.Add(newSystemTest()).Add(newSystemTest1())._systems.Initialize();}publicvoidUpdate(){_world.ExecuteTasks();_systems.OnUpdate(Time.deltaTime);}publicvoidOnDestroy(){_systems.OnDestroy();_world.Destroy();}}
Important! After the systems are initialized, you cannot add new systems.
Telegram:ludaludaed
About
🧁KECS is a fast and easy C# Entity Component System framework for writing your own games.