using System.Collections.Generic; using System.Linq; public class Buff { private BuffType mType; private int mAmount; private BattleTime mEnd; public Buff(BuffType type, int amount, BattleTime end) { mType = type; mAmount = amount; mEnd = end; } public BuffType Type {get => mType;} public int Amount {get => mAmount;} public BattleTime End {get => mEnd;} } public class BuffManager { private Dictionary> mBuffs = new Dictionary>(); public BuffManager(BattleClock clock) { clock.OnTimeChanged += NotifyBattleTime; } public void AddBuff(BuffParticipantEffect effect, BattleTime timeNow) { List current = GetAllBuffsFor(effect.Participant); current.Add(new Buff(effect.Type, effect.Amount, CalculateEnd(timeNow, effect.Duration))); mBuffs[effect.Participant] = current; effect.Participant.TriggerOnBuffsChanged(); } public int GetTotalBuffAmountOfType(BattleParticipant participant, BuffType type) { int total = 0; foreach (Buff buff in GetAllBuffsFor(participant)) { if (buff.Type == type) { total += buff.Amount; } } return total; } public List GetAllBuffsFor(BattleParticipant participant) { if (mBuffs.ContainsKey(participant)) { return mBuffs[participant]; } else { return new List(); } } private void NotifyBattleTime(BattleTime timeNow) { foreach (KeyValuePair> p in mBuffs) { BattleParticipant participant = p.Key; List buffs = p.Value; int buffsRemoved = buffs.RemoveAll(buff => timeNow.Int >= buff.End.Int); if (buffsRemoved > 0) { participant.TriggerOnBuffsChanged(); } } } private static BattleTime CalculateEnd(BattleTime timeNow, BuffDuration duration) { if (duration == BuffDuration.UntilEndOfNextHeroTurn) { return new BattleTime(timeNow.RoundNumber + 1, RoundStep.EndOfHeroTurn); } throw new System.Exception(); } }