112 lines
2.9 KiB
C#
112 lines
2.9 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using Gameplay.Character;
|
|
using Gameplay.Common;
|
|
|
|
namespace Gameplay.Level
|
|
{
|
|
public partial class Level
|
|
{
|
|
const int INVALID_STAGE_INDEX = -1;
|
|
|
|
public int currentStageIndex { get; private set; } = INVALID_STAGE_INDEX;
|
|
|
|
public LevelData.Stage CurrentStage =>
|
|
CheckStageIndex(currentStageIndex) ? _levelData.stages[currentStageIndex] : null;
|
|
|
|
public void InitRules()
|
|
{
|
|
currentStageIndex = INVALID_STAGE_INDEX;
|
|
InitStarConditions();
|
|
EnterNextStage();
|
|
}
|
|
|
|
public void ReleaseRules()
|
|
{
|
|
ReleaseStarConditions();
|
|
ExitStage();
|
|
}
|
|
|
|
public void EnterNextStage()
|
|
{
|
|
var nextStageIndex = currentStageIndex + 1;
|
|
EnterStage(nextStageIndex);
|
|
}
|
|
|
|
public void EnterStage(int stageIndex)
|
|
{
|
|
if (!CheckStageIndex(stageIndex))
|
|
return;
|
|
if (currentStageIndex != INVALID_STAGE_INDEX)
|
|
{
|
|
ExitStage();
|
|
}
|
|
|
|
currentStageIndex = stageIndex;
|
|
InitStageConditions();
|
|
}
|
|
|
|
public void ExitStage()
|
|
{
|
|
if (CheckStageIndex(currentStageIndex))
|
|
{
|
|
currentStageIndex = INVALID_STAGE_INDEX;
|
|
ReleaseStageConditions();
|
|
}
|
|
}
|
|
|
|
public async void TryExitLevel(bool isSuccess)
|
|
{
|
|
await ChangeStateNextFrame(new EndState(this));
|
|
LevelManager.Instance.TryExitLevel(isSuccess);
|
|
DebugUtil.LogY($"exit level,result:{isSuccess}");
|
|
}
|
|
|
|
private void OnStageSuccess()
|
|
{
|
|
if (IsAllStageFinished())
|
|
{
|
|
TryExitLevel(true);
|
|
}
|
|
else
|
|
{
|
|
EnterNextStage();
|
|
}
|
|
}
|
|
|
|
private void OnStageFailed()
|
|
{
|
|
TryExitLevel(false);
|
|
}
|
|
|
|
private bool CheckStageIndex(int index)
|
|
{
|
|
return index > INVALID_STAGE_INDEX && index < _levelData.stages.Count;
|
|
}
|
|
|
|
public static bool IsEnemy(GameUnit character1, GameUnit character2)
|
|
{
|
|
return IsEnemy(character1.commonData.troopId, character2.commonData.troopId);
|
|
}
|
|
|
|
public static bool IsEnemy(int troop1, int troop2)
|
|
{
|
|
return troop1 != troop2;
|
|
}
|
|
|
|
public bool IsAllDead(int troopId)
|
|
{
|
|
return _characterManager.FindOne(c => c.troopId == troopId && !c.statusData.isDead) == null;
|
|
}
|
|
|
|
private bool IsAllStageFinished()
|
|
{
|
|
return currentStageIndex >= _levelData.stages.Count - 1;
|
|
}
|
|
|
|
private async UniTask ChangeStateNextFrame(LevelBaseState state)
|
|
{
|
|
await UniTask.NextFrame();
|
|
_stateMachine.ChangeState(state);
|
|
}
|
|
}
|
|
} |