NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Level/LevelManager.Action.cs

173 lines
5.6 KiB
C#
Raw Normal View History

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