Skip to content

Instantly share code, notes, and snippets.

@xjjon
Created January 24, 2023 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjjon/33def9a56c3a319fd22f685db906a284 to your computer and use it in GitHub Desktop.
Save xjjon/33def9a56c3a319fd22f685db906a284 to your computer and use it in GitHub Desktop.
public class BuffableEntity: MonoBehaviour
{
private readonly Dictionary<ScriptableBuff, TimedBuff> _buffs = new Dictionary<ScriptableBuff, TimedBuff>();
void Update()
{
//OPTIONAL, return before updating each buff if game is paused
//if (Game.isPaused)
// return;
foreach (var buff in _buffs.Values.ToList())
{
buff.Tick(Time.deltaTime);
if (buff.IsFinished)
{
_buffs.Remove(buff.Buff);
}
}
}
public void AddBuff(TimedBuff buff)
{
if (_buffs.ContainsKey(buff.Buff))
{
_buffs[buff.Buff].Activate();
}
else
{
_buffs.Add(buff.Buff, buff);
buff.Activate();
}
}
public void RemoveBuff(ScriptableBuff buff) {
if (_buffs.ContainsKey(buff)) {
_buffs.Remove(buff);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment