using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using Framework;
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;
///
/// 星级使用前3位,战役使用15位
///
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 StarResult
{
get
{
List 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)
{
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");
}
}
}