//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Thursday, February 28, 2019 //-------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Snxxz.UI { public class EquipStarWin : Window { [SerializeField] RectTransform m_LevelContainer; [SerializeField] Star[] m_Stars; [SerializeField] ItemCell m_TargetEquip; [SerializeField] Text m_EquipStar; [SerializeField] Materials m_Materials; [SerializeField] Text m_SuccessRate; [SerializeField] RectTransform m_EmptyHint; [SerializeField] RectTransform m_PropertyDetails; [SerializeField] BaseProperty[] m_BasePropertys; [SerializeField] Text[] m_LevelUpPropertys; [SerializeField] Button m_StarUpgrade; [SerializeField] RectTransform m_MaxStarLevelHint; [SerializeField] EquipStarMaterialCandidateWidget m_MaterialCandidateWidget; [SerializeField] UIEffect m_EffectStarFill; [SerializeField] UIEffect m_EffectSuccess; [SerializeField] UIEffect m_EffectFailed; List levelBehaviours = new List(); EquipStarModel model { get { return ModelCenter.Instance.GetModel(); } } EquipModel equipModel { get { return ModelCenter.Instance.GetModel(); } } PackModel packModel { get { return ModelCenter.Instance.GetModel(); } } #region Built-in protected override void BindController() { } protected override void AddListeners() { m_StarUpgrade.SetListener(StarUpgrade); } protected override void OnPreOpen() { if (model.selectedLevel.value == 0) { model.SelectLevel(equipModel.GetLastestUnLockEquipSet()); } DisplayBaseInfo(); } protected override void OnAfterOpen() { } protected override void OnPreClose() { } protected override void OnAfterClose() { model.ResetOperateParams(); } protected override void OnActived() { base.OnActived(); DisplayDynamicInfo(true); } protected override void LateUpdate() { base.LateUpdate(); DisplayDynamicInfo(false); } #endregion private void DisplayBaseInfo() { CreateLevelBehaviours(); } private void DisplayDynamicInfo(bool force) { if (force || model.selectedLevel.dirty) { var level = model.selectedLevel.Fetch(); for (var i = 0; i < levelBehaviours.Count; i++) { var behaviour = levelBehaviours[i]; if (behaviour.level != level) { behaviour.Dispose(); } } for (var i = 0; i < levelBehaviours.Count; i++) { var behaviour = levelBehaviours[i]; if (behaviour.level == level) { behaviour.Display(); } } } if (force || model.selectedPlace.dirty) { var currentStarLevel = 0; var equipPosition = new Int2(model.selectedLevel.value, model.selectedPlace.Fetch()); var equipGuid = equipModel.GetEquip(equipPosition); var equip = packModel.GetItemByGuid(equipGuid); if (equip != null) { m_TargetEquip.gameObject.SetActive(true); m_TargetEquip.Init(equip); m_TargetEquip.button.SetListener(ViewEquipTip); currentStarLevel = model.GetEquipStarLevel(equipPosition); m_EquipStar.gameObject.SetActive(true); m_EquipStar.text = currentStarLevel >= 1 ? string.Format("{0}星", currentStarLevel) : ""; } else { m_EquipStar.gameObject.SetActive(false); m_TargetEquip.gameObject.SetActive(false); } DisplayMaterialSlots(equipPosition, currentStarLevel); DisplayPropertyPreview(equipPosition, currentStarLevel, EquipStarModel.GetMaxStarLevel(equipPosition.x)); DisplayStarUpgradeButton(equipPosition, currentStarLevel); } if (force || model.equipStarLevel.dirty || model.equipMaxStarLevel.dirty) { var equipPosition = new Int2(model.selectedLevel.value, model.selectedPlace.value); var starLevel = model.equipStarLevel.Fetch(); var maxStar = model.equipMaxStarLevel.Fetch(); DisplayPropertyPreview(equipPosition, starLevel, EquipStarModel.GetMaxStarLevel(equipPosition.x)); DisplayStarUpgradeButton(equipPosition, starLevel); } if (force || model.stars.dirty) { DisplayStars(model.stars.Fetch()); } if (force || model.starUpgradeProbability.dirty) { DisplaySuccessRate(model.starUpgradeProbability.Fetch()); } if (force || model.operateMaterialIndex.dirty) { var index = model.operateMaterialIndex.Fetch(); DisplayMaterialCandidates(index); } if (force || model.starResultEffect.dirty) { var result = model.starResultEffect.Fetch(); model.starResultEffect.value = 0; PlayStarUpgradeEffect(result); } } private void DisplayStars(List stars) { StopCoroutine("Co_DisplayStars"); StartCoroutine("Co_DisplayStars", stars); } private void DisplayMaterialSlots(Int2 equipPosition, int currentStarLevel) { var equipGuid = equipModel.GetEquip(equipPosition); var equip = packModel.GetItemByGuid(equipGuid); var maxStarLevel = equip != null ? EquipStarModel.GetMaxStarLevel(equip.config.ItemColor, equip.config.LV) : 0; var upgradable = maxStarLevel > 0; var isMax = upgradable && currentStarLevel >= maxStarLevel; if (!upgradable || isMax) { for (var i = 1; i <= 5; i++) { m_Materials[i].Display(false); } m_Materials.materialSpecial.Display(false, 0, 0); } else { var starConfig = EquipStarConfig.Get(equipPosition.x, equipPosition.y, currentStarLevel + 1); for (var i = 1; i <= 5; i++) { m_Materials[i].Display(starConfig != null && i <= starConfig.CostEquipCnt); } if (starConfig != null) { m_Materials.materialSpecial.Display(starConfig.CostItemDict.x != 0, starConfig.CostItemDict.x, starConfig.CostItemDict.y); } } } private void DisplayPropertyPreview(Int2 equipPosition, int currentStar, int maxStar) { var equipGuid = equipModel.GetEquip(equipPosition); var isMax = maxStar > 0 && currentStar >= maxStar; var upgradable = model.IsEquipPlaceUpgradable(equipGuid); if (isMax) { m_EmptyHint.gameObject.SetActive(false); m_PropertyDetails.gameObject.SetActive(true); DisplayBaseProperty(equipPosition, currentStar, currentStar); DisplayLevelUpProperty(equipPosition, currentStar, currentStar); } else if (upgradable) { m_EmptyHint.gameObject.SetActive(false); m_PropertyDetails.gameObject.SetActive(true); DisplayBaseProperty(equipPosition, currentStar, currentStar + 1); DisplayLevelUpProperty(equipPosition, currentStar, currentStar + 1); } else { m_EmptyHint.gameObject.SetActive(true); m_PropertyDetails.gameObject.SetActive(false); } } private void DisplayBaseProperty(Int2 equipPosition, int currentStar, int nextStar) { var nowConfig = EquipStarConfig.Get(equipPosition.x, equipPosition.y, currentStar); var nextConfig = EquipStarConfig.Get(equipPosition.x, equipPosition.y, nextStar); if (nextConfig == null) { nextConfig = nowConfig; } var count = nextConfig != null ? nextConfig.BaseAttrInfo.Length : 0; for (var i = 0; i < m_BasePropertys.Length; i++) { var behaviour = m_BasePropertys[i]; if (i < count) { var current = nowConfig == null ? 0 : nowConfig.BaseAttrInfo[i].y; var next = nextConfig == null ? 0 : nextConfig.BaseAttrInfo[i].y; behaviour.Display(nextConfig == null ? 0 : nextConfig.BaseAttrInfo[i].x, current, next); } else { behaviour.Hide(); } } } private void DisplayLevelUpProperty(Int2 equipPosition, int currentStar, int nextStar) { var configs = EquipStarConfig.GetConfigs(equipPosition.x, equipPosition.y); var levelUpProperties = new Dictionary(); var prePropertiesCount = 0; foreach (var config in configs) { if (config.StarAttrInfo.Length > prePropertiesCount) { var length = config.StarAttrInfo.Length; levelUpProperties[config.Star] = config.StarAttrInfo[length - 1]; prePropertiesCount = config.StarAttrInfo.Length; } } var keys = new List(levelUpProperties.Keys); for (var i = 0; i < m_LevelUpPropertys.Length; i++) { var behaviour = m_LevelUpPropertys[i]; if (i < keys.Count) { var star = keys[i]; var property = levelUpProperties[star]; behaviour.gameObject.SetActive(true); behaviour.text = string.Format("【{0}星】{1}", star, PlayerPropertyConfig.GetFullDescription(property)); behaviour.color = UIHelper.GetUIColor(currentStar >= star ? TextColType.Green : TextColType.Gray); } else { behaviour.gameObject.SetActive(false); } } } private void DisplaySuccessRate(int successRate) { if (successRate >= 0) { m_SuccessRate.text = string.Format("成功率 {0}%", successRate); } else { m_SuccessRate.text = string.Empty; } } private void DisplayMaterialCandidates(int materialIndex) { if (materialIndex == 0) { m_MaterialCandidateWidget.gameObject.SetActive(false); } else { m_MaterialCandidateWidget.gameObject.SetActive(true); var equipPosition = new Int2(model.selectedLevel.value, model.selectedPlace.value); var starLevel = model.GetEquipStarLevel(equipPosition); m_MaterialCandidateWidget.Display(equipPosition, starLevel + 1); var position = Vector3.zero; m_MaterialCandidateWidget.transform.localPosition = position; } } private void DisplayStarUpgradeButton(Int2 equipPosition, int currentStarLevel) { if (equipPosition.y == 0) { m_StarUpgrade.gameObject.SetActive(false); m_MaxStarLevelHint.gameObject.SetActive(false); } else { var maxLevel = EquipStarModel.GetMaxStarLevel(equipPosition.x); var isMax = currentStarLevel >= maxLevel; m_StarUpgrade.gameObject.SetActive(!isMax); m_MaxStarLevelHint.gameObject.SetActive(isMax); } } private void StarUpgrade() { var equipPosition = new Int2(model.selectedLevel.value, model.selectedPlace.value); var currentStarLevel = model.GetEquipStarLevel(equipPosition); model.DoStarUpgrade(equipPosition, currentStarLevel + 1); } private void CreateLevelBehaviours() { var levels = equipModel.GetUnLockedEquipSets(); var gap = levels.Count - levelBehaviours.Count; for (var i = 0; i < gap; i++) { var instance = UIUtility.CreateWidget("EquipStarLevelSelectBehaviour", "EquipStarLevelSelectBehaviour"); var behaviour = instance.GetComponent(); behaviour.transform.SetParentEx(m_LevelContainer, Vector3.zero, Quaternion.identity, Vector3.one); levelBehaviours.Add(behaviour); } for (var i = 0; i < levelBehaviours.Count; i++) { var behaviour = levelBehaviours[i]; if (i < levels.Count) { behaviour.gameObject.SetActive(true); behaviour.UnInit(); behaviour.Init(levels[i]); } else { behaviour.UnInit(); behaviour.gameObject.SetActive(false); } } } private void PlayStarUpgradeEffect(int result) { if (result == 1 || result == -1) { StopCoroutine("Co_PlaySuccessEffect"); StopCoroutine("Co_PlayFailedEffect"); } if (result == 1) { StartCoroutine("Co_PlaySuccessEffect"); } else if (result == -1) { StartCoroutine("Co_PlayFailedEffect"); } } IEnumerator Co_PlaySuccessEffect() { m_EffectStarFill.Play(); yield return WaitingForSecondConst.WaitMS700; m_EffectSuccess.Play(); } IEnumerator Co_PlayFailedEffect() { m_EffectStarFill.Play(); yield return WaitingForSecondConst.WaitMS700; m_EffectFailed.Play(); } IEnumerator Co_DisplayStars(List stars) { var hasNewStar = stars.FindIndex(x => { return x.newGet; }) != -1; if (hasNewStar) { yield return WaitingForSecondConst.WaitMS2000; } for (var i = 0; i < m_Stars.Length; i++) { if (i < stars.Count) { var star = stars[i]; m_Stars[i].Display(star.actived); if (star.newGet) { m_Stars[i].PlayEffect(); } } else { m_Stars[i].Hide(); } } } private void ViewEquipTip() { var level = model.selectedLevel.value; var place = model.selectedPlace.value; var equipPosition = new Int2(level, place); var equipGuid = equipModel.GetEquip(equipPosition); var itemTipsModel = ModelCenter.Instance.GetModel(); itemTipsModel.SetItemTipsModel(PackType.Item, equipGuid, false, true); itemTipsModel.ShowTip(); } [System.Serializable] public class Star { public RectTransform container; public Image imageBase; public Image imageStar; public UIEffect newStarEffect; public void Display(bool active) { container.gameObject.SetActive(true); imageStar.gameObject.SetActive(active); } public void Hide() { container.gameObject.SetActive(false); } public void PlayEffect() { newStarEffect.Play(); } } [System.Serializable] public class Materials { public EquipStarUpgradeMaterialBehaviour m_Material1; public EquipStarUpgradeMaterialBehaviour m_Material2; public EquipStarUpgradeMaterialBehaviour m_Material3; public EquipStarUpgradeMaterialBehaviour m_Material4; public EquipStarUpgradeMaterialBehaviour m_Material5; public EquipStarUpgradeSpecialMaterialBehaviour materialSpecial; public EquipStarUpgradeMaterialBehaviour this[int index] { get { switch (index) { case 1: return m_Material1; case 2: return m_Material2; case 3: return m_Material3; case 4: return m_Material4; case 5: return m_Material5; default: return null; } } } } [System.Serializable] public class BaseProperty { public RectTransform container; public Text current; public Image arrow; public Text next; public void Display(int propertyId, int currentValue, int nextValue) { container.gameObject.SetActive(true); var config = PlayerPropertyConfig.Get(propertyId); if (config != null) { this.current.text = StringUtility.Contact(config.Name, ":", PlayerPropertyConfig.GetValueDescription(propertyId, currentValue)); if (currentValue != nextValue) { this.arrow.gameObject.SetActive(true); this.next.gameObject.SetActive(true); this.next.text = PlayerPropertyConfig.GetValueDescription(propertyId, nextValue); } else { this.arrow.gameObject.SetActive(false); this.next.gameObject.SetActive(false); } } } public void Hide() { container.gameObject.SetActive(false); } } } }