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

169 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Framework;
using Gameplay.Net;
using System;
using Code.Scripts.Gameplay.Game;
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 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;
}
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)
{
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;
_enterLevelFinishCallBack?.Invoke();
_enterLevelFinishCallBack = null;
}
else
Debug.LogError($"levelID 不合法,无法进入关卡,levelID:{_levelIDTryToEnter}");
_levelIDTryToEnter = NO_LEVEL_FLAG;
}
private void OnServerResponseExitLevel(ItemList itemList)
{
if (!IsInLevel)
{
Debug.LogError("当前未处于关卡中");
return;
}
if (CurrLevel.IsStory)
ExitLevelDirectly();
else
{
if (IsSuccess)
{
_ = UIManager.Instance.OpenWindow(UINameConst.UIPassGame, itemList);
}
else
{
if (!UIManager.Instance.IsWindowOpening(UINameConst.UIDefeatGame))
{
_ = UIManager.Instance.OpenWindow(UINameConst.UIDefeatGame);
}
}
}
}
#region API
//向服务器发送消息准备进入关卡
public void TryEnterLevel(int id, Action finishCallback = null)
{
if (IsInLevel)
{
Debug.LogError($"进入关卡失败!当前正处于关卡中!当前关卡ID{CurrLevel.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 (!IsInLevel)
{
Debug.LogError("当前未处于关卡中");
return;
}
var starCount = CurrentLevel?.StarCount ?? 0;
IsSuccess = isSuccess;
NetHelper.ExitLevel(isSuccess, (uint)starCount);
_exitLevelFinishCallBack = finishCallback;
}
public void ExitLevelDirectly()
{
CurrLevel = null;
IsInLevel = false;
_exitLevelFinishCallBack?.Invoke();
_exitLevelFinishCallBack = null;
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
}
}