using Snxxz.UI; using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace Snxxz.UI { public class ComposeDogzEquipWin : Window { [SerializeField] ScrollerController cellCtrl; [Header("固定物品和非固定物品")] [SerializeField] List fixedAndUnfixeds = new List(); [Header("合成物品")] [SerializeField] List makeItems = new List(); [SerializeField] Text successRateText; [SerializeField] Button helpBtn; [SerializeField] Button composeBtn; [SerializeField] Button onekeyPutBtn; [SerializeField] GameObject notChooseBG; [SerializeField] GameObject composeEquip; [SerializeField] UIEffect composeEffect; [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; ItemCompoundConfig compoundModel = null; 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; composeWinModel.ResetModelEvent += ResetModel; } protected override void AddListeners() { composeBtn.AddListener(OnClickComposeBtn); onekeyPutBtn.AddListener(OnClickOnekeyPutBtn); } protected override void OnPreOpen() { composeWinModel.UpdateSecondTypeEvent += UpdateSecondType; composeWinModel.UpdateThirdTypeEvent += UpdateThirdType; composeWinModel.UpdateReduceEvent += UpdateReduce; selectModel.UpdateSelectEvent += UpdateSelect; 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.UpdateSecondTypeEvent -= UpdateSecondType; composeWinModel.UpdateThirdTypeEvent -= UpdateThirdType; composeWinModel.UpdateReduceEvent -= UpdateReduce; selectModel.UpdateSelectEvent -= UpdateSelect; DTCA814_tagMCMakeItemAnswer.MakeItemAnswerEvent -= OnComposeAnswer; } protected override void OnAfterClose() { } #region 新的逻辑 private void Display() { composeWinModel.TryGetFirstTypeData((int)ComposeFuncType.MountDogz, out firstTypeData); if (firstTypeData == null) return; successRate = 0; DefaultSelect(); CreateTypeCell(); JumpIndex(); } private void DefaultSelect() { bool isJumpTo = false; if (composeWinModel.secondType != 0) { isJumpTo = true; curSecondType = composeWinModel.secondType; curThirdType = composeWinModel.thirdType; composeWinModel.ResetJumpToModel(); } composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.MountDogz, curSecondType, curThirdType, out thirdTypeData); if (thirdTypeData != null) { RefreshUI(thirdTypeData.itemCompound); if (isJumpTo) { isJumpTo = false; if (!composeWinModel.IsEnoughFixedMat(thirdTypeData.itemCompound) || !composeWinModel.IsEnoughUnfixedMat(thirdTypeData.itemCompound)) { ServerTipDetails.DisplayNormalTip(Language.Get("Compose101")); } else if (!composeWinModel.IsEnoughMoney(thirdTypeData.itemCompound)) { ServerTipDetails.DisplayNormalTip(Language.Get("Z1011")); } } } else { RefreshUI(null); } } 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(); composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.MountDogz, curSecondType, curThirdType, out thirdTypeData); if (thirdTypeData != null) { ChangeUIState(false,true); RefreshUI(thirdTypeData.itemCompound); } else { RefreshUI(null); ChangeUIState(true,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.MountDogz, cell.index, out _secondTypeData); if (_secondTypeData != null) { var thirdTypeDict = _secondTypeData.thirdTypeDict; foreach (var third in thirdTypeDict.Keys) { firstTypeCell.SetDisplay((int)ComposeFuncType.MountDogz, cell.index, third, curSecondType); break; } } break; case ScrollerDataType.Normal: ComposeSecondTypeCell secondTypeCell = cell.GetComponent(); secondTypeCell.SetDisplay((int)ComposeFuncType.MountDogz, 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.MountDogz, 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) { preSecondType = curSecondType; selectModel.ClearSelectModel(); } } private void UpdateThirdType(int thirdType) { composeWinModel.TryGetThirdTypeData((int)ComposeFuncType.MountDogz, curSecondType, thirdType, out thirdTypeData); curThirdType = thirdType; OnClickThirdType(); CreateTypeCell(); } private void OnClickThirdType() { if (thirdTypeData == null) return; if (preThirdType != curThirdType) { preThirdType = curThirdType; } selectModel.ClearSelectModel(); } private void ChangeUIState(params bool[] isShows) { notChooseBG.SetActive(isShows[0]); composeEquip.SetActive(isShows[1]); } 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[] unfixedIds = compoundModel.unfixedItemID; int[] unfixedDisplays = compoundModel.unfixedItemDisplay; 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"))); } } } 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; } } #endregion private void ResetModel() { curSecondType = 0; curThirdType = 0; compoundModel = null; if(!WindowJumpMgr.Instance.IsJumpState) { preSecondType = 0; preThirdType = 0; } } 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() { successRate = 0; if (compoundModel != null) { successRate = compoundModel.successRate; } 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; for (int i = 0; i < unfixedDisplays.Length; i++) { var unfixedDisplay = unfixedDisplays[i]; if (unfixedDisplay != 0) { var matCell = fixedAndUnfixeds[unfixedDisplay - 1]; if (matCell.itemModel == null) { foreach (var index in unfixedSelectItemDict.Keys) { if (!unfixeAddDict.ContainsKey(index)) { selectModel.AddHaveUnfixedSelectItem(index); UpdateSelect(matCell, index, SelectItemType.unfixed); break; } } } } } } private void OnClickComposeBtn() { if (compoundModel == null) return; bool isTrailer = composeWinModel.IsTrailerByLevel(compoundModel); if (isTrailer) { ServerTipDetails.DisplayNormalTip(Language.Get("FuncOpenLv", compoundModel.levelNeed)); return; } composeWinModel.SendComposeRequest(compoundModel,fixedItemIndexDict,composeEffect,1); } 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(); } } } }