241 lines
6.9 KiB
C#
241 lines
6.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
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;
|
||
|
||
/// <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数组,如完成了1,3星级条件,则返回[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()
|
||
{
|
||
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, bool isGiveup = false)
|
||
{
|
||
// TODO: 临时处理,后续需要修改
|
||
SetPause(true);
|
||
|
||
await ChangeStateNextFrame(new EndState(this));
|
||
if (!IsPvPLevel)
|
||
LevelManager.Instance.TryExitLevel(isSuccess, isGiveup: isGiveup);//在这里进行关卡打点事件记录
|
||
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();
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
private async UniTask ChangeStateNextFrame(LevelBaseState state)
|
||
{
|
||
await UniTask.NextFrame();
|
||
_stateMachine.ChangeState(state);
|
||
}
|
||
}
|
||
} |