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

241 lines
6.9 KiB
C#
Raw Normal View History

2023-11-08 15:05:19 +08:00
using System;
2024-01-30 15:10:12 +08:00
using System.Collections.Generic;
2023-11-08 15:05:19 +08:00
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-12-14 19:00:39 +08:00
using Gameplay.Unit.Data;
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;
2024-09-23 17:40:22 +08:00
/// <summary>
/// 星级使用前3位战役使用15位
/// </summary>
private readonly bool[] _starTargets = new bool[15];
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);
2024-09-23 17:40:22 +08:00
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;
}
}
2024-01-30 15:10:12 +08:00
//index数组如完成了13星级条件则返回[1,3]
public List<uint> StarResult
{
get
{
2024-09-23 17:40:22 +08:00
List<uint> result = new();
2024-01-30 15:10:12 +08:00
for (int i = 0; i < _starTargets.Length; i++)
{
if(_starTargets[i])
result.Add((uint)i+1);
}
return result;
}
}
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
}
}
2024-09-30 13:16:30 +08:00
public async void TryExitLevel(bool isSuccess, bool isGiveup = false)
2023-08-22 19:08:45 +08:00
{
// TODO: 临时处理,后续需要修改
SetPause(true);
2023-11-06 18:20:55 +08:00
await ChangeStateNextFrame(new EndState(this));
2024-01-03 16:40:57 +08:00
if (!IsPvPLevel)
2024-09-30 13:16:30 +08:00
LevelManager.Instance.TryExitLevel(isSuccess, isGiveup: isGiveup);//在这里进行关卡打点事件记录
2024-01-03 16:40:57 +08:00
else
{
PVPManager.Instance.FinishPVPFight(isSuccess);
}
2023-11-06 18:20:55 +08:00
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
{
2024-09-30 13:16:30 +08:00
TryExitLevel(true);//进行通关打点
2023-11-06 18:20:55 +08:00
}
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
{
2024-09-30 13:16:30 +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;
}
2024-01-10 12:57:10 +08:00
// 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;
// }
2023-11-08 12:59:16 +08:00
public int GetFactionCount(cfg.CharacterCfg.Efaction faction, int troopId)
{
2023-12-14 14:42:27 +08:00
return GameUnitManager.instance.characterManager.FindAll(c =>
2023-11-08 12:59:16 +08:00
c.troopId == troopId && c.runtimeData.configData.faction == faction).Count;
}
public int GetJobCount(cfg.CharacterCfg.Ejob job, int troopId)
{
2023-12-14 14:42:27 +08:00
return GameUnitManager.instance.characterManager.FindAll(c =>
2023-11-08 12:59:16 +08:00
c.troopId == troopId && c.runtimeData.configData.job == job).Count;
}
public int GetRarityCount(int rarity, int troopId)
{
2023-12-14 14:42:27 +08:00
return GameUnitManager.instance.characterManager.FindAll(c =>
2023-11-08 12:59:16 +08:00
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();
}
2024-09-24 16:10:22 +08:00
public string GetScoreTargetLocalization(int index)
{
return _levelData.scoreTarget[index].GetConditionLocalization();
}
public int GetScoreTargetScore(int index)
{
return _levelData.scoreTarget[index].score;
}
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
}
}