Skip to content

Commit

Permalink
+ more threading support
Browse files Browse the repository at this point in the history
+ removed "Commands" class
+ Deferred(Action<World> fn) function
  • Loading branch information
andreakarasho committed Apr 1, 2024
1 parent 08e5669 commit c6c2ab4
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 334 deletions.
25 changes: 17 additions & 8 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
.RunIf(() => true)
.RunIf((Res<GameStates> state, Res<GameStates> state1, Res<GameStates> state2, Query<Velocity> velQuery) => state.Value == GameStates.InGame);

scheduler.AddSystem((Local<int> i32, Res<string> str, Commands commands) => {
scheduler.AddSystem((Local<int> i32, Res<string> str, SchedulerState schedState) => {
Console.WriteLine(i32.Value++);

commands.AddResource(23ul);
schedState.AddResource(23ul);
}, Stages.Startup);
scheduler.AddSystem((Res<ulong> ul) => {
Console.WriteLine(ul.Value++);
Expand Down Expand Up @@ -83,13 +83,20 @@ Res<string> myText
Console.WriteLine("What: {0}", myText.Value);
}
);
scheduler.AddSystem((Commands commands) => {
var ff = commands.Entity()
scheduler.AddSystem((World world) => {

world.BeginDeferred();
var ff = world.Entity()
.Set<Position>(default)
.Set<Velocity>(default);
});
scheduler.AddSystem((Commands commands) => {
commands.Merge();
world.EndDeferred();

world.Deferred(w => {
var ff = w.Entity()
.Set<Position>(default)
.Set<Velocity>(default)
.Set<Likes>();
});
});
scheduler.AddSystem((World world) => {
Console.WriteLine("entities in world {0}", world.EntityCount);
Expand Down Expand Up @@ -128,7 +135,7 @@ Res<string> myText
// ecs.Entity<MyEvent>();

ecs.Query<Position>()
.Each((EntityView ent) => {
.EachJob((EntityView ent, ref Position pos) => {

ent.Set(new MyEvent() { Value = 2 });

Expand All @@ -138,6 +145,8 @@ Res<string> myText
ref var v2 = ref ent.Get<MyEvent>();
v.Value += 1;

ent.Unset<MyEvent>();


// var ee = ent.World.Entity()
// .Set<MyEvent>(new MyEvent() { Value = 222 });
Expand Down
223 changes: 0 additions & 223 deletions src/Commands.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public QueryInternal GetEnumerator()

public void Each(QueryFilterDelegateWithEntity fn)
{
World.Lock();
World.BeginDeferred();

foreach (var arch in this)
{
Expand All @@ -187,6 +187,6 @@ public void Each(QueryFilterDelegateWithEntity fn)
}
}

World.Unlock();
World.EndDeferred();
}
}
67 changes: 27 additions & 40 deletions src/System.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Scheduler(World world)
_systems[i] = new ();

AddSystemParam(world);
AddSystemParam(new Commands(world, this));
AddSystemParam(new SchedulerState(this));
}


Expand Down Expand Up @@ -117,7 +117,10 @@ internal Scheduler AddSystemParam<T>(T param) where T : ISystemParam
return this;
}

internal bool ResourceExists<T>() => _resources.ContainsKey(typeof(T));
internal bool ResourceExists<T>() where T : ISystemParam
{
return _resources.ContainsKey(typeof(T));
}
}

public interface IPlugin
Expand Down Expand Up @@ -219,27 +222,6 @@ void ISystemParam.New(object arguments)
}
}

partial class Commands : ISystemParam
{
private readonly Scheduler? _scheduler;

public Commands() : this(null!) { }

public Commands(World world, Scheduler scheduler) : this(world)
=> _scheduler = scheduler;

void ISystemParam.New(object arguments)
{
World = (World) arguments;
}

public void AddResource<T>(T resource)
=> _scheduler?.AddResource(resource);

public bool ResourceExists<T>()
=> _scheduler?.ResourceExists<Res<T>>() ?? false;
}

public sealed class Res<T> : ISystemParam
{
private T? _t;
Expand Down Expand Up @@ -278,25 +260,30 @@ void ISystemParam.New(object arguments)
}
}

// public sealed class SystemState : ISystemParam
// {
// private readonly Scheduler _scheduler;
public sealed class SchedulerState : ISystemParam
{
private readonly Scheduler _scheduler;

// internal SystemState(Scheduler scheduler)
// {
// _scheduler = scheduler;
// }
internal SchedulerState(Scheduler scheduler)
{
_scheduler = scheduler;
}

// public SystemState()
// => throw new Exception("You are not allowed to initialixze this object by yourself!");
public SchedulerState()
=> throw new Exception("You are not allowed to initialixze this object by yourself!");

// public void AddResource<T>(T resource)
// {
// _scheduler.AddResource(resource);
// }
public void AddResource<T>(T resource)
{
_scheduler.AddResource(resource);
}

// void ISystemParam.New(object arguments)
// {
public bool ResourceExists<T>()
{
return _scheduler.ResourceExists<Res<T>>();
}

// }
// }
void ISystemParam.New(object arguments)
{

}
}
Loading

0 comments on commit c6c2ab4

Please sign in to comment.