Entitas-csharp: What should I write in GetTrigger() or Filter(), to trigger ReactiveSystem by time?

Created on 3 Jul 2018  路  4Comments  路  Source: sschmid/Entitas-CSharp

I want a order system.
The entities with order component, will do their order lists.
The order list is about time.
eg.
time=0 set entity.orderComponent.order_id = "Move00"
time=0.3 set entity.orderComponent.order_id = "Move01"
time=2 set entity.orderComponent.order_id = "Move02"

But I don't know how to trigger ReactiveSystem by time. Need help:D.

Most helpful comment

Introduce a TickComponent that is getting increased by every execution cycle. Now you can react to that component. You also can save the delta time by an DeltaTimeComponent on the same entity. Now you can also react to tick and save the delta time. In your feature you can react to tick or delta time.Depending on what you need.

All 4 comments

For interval-based behavior I'm using these:
Components:
```Components.cs
public sealed class RemainingCooldown : IComponent { public float value; }

// CooldownTime is used to refresh interval-based cooldown. if !entity.hasCooldownTime, it will be an one-off cooldown.
public sealed class CooldownTime : IComponent { public float value; }

```CooldownSystem.cs
public class CooldownSystem : IExecuteSystem
{
  IGroup<GameEntity> _timers;

  public CooldownSystem(Contexts contexts)
  {
    _timers = contexts.game.getGroup(GameMatcher.RemainingCooldown);
  }

  public void Execute()
  {
    float deltaTime = _contexts.game.time.deltaTime;

    foreach (var timer in _timers)
    {
      if (timer.remainingCooldown.value > 0)
      {
        spawner.ReplaceRemainingCooldown(
          spawner.remainingCooldown.timer - deltaTime
        );
      }
      else if(timer.hasCooldownTime) // refresh cooldown if needed
      {
        timer.ReplaceRemainingCooldown(
          timer.remainingCooldown.timer + spawner.cooldownTime.value - deltaTime
        );
      }
    }
  }
}

Usage: add RemainingCooldown to an entity and react to that, filtering only entities that has remainingCooldown.value <= 0 (off cooldown)
(this file is not complete)
UpdateSomethingSystem.cs public class UpdateSomethingSystem : ReactiveSystem<GameEntity> { protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context) { return context.CreateCollector(GameMatcher.AllOf(GameMatcher.UsefulComponent, GameMatcher.RemainingCooldown)); } protected override bool Filter(GameEntity entity) { return entity.hasUsefulComponent && entity.hasRemainingCooldown && entity.remainingCooldown.value <= 0; } protected override void Execute(List<GameEntity> entities) { foreach(var entityThatIsOffCooldown in entities) { // do something here } } }

Introduce a TickComponent that is getting increased by every execution cycle. Now you can react to that component. You also can save the delta time by an DeltaTimeComponent on the same entity. Now you can also react to tick and save the delta time. In your feature you can react to tick or delta time.Depending on what you need.

Before this, I thought IExecuteSystem is read-only, then I can't reset my component value in it. And now I know Replace[ComponentName]. It works!
Thank you~~

Just I need

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CCludts picture CCludts  路  3Comments

CuriousHub picture CuriousHub  路  5Comments

yanjingzhaisun picture yanjingzhaisun  路  4Comments

angelotadres picture angelotadres  路  5Comments

sschmid picture sschmid  路  4Comments