diff --git a/Assets/Battle/BattleController.cs b/Assets/Battle/BattleController.cs index de01d26..22d4405 100644 --- a/Assets/Battle/BattleController.cs +++ b/Assets/Battle/BattleController.cs @@ -51,7 +51,9 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe this.heroCardDatabase = new HeroCardDatabase(); this.monsterCardDatabase = new MonsterCardDatabase(); - this.state = new BattleState(); + this.clock = new BattleClock(); + this.buffManager = new BuffManager(this.clock); + this.state = new BattleState(this.buffManager); this.heroEnergy = new EnergyPool(); this.heroEnergyUI.Init(this.heroEnergy); @@ -80,7 +82,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe this.monsters[i] = monster; } - this.battleSprites.Init(this.heroes, this.monsters); + this.battleSprites.Init(this.heroes, this.monsters, this.state); BattleParticipant[] cardOwners = new BattleParticipant[parameters.HCardIds.Length]; for (int i = 0; i < parameters.HCardIds.Length; i++) { @@ -96,8 +98,6 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe this.monsterDeck = new BattleDeck( this.monsterCardDatabase, parameters.MCardIds.Cast().ToArray(), mCardOwners); - this.clock = new BattleClock(); - this.buffManager = new BuffManager(this.clock); this.participantActivity = new ParticipantActivity(); this.playArea = new PlayArea(); diff --git a/Assets/Battle/BattleParticipant.cs b/Assets/Battle/BattleParticipant.cs index 38d5750..3773b12 100644 --- a/Assets/Battle/BattleParticipant.cs +++ b/Assets/Battle/BattleParticipant.cs @@ -9,6 +9,7 @@ public class BattleParticipant public event BattleParticipantUpdateAction OnHit; public event BattleParticipantUpdateAction OnDeath; public event BattleParticipantUpdateAction OnStatusChanged; + public event BattleParticipantUpdateAction OnBuffsChanged; protected TeamChoice team; protected string displayName; @@ -185,4 +186,10 @@ public class BattleParticipant this.OnStatusChanged(this); } } + + public void TriggerOnBuffsChanged() { + if (this.OnBuffsChanged != null) { + this.OnBuffsChanged(this); + } + } } diff --git a/Assets/Battle/BattleSprites.cs b/Assets/Battle/BattleSprites.cs index 1d7acfe..8d59798 100644 --- a/Assets/Battle/BattleSprites.cs +++ b/Assets/Battle/BattleSprites.cs @@ -16,7 +16,7 @@ public class BattleSprites: MonoBehaviour private Dictionary mMonsterSprites; private Dictionary mMonsterUIs; - public void Init(BattleHero[] heroes, BattleMonster[] monsters) { + public void Init(BattleHero[] heroes, BattleMonster[] monsters, BattleState battleState) { mHeroSprites = new Dictionary(); mHeroUIs = new Dictionary(); mMonsterSprites = new Dictionary(); @@ -35,7 +35,7 @@ public class BattleSprites: MonoBehaviour hero.OnDeath += bps.NotifyDeath; HeroInfoUI heroUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI)); - heroUI.Init(sprite, hero); + heroUI.Init(sprite, hero, battleState); mHeroUIs[hero] = heroUI; } @@ -52,7 +52,7 @@ public class BattleSprites: MonoBehaviour monster.OnDeath += bps.NotifyDeath; HeroInfoUI monsterUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI)); - monsterUI.Init(sprite, monster); + monsterUI.Init(sprite, monster, battleState); mMonsterUIs[monster] = monsterUI; } diff --git a/Assets/Battle/BattleState.cs b/Assets/Battle/BattleState.cs index c221879..3c62ee0 100644 --- a/Assets/Battle/BattleState.cs +++ b/Assets/Battle/BattleState.cs @@ -8,6 +8,11 @@ public class BattleState private TeamChoice mTeamsTurn = TeamChoice.Monsters; private bool mPlayedEmotion; private int mRound = 0; + private BuffManager buffManager; + + public BattleState(BuffManager buffManager) { + this.buffManager = buffManager; + } public bool IsTeamsTurn(TeamChoice team) { return team == mTeamsTurn; @@ -38,4 +43,6 @@ public class BattleState mRound++; } } + + public BuffManager BuffManager {get => this.buffManager;} } diff --git a/Assets/Battle/BuffManager.cs b/Assets/Battle/BuffManager.cs index 5fb9af4..0f7165d 100644 --- a/Assets/Battle/BuffManager.cs +++ b/Assets/Battle/BuffManager.cs @@ -29,6 +29,7 @@ public class BuffManager { 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) { @@ -50,8 +51,13 @@ public class BuffManager { } private void NotifyBattleTime(BattleTime timeNow) { - foreach (List buffs in mBuffs.Values) { - buffs.RemoveAll(buff => timeNow.Int >= buff.End.Int); + 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(); + } } } diff --git a/Assets/Battle/UI/HeroInfoUI.cs b/Assets/Battle/UI/HeroInfoUI.cs index f307610..0f2e657 100644 --- a/Assets/Battle/UI/HeroInfoUI.cs +++ b/Assets/Battle/UI/HeroInfoUI.cs @@ -7,6 +7,8 @@ public class HeroInfoUI : MonoBehaviour { protected GameObject target; protected BattleParticipant battleParticipant; + protected BattleState battleState; + public Text nameText; public Image healthBarBgImage; public Image healthBarFgImage; @@ -24,11 +26,13 @@ public class HeroInfoUI : MonoBehaviour private static Color MED_HP_COLOR = new Color(0.8f, 0.8f, 0f); private static Color LOW_HP_COLOR = new Color(0.8f, 0f, 0f); - public void Init(GameObject sprite, BattleParticipant battleParticipant) { + public void Init( + GameObject sprite, BattleParticipant battleParticipant, BattleState battleState) { this.target = sprite; this.battleParticipant = battleParticipant; + this.battleState = battleState; - this.statusUI.Init(battleParticipant); + this.statusUI.Init(battleParticipant, battleState); } // Start is called before the first frame update diff --git a/Assets/Battle/UI/StatusIconUI.cs b/Assets/Battle/UI/StatusIconUI.cs index c62d045..4d5b295 100644 --- a/Assets/Battle/UI/StatusIconUI.cs +++ b/Assets/Battle/UI/StatusIconUI.cs @@ -11,16 +11,24 @@ public class StatusIconUI : MonoBehaviour private BattleStatus status; - public void Init(BattleStatus status) { - this.status = status; + public void InitStatus(BattleStatus status) { Sprite sprite = Resources.Load(GetStatusResourcePath(status)); this.statusImage.sprite = sprite; } + public void InitBuff(BuffType buffType) { + Sprite sprite = Resources.Load(GetBuffResourcePath(buffType)); + this.statusImage.sprite = sprite; + } + private static string GetStatusResourcePath(BattleStatus status) { return "BattleIcons/StatusIcon" + status; } + private static string GetBuffResourcePath(BuffType buffType) { + return "BattleIcons/BuffIcon" + buffType; + } + public void SetAmount(int amount, int index) { this.turnsText.text = "" + amount; this.transform.localPosition = new Vector2(-index * 22, 0); diff --git a/Assets/Battle/UI/StatusUI.cs b/Assets/Battle/UI/StatusUI.cs index e46309d..638ce28 100644 --- a/Assets/Battle/UI/StatusUI.cs +++ b/Assets/Battle/UI/StatusUI.cs @@ -10,14 +10,20 @@ public class StatusUI : MonoBehaviour public StatusIconUI iconPrefab; private BattleParticipant battleParticipant; + private BattleState battleState; - private Dictionary icons; + private Dictionary statusIcons; - public void Init(BattleParticipant battleParticipant) { + private Dictionary buffIcons; + + public void Init(BattleParticipant battleParticipant, BattleState battleState) { this.battleParticipant = battleParticipant; + this.battleState = battleState; - this.icons = new Dictionary(); - this.battleParticipant.OnStatusChanged += this.NotifyBattleStatusChanged; + this.statusIcons = new Dictionary(); + this.buffIcons = new Dictionary(); + this.battleParticipant.OnStatusChanged += this.NotifyBattleStatusOrBuffsChanged; + this.battleParticipant.OnBuffsChanged += this.NotifyBattleStatusOrBuffsChanged; } // Start is called before the first frame update @@ -25,10 +31,10 @@ public class StatusUI : MonoBehaviour UpdateAppearance(); } - public void NotifyBattleStatusChanged(BattleParticipant updatedParticipant) { + public void NotifyBattleStatusOrBuffsChanged(BattleParticipant updatedParticipant) { if (updatedParticipant != this.battleParticipant) { throw new System.InvalidOperationException( - "NotifyBattleStatusChanged was notified about a different participant."); + "NotifyBattleStatusOrBuffsChanged was notified about a different participant."); } UpdateAppearance(); } @@ -41,21 +47,44 @@ public class StatusUI : MonoBehaviour } StatusIconUI iconUI = null; - this.icons.TryGetValue(status, out iconUI); + this.statusIcons.TryGetValue(status, out iconUI); if (this.battleParticipant.HasStatus(status)) { if (iconUI == null) { iconUI = Instantiate(this.iconPrefab.gameObject, this.transform).GetComponent(); - iconUI.Init(status); - this.icons[status] = iconUI; + iconUI.InitStatus(status); + this.statusIcons[status] = iconUI; } - iconUI.SetAmount(this.battleParticipant.GetStatusAmount(status), index); - index++; + iconUI.SetAmount(this.battleParticipant.GetStatusAmount(status), index++); } else { if (iconUI != null) { Destroy(iconUI.gameObject); - this.icons.Remove(status); + this.statusIcons.Remove(status); } } } + + Dictionary previousBuffIcons = new Dictionary(this.buffIcons); + List currentBuffs = this.battleState.BuffManager.GetAllBuffsFor(this.battleParticipant); + + foreach (Buff buff in currentBuffs) { + StatusIconUI iconUI = null; + previousBuffIcons.TryGetValue(buff.Type, out iconUI); + if (iconUI == null) { + iconUI = Instantiate(this.iconPrefab.gameObject, this.transform).GetComponent(); + iconUI.InitBuff(buff.Type); + this.buffIcons[buff.Type] = iconUI; + } else { + previousBuffIcons.Remove(buff.Type); + } + iconUI.SetAmount(buff.Amount, index++); + } + + // If a buff is left in previousBuffIcons, it has been removed. + foreach (KeyValuePair p in previousBuffIcons) { + BuffType buffType = p.Key; + StatusIconUI iconUI = p.Value; + Destroy(iconUI.gameObject); + this.buffIcons.Remove(buffType); + } } }