Skip to content

Commit

Permalink
commands.AddResource
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Mar 29, 2024
1 parent 06935c8 commit d06da8b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
13 changes: 9 additions & 4 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@
.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) => {
scheduler.AddSystem((Local<int> i32, Res<string> str, Commands commands) => {
Console.WriteLine(i32.Value++);
});

commands.AddResource(23ul);
}, Stages.Startup);
scheduler.AddSystem((Res<ulong> ul) => {
Console.WriteLine(ul.Value++);
});
scheduler.AddState<GameStates>();
scheduler.AddSystem(() => Console.WriteLine("playing the game"))
.RunIf((Res<GameStates> state) => state.Value == GameStates.InGame);
.RunIf((Res<GameStates> state) => state == GameStates.InGame);

scheduler.AddSystem(() => Console.WriteLine("game paused"))
.RunIf((Res<GameStates> state) => state.Value == GameStates.Paused)
.RunIf((Query<Position, (With<PlayerTag>, Without<ComponentInfo>)> query) => query.Count() > 0 && query.Single<Position>().X > 0);
.RunIf((Query<Position, (With<PlayerTag>, Without<ComponentInfo>)> query)
=> query.Count() > 0 && query.Single<Position>().X > 0);

scheduler.AddSystem((Res<GameStates> state) =>
state.Value = state.Value switch
Expand Down
34 changes: 34 additions & 0 deletions src/System.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public Scheduler(World world)
_systems[i] = new ();

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


Expand Down Expand Up @@ -218,12 +219,22 @@ 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 sealed class Res<T> : ISystemParam
Expand Down Expand Up @@ -263,3 +274,26 @@ void ISystemParam.New(object arguments)
//throw new Exception("Resources must be initialized using 'scheduler.AddResource<T>' api");
}
}

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

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

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

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

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

// }
// }

0 comments on commit d06da8b

Please sign in to comment.