Skip to content

Instantly share code, notes, and snippets.

@FNGgames
Last active October 20, 2021 10:05
Show Gist options
  • Save FNGgames/4d58185689a3e8b99039ec8e1c94767b to your computer and use it in GitHub Desktop.
Save FNGgames/4d58185689a3e8b99039ec8e1c94767b to your computer and use it in GitHub Desktop.
Entitas Timer System
using Entitas;
using UnityEngine;
public class TimerSystem : IExecuteSystem, ICleanupSystem
{
readonly IGroup<GameEntity> _timers;
readonly IGroup<GameEntity> _timerCompletes;
readonly GameContext _context;
public TimerSystem(Contexts contexts)
{
_context = contexts.game;
_timers = _context.GetGroup(GameMatcher.Timer);
_timerCompletes = _context.GetGroup(GameMatcher.TimerComplete);
}
public void Execute()
{
foreach (var e in _timers.GetEntities())
{
if (e.timer.time > 0)
{
e.ReplaceTimer(e.timer.time - Time.deltaTime);
}
else
{
e.isTimerComplete = true;
}
}
}
public void Cleanup()
{
foreach (var e in _timerCompletes.GetEntities())
{
e.RemoveTimer();
e.isTimerComplete = false;
}
}
}
public class TimerComponent : IComponent
{
public float time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment