Last active
October 20, 2021 10:05
-
-
Save FNGgames/4d58185689a3e8b99039ec8e1c94767b to your computer and use it in GitHub Desktop.
Entitas Timer System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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