90 lines
3.4 KiB
C#
90 lines
3.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class StatusUI : MonoBehaviour
|
|
{
|
|
public StatusIconUI iconPrefab;
|
|
|
|
private BattleParticipant battleParticipant;
|
|
private BattleState battleState;
|
|
|
|
private Dictionary<BattleStatus, StatusIconUI> statusIcons;
|
|
|
|
private Dictionary<BuffType, StatusIconUI> buffIcons;
|
|
|
|
public void Init(BattleParticipant battleParticipant, BattleState battleState) {
|
|
this.battleParticipant = battleParticipant;
|
|
this.battleState = battleState;
|
|
|
|
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
|
|
void Start() {
|
|
UpdateAppearance();
|
|
}
|
|
|
|
public void NotifyBattleStatusOrBuffsChanged(BattleParticipant updatedParticipant) {
|
|
if (updatedParticipant != this.battleParticipant) {
|
|
throw new System.InvalidOperationException(
|
|
"NotifyBattleStatusOrBuffsChanged was notified about a different participant.");
|
|
}
|
|
UpdateAppearance();
|
|
}
|
|
|
|
private void UpdateAppearance() {
|
|
int index = 0;
|
|
foreach (BattleStatus status in Enum.GetValues(typeof(BattleStatus)).Cast<BattleStatus>()) {
|
|
if (status == BattleStatus.None) {
|
|
continue;
|
|
}
|
|
|
|
StatusIconUI iconUI = null;
|
|
this.statusIcons.TryGetValue(status, out iconUI);
|
|
if (this.battleParticipant.HasStatus(status)) {
|
|
if (iconUI == null) {
|
|
iconUI = Instantiate(this.iconPrefab.gameObject, this.transform).GetComponent<StatusIconUI>();
|
|
iconUI.InitStatus(status);
|
|
this.statusIcons[status] = iconUI;
|
|
}
|
|
iconUI.SetAmount(this.battleParticipant.GetStatusAmount(status), index++);
|
|
} else {
|
|
if (iconUI != null) {
|
|
Destroy(iconUI.gameObject);
|
|
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);
|
|
}
|
|
}
|
|
}
|