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

273 lines
8.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 System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.Unit;
namespace Gameplay.Level
{
public partial class Level
{
/// <summary>
/// 初始阶段索引
/// </summary>
const int INVALID_STAGE_INDEX = -1;
private const int STAR_TARGET_COUNT = 3;
/// <summary>
/// 星级使用前3位战役使用15位
/// </summary>
private readonly bool[] _starTargets = new bool[15];
public int currentStageIndex { get; private set; } = INVALID_STAGE_INDEX;
public int StarCount => _starTargets.Count(isSuccess => isSuccess);
public int Score
{
get
{
int score = 0;
if (null == LevelManager.Instance.CurrLevel)
return score;
for (int i = 0; i < LevelManager.Instance.CurrLevel.LevelData.scoreTarget.Count; ++i)
{
if (i >= _starTargets.Length)
break;
if (_starTargets[i])
score += LevelManager.Instance.CurrLevel.LevelData.scoreTarget[i].score;
}
return score;
}
}
//index数组如完成了13星级条件则返回[1,3]
public List<uint> StarResult
{
get
{
List<uint> result = new();
for (int i = 0; i < _starTargets.Length; i++)
{
if(_starTargets[i])
result.Add((uint)i+1);
}
return result;
}
}
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()
{
EnterStage(currentStageIndex + 1);
}
/// <summary>
/// 进入阶段
/// </summary>
/// <param name="stageIndex"></param>
public void EnterStage(int stageIndex) // TODO 特殊处理,防止关卡注册事件失败
{
if (!CheckStageIndex(stageIndex))
{
DebugUtil.LogError($"[关卡]进入阶段失败,阶段索引{stageIndex}");
currentStageIndex = 0;
InitStageConditions();
DebugUtil.Log($"[关卡]初始化阶段:{currentStageIndex}");
return;
}
/*if (currentStageIndex != INVALID_STAGE_INDEX)
ExitStage();*/
currentStageIndex = stageIndex;
InitStageConditions();
DebugUtil.Log($"[关卡]初始化阶段:{currentStageIndex}");
}
public void ExitStage()
{
if (CheckStageIndex(currentStageIndex))
{
DebugUtil.LogError("[关卡]退出阶段");
currentStageIndex = INVALID_STAGE_INDEX;
ReleaseStageConditions();
}
}
public async void TryExitLevel(bool isSuccess, bool isGiveup = false)
{
await WaitLevelEnd(isSuccess);
if (isSuccess)
{
EventManager.Instance.Send(EventManager.EventName.LevelStageSuccess);
}
if (!IsPvPLevel)
LevelManager.Instance.TryExitLevel(isSuccess, isGiveup: isGiveup);//在这里进行关卡打点事件记录
else
{
PVPManager.Instance.FinishPVPFight(isSuccess);
}
DebugUtil.LogY($"exit level,result:{isSuccess}");
}
private async void OnStageSuccess()
{
DebugUtil.Log($"[关卡]第{currentStageIndex + 1}阶段胜利");
if (IsAllStageFinished())
{
DebugUtil.Log("[关卡]关卡胜利");
if (cfg.ConditionType.CombatFallBack == CurrentStage.successCondition.conditionType) // 特殊处理撤离胜利后等2秒结算
await UniTask.WaitForSeconds(2f);
TryExitLevel(true);//进行通关打点
}
else
{
DebugUtil.Log($"[关卡]进入下一阶段");
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();
}
public string GetScoreTargetLocalization(int index)
{
return _levelData.scoreTarget[index].GetConditionLocalization();
}
public int GetScoreTargetScore(int index)
{
return _levelData.scoreTarget[index].score;
}
private bool IsAllStageFinished()
{
return currentStageIndex >= _levelData.stages.Count - 1;
}
public async UniTask WaitLevelEnd(bool isSuccess)
{
DebugUtil.LogY("TryEndLevel");
if (isSuccess)
{
_fsm.SetForceChangeState(ELevelState.EndStory);
}
else
{
_fsm.SetForceChangeState(ELevelState.End);
}
while (_fsm.currentState.id != ELevelState.End)
{
await UniTask.NextFrame();
}
DebugUtil.LogY("EndLevel");
}
}
}