using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using TableConfig; using DG.Tweening; namespace Snxxz.UI { public class TreasureProperty : MonoBehaviour { [SerializeField] Text m_FightPower; [SerializeField] Image m_Bottom; [SerializeField] RectTransform[] m_PropertyComs; [SerializeField] Property[] m_Properties; [SerializeField] RectTransform m_ContainerProperty; [SerializeField] RectTransform m_ContainerName; [SerializeField] Image m_TreasureName; [SerializeField] PositionTween m_TreasureNameTween; Treasure treasure; TreasureStage m_TreasureStage; TreasureModel m_Model; TreasureModel model { get { return m_Model ?? (m_Model = ModelCenter.Instance.GetModel()); } } public RectTransform containerName { get { return m_ContainerName; } } public RectTransform containerProperty { get { return m_ContainerProperty; } } public PositionTween treasureNameTween { get { return m_TreasureNameTween; } } Dictionary m_PropertyDict = new Dictionary(); Dictionary m_BeforeProperty = new Dictionary(); public void Display(int _treasure, bool _stageUp = false) { if (!model.TryGetTreasure(_treasure, out treasure)) { return; } var config = Config.Instance.Get(treasure.id); if (treasure.stage < treasure.treasureStages.Count) { m_TreasureStage = treasure.treasureStages[treasure.stage]; } m_PropertyDict.Clear(); if (config.Category == (int)TreasureCategory.Human) { for (int i = 0; i < treasure.treasureStages.Count; i++) { TreasureStage _treasureStage = treasure.treasureStages[i]; if (treasure.stage >= _treasureStage.stage && _treasureStage.unlockType == TreasureStageUnlock.Property) { foreach (var _key in _treasureStage.propertyDict.Keys) { if (m_PropertyDict.ContainsKey(_key)) { m_PropertyDict[_key] += _treasureStage.propertyDict[_key]; } else { m_PropertyDict[_key] = _treasureStage.propertyDict[_key]; } } } } } int _index = 0; foreach (var _key in m_PropertyDict.Keys) { if (_index < m_PropertyComs.Length && _index < m_Properties.Length) { m_PropertyComs[_index].gameObject.SetActive(true); m_Properties[_index].Display(_key, m_PropertyDict[_key]); if (_stageUp && !m_BeforeProperty.ContainsKey(_key)) { m_Properties[_index].StartAlphaTween(0, 1); } else if (_stageUp && m_BeforeProperty.ContainsKey(_key) && m_BeforeProperty[_key] < m_PropertyDict[_key]) { StartCoroutine(Co_NumberClock(m_Properties[_index], m_BeforeProperty[_key], m_PropertyDict[_key])); } } _index++; } m_Bottom.gameObject.SetActive(_index > 0); for (int i = _index; i < m_PropertyComs.Length; i++) { m_PropertyComs[i].gameObject.SetActive(false); } m_BeforeProperty.Clear(); foreach (var _key in m_PropertyDict.Keys) { m_BeforeProperty.Add(_key, m_PropertyDict[_key]); } if (treasure.state != TreasureState.Collected) { m_FightPower.text = treasure.treasureStages.Count > 0 ? treasure.treasureStages[0].GetFightPower().ToString() : string.Empty; } else { m_FightPower.text = model.GetTreasureFightPower(_treasure).ToString(); } m_ContainerProperty.gameObject.SetActive(config.Category == 1); m_TreasureName.SetSprite(config.TreasureNameIcon); } public void SetActive(bool _active) { if (!_active) { m_Bottom.gameObject.SetActive(_active); } m_ContainerProperty.gameObject.SetActive(_active); m_ContainerName.gameObject.SetActive(_active); } IEnumerator Co_NumberClock(Property _property, int _before, int _target) { float _timer = 0f; int _value = _before; while (_timer < 0.5f) { _timer += Time.deltaTime; _property.propertyValue.text = StringUtility.Contact("+", _value); yield return null; _value = (int)((_target - _before) * _timer / 0.5f) + _before; } _property.propertyValue.text = StringUtility.Contact("+", _target); } } [Serializable] public class Property { [SerializeField] Text m_Property; [SerializeField] Text m_Value; public Text propertyValue { get { return m_Value; } } public void Display(int _property, float _value) { var _cfg = Config.Instance.Get(_property); m_Property.text = _cfg.Name; m_Value.text = StringUtility.Contact("+", _value); } public void StartAlphaTween(float _from, float _to) { m_Property.color = m_Property.color.SetA(_from); m_Property.DOColor(m_Property.color.SetA(_to), 1.0f); } } }