61 lines
2 KiB
C#
61 lines
2 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 Dictionary<BattleStatus, StatusIconUI> icons;
|
|
|
|
public void Init(BattleParticipant battleParticipant) {
|
|
this.battleParticipant = battleParticipant;
|
|
|
|
this.icons = new Dictionary<BattleStatus, StatusIconUI>();
|
|
this.battleParticipant.OnStatusChanged += this.NotifyBattleStatusChanged;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start() {
|
|
UpdateAppearance();
|
|
}
|
|
|
|
public void NotifyBattleStatusChanged(BattleParticipant updatedParticipant) {
|
|
if (updatedParticipant != this.battleParticipant) {
|
|
throw new System.InvalidOperationException(
|
|
"NotifyBattleStatusChanged 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.icons.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.SetAmount(this.battleParticipant.GetStatusAmount(status), index);
|
|
index++;
|
|
} else {
|
|
if (iconUI != null) {
|
|
Destroy(iconUI.gameObject);
|
|
this.icons.Remove(status);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|