Display power buffs
This commit is contained in:
parent
515602b670
commit
034e6beca6
8 changed files with 86 additions and 25 deletions
|
|
@ -10,14 +10,20 @@ public class StatusUI : MonoBehaviour
|
|||
public StatusIconUI iconPrefab;
|
||||
|
||||
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.battleState = battleState;
|
||||
|
||||
this.icons = new Dictionary<BattleStatus, StatusIconUI>();
|
||||
this.battleParticipant.OnStatusChanged += this.NotifyBattleStatusChanged;
|
||||
this.statusIcons = new Dictionary<BattleStatus, StatusIconUI>();
|
||||
this.buffIcons = new Dictionary<BuffType, StatusIconUI>();
|
||||
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<StatusIconUI>();
|
||||
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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue