using Core.Economy;
using System.Xml;
using TMPro;
using TowerDefense.Level;
using UnityEngine;
using UnityEngine.UI;
namespace TowerDefense.UI.HUD
{
///
/// A class for controlling the displaying the currency
///
public class CurrencyUI : MonoBehaviour
{
///
/// TextMeshPro对应的金币数据
///
public TextMeshProUGUI displayText;
// 对手的金币显示
//public Text displayOpp;
///
/// The currency prefix to display next to the amount
///
public string currencySymbol = "$";
protected Currency m_Currency;
protected Currency m_CurrencyOpp;
///
/// Assign the correct currency value
///
protected virtual void Start()
{
}
protected void Update()
{
if (m_Currency != null) return ;
if ( (LevelManager.instance != null) )
{
m_Currency = LevelManager.instance.currency;
//m_CurrencyOpp = OpponentMgr.instance.currency;
UpdateDisplay();
m_Currency.currencyChanged += UpdateDisplay;
//m_CurrencyOpp.currencyChanged += UpdateDisplay;
}
else
{
Debug.LogError("[UI] No level manager to get currency from");
}
}
///
/// Unsubscribe from events
///
protected virtual void OnDestroy()
{
if (m_Currency != null)
{
m_Currency.currencyChanged -= UpdateDisplay;
if( m_CurrencyOpp != null )
m_CurrencyOpp.currencyChanged -= UpdateDisplay;
}
}
///
/// A method for updating the display based on the current currency
///
protected void UpdateDisplay()
{
int current = m_Currency.currentCurrency;
displayText.text = AddNumberSemi(current.ToString());
//m_CurrencyOpp = OpponentMgr.instance.currency;
//current = m_CurrencyOpp.currentCurrency;
//displayOpp.text = current.ToString();
}
///
/// 在数字中加入分号.
///
///
protected string AddNumberSemi( string str)
{
int len = str.Length;
for( int ipos = len-3;ipos>0;ipos -= 3 )
{
str = str.Insert(ipos, ",");
}
return str;
}
}
}