Skip to content

Commit

Permalink
+ systems
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Mar 10, 2024
1 parent d3e32a9 commit fcd982f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,52 @@ ecs.Filter<Not<Disabled>>()
.Query((EntityView entity) => { });
```

Systems

```csharp
using var ecs = new World();

// Declare a system manager
var systems = new SystemManager(ecs);

// Bind your systems. You can bind one system per type!
var moveSystem = systems.Add<MoveSystem>("My optional name");

// Update all systems to this system manager
systems.Update();

// You can disable a system
moveSystem.Disable();

// ... and enable it
moveSystem.Enable();

// Delete
systems.Delete<MoveSystem>();

// Find a system to do some fancy stuff
var foundSystem = systems.Find<MoveSystem>();



sealed class MoveSystem : EcsSystem {
public override void OnCreate() {
Ecs.Entity()
.Set<Position>(new () { X = 0, Y = 0})
.Set<Velocity>(new () { X = 12.0f, Y = 1f });
}

public override void OnUpdate() {
var deltaTime = Time.Delta;
Ecs.Query((ref Position pos, ref Velocity vel) => {
pos.X += vel.X * deltaTime;
pos.Y += vel.Y * deltaTime;
});
}
}

```

# Credits

Base code idea inspired by:
Expand Down
32 changes: 17 additions & 15 deletions plugins/TinyEcs.Plugins/Systems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace TinyEcs;

public abstract class System
public abstract class EcsSystem
{
#nullable disable
[NotNull] public World Ecs { get; internal set; }
[NotNull] public string Name { get; internal set; }
#nullable restore

public bool IsEnabled { get; private set; } = true;
internal bool IsDestroyed { get; set; }
Expand Down Expand Up @@ -35,11 +37,11 @@ public virtual void OnDestroy() { }

private void Enable(bool enable)
{
var tmp = IsEnabled;
IsEnabled = enable;
if (tmp == enable)
if (IsEnabled == enable)
return;

IsEnabled = enable;

if (enable)
OnStart();
else
Expand All @@ -51,27 +53,27 @@ private void Enable(bool enable)
public sealed class SystemManager
{
private readonly World _ecs;
private readonly List<System> _systems, _toCreate, _toDelete;
private readonly Dictionary<Type, System> _hashes;
private readonly List<EcsSystem> _systems, _toCreate, _toDelete;
private readonly Dictionary<Type, EcsSystem> _hashes;

public SystemManager(World ecs)
{
_ecs = ecs;
_hashes = new Dictionary<Type, System>();
_systems = new List<System>();
_toCreate = new List<System>();
_toDelete = new List<System>();
_hashes = new Dictionary<Type, EcsSystem>();
_systems = new List<EcsSystem>();
_toCreate = new List<EcsSystem>();
_toDelete = new List<EcsSystem>();
}

public T Add<T>(string name = "") where T : System, new()
public T Add<T>(string name = "") where T : EcsSystem, new()
{
if (_hashes.TryGetValue(typeof(T), out var system))
return (T)system;

system = new T
{
Ecs = _ecs,
Name = string.IsNullOrWhiteSpace(name) ? typeof(T).Name : name
Name = string.IsNullOrWhiteSpace(name) ? typeof(T).ToString() : name
};

_hashes.Add(typeof(T), system);
Expand All @@ -81,7 +83,7 @@ public SystemManager(World ecs)
return (T)system;
}

public void Delete<T>() where T : System
public void Delete<T>() where T : EcsSystem
{
var found = Find<T>();
if (found == null || found.IsDestroyed) return;
Expand All @@ -93,7 +95,7 @@ public void Delete<T>() where T : System
}
}

public T? Find<T>() where T : System
public T? Find<T>() where T : EcsSystem
{
_ = _hashes.TryGetValue(typeof(T), out var system);
return system as T;
Expand Down Expand Up @@ -136,7 +138,6 @@ public void Update()
sys.OnUpdate();
sys.OnAfterUpdate();
}

}

public void Clear()
Expand All @@ -146,5 +147,6 @@ public void Clear()
_systems.ForEach(s => s.OnDestroy());
_systems.Clear();
_toDelete.Clear();
_hashes.Clear();
}
}
5 changes: 3 additions & 2 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

using var ecs = new World();
var systems = new SystemManager(ecs);
systems.Add<MoveSystem>();
var ff = systems.Add<MoveSystem>();

Console.WriteLine("system name : {0}", ff.Name);

var e = ecs.Entity("Main")
.Set<Position>(new Position() {X = 2})
Expand Down Expand Up @@ -191,7 +192,7 @@ struct Chunk;
struct ChunkTile;


sealed class MoveSystem : TinyEcs.System
sealed class MoveSystem : EcsSystem
{
public override void OnCreate()
{
Expand Down
14 changes: 7 additions & 7 deletions samples/TinyEcsGame/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct Rotation
}


sealed class MoveSystem : TinyEcs.System
sealed class MoveSystem : TinyEcs.EcsSystem
{
public override void OnUpdate()
{
Expand All @@ -78,7 +78,7 @@ public override void OnUpdate()
}
}

sealed class CheckBorderSystem : TinyEcs.System
sealed class CheckBorderSystem : TinyEcs.EcsSystem
{
public Vector2 WindowSize { get; set; }

Expand Down Expand Up @@ -111,7 +111,7 @@ public override void OnUpdate()
}
}

sealed class BeginRenderSystem : TinyEcs.System
sealed class BeginRenderSystem : TinyEcs.EcsSystem
{
public override void OnUpdate()
{
Expand All @@ -120,15 +120,15 @@ public override void OnUpdate()
}
}

sealed class EndRenderSystem : TinyEcs.System
sealed class EndRenderSystem : TinyEcs.EcsSystem
{
public override void OnUpdate()
{
Raylib.EndDrawing();
}
}

sealed class RenderEntities : TinyEcs.System
sealed class RenderEntities : TinyEcs.EcsSystem
{
public override void OnUpdate()
{
Expand All @@ -139,7 +139,7 @@ public override void OnUpdate()
}
}

sealed class RenderText : TinyEcs.System
sealed class RenderText : TinyEcs.EcsSystem
{
public override void OnUpdate()
{
Expand All @@ -157,7 +157,7 @@ public override void OnUpdate()
}
}

sealed class SpawnEntities : TinyEcs.System
sealed class SpawnEntities : TinyEcs.EcsSystem
{
public int EntitiesToSpawn { get; set; }
public Vector2 WindowSize { get; set; }
Expand Down

0 comments on commit fcd982f

Please sign in to comment.