Skip to content

Commit

Permalink
systems
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Mar 9, 2024
1 parent be16e2b commit aeed5bd
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 103 deletions.
150 changes: 150 additions & 0 deletions plugins/TinyEcs.Plugins/Systems.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System.Diagnostics.CodeAnalysis;

namespace TinyEcs;

public abstract class System
{
[NotNull] public World Ecs { get; internal set; }

Check warning on line 7 in plugins/TinyEcs.Plugins/Systems.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Ecs' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in plugins/TinyEcs.Plugins/Systems.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Ecs' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
[NotNull] public string Name { get; internal set; }

Check warning on line 8 in plugins/TinyEcs.Plugins/Systems.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public bool IsEnabled { get; private set; } = true;
internal bool IsDestroyed { get; set; }


public virtual void OnCreate() { }

public virtual void OnStart() { }

public virtual void OnStop() { }

public virtual void OnBeforeUpdate() { }

public virtual void OnUpdate() { }

public virtual void OnAfterUpdate() { }

public virtual void OnCleanup() { }

public virtual void OnDestroy() { }


public void Enable() => Enable(true);

public void Disable() => Enable(false);


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

if (enable)
OnStart();
else
OnStop();
}
}


public sealed class SystemManager
{
private readonly World _ecs;
private readonly List<System> _systems, _toCreate, _toDelete;
private readonly Dictionary<Type, System> _hashes;

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

public T Add<T>(string name = "") where T : System, 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
};

_hashes.Add(typeof(T), system);
_systems.Add(system);
_toCreate.Add(system);

return (T)system;
}

public void Delete<T>() where T : System
{
var found = Find<T>();
if (found == null || found.IsDestroyed) return;

if (_hashes.Remove(typeof(T)))
{
found.IsDestroyed = true;
_toDelete.Add(found);
}
}

public T? Find<T>() where T : System
{
_ = _hashes.TryGetValue(typeof(T), out var system);
return system as T;
}

public void Update()
{
foreach (var sys in _toDelete)
sys.Disable();

foreach (var sys in _toDelete)
sys.OnCleanup();

foreach (var sys in _toDelete)
sys.OnDestroy();

foreach (var sys in _toDelete)
_systems.Remove(sys);

if (_toDelete.Count > 0)
_toDelete.Clear();

if (_toCreate.Count > 0)
{
foreach (var sys in _toCreate)
{
sys.OnCreate();
if (sys.IsEnabled)
sys.OnStart();
}

_toCreate.Clear();
}

foreach (var sys in _systems)
{
if (!sys.IsEnabled) continue;

sys.OnBeforeUpdate();
sys.OnUpdate();
sys.OnAfterUpdate();
}

}

public void Clear()
{
_systems.ForEach(s => s.Disable());
_systems.ForEach(s => s.OnCleanup());
_systems.ForEach(s => s.OnDestroy());
_systems.Clear();
_toDelete.Clear();
}
}
25 changes: 25 additions & 0 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
const int ENTITIES_COUNT = (524_288 * 2 * 1);

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


var e = ecs.Entity("Main")
.Set<Position>(new Position() {X = 2})
.Set<Velocity>(new Velocity());

systems.Update();

var rabbit = ecs.Entity();
var eats = ecs.Entity();
Expand Down Expand Up @@ -185,3 +189,24 @@ struct Context2 {}

struct Chunk;
struct ChunkTile;


sealed class MoveSystem : TinyEcs.System
{
public override void OnCreate()
{
Console.WriteLine("system {0} created", Name);
}

public override void OnStart()
{
Console.WriteLine("system {0} started", Name);
}

public override void OnUpdate()
{
Ecs.Query((ref Position pos, ref Velocity vel) => {
Console.WriteLine("aa");
});
}
}
Loading

0 comments on commit aeed5bd

Please sign in to comment.