using Snxxz.UI; using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace Snxxz.UI { public class ComposeEquipWin : Window { [SerializeField] ScrollerController cellCtrl; [SerializeField] ScrollerController equipTypeLineCellCtrl; [SerializeField] GameObject increaseRateItem; [SerializeField] GameObject increaseGridCell; [SerializeField] GameObject three_ComposeEquip; [SerializeField] GameObject five_ComposeEquip; [Header("固定物品和三个非固定物品")] [SerializeField] List three_FixedAndUnfixeds = new List(); [Header("固定物品和五个非固定物品")] [SerializeField] List five_FixedAndUnfixeds = new List(); [Header("合成物品")] [SerializeField] List makeItems = new List(); [Header("提高成功率")] [SerializeField] ComposeMatCell increaseRateMatCell; [SerializeField] Text increaseText; [SerializeField] PushSwitchBtn switchBtn; [SerializeField] Text successRateText; [SerializeField] Text dismantleText; [SerializeField] Button helpBtn; [SerializeField] Button composeBtn; [SerializeField] Button onekeyPutBtn; [SerializeField] GameObject notChooseBG; [SerializeField] GameObject chooseComposeEquip; [SerializeField] GameObject container_ComposeEquip; [SerializeField] UIEffect composeEffect_Three; [SerializeField] UIEffect composeEffect_Five; [SerializeField] UIEffect successEffect; [SerializeField] UIEffect failEffect; ComposeWinModel _composeWinModel; ComposeWinModel composeWinModel { get { return _composeWinModel ?? (_composeWinModel = ModelCenter.Instance.GetModel()); } } PackModel _playerPack; PackModel playerPack { get { return _playerPack ?? (_playerPack = ModelCenter.Instance.GetModel()); } } ComposeWinModel.ComposeFirstTypeData firstTypeData; ComposeWinModel.ComposeSecondTypeData secondTypeData; ComposeWinModel.ComposeThirdTypeData thirdTypeData; Dictionary> fixedItemIndexDict = new Dictionary>(); private int curSecondType = 0; private int curThirdType = 0; private int preSecondType = 0; private int preThirdType = 0; [SerializeField] int initSuccessRate = 0; private int curComposeEquipIndex = -1; public static int selectEquipPlace; ItemCompoundConfig compoundModel = null; bool isIncreaseRate = false; int successRate = 0; SelectEquipModel _selectModel; SelectEquipModel selectModel { get { return _selectModel ?? (_selectModel = ModelCenter.Instance.GetModel()); } } protected override void BindController() { cellCtrl.OnRefreshCell += RefreshTypeCell; cellCtrl.vertical = true; cellCtrl.lockType = EnhanceLockType.KeepVertical; equipTypeLineCellCtrl.OnRefreshCell += RefreshEquipTypeLineCell; composeWinModel.ResetModelEvent += ResetModel; } protected override void AddListeners() { composeBtn.AddListener(OnClickComposeBtn); onekeyPutBtn.AddListener(OnClickOnekeyPutBtn); } protected override void OnPreOpen() { composeWinModel.UpdateSendComposeEvent += UpdateSendCompose; composeWinModel.UpdateSecondTypeEvent += UpdateSecondType; composeWinModel.UpdateThirdTypeEvent += UpdateThirdType; composeWinModel.UpdateReduceEvent += UpdateReduce; selectModel.UpdateSelectEvent += UpdateSelect; composeWinModel.UpdateEquipTypeEvent += UpdateEquipType; DTCA814_tagMCMakeItemAnswer.MakeItemAnswerEvent += OnComposeAnswer; UIEventTrigger.Get(helpBtn.gameObject).OnDown = OnClickHelpBtn; UIEventTrigger.Get(helpBtn.gameObject).OnUp = OnDownUp; Display(); } protected override void OnAfterOpen() { this.transform.SetAsLastSibling(); } protected override void OnPreClose() { composeWinModel.UpdateSendComposeEvent -= UpdateSendCompose; composeWinModel.UpdateReduceEvent -= UpdateReduce; DTCA814_tagMCMakeItemAnswer.MakeItemAnswerEvent -= OnComposeAnswer; composeWinModel.UpdateSecondTypeEvent -= UpdateSecondType; composeWinModel.UpdateThirdTypeEvent -= UpdateThirdType; selectModel.UpdateSelectEvent -= UpdateSelect; composeWinModel.UpdateEquipTypeEvent -= UpdateEquipType; selectEquipPlace = -1; } protected override void OnAfterClose() { } #region 新的逻辑 private void Display() { composeWinModel.TryGetFirstTypeData((int)ComposeFuncType.Equip, out firstTypeData); if (firstTypeData == null) return; isIncreaseRate = false; successRate = 0; DefaultSelect(); CreateTypeCell(); JumpIndex(); } private void DefaultSelect() { if (composeWinModel.secondType != 0) { curSecondType = composeWinModel.secondType; curThirdType = composeWinModel.thirdType; composeWinModel.ResetJumpToModel(); } if (composeWinModel.CurComposeModel == null) { curComposeEquipIndex = -1; } List thirdTypeDatas = null; composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Equip, curSecondType, curThirdType, out thirdTypeDatas); if (thirdTypeDatas != null) { for (int i = 0; i < thirdTypeDatas.Count; i++) { var itemConfig = ItemConfig.Get(thirdTypeDatas[i].itemCompound.makeID[0]); if (itemConfig.EquipPlace == selectEquipPlace) { curComposeEquipIndex = i; break; } } } } private void CreateTypeCell() { if (firstTypeData == null) return; cellCtrl.Refresh(); foreach (var second in firstTypeData.secondTypeDict.Keys) { cellCtrl.AddCell(ScrollerDataType.Header, second); if (second == curSecondType) { var thirdTypeDict = firstTypeData.secondTypeDict[second].thirdTypeDict; foreach (var third in thirdTypeDict.Keys) { if (third != 0) { cellCtrl.AddCell(ScrollerDataType.Normal, third); } } } } cellCtrl.Restart(); if (curThirdType != 0) { CreateEquipTypeLineCell(); composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Equip, curSecondType, curThirdType, out thirdTypeData); if (thirdTypeData != null) { ItemConfig itemConfig = ItemConfig.Get(thirdTypeData.itemCompound.makeID[0]); if (itemConfig != null && itemConfig.ItemColor == 6) { dismantleText.gameObject.SetActive(true); } else { dismantleText.gameObject.SetActive(false); } } } else { RefreshUI(null); dismantleText.gameObject.SetActive(false); ChangeUIState(true, false, false); } } private void RefreshTypeCell(ScrollerDataType type, CellView cell) { switch (type) { case ScrollerDataType.Header: ComposeWinModel.ComposeSecondTypeData _secondTypeData = null; ComposeFirstTypeCell firstTypeCell = cell.GetComponent(); composeWinModel.TryGetSecondTypeData((int)ComposeFuncType.Equip, cell.index, out _secondTypeData); if (_secondTypeData != null) { var thirdTypeDict = _secondTypeData.thirdTypeDict; foreach (var third in thirdTypeDict.Keys) { firstTypeCell.SetDisplay((int)ComposeFuncType.Equip, cell.index, third, curSecondType); break; } } break; case ScrollerDataType.Normal: ComposeSecondTypeCell secondTypeCell = cell.GetComponent(); secondTypeCell.SetDisplay((int)ComposeFuncType.Equip, curSecondType, cell.index, curThirdType); break; } } private void JumpIndex() { if (firstTypeData == null) return; var secondTypeDict = firstTypeData.secondTypeDict; if (secondTypeDict.Count >= 1 && curSecondType != 0 && curThirdType >= 1) { cellCtrl.JumpIndex(GetCurIndex(curSecondType)); } } private int GetCurIndex(int secondType) { if (firstTypeData == null) return 0; int curIndex = 0; var secondTypeDict = firstTypeData.secondTypeDict; List secondlist = secondTypeDict.Keys.ToList(); for (int i = 0; i < secondlist.Count; i++) { if (secondType == secondlist[i]) { curIndex = i; break; } } if (curThirdType >= 4) { curIndex = curIndex + curThirdType - 2; } return curIndex; } private void UpdateSecondType(int secondType) { curThirdType = 0; curSecondType = secondType; composeWinModel.TryGetSecondTypeData((int)ComposeFuncType.Equip, secondType, out secondTypeData); OnClickSecondType(); CreateTypeCell(); JumpIndex(); } private void OnClickSecondType() { if (secondTypeData == null) return; var thirdTypeDict = secondTypeData.thirdTypeDict; if (curThirdType == 0) { foreach (var thirdType in thirdTypeDict.Keys) { if (thirdType != 0) { if (preSecondType == curSecondType) { curSecondType = 0; preSecondType = 0; } else { curThirdType = thirdType; } } break; } } if (preSecondType != curSecondType) { isIncreaseRate = false; preSecondType = curSecondType; curComposeEquipIndex = -1; selectModel.ClearSelectModel(); } } private void UpdateThirdType(int thirdType) { composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Equip, curSecondType, thirdType, out thirdTypeData); curThirdType = thirdType; OnClickThirdType(); CreateTypeCell(); } private void OnClickThirdType() { if (thirdTypeData == null) return; if (preThirdType != curThirdType) { isIncreaseRate = false; preThirdType = curThirdType; } curComposeEquipIndex = -1; selectModel.ClearSelectModel(); } private void CreateEquipTypeLineCell() { List thirdTypeDatas = null; bool isthird = composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Equip, curSecondType, curThirdType, out thirdTypeDatas); if (!isthird) return; if (thirdTypeDatas.Count > 1) { ChangeUIState(false, true, false); equipTypeLineCellCtrl.Refresh(); int line = thirdTypeDatas.Count / 3; if (thirdTypeDatas.Count % 3 > 0) { line += 1; } int i = 0; for (i = 0; i < line; i++) { equipTypeLineCellCtrl.AddCell(ScrollerDataType.Header, i); } equipTypeLineCellCtrl.Restart(); } else { if (thirdTypeDatas.Count > 0) { ItemCompoundConfig tagItemCompound = thirdTypeDatas[0].itemCompound; UpdateEquipType(tagItemCompound, 0); RefreshUI(tagItemCompound); ChangeUIState(false, false, true); } } if (curComposeEquipIndex != -1) { ItemCompoundConfig tagItemCompound = thirdTypeDatas[curComposeEquipIndex].itemCompound; UpdateEquipType(tagItemCompound, curComposeEquipIndex); } } private void ChangeUIState(params bool[] isShows) { notChooseBG.SetActive(isShows[0]); chooseComposeEquip.SetActive(isShows[1]); container_ComposeEquip.SetActive(isShows[2]); } private void UpdateComposeMat() { int[] makeIds = compoundModel.makeID; for (int i = 0; i < makeItems.Count; i++) { var matCell = makeItems[i]; if (makeIds != null && i < makeIds.Length) { matCell.SetDisplay(compoundModel, NeedMatType.MakeItem, false, "", makeIds[i]); } else { matCell.SetDisplay(compoundModel, NeedMatType.MakeItem, true); } } int[] fixedIds = compoundModel.itemID; int[] fixedDisplays = compoundModel.itemDisplay; int[] unfixedDisplays = compoundModel.unfixedItemDisplay; if (compoundModel.unfixedItemCount <= 3) { three_ComposeEquip.SetActive(true); five_ComposeEquip.SetActive(false); } else { three_ComposeEquip.SetActive(false); five_ComposeEquip.SetActive(true); } var fixedAndUnfixeds = compoundModel.unfixedItemCount > 3 ? five_FixedAndUnfixeds : three_FixedAndUnfixeds; for (int i = 0; i < fixedAndUnfixeds.Count; i++) { var matCell = fixedAndUnfixeds[i]; matCell.SetDisplay(compoundModel, NeedMatType.Nothing, true); } for (int i = 0; i < fixedDisplays.Length; i++) { var fixedDisplay = fixedDisplays[i]; if (fixedDisplay != 0) { var matCell = fixedAndUnfixeds[fixedDisplay - 1]; int fixedId = 0; composeWinModel.TryGetIdByDisplay(NeedMatType.fixedItem, compoundModel, fixedDisplay, out fixedId); matCell.SetDisplay(compoundModel, NeedMatType.fixedItem, false, "", fixedId); } } for (int i = 0; i < unfixedDisplays.Length; i++) { var unfixedDisplay = unfixedDisplays[i]; if (unfixedDisplay != 0) { var matCell = fixedAndUnfixeds[unfixedDisplay - 1]; matCell.SetDisplay(compoundModel, NeedMatType.unfixedItem, false, UIHelper.ReplaceNewLine(Language.Get("ComposeWin_PutInText_2"))); } } int[] increases = compoundModel.successRateIncrease; if (increases == null || increases.Length < 2) { isIncreaseRate = false; increaseRateMatCell.gameObject.SetActive(isIncreaseRate); } else { int increaseId = increases[0]; int increaseNeedNum = increases[1]; int increaseRate = increases[2] / 100; increaseRateMatCell.gameObject.SetActive(true); increaseRateMatCell.SetDisplay(compoundModel, NeedMatType.IncreaseItem, false, "", increaseId); increaseText.text = Language.Get("Compose109", StringUtility.Contact(increaseRate, "%")); int haveIncreaseNum = playerPack.GetItemCountByID(PackType.Item, increaseId); ItemConfig itemConfig = ItemConfig.Get(increaseId); isIncreaseRate = haveIncreaseNum >= increaseNeedNum ? isIncreaseRate : false; switchBtn.RefreshSwitchUI(isIncreaseRate); switchBtn.switchBtn.RemoveAllListeners(); switchBtn.switchBtn.AddListener(() => { if (switchBtn.onObj.activeInHierarchy) { isIncreaseRate = false; switchBtn.RefreshSwitchUI(false); } else { if (haveIncreaseNum >= increaseNeedNum) { isIncreaseRate = true; switchBtn.RefreshSwitchUI(true); } else { isIncreaseRate = false; ServerTipDetails.DisplayNormalTip(Language.Get("Compose110", increaseNeedNum, itemConfig.ItemName)); } } RefreshComposeSuccessRate(); }); } } private void UpdateSelect(ComposeMatCell matCell, int itemIndex, SelectItemType selectType) { switch (selectType) { case SelectItemType.unfixed: matCell.SetDisplay(compoundModel, NeedMatType.unfixedItem, false, "", 0, itemIndex); break; case SelectItemType.addons: matCell.SetDisplay(compoundModel, NeedMatType.addItem, false, "", 0, itemIndex); break; } } private void UpdateReduce(ComposeMatCell matCell, NeedMatType matType) { switch (matType) { case NeedMatType.unfixedItem: matCell.SetDisplay(compoundModel, matType, false, UIHelper.ReplaceNewLine(Language.Get("ComposeWin_PutInText_2"))); break; case NeedMatType.addItem: matCell.SetDisplay(compoundModel, matType, false); break; } } private void UpdateEquipType(ItemCompoundConfig itemCompound, int equipIndex) { curComposeEquipIndex = equipIndex; ChangeUIState(false, false, true); RefreshComposeSuccessRate(); RefreshUI(itemCompound); } #endregion private void ResetModel() { curSecondType = 0; curThirdType = 0; compoundModel = null; if (!WindowJumpMgr.Instance.IsJumpState) { curComposeEquipIndex = -1; preSecondType = 0; preThirdType = 0; } } private void RefreshEquipTypeLineCell(ScrollerDataType type, CellView cell) { List thirdTypeDatas = null; composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.Equip, curSecondType, curThirdType, out thirdTypeDatas); EquipTypeLineCell typeLineCell = cell.GetComponent(); typeLineCell.Refresh(cell, thirdTypeDatas); } private void RefreshUI(ItemCompoundConfig compoundModel) { this.compoundModel = compoundModel; if (compoundModel == null) { composeBtn.gameObject.SetActive(false); onekeyPutBtn.gameObject.SetActive(false); } else { composeWinModel.SetCurComposeModel(compoundModel); composeBtn.gameObject.SetActive(true); onekeyPutBtn.gameObject.SetActive(true); UpdateComposeMat(); RefreshComposeSuccessRate(); } } public void RefreshComposeSuccessRate() { if (compoundModel == null) return; successRate = 0; int increaseRate = 0; if (compoundModel != null) { successRate = compoundModel.successRate; } if (isIncreaseRate) { int[] increases = compoundModel.successRateIncrease; if (increases != null && increases.Length > 1) { increaseRate = increases[2] / 100; } } if (increaseRate > 0) { string extraAddRate = UIHelper.AppendColor(TextColType.Green, StringUtility.Contact("+", increaseRate, "%"), true); successRateText.text = Language.Get("HallowsWin_Success", StringUtility.Contact(successRate / 100, "%", extraAddRate)); } else { successRateText.text = Language.Get("HallowsWin_Success", StringUtility.Contact(successRate / 100, "%")); } } private void OnClickOnekeyPutBtn() { if (compoundModel == null) return; var unfixedSelectItemDict = selectModel.GetUnfixedItemModel(); var unfixeAddDict = selectModel.GetHaveUnfixedSelectItem(); int[] unfixedDisplays = compoundModel.unfixedItemDisplay; var fixedAndUnfixeds = compoundModel.unfixedItemCount > 3 ? five_FixedAndUnfixeds : three_FixedAndUnfixeds; List unSelectItems = unfixedSelectItemDict.Values.ToList(); unSelectItems.Sort(CompareByOverdueTime); for (int i = 0; i < unfixedDisplays.Length; i++) { var unfixedDisplay = unfixedDisplays[i]; if (unfixedDisplay != 0) { var matCell = fixedAndUnfixeds[unfixedDisplay - 1]; if (matCell.itemModel == null) { for (int j = 0; j < unSelectItems.Count; j++) { ItemModel _model = unSelectItems[j]; if (!unfixeAddDict.ContainsKey(_model.gridIndex)) { selectModel.AddHaveUnfixedSelectItem(_model.gridIndex); UpdateSelect(matCell, _model.gridIndex, SelectItemType.unfixed); break; } } } } } } private int CompareByOverdueTime(ItemModel start, ItemModel end) { int remainTime1 = 0; int remainTime2 = 0; bool isRemain1 = TryGetRemainTime(start, out remainTime1); bool isRemain2 = TryGetRemainTime(end, out remainTime2); if (isRemain1.CompareTo(isRemain2) != 0) return -isRemain1.CompareTo(isRemain2); if (remainTime1.CompareTo(remainTime2) != 0) return -remainTime1.CompareTo(remainTime2); return 0; } private bool TryGetRemainTime(ItemModel model, out int seconds) { seconds = 0; if (model.config.EquipPlace != (int)RoleEquipType.Guard1 || model.config.ExpireTime <= 0) return false; ItemCDCool cool = KnapsackTimeCDMgr.Instance.GetItemCoolById(model.guid); bool isShow = model.config.ExpireTime > 0; if (isShow) { List itemEffectTime = model.GetUseData((int)ItemUseDataKey.createTime); seconds = model.config.ExpireTime; if (itemEffectTime != null && itemEffectTime[0] != 0 && model.guid != "") { seconds = cool == null ? 0 : (int)cool.GetRemainTime(); } return true; } return false; } private void UpdateSendCompose() { if (compoundModel == null) return; SetFixedItemIndexDic(); composeWinModel.SendComposeRequest(compoundModel, fixedItemIndexDict, GetPlayUIEffct(), 1, 10000, isIncreaseRate); } private void OnClickComposeBtn() { if (compoundModel != null) { bool isTrailer = composeWinModel.IsTrailerByLevel(compoundModel); if (isTrailer) { ServerTipDetails.DisplayNormalTip(Language.Get("FuncOpenLv", compoundModel.levelNeed)); return; } } ItemConfig itemConfig = ItemConfig.Get(compoundModel.makeID[0]); switch ((RoleEquipType)itemConfig.EquipPlace) { case RoleEquipType.Guard1: if (!composeWinModel.IsEnoughUnfixedMat(compoundModel)) { ServerTipDetails.DisplayNormalTip(Language.Get("Compose101")); } else { WindowCenter.Instance.Open(); } break; default: SetFixedItemIndexDic(); composeWinModel.SendComposeRequest(compoundModel, fixedItemIndexDict, GetPlayUIEffct(), 1, 10000, isIncreaseRate); break; } } private void SetFixedItemIndexDic() { fixedItemIndexDict.Clear(); if (compoundModel == null) return; var packType = composeWinModel.GetPackTypeByMakerId(compoundModel.makeID); SinglePack singlePack = playerPack.GetSinglePack(packType); if (singlePack == null) return; int[] fixedIds = compoundModel.itemID; int[] fixedCounts = compoundModel.itemCount; for (int i = 0; i < fixedIds.Length; i++) { var itemIndexlist = singlePack.GetItemIndexsAppointedCount(fixedIds[i], fixedCounts[i]); if (itemIndexlist != null) { fixedItemIndexDict.Add(fixedIds[i], itemIndexlist); } } } private UIEffect GetPlayUIEffct() { if (compoundModel == null) return null; if (compoundModel.unfixedItemCount <= 3) { return composeEffect_Three; } return composeEffect_Five; } private void OnComposeAnswer(HA814_tagMCMakeItemAnswer answer) { if (answer.MakeType != (int)MakeType.ItemCompound) return; if (answer.Result == 1) { successEffect.Play(); } else { failEffect.Play(); } RefreshUI(compoundModel); selectModel.ClearSelectModel(); } private void OnDownUp(GameObject go) { WindowCenter.Instance.Close(); } private void OnClickHelpBtn(GameObject go) { if (compoundModel != null) { WindowCenter.Instance.Open(); } } } }