Display power buffs

This commit is contained in:
henriquenakashima 2020-10-10 21:49:04 -04:00
parent 515602b670
commit 034e6beca6
8 changed files with 86 additions and 25 deletions

View file

@ -51,7 +51,9 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.heroCardDatabase = new HeroCardDatabase(); this.heroCardDatabase = new HeroCardDatabase();
this.monsterCardDatabase = new MonsterCardDatabase(); 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.heroEnergy = new EnergyPool();
this.heroEnergyUI.Init(this.heroEnergy); this.heroEnergyUI.Init(this.heroEnergy);
@ -80,7 +82,7 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.monsters[i] = monster; 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]; BattleParticipant[] cardOwners = new BattleParticipant[parameters.HCardIds.Length];
for (int i = 0; i < parameters.HCardIds.Length; i++) { for (int i = 0; i < parameters.HCardIds.Length; i++) {
@ -96,8 +98,6 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
this.monsterDeck = new BattleDeck<MonsterCard>( this.monsterDeck = new BattleDeck<MonsterCard>(
this.monsterCardDatabase, parameters.MCardIds.Cast<int>().ToArray(), mCardOwners); this.monsterCardDatabase, parameters.MCardIds.Cast<int>().ToArray(), mCardOwners);
this.clock = new BattleClock();
this.buffManager = new BuffManager(this.clock);
this.participantActivity = new ParticipantActivity(); this.participantActivity = new ParticipantActivity();
this.playArea = new PlayArea(); this.playArea = new PlayArea();

View file

@ -9,6 +9,7 @@ public class BattleParticipant
public event BattleParticipantUpdateAction OnHit; public event BattleParticipantUpdateAction OnHit;
public event BattleParticipantUpdateAction OnDeath; public event BattleParticipantUpdateAction OnDeath;
public event BattleParticipantUpdateAction OnStatusChanged; public event BattleParticipantUpdateAction OnStatusChanged;
public event BattleParticipantUpdateAction OnBuffsChanged;
protected TeamChoice team; protected TeamChoice team;
protected string displayName; protected string displayName;
@ -185,4 +186,10 @@ public class BattleParticipant
this.OnStatusChanged(this); this.OnStatusChanged(this);
} }
} }
public void TriggerOnBuffsChanged() {
if (this.OnBuffsChanged != null) {
this.OnBuffsChanged(this);
}
}
} }

View file

@ -16,7 +16,7 @@ public class BattleSprites: MonoBehaviour
private Dictionary<BattleMonster, GameObject> mMonsterSprites; private Dictionary<BattleMonster, GameObject> mMonsterSprites;
private Dictionary<BattleMonster, HeroInfoUI> mMonsterUIs; private Dictionary<BattleMonster, HeroInfoUI> mMonsterUIs;
public void Init(BattleHero[] heroes, BattleMonster[] monsters) { public void Init(BattleHero[] heroes, BattleMonster[] monsters, BattleState battleState) {
mHeroSprites = new Dictionary<BattleHero, GameObject>(); mHeroSprites = new Dictionary<BattleHero, GameObject>();
mHeroUIs = new Dictionary<BattleHero, HeroInfoUI>(); mHeroUIs = new Dictionary<BattleHero, HeroInfoUI>();
mMonsterSprites = new Dictionary<BattleMonster, GameObject>(); mMonsterSprites = new Dictionary<BattleMonster, GameObject>();
@ -35,7 +35,7 @@ public class BattleSprites: MonoBehaviour
hero.OnDeath += bps.NotifyDeath; hero.OnDeath += bps.NotifyDeath;
HeroInfoUI heroUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI)); HeroInfoUI heroUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI));
heroUI.Init(sprite, hero); heroUI.Init(sprite, hero, battleState);
mHeroUIs[hero] = heroUI; mHeroUIs[hero] = heroUI;
} }
@ -52,7 +52,7 @@ public class BattleSprites: MonoBehaviour
monster.OnDeath += bps.NotifyDeath; monster.OnDeath += bps.NotifyDeath;
HeroInfoUI monsterUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI)); HeroInfoUI monsterUI = (HeroInfoUI) Instantiate(mHeroUIPrefab.gameObject, mUiCanvas.transform).GetComponent(typeof(HeroInfoUI));
monsterUI.Init(sprite, monster); monsterUI.Init(sprite, monster, battleState);
mMonsterUIs[monster] = monsterUI; mMonsterUIs[monster] = monsterUI;
} }

View file

@ -8,6 +8,11 @@ public class BattleState
private TeamChoice mTeamsTurn = TeamChoice.Monsters; private TeamChoice mTeamsTurn = TeamChoice.Monsters;
private bool mPlayedEmotion; private bool mPlayedEmotion;
private int mRound = 0; private int mRound = 0;
private BuffManager buffManager;
public BattleState(BuffManager buffManager) {
this.buffManager = buffManager;
}
public bool IsTeamsTurn(TeamChoice team) { public bool IsTeamsTurn(TeamChoice team) {
return team == mTeamsTurn; return team == mTeamsTurn;
@ -38,4 +43,6 @@ public class BattleState
mRound++; mRound++;
} }
} }
public BuffManager BuffManager {get => this.buffManager;}
} }

View file

@ -29,6 +29,7 @@ public class BuffManager {
List<Buff> current = GetAllBuffsFor(effect.Participant); List<Buff> current = GetAllBuffsFor(effect.Participant);
current.Add(new Buff(effect.Type, effect.Amount, CalculateEnd(timeNow, effect.Duration))); current.Add(new Buff(effect.Type, effect.Amount, CalculateEnd(timeNow, effect.Duration)));
mBuffs[effect.Participant] = current; mBuffs[effect.Participant] = current;
effect.Participant.TriggerOnBuffsChanged();
} }
public int GetTotalBuffAmountOfType(BattleParticipant participant, BuffType type) { public int GetTotalBuffAmountOfType(BattleParticipant participant, BuffType type) {
@ -50,8 +51,13 @@ public class BuffManager {
} }
private void NotifyBattleTime(BattleTime timeNow) { private void NotifyBattleTime(BattleTime timeNow) {
foreach (List<Buff> buffs in mBuffs.Values) { foreach (KeyValuePair<BattleParticipant, List<Buff>> p in mBuffs) {
buffs.RemoveAll(buff => timeNow.Int >= buff.End.Int); BattleParticipant participant = p.Key;
List<Buff> buffs = p.Value;
int buffsRemoved = buffs.RemoveAll(buff => timeNow.Int >= buff.End.Int);
if (buffsRemoved > 0) {
participant.TriggerOnBuffsChanged();
}
} }
} }

View file

@ -7,6 +7,8 @@ public class HeroInfoUI : MonoBehaviour
{ {
protected GameObject target; protected GameObject target;
protected BattleParticipant battleParticipant; protected BattleParticipant battleParticipant;
protected BattleState battleState;
public Text nameText; public Text nameText;
public Image healthBarBgImage; public Image healthBarBgImage;
public Image healthBarFgImage; 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 MED_HP_COLOR = new Color(0.8f, 0.8f, 0f);
private static Color LOW_HP_COLOR = new Color(0.8f, 0f, 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.target = sprite;
this.battleParticipant = battleParticipant; this.battleParticipant = battleParticipant;
this.battleState = battleState;
this.statusUI.Init(battleParticipant); this.statusUI.Init(battleParticipant, battleState);
} }
// Start is called before the first frame update // Start is called before the first frame update

View file

@ -11,16 +11,24 @@ public class StatusIconUI : MonoBehaviour
private BattleStatus status; private BattleStatus status;
public void Init(BattleStatus status) { public void InitStatus(BattleStatus status) {
this.status = status;
Sprite sprite = Resources.Load<Sprite>(GetStatusResourcePath(status)); Sprite sprite = Resources.Load<Sprite>(GetStatusResourcePath(status));
this.statusImage.sprite = sprite; this.statusImage.sprite = sprite;
} }
public void InitBuff(BuffType buffType) {
Sprite sprite = Resources.Load<Sprite>(GetBuffResourcePath(buffType));
this.statusImage.sprite = sprite;
}
private static string GetStatusResourcePath(BattleStatus status) { private static string GetStatusResourcePath(BattleStatus status) {
return "BattleIcons/StatusIcon" + status; return "BattleIcons/StatusIcon" + status;
} }
private static string GetBuffResourcePath(BuffType buffType) {
return "BattleIcons/BuffIcon" + buffType;
}
public void SetAmount(int amount, int index) { public void SetAmount(int amount, int index) {
this.turnsText.text = "" + amount; this.turnsText.text = "" + amount;
this.transform.localPosition = new Vector2(-index * 22, 0); this.transform.localPosition = new Vector2(-index * 22, 0);

View file

@ -10,14 +10,20 @@ public class StatusUI : MonoBehaviour
public StatusIconUI iconPrefab; public StatusIconUI iconPrefab;
private BattleParticipant battleParticipant; private BattleParticipant battleParticipant;
private BattleState battleState;
private Dictionary<BattleStatus, StatusIconUI> icons; private Dictionary<BattleStatus, StatusIconUI> statusIcons;
public void Init(BattleParticipant battleParticipant) { private Dictionary<BuffType, StatusIconUI> buffIcons;
public void Init(BattleParticipant battleParticipant, BattleState battleState) {
this.battleParticipant = battleParticipant; this.battleParticipant = battleParticipant;
this.battleState = battleState;
this.icons = new Dictionary<BattleStatus, StatusIconUI>(); this.statusIcons = new Dictionary<BattleStatus, StatusIconUI>();
this.battleParticipant.OnStatusChanged += this.NotifyBattleStatusChanged; this.buffIcons = new Dictionary<BuffType, StatusIconUI>();
this.battleParticipant.OnStatusChanged += this.NotifyBattleStatusOrBuffsChanged;
this.battleParticipant.OnBuffsChanged += this.NotifyBattleStatusOrBuffsChanged;
} }
// Start is called before the first frame update // Start is called before the first frame update
@ -25,10 +31,10 @@ public class StatusUI : MonoBehaviour
UpdateAppearance(); UpdateAppearance();
} }
public void NotifyBattleStatusChanged(BattleParticipant updatedParticipant) { public void NotifyBattleStatusOrBuffsChanged(BattleParticipant updatedParticipant) {
if (updatedParticipant != this.battleParticipant) { if (updatedParticipant != this.battleParticipant) {
throw new System.InvalidOperationException( throw new System.InvalidOperationException(
"NotifyBattleStatusChanged was notified about a different participant."); "NotifyBattleStatusOrBuffsChanged was notified about a different participant.");
} }
UpdateAppearance(); UpdateAppearance();
} }
@ -41,21 +47,44 @@ public class StatusUI : MonoBehaviour
} }
StatusIconUI iconUI = null; StatusIconUI iconUI = null;
this.icons.TryGetValue(status, out iconUI); this.statusIcons.TryGetValue(status, out iconUI);
if (this.battleParticipant.HasStatus(status)) { if (this.battleParticipant.HasStatus(status)) {
if (iconUI == null) { if (iconUI == null) {
iconUI = Instantiate(this.iconPrefab.gameObject, this.transform).GetComponent<StatusIconUI>(); iconUI = Instantiate(this.iconPrefab.gameObject, this.transform).GetComponent<StatusIconUI>();
iconUI.Init(status); iconUI.InitStatus(status);
this.icons[status] = iconUI; this.statusIcons[status] = iconUI;
} }
iconUI.SetAmount(this.battleParticipant.GetStatusAmount(status), index); iconUI.SetAmount(this.battleParticipant.GetStatusAmount(status), index++);
index++;
} else { } else {
if (iconUI != null) { if (iconUI != null) {
Destroy(iconUI.gameObject); Destroy(iconUI.gameObject);
this.icons.Remove(status); this.statusIcons.Remove(status);
} }
} }
} }
Dictionary<BuffType, StatusIconUI> previousBuffIcons = new Dictionary<BuffType, StatusIconUI>(this.buffIcons);
List<Buff> 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<StatusIconUI>();
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<BuffType, StatusIconUI> p in previousBuffIcons) {
BuffType buffType = p.Key;
StatusIconUI iconUI = p.Value;
Destroy(iconUI.gameObject);
this.buffIcons.Remove(buffType);
}
} }
} }