using Framework; using Gameplay.Net; using System; using System.Collections.Generic; using Code.Scripts.Gameplay.Game; using Cysharp.Threading.Tasks; using UnityEngine; using Debug = DebugUtil; namespace Gameplay.Level { public partial class LevelManager { //todo 修改服务器回消息底层逻辑后处理进入关卡失败的逻辑 const int NO_LEVEL_FLAG = -1; private int _levelIDTryToEnter = NO_LEVEL_FLAG; private object _levelParam; private Action _enterLevelFinishCallBack; private Action _exitLevelFinishCallBack; public bool IsInLevel { get; private set; } public bool IsSuccess { get; private set; } public LevelInfo CurrLevel { get; private set; } private void InitAction() { IsInLevel = false; CurrLevel = null; RegisterEventAction(); } private void ReleaseAction() { UnregisterEventAction(); _enterLevelFinishCallBack = null; _exitLevelFinishCallBack = null; _levelParam = null; } private void RegisterEventAction() { EventManager.Instance.Register(EventManager.EventName.ServerResponseEnterLevel, OnServerResponseEnterLevel); EventManager.Instance.Register(EventManager.EventName.ServerResponseExitLevel, OnServerResponseExitLevel); } private void UnregisterEventAction() { EventManager.Instance.Unregister(EventManager.EventName.ServerResponseEnterLevel, OnServerResponseEnterLevel); EventManager.Instance.Unregister(EventManager.EventName.ServerResponseExitLevel, OnServerResponseExitLevel); } //服务器回消息,进入关卡 private void OnServerResponseEnterLevel() { if (_levelIDTryToEnter != NO_LEVEL_FLAG) { _EnterLevelDirectly(_levelIDTryToEnter); } else Debug.LogError($"levelID 不合法,无法进入关卡,levelID:{_levelIDTryToEnter}"); _levelIDTryToEnter = NO_LEVEL_FLAG; } private void OnServerResponseExitLevel(ItemList itemList) { if (!IsInLevel) { Debug.LogError("当前未处于关卡中"); return; } if (CurrLevel.IsStory) { ExitLevelDirectly(_exitLevelFinishCallBack); _exitLevelFinishCallBack = null; } else { if (IsSuccess) { CommonUtils.CaptureScreenshot(); UIManager.Instance.OpenWindow(UINameConst.UIMissionWin, itemList).Forget(); } else { if (!UIManager.Instance.IsWindowOpening(UINameConst.UIDefeatGame)) { CommonUtils.CaptureScreenshot(); UIManager.Instance.OpenWindow(UINameConst.UIDefeatGame).Forget(); } } } SaveFightLevel(CurrLevel); } private void _EnterLevelDirectly(int levelID) { var levelInfo = Instance.GetLevelInfoByID(levelID); if (levelInfo == null) { Debug.LogError($"关卡不存在,id:{levelID}"); return; } if (levelInfo.IsStory) GameStateManager.Instance.ChangeState(new GameStateStory(levelInfo.StoryID + "")); else GameStateManager.Instance.ChangeState(new GameStateLevel(levelID)); CurrLevel = levelInfo; IsInLevel = true; _enterLevelFinishCallBack?.Invoke(); _enterLevelFinishCallBack = null; } public void _ExitLevelDirectly() { if (!CurrLevel.IsStory) TeamManager.Instance.Release(); CurrLevel = null; IsInLevel = false; GameStateManager.Instance.ChangeState(new GameStateMainScene()); _exitLevelFinishCallBack?.Invoke(); _exitLevelFinishCallBack = null; _cacheSettleData = null; } #region API //向服务器发送消息准备进入关卡 public void TryEnterLevel(int id, Action finishCallback = null, object param = null) { if (IsInLevel) { Debug.LogError($"进入关卡失败!当前正处于关卡中!当前关卡ID:{CurrLevel.ID}"); return; } var levelInfo = GetLevelInfoByID(id); if (levelInfo == null) { Debug.LogError($"关卡不存在,id:{id}"); return; } if (!levelInfo.CanEnter) { Debug.LogError($"关卡无法进入,id:{id}"); return; } var team = TeamEditManager.Instance.GetSelectTeam(); _cacheSettleData = new LevelSettleData(id, team); NetHelper.EnterLevel((uint)id, 0); _enterLevelFinishCallBack = finishCallback; _levelParam = param; _levelIDTryToEnter = id; } //向服务器发送消息退出关卡 public void TryExitLevel(bool isSuccess, Action finishCallback = null) { if (!IsInLevel) { Debug.LogError("当前未处于关卡中"); return; } var StarResult = CurrentLevel?.StarResult ?? new List(); IsSuccess = isSuccess; NetHelper.ExitLevel(isSuccess, StarResult); _exitLevelFinishCallBack = finishCallback; } //直接进入关卡,不通过服务器 public void EnterLevelDirectly(int levelID, Action enterLevelFinishCallBack = null, object param = null) { _enterLevelFinishCallBack = enterLevelFinishCallBack; _levelParam = param; _EnterLevelDirectly(levelID); } //直接退出关卡,不走服务器 public void ExitLevelDirectly(Action exitLevelFinishCallBack = null) { _exitLevelFinishCallBack = exitLevelFinishCallBack; _ExitLevelDirectly(); } public void GetStarReward(int levelGroupID, int index) { if (_levelGroupDic.TryGetValue(levelGroupID, out var chapterInfo)) { if (chapterInfo.CanGetReward(index)) { DebugUtil.Log($"[Level] 发送领取章节奖励!章节ID{levelGroupID} index:{index}"); NetHelper.LevelGroupReward((uint)levelGroupID, (uint)index); return; } } Debug.LogError($"无法领取章节奖励,chapter ID:{levelGroupID} index:{index}"); } #endregion API } }