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

273 lines
9.2 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 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;
/// <summary>
/// 剧情加载完成后是否立即播放
/// </summary>
private bool isPlay;
private object _levelParam;
private Action _enterLevelFinishCallBack;
private Action _exitLevelFinishCallBack;
/// <summary>
/// 退出关卡是否切换主场景回调
/// </summary>
public Func<bool> ExitLevelChangeMainFunc;
private int _bgmID;
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<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)
{
_EnterLevelDirectly(_levelIDTryToEnter);
}
else
Debug.LogError($"levelID 不合法,无法进入关卡,levelID:{_levelIDTryToEnter}");
_levelIDTryToEnter = NO_LEVEL_FLAG;
}
private async void OnServerResponseExitLevel(ItemList itemList)
{
if (!IsInLevel)
{
Debug.LogError("当前未处于关卡中");
return;
}
if (CurrLevel.IsStory)
{
ExitLevelDirectly(_exitLevelFinishCallBack);
_exitLevelFinishCallBack = null;
}
else
{
StopBGM();
if (IsSuccess)
{
// CommonUtils.CaptureScreenshot();
// UIManager.Instance.OpenWindow(UINameConst.UIMissionWin, itemList).Forget();
await ShowTransitionMgr.Instance.ShowLevelTransition(UI_LevelTransitionController.OpenType.Success);
await SettleSceneProcess(itemList);
}
else
{
await ShowTransitionMgr.Instance.ShowLevelTransition(UI_LevelTransitionController.OpenType.Fail);
if (!UIManager.Instance.IsWindowOpening(UINameConst.UIDefeatGame))
{
UIDefeatGameController.Open().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 + "", isPlay));
else
GameStateManager.Instance.ChangeState(new GameStateLevel(levelID));
CurrLevel = levelInfo;
IsInLevel = true;
_enterLevelFinishCallBack?.Invoke();
_enterLevelFinishCallBack = null;
if (!levelInfo.IsStory)
PlayBGM();
}
public void _ExitLevelDirectly()
{
if (!CurrLevel.IsStory)
TeamManager.Instance.Release();
var levelID = CurrLevel.ID;
var isPVP = false;
if (CurrentLevel != null)
{
isPVP = CurrentLevel.IsPvPLevel;
}
CurrLevel = null;
IsInLevel = false;
if (null != ExitLevelChangeMainFunc && !ExitLevelChangeMainFunc.Invoke())
EventManager.Instance.Send(EventManager.EventName.GuideExitLevel);
else
{
if (isPVP)
GameStateManager.Instance.ChangeState(new GameStateMainScene());
// GameStateManager.Instance.ChangeState(new GameStateMainScene(
// () => { JumpToManager.JumpFunc(cfg.JumpFuncType.PVP_Main); }));
else
GameStateManager.Instance.ChangeState(new GameStateMainScene(false,
() => { JumpToManager.JumpFunc(cfg.JumpFuncType.Level_Select, levelID); }));
}
ExitLevelChangeMainFunc = null;
_exitLevelFinishCallBack?.Invoke();
_exitLevelFinishCallBack = null;
_cacheSettleData = null;
}
#region API
/// <summary>
/// 向服务器发送消息准备进入关卡
/// </summary>
/// <param name="id"></param>
/// <param name="finishCallback"></param>
/// <param name="param"></param>
/// <param name="isPlay">剧情加载完成后是否立即播放</param>
public void TryEnterLevel(int id, Action finishCallback = null, object param = null, bool isPlay = true)
{
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;
this.isPlay = isPlay;
}
//向服务器发送消息退出关卡
public void TryExitLevel(bool isSuccess, Action finishCallback = null)
{
if (!IsInLevel)
{
Debug.LogError("当前未处于关卡中");
return;
}
var StarResult = CurrentLevel?.StarResult ?? new List<uint>();
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);
var levelInfo = GetLevelInfoByID(levelID);
if (!levelInfo.IsStory)
PlayBGM();
}
//直接退出关卡,不走服务器
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}");
}
public async void PlayBGM()
{
//_bgmID = await AudioManager.Instance.PlayAudio("NLD_BGM_01");
_bgmID = AudioManager.Instance.PlayAudio("bgm_01");
}
public void StopBGM()
{
AudioManager.Instance.AudioMgrObj.StopMusicById(_bgmID);
}
#endregion API
}
}