mixedcards/Assets/Battle/BuffManager.cs
2020-10-10 21:49:04 -04:00

70 lines
No EOL
2.2 KiB
C#

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<BattleParticipant, List<Buff>> mBuffs = new Dictionary<BattleParticipant, List<Buff>>();
public BuffManager(BattleClock clock) {
clock.OnTimeChanged += NotifyBattleTime;
}
public void AddBuff(BuffParticipantEffect effect, BattleTime timeNow) {
List<Buff> 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<Buff> GetAllBuffsFor(BattleParticipant participant) {
if (mBuffs.ContainsKey(participant)) {
return mBuffs[participant];
} else {
return new List<Buff>();
}
}
private void NotifyBattleTime(BattleTime timeNow) {
foreach (KeyValuePair<BattleParticipant, List<Buff>> p in mBuffs) {
BattleParticipant participant = p.Key;
List<Buff> 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();
}
}