https://github.com/judelco/coreecs
Deterministic lightweight ECS framework
https://github.com/judelco/coreecs
csharp design-patterns ecs entity entity-component entity-component-system entity-framework game game-development game-engine gamedev performance unity unity3d
Last synced: 10 months ago
JSON representation
Deterministic lightweight ECS framework
- Host: GitHub
- URL: https://github.com/judelco/coreecs
- Owner: JuDelCo
- License: mit
- Created: 2019-09-25T20:10:56.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-26T19:59:58.000Z (12 months ago)
- Last Synced: 2025-04-29T00:14:18.960Z (11 months ago)
- Topics: csharp, design-patterns, ecs, entity, entity-component, entity-component-system, entity-framework, game, game-development, game-engine, gamedev, performance, unity, unity3d
- Language: C#
- Homepage:
- Size: 79.1 KB
- Stars: 18
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
JuCore ECS
=====================
JuCore ECS is a deterministic lightweight ECS framework.
It's heavily inspired in Entitas, but with some differences:
- It's deterministic (does not rely on hashtables)
- Does not use code generators (no reflection, less problems)
- Easier to start using it (more straightforward, see documentation below)
- No GC collections (when reusing destroyed entities)
- Similar perfomance (10~15% less, but handles well all requirements for a game)
Any feedback is welcome !
See also
=====================
- [JuCore](https://github.com/JuDelCo/Core) - Core package base (service locator and useful services)
- [JuCore Math](https://github.com/JuDelCo/CoreMath) - Linear algebra math library, also 2D/3D physics, noise functions and IK
Install
=====================
- For **Unity**, update the dependencies in the ```/Packages/manifest.json``` file in your project folder by adding:
```json
"com.judelco.core.ecs": "https://github.com/JuDelCo/CoreECS.git#v1.14.0",
```
- For native **.NET projects**, **Godot**, etc... run the following command in the console of your .NET project to add the package:
```bash
dotnet add package JuDelCo.Lib.CoreECS
```
Documentation
=====================
Note: All components should be structs for best performance and memory efficiency.
#### Defining components
```csharp
public struct Speed : IComponent
{
public float value;
public Speed(float value) // Optional, syntactic sugar for later
{
this.value = value;
}
}
```
#### Create a Context
```csharp
// IMPORTANT: You need to pass the count of component types you will use to the context
const int COMPONENT_TYPES_COUNT = 9;
var context = new Context(Constants.COMPONENT_TYPES_COUNT);
```
#### Creating entities
```csharp
var entity = context.CreateEntity();
entity.Add(new Speed(2f));
```
#### Entity management
```csharp
entity.Add(new Position(3, 7));
entity.Replace(new Health(10));
entity.Remove();
var hasSpeed = entity.Has();
var healthData = entity.Get();
// You can also chain methods!
context.CreateEntity()
.Add(new Speed(2f))
.Add(new Position(3, 7))
.Replace(new Health(10)) // Note: Replace will add the component if doesn't have it yet
.Remove();
```
#### Group management
```csharp
// Returns a group containing always all entities with Position and Speed components.
var group = context.GetGroup(MatcherGenerator.AllOf());
var entities = group.GetEntities();
foreach (var e in entities)
{
// ...
}
```
#### Execute systems
```csharp
// You can also use IInitializeSystem, ICleanupSystem and ITearDownSystem systems
public class MovementSystem : IExecuteSystem
{
private IGroup group;
public MovementSystem(IContext context)
{
// You can also use a shorthand for AllOf components!
// This is equivalent to: GetGroup(MatcherGenerator.AllOf())
group = context.GetGroup();
}
public void Execute()
{
foreach (var e in group.GetEntities())
{
var position = e.Get();
var speed = e.Get().value;
position.x += speed;
e.Replace(position);
}
}
}
```
#### Reactive systems
```csharp
public class RenderPositionSystem : ReactiveSystem
{
private IContext context;
private IGroup group;
public RenderPositionSystem(IContext context) : base(context)
{
this.context = context;
}
protected override ICollector GetTrigger(IContext context)
{
return context.GetGroup().CreateCollector(GroupEvent.Added);
}
protected override bool Filter(IEntity entity)
{
// Yes, you can use multiple types in Has method !
return entity.Has();
}
protected override void Execute(List entities)
{
foreach (var e in entities)
{
var position = e.Get();
e.Get().gameObject.transform.position = new Vector3(position.x, position.y, position.z);
}
}
}
```
The MIT License (MIT)
=====================
Copyright © 2019-2025 Juan Delgado (@JuDelCo)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.