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

167 lines
4.6 KiB
C#
Raw Normal View History

2023-11-08 15:05:19 +08:00
using System;
using System.Linq;
2023-10-19 15:00:19 +08:00
using Cysharp.Threading.Tasks;
2023-08-22 19:08:45 +08:00
using Gameplay.Character;
using Gameplay.Common;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
2023-08-22 19:08:45 +08:00
namespace Gameplay.Level
{
public partial class Level
{
2023-11-06 18:20:55 +08:00
const int INVALID_STAGE_INDEX = -1;
2023-11-08 15:05:19 +08:00
private const int STAR_TARGET_COUNT = 3;
private readonly bool[] _starTargets = new bool[3];
2023-08-22 19:08:45 +08:00
2023-11-06 18:20:55 +08:00
public int currentStageIndex { get; private set; } = INVALID_STAGE_INDEX;
2023-11-08 15:05:19 +08:00
public int StarCount => _starTargets.Count(isSuccess => isSuccess);
2023-11-06 18:20:55 +08:00
public LevelData.Stage CurrentStage =>
CheckStageIndex(currentStageIndex) ? _levelData.stages[currentStageIndex] : null;
public void InitRules()
2023-08-22 19:08:45 +08:00
{
2023-11-07 12:17:14 +08:00
currentStageIndex = INVALID_STAGE_INDEX;
2023-11-06 18:20:55 +08:00
InitStarConditions();
EnterNextStage();
2023-08-22 19:08:45 +08:00
}
2023-11-06 18:20:55 +08:00
public void ReleaseRules()
2023-08-22 19:08:45 +08:00
{
2023-11-06 18:20:55 +08:00
ReleaseStarConditions();
ExitStage();
2023-08-22 19:08:45 +08:00
}
2023-11-06 18:20:55 +08:00
public void EnterNextStage()
2023-08-22 19:08:45 +08:00
{
2023-11-06 18:20:55 +08:00
var nextStageIndex = currentStageIndex + 1;
EnterStage(nextStageIndex);
2023-08-22 19:08:45 +08:00
}
2023-11-06 18:20:55 +08:00
public void EnterStage(int stageIndex)
{
if (!CheckStageIndex(stageIndex))
return;
if (currentStageIndex != INVALID_STAGE_INDEX)
{
ExitStage();
}
2023-08-22 19:08:45 +08:00
2023-11-06 18:20:55 +08:00
currentStageIndex = stageIndex;
InitStageConditions();
}
2023-08-22 19:08:45 +08:00
2023-11-06 18:20:55 +08:00
public void ExitStage()
2023-08-22 19:08:45 +08:00
{
2023-11-06 18:20:55 +08:00
if (CheckStageIndex(currentStageIndex))
2023-08-22 19:08:45 +08:00
{
2023-11-06 18:20:55 +08:00
currentStageIndex = INVALID_STAGE_INDEX;
ReleaseStageConditions();
2023-08-22 19:08:45 +08:00
}
}
2023-11-06 18:20:55 +08:00
public async void TryExitLevel(bool isSuccess)
2023-08-22 19:08:45 +08:00
{
// TODO: 临时处理,后续需要修改
SetPause(true);
2023-11-06 18:20:55 +08:00
await ChangeStateNextFrame(new EndState(this));
LevelManager.Instance.TryExitLevel(isSuccess);
DebugUtil.LogY($"exit level,result:{isSuccess}");
2023-08-22 19:08:45 +08:00
}
2023-11-08 12:59:16 +08:00
2023-11-06 18:20:55 +08:00
private void OnStageSuccess()
2023-08-22 19:08:45 +08:00
{
2023-11-06 18:20:55 +08:00
if (IsAllStageFinished())
2023-08-22 19:08:45 +08:00
{
2023-11-06 18:20:55 +08:00
TryExitLevel(true);
}
else
{
EnterNextStage();
2023-08-22 19:08:45 +08:00
}
}
2023-10-19 15:00:19 +08:00
2023-11-06 18:20:55 +08:00
private void OnStageFailed()
2023-10-19 15:00:19 +08:00
{
2023-11-06 18:20:55 +08:00
TryExitLevel(false);
2023-10-19 15:00:19 +08:00
}
2023-11-06 18:20:55 +08:00
private bool CheckStageIndex(int index)
2023-10-19 15:00:19 +08:00
{
2023-11-06 18:20:55 +08:00
return index > INVALID_STAGE_INDEX && index < _levelData.stages.Count;
2023-10-19 15:00:19 +08:00
}
2023-11-06 18:20:55 +08:00
public static bool IsEnemy(GameUnit character1, GameUnit character2)
2023-10-19 15:00:19 +08:00
{
2023-11-06 18:20:55 +08:00
return IsEnemy(character1.commonData.troopId, character2.commonData.troopId);
}
2023-10-19 15:00:19 +08:00
2023-11-06 18:20:55 +08:00
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;
}
2023-11-08 12:59:16 +08:00
public bool IsAnyOneDead(int troopId)
{
return _characterManager.FindOne(c => c.troopId == troopId && c.statusData.isDead) != null;
}
public int GetFactionCount(cfg.CharacterCfg.Efaction faction, int troopId)
{
return _characterManager.FindAll(c =>
c.troopId == troopId && c.runtimeData.configData.faction == faction).Count;
}
public int GetJobCount(cfg.CharacterCfg.Ejob job, int troopId)
{
return _characterManager.FindAll(c =>
c.troopId == troopId && c.runtimeData.configData.job == job).Count;
}
public int GetRarityCount(int rarity, int troopId)
{
return _characterManager.FindAll(c =>
c.troopId == troopId && (int)c.runtimeData.configData.rarity == rarity).Count;
}
2023-11-08 15:05:19 +08:00
public string GetCurrStageSuccessConditionDesc()
{
if (CheckStageIndex(currentStageIndex))
{
return CurrentStage.successCondition.GetConditionLocalization();
}
return null;
}
public bool GetStarIsSuccess(int index)
{
return _starTargets[index];
}
public string GetStarTargetLocalization(int index)
{
return _levelData.starTarget[index].GetConditionLocalization();
}
2023-11-06 18:20:55 +08:00
private bool IsAllStageFinished()
{
return currentStageIndex >= _levelData.stages.Count - 1;
}
2023-11-08 12:59:16 +08:00
2023-11-06 18:20:55 +08:00
private async UniTask ChangeStateNextFrame(LevelBaseState state)
{
await UniTask.NextFrame();
_stateMachine.ChangeState(state);
2023-10-19 15:00:19 +08:00
}
2023-08-22 19:08:45 +08:00
}
}