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

116 lines
4.0 KiB
C#

using System.Collections;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.Character;
using Gameplay.Common;
namespace Gameplay.Level
{
public partial class Level
{
private void RegisterAllEvent()
{
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_DEAD, _OnUnitDead);
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_REMOVE, _OnUnitRmove);
EventManager.Instance.Register<EDArriveBlock>(EventManager.EventName.LevelCharacterArriveBlock, OnCharacterArriveBlock);
EventManager.Instance.Register<GameUnit>(EventManager.EventName.LevelUnitBorn, _OnUnitBorn);
}
private void UnRegisterAllEvent()
{
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.LevelUnitBorn, _OnUnitBorn);
EventManager.Instance.Unregister<EDArriveBlock>(EventManager.EventName.LevelCharacterArriveBlock, OnCharacterArriveBlock);
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_REMOVE, _OnUnitRmove);
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_DEAD, _OnUnitDead);
}
private void _OnUnitDead(GameUnit unit)
{
var level = LevelManager.Instance.CurrentLevel;
if (level == null) return;
if (level.selectUnit == unit)
{
level.UnSelectUnit();
}
}
private void _OnUnitRmove(GameUnit unit)
{
switch (unit.gameunitType)
{
case EGameUnitType.Character:
OnCharacterDeadRemove(unit as Character.Character);
break;
}
}
private void OnCharacterDeadRemove(Character.Character character)
{
StarGoalOnCharacterDeadRemove(character);
if (CheckIsFail(out var failCondition))
{
OnLevelFail(failCondition);
}
else if (CheckIsSuccess(out var goal))
{
OnStageSuccess(goal);
}
}
private void OnCharacterArriveBlock(EDArriveBlock edArriveBlock)
{
if (CheckIsSuccess(out var goal))
{
OnStageSuccess(goal);
}
}
private void _OnUnitBorn(GameUnit unit)
{
if (unit.gameunitType == EGameUnitType.Character)
{
OnCharacterBorn(unit as Character.Character);
}
}
private void OnCharacterBorn(Character.Character character)
{
StarGoalOnCharacterBorn(character);
}
private async void OnLevelFail(LevelData.FailCondition failCondition)
{
await ChangeStateNextFrame(new EndState(this));
DebugUtil.LogY($"level fail, reason : {failCondition.failType}");
EventManager.Instance.Send(EventManager.EventName.LevelFail, failCondition);
}
private async UniTask OnStageSuccess(LevelData.Goal goal)
{
_currentStageIndex++;
StarGoalManualUpdate();
DebugUtil.LogY($"stage success, next stage index : {_currentStageIndex}");
if (IsAllStageFinished())
{
await ChangeStateNextFrame(new EndState(this));
DebugUtil.LogY($"level success");
}
EventManager.Instance.Send(IsAllStageFinished()
? EventManager.EventName.LevelComplete
: EventManager.EventName.LevelStageSuccess, goal);
}
private bool IsAllStageFinished()
{
return _currentStageIndex >= _levelData.stages.Count;
}
private async UniTask ChangeStateNextFrame(LevelBaseState state)
{
await UniTask.NextFrame();
_stateMachine.ChangeState(state);
}
}
}