190 lines
5.3 KiB
C#
190 lines
5.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Cysharp.Threading.Tasks;
|
|
using Gameplay.Character;
|
|
using Gameplay.Common;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Unit.Data;
|
|
|
|
namespace Gameplay.Level
|
|
{
|
|
public partial class Level
|
|
{
|
|
const int INVALID_STAGE_INDEX = -1;
|
|
private const int STAR_TARGET_COUNT = 3;
|
|
|
|
private readonly bool[] _starTargets = new bool[3];
|
|
|
|
public int currentStageIndex { get; private set; } = INVALID_STAGE_INDEX;
|
|
|
|
public int StarCount => _starTargets.Count(isSuccess => isSuccess);
|
|
|
|
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)
|
|
{
|
|
// TODO: 临时处理,后续需要修改
|
|
SetPause(true);
|
|
|
|
await ChangeStateNextFrame(new EndState(this));
|
|
if (!IsPvPLevel)
|
|
LevelManager.Instance.TryExitLevel(isSuccess);
|
|
else
|
|
{
|
|
PVPManager.Instance.FinishPVPFight(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)
|
|
// {
|
|
// var allInfightUnits = GameUnitManager.instance.infightUnits;
|
|
// for (int i = 0; i < allInfightUnits.count; i++)
|
|
// {
|
|
// var unit = allInfightUnits.GetByIndex(i);
|
|
// if (unit.commonData.troopId != troopId)
|
|
// {
|
|
// continue;
|
|
// }
|
|
//
|
|
// if (unit.gameunitType == EGameUnitType.Character)
|
|
// {
|
|
// return false;
|
|
// }
|
|
//
|
|
// if (unit.gameunitType == EGameUnitType.Vehicle)
|
|
// {
|
|
// if (!unit.statusData.isDead)
|
|
// {
|
|
// return false;
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// return true;
|
|
// }
|
|
|
|
public int GetFactionCount(cfg.CharacterCfg.Efaction faction, int troopId)
|
|
{
|
|
return GameUnitManager.instance.characterManager.FindAll(c =>
|
|
c.troopId == troopId && c.runtimeData.configData.faction == faction).Count;
|
|
}
|
|
|
|
public int GetJobCount(cfg.CharacterCfg.Ejob job, int troopId)
|
|
{
|
|
return GameUnitManager.instance.characterManager.FindAll(c =>
|
|
c.troopId == troopId && c.runtimeData.configData.job == job).Count;
|
|
}
|
|
|
|
public int GetRarityCount(int rarity, int troopId)
|
|
{
|
|
return GameUnitManager.instance.characterManager.FindAll(c =>
|
|
c.troopId == troopId && (int)c.runtimeData.configData.rarity == rarity).Count;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
private bool IsAllStageFinished()
|
|
{
|
|
return currentStageIndex >= _levelData.stages.Count - 1;
|
|
}
|
|
|
|
private async UniTask ChangeStateNextFrame(LevelBaseState state)
|
|
{
|
|
await UniTask.NextFrame();
|
|
_stateMachine.ChangeState(state);
|
|
}
|
|
}
|
|
} |