141 lines
4.7 KiB
C#
141 lines
4.7 KiB
C#
using Framework;
|
||
using Gameplay.Net;
|
||
using System;
|
||
using Debug = DebugUtil;
|
||
|
||
namespace Gameplay.Level
|
||
{
|
||
public partial class LevelManager
|
||
{
|
||
//todo 修改服务器回消息底层逻辑后处理进入关卡失败的逻辑
|
||
|
||
const int NO_LEVEL_FLAG = -1;
|
||
|
||
private int _levelIDTryToEnter = NO_LEVEL_FLAG;
|
||
private Action _enterLevelFinishCallBack;
|
||
private Action _exitLevelFinishCallBack;
|
||
public bool IsSuccess { get; private set; }
|
||
|
||
private void InitAction()
|
||
{
|
||
RegisterEventAction();
|
||
}
|
||
|
||
private void ReleaseAction()
|
||
{
|
||
UnregisterEventAction();
|
||
_enterLevelFinishCallBack = null;
|
||
_exitLevelFinishCallBack = null;
|
||
}
|
||
|
||
private void RegisterEventAction()
|
||
{
|
||
EventManager.Instance.Register(EventManager.EventName.ServerResponseEnterLevel, OnServerResponseEnterLevel);
|
||
EventManager.Instance.Register<ItemList>(EventManager.EventName.ServerResponseExitLevel, OnServerResponseExitLevel);
|
||
}
|
||
|
||
private void UnregisterEventAction()
|
||
{
|
||
EventManager.Instance.Unregister(EventManager.EventName.ServerResponseEnterLevel, OnServerResponseEnterLevel);
|
||
EventManager.Instance.Unregister<ItemList>(EventManager.EventName.ServerResponseExitLevel, OnServerResponseExitLevel);
|
||
}
|
||
|
||
//服务器回消息,进入关卡
|
||
private void OnServerResponseEnterLevel()
|
||
{
|
||
if (_levelIDTryToEnter != NO_LEVEL_FLAG)
|
||
{
|
||
_enterLevelFinishCallBack?.Invoke();
|
||
_enterLevelFinishCallBack = null;
|
||
GameStateManager.Instance.ChangeState(new GameStateLevel(_levelIDTryToEnter));
|
||
}
|
||
else
|
||
Debug.LogError($"levelID 不合法,无法进入关卡,levelID:{_levelIDTryToEnter}");
|
||
_levelIDTryToEnter = NO_LEVEL_FLAG;
|
||
}
|
||
|
||
private void OnServerResponseExitLevel(ItemList itemList)
|
||
{
|
||
if (CurrentLevel == null)
|
||
{
|
||
Debug.LogError("当前未处于关卡中");
|
||
return;
|
||
}
|
||
if (IsSuccess)
|
||
{
|
||
_ = UIManager.Instance.OpenWindow(UINameConst.UIPassGame, itemList);
|
||
}
|
||
else
|
||
{
|
||
if (!UIManager.Instance.IsWindowOpening(UINameConst.UIDefeatGame))
|
||
{
|
||
_ = UIManager.Instance.OpenWindow(UINameConst.UIDefeatGame);
|
||
}
|
||
}
|
||
_exitLevelFinishCallBack?.Invoke();
|
||
_exitLevelFinishCallBack = null;
|
||
}
|
||
|
||
#region API
|
||
//向服务器发送消息准备进入关卡
|
||
public void TryEnterLevel(int id, Action finishCallback = null)
|
||
{
|
||
if (CurrentLevel != null)
|
||
{
|
||
Debug.LogError($"进入关卡失败!当前正处于关卡中!当前关卡ID:{CurrentLevel.ID}");
|
||
return;
|
||
}
|
||
var levelInfo = GetLevelInfoByID(id);
|
||
if (levelInfo == null)
|
||
{
|
||
return;
|
||
}
|
||
if (!levelInfo.CanEnter)
|
||
{
|
||
Debug.LogError($"关卡无法进入,id:{id}");
|
||
return;
|
||
}
|
||
NetHelper.EnterLevel((uint)id, 0);
|
||
_enterLevelFinishCallBack = finishCallback;
|
||
_levelIDTryToEnter = id;
|
||
}
|
||
|
||
//向服务器发送消息退出关卡
|
||
public void TryExitLevel(bool isSuccess, Action finishCallback = null)
|
||
{
|
||
if (CurrentLevel == null)
|
||
{
|
||
Debug.LogError("当前未处于关卡中");
|
||
return;
|
||
}
|
||
|
||
var starCount = CurrentLevel.StarCount;
|
||
IsSuccess = isSuccess;
|
||
NetHelper.ExitLevel(isSuccess, (uint)starCount);
|
||
_exitLevelFinishCallBack = finishCallback;
|
||
}
|
||
|
||
public void ExitLevelDirectly()
|
||
{
|
||
TeamManager.Instance.Release();
|
||
GameStateManager.Instance.ChangeState(new GameStateMainScene());
|
||
}
|
||
|
||
public void GetStarReward(int chapterID, int index)
|
||
{
|
||
if (_chapterInfoDic.TryGetValue(chapterID, out var chapterInfo))
|
||
{
|
||
if (chapterInfo.CanGetReward(index))
|
||
{
|
||
DebugUtil.Log($"[Level] 发送领取章节奖励!章节ID{chapterID} index:{index}");
|
||
NetHelper.ChapterReward((uint)chapterID, (uint)index);
|
||
return;
|
||
}
|
||
}
|
||
|
||
Debug.LogError($"无法领取章节奖励,chapter ID:{chapterID} index:{index}");
|
||
}
|
||
|
||
#endregion API
|
||
}
|
||
} |