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

474 lines
16 KiB
C#
Raw Normal View History

using AOT.PostProcess.BlackArea;
2024-11-25 18:27:04 +08:00
using Code.Scripts.Gameplay.Fight.Evacuate;
using Code.Scripts.Gameplay.Fight.ExtLogic;
2023-08-22 19:08:45 +08:00
using Framework;
using Gameplay.Area;
using Gameplay.Area.Ready;
2023-08-22 19:08:45 +08:00
using Gameplay.Buff;
using Gameplay.Bullet;
using Gameplay.Character;
2024-05-30 13:07:39 +08:00
using Gameplay.Fight.Capture;
2024-07-01 13:08:37 +08:00
using Gameplay.Guide;
2023-08-22 19:08:45 +08:00
using Gameplay.Pause;
using Gameplay.Skill;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
using Gameplay.Unit.Data;
2023-08-22 19:08:45 +08:00
using Gameplay.Vehicle.Impl;
2024-11-14 14:22:30 +08:00
using LTGame;
2024-08-09 18:08:27 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
using TGS;
using UnityEngine;
2023-10-19 15:00:19 +08:00
using Utils.Bounds;
2023-08-22 19:08:45 +08:00
namespace Gameplay.Level
{
2024-11-14 14:22:30 +08:00
public class LevelStateBattle : BaseLevelState
2023-08-22 19:08:45 +08:00
{
2024-12-03 19:10:28 +08:00
2024-11-14 14:22:30 +08:00
public LevelStateBattle(Level level) : base(level, ELevelState.Battle)
{
}
2023-08-22 19:08:45 +08:00
2023-10-19 15:00:19 +08:00
private bool _canUpdate;
private bool _oldPauseStatus;
2023-08-22 19:08:45 +08:00
2024-11-14 14:22:30 +08:00
protected override void _OnEnter(NBaseState exitState,
object param)
2023-08-22 19:08:45 +08:00
{
2024-11-14 14:22:30 +08:00
base._OnEnter(exitState, param);
2024-11-15 18:21:15 +08:00
if (exitState.id == ELevelState.Prepare)
{
2024-11-15 20:01:13 +08:00
_SetPlayerControl();
2024-12-03 19:10:28 +08:00
GameUnitManager.instance.EnterBattle();
// 这个必须在GameUnitManager.instance.EnterBattle()之后
// 因为他会给unit增加buff
BoundsManager.instance.Init();
_level.LevelTime = 0;
}
2024-12-03 19:10:28 +08:00
2023-08-22 19:08:45 +08:00
_level.isInBattle = true;
EventManager.Instance.Send(EventManager.EventName.LevelBattleBegin);
2024-12-03 19:10:28 +08:00
2024-11-26 14:04:04 +08:00
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UI_CLICK_UNIT, _OnUIClickUnit);
2024-12-03 19:10:28 +08:00
2024-08-12 13:05:22 +08:00
var cameraInfo = _level.Map.MapData.cameraMarginInfo;
2025-04-28 15:26:51 +08:00
CameraManager.Instance.MainCamera.GetComponent<CameraController>().CameraData.rightMargin = cameraInfo.rightMarginInFight;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().CameraData.leftMargin = cameraInfo.leftMarginInFight;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().CameraData.topMargin = cameraInfo.topMarginInFight;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().CameraData.bottomMargin = cameraInfo.bottomMarginInFight;
2024-12-03 19:10:28 +08:00
2024-08-15 18:18:12 +08:00
CameraManager.Instance.MainCamera.GetComponent<CameraController>().AdjustSelf();
2024-12-03 19:10:28 +08:00
LevelManager.Instance.CurrentLevel.SetSpeed(ELevelSpeed.DEFAULT_SPEED);
EventManager.Instance.Send(EventManager.EventName.FightStoryHide);
2024-12-03 19:10:28 +08:00
2024-11-22 14:25:49 +08:00
_WaitBlackOut();
if (!GameSettingManager.Instance.SettingData.IsOpenGrid)
{
LevelManager.Instance.CurrentLevel.Map.ShowInfo(Map.InfoMask.ShowCells, false);
}
2023-08-22 19:08:45 +08:00
}
private float _minTime = 1 / 30f;
private float _innerDt;
2024-11-14 14:22:30 +08:00
protected override void _OnRunning()
2023-08-22 19:08:45 +08:00
{
2024-11-14 14:22:30 +08:00
base._OnRunning();
PauseManager.instance.LogicUpdate(deltaTime);
2023-08-22 19:08:45 +08:00
SkillManager.instance.LogicUpdate(deltaTime);
if (AreaManager.instance.isInSkillPreview) return;
_level.LevelTime += deltaTime;
GameUnitManager.instance.MoveLogicUpdate(deltaTime);
_innerDt += deltaTime;
if (_innerDt < _minTime) return;
deltaTime = _innerDt;
_innerDt = 0f;
2023-08-22 19:08:45 +08:00
BulletManager.instance.LogicUpdate(deltaTime);
2023-12-14 15:31:46 +08:00
GameUnitManager.instance.LogicUpdate(deltaTime);
2024-07-09 15:03:48 +08:00
BuffManager.instance.LogicUpdate(deltaTime);
ExtLogicManager.instance.LogicUpdate(deltaTime);
2023-08-22 19:08:45 +08:00
AreaManager.instance.LogicUpdate(deltaTime);
2024-05-30 13:07:39 +08:00
CaptureManager.instance.LogicUpdate(deltaTime);
BlackAreaManager.instance.LogicUpdate(deltaTime);
2024-11-25 18:27:04 +08:00
EvacuateManager.instance.LogicUpdate();
2024-12-19 16:18:45 +08:00
EventManager.Instance.Send(EventManager.EventName.LevelTimeIncrease);
2024-11-15 18:21:15 +08:00
_level.fightStoryManager.LogicUpdate(deltaTime);
if (_level.fightStoryManager.CheckCanTrigger())
{
isFinished = true;
nextState = ELevelState.BattleStory;
}
2023-08-22 19:08:45 +08:00
}
2024-11-14 14:22:30 +08:00
protected override void _OnExit(NBaseState exitState,
object param)
2023-08-22 19:08:45 +08:00
{
2024-11-14 14:22:30 +08:00
base._OnExit(exitState, param);
2024-12-03 19:10:28 +08:00
2024-11-14 14:22:30 +08:00
_level.isInBattle = false;
2024-11-26 14:04:04 +08:00
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UI_CLICK_UNIT, _OnUIClickUnit);
}
private void _OnUIClickUnit(GameUnit clickUnit)
{
if (AreaManager.instance.readyAreaManager.isShow)
{
_HandleOnReadyShow(clickUnit);
}
else
{
_HandleOnReadyNotShow(clickUnit);
}
}
private void _HandleOnReadyShow(GameUnit clickUnit)
{
if (clickUnit == AreaManager.instance.readyAreaManager.readyUnit)
{
return;
}
// 关闭准备区域
AreaManager.instance.readyAreaManager.HideReadyArea();
_level.SetPause(_oldPauseStatus);
_HandleOnReadyNotShow(clickUnit);
}
private void _HandleOnReadyNotShow(GameUnit clickUnit)
{
if (clickUnit.statusData.isOnVehicle)
{
_level.UnSelectUnit();
_HandleOnVehicle(clickUnit);
}
else
{
_HandleNotOnVehicle(clickUnit);
}
}
private void _HandleOnVehicle(GameUnit clickUnit)
{
if (clickUnit is Character.Character unitCharacter)
{
var belongVehicle = unitCharacter.runtimeData.belongVehicle;
if (belongVehicle == null)
{
DebugUtil.LogError("单位没有载具");
return;
}
// 居中镜头
CameraManager.Instance.MainCamera.GetComponent<CameraController>().KeepCenter(belongVehicle.transform.position);
AreaManager.instance.readyAreaManager.ShowReadyArea(EReadyAreaStatus.AroundVehicle, clickUnit);
_oldPauseStatus = _level.IsPause();
_level.SetPause(true);
}
else
{
DebugUtil.LogError("在载具上的单位必须是角色");
}
}
private void _HandleNotOnVehicle(GameUnit clickUnit)
2024-11-26 14:04:04 +08:00
{
2024-11-27 16:14:44 +08:00
// 居中镜头
2024-11-28 12:10:05 +08:00
CameraManager.Instance.MainCamera.GetComponent<CameraController>().KeepCenter(clickUnit.transform.position);
if (_level.selectUnit == clickUnit)
{
_TryEnterSkillPreRelease(clickUnit);
}
else
2024-11-27 16:14:44 +08:00
{
2024-11-28 12:10:05 +08:00
_level.SelectUnit(clickUnit);
2024-11-27 16:14:44 +08:00
}
2024-11-26 14:59:20 +08:00
}
private void _TryEnterSkillPreRelease(GameUnit unit)
{
2024-12-02 19:33:16 +08:00
if (unit.statusData.isRetreat)
{
UITipsManager.ShowTips(CommonUtils.GetLocalizeText("battle_skill_retreat"));
2024-12-02 19:33:16 +08:00
return;
}
if (unit.statusData.isDead)
{
UITipsManager.ShowTips(CommonUtils.GetLocalizeText("battle_skill_is_dead"));
2024-12-02 19:33:16 +08:00
return;
}
2024-12-03 19:10:28 +08:00
2024-11-26 14:59:20 +08:00
var unitSkillManager = SkillManager.instance.GetUnitSkillManager(unit);
if (unitSkillManager == null)
{
2025-01-20 16:35:04 +08:00
DebugUtil.LogError("单位无技能管理器");
2024-11-26 14:59:20 +08:00
return;
}
var positiveSkill = unitSkillManager.positiveSkill;
if (positiveSkill == null)
{
2025-01-20 16:35:04 +08:00
DebugUtil.LogError("单位无主动技能");
2024-11-26 14:59:20 +08:00
return;
}
if (positiveSkill.canRelease)
{
DebugUtil.LogG("进入技能释放状态");
AreaManager.instance.skillAreaSelector2.EnterPreview(positiveSkill);
}
else
{
UITipsManager.ShowTips(CommonUtils.GetLocalizeText("battle_skill_not_ready"));
2024-11-26 14:59:20 +08:00
}
2023-08-22 19:08:45 +08:00
}
2024-11-15 20:01:13 +08:00
private void _SetPlayerControl()
{
if (_level.fightStoryManager.hasStory
&& _level.fightStoryManager.fightStoryData.forceUserTeam)
{
var teamData = _level.fightStoryManager.fightStoryData.playerTeamData;
if (teamData == null)
{
return;
}
for (int i = 0; i < teamData.memberList.Count; i++)
{
var memberInfo = teamData.memberList[i];
if (memberInfo == null)
{
continue;
}
if (memberInfo.unitLocalId == 0)
{
continue;
}
var unit = GameUnitManager.instance.infightUnits.SearchByLocalId(memberInfo.unitLocalId);
if (unit == null)
{
continue;
}
unit.commonData.canControl = true;
}
}
}
2023-08-22 19:08:45 +08:00
protected override void _OnClick(Vector2 screenPosition)
{
2024-12-03 19:10:28 +08:00
var clickCellIndex = GroundHitHelper.CalcHitCellIndex(screenPosition, _level.Map.TerrainGridSystem);
2023-08-22 19:08:45 +08:00
if (clickCellIndex < 0)
return;
2024-12-03 19:10:28 +08:00
if (GuideManager.Instance.IsGuiding &&
GuideManager.Instance.IsControlFloor &&
GuideManager.Instance.FloorIndex != clickCellIndex)
{
return;
}
2024-11-27 12:40:46 +08:00
if (AreaManager.instance.isInSkillPreview)
{
EventManager.Instance.Send(EventManager.EventName.INFIGHT_UI_SKILL_CLICK_SCREEN, clickCellIndex);
return;
}
2024-07-01 13:08:37 +08:00
CheckCellClick(_level.Map.TerrainGridSystem, clickCellIndex, 0);
2023-08-22 19:08:45 +08:00
}
protected override void _OnCellClick(TerrainGridSystem tgs,
int cellIndex,
int buttonIndex)
{
if (InputManager.Instance.HandledThisFrame())
{
return;
}
2024-09-30 18:06:04 +08:00
if (AreaManager.instance.readyAreaManager.isShow)
{
// 上阵模式
_HandleUpUnit(cellIndex);
return;
}
if (PVEManager.Instance.AutoFight)
{
CommonUtils.ShowMessageTips("自动战斗中,不可操作");
return;
}
2024-09-30 18:06:04 +08:00
var selectUnit = _level.selectUnit;
if (selectUnit == null)
{
// 没有选中单位,进行选中操作
_TrySelect(cellIndex);
}
else
{
// 有选中单位,进行移动操作
_TryMove(cellIndex);
}
}
private void _HandleUpUnit(int cellIndex)
{
// 不是准备区域
_GetOutVehicle(cellIndex);
2024-09-30 18:06:04 +08:00
}
2024-12-03 19:10:28 +08:00
2024-09-30 18:06:04 +08:00
private void _TryMove(int cellIndex)
{
2024-12-02 19:33:16 +08:00
var selectUnit = _level.selectUnit;
if (selectUnit == null)
{
DebugUtil.LogError("没有选中单位");
return;
}
if (selectUnit.statusData.isRetreat
&& selectUnit.transData.cellIndex == cellIndex)
{
// 取消选中
_level.UnSelectUnit();
return;
}
2024-12-03 19:10:28 +08:00
2024-09-30 18:06:04 +08:00
if (!AreaManager.instance.moveAreaView.CheckIsInArea(cellIndex))
{
// 不在区域内,提示操作无效
UITipsManager.ShowTips("无法移动到该位置");
return;
}
// 先进行寻路, 再进行移动
selectUnit.pathData.FindOrCache(cellIndex);
2024-09-30 18:06:04 +08:00
selectUnit.MoveTo(cellIndex, true);
_CheckAfterMove(cellIndex);
2024-12-03 19:10:28 +08:00
2024-12-02 19:33:16 +08:00
_level.UnSelectUnit();
2024-09-30 18:06:04 +08:00
}
private void _CheckAfterMove(int cellIndex)
{
var selectUnit = _level.selectUnit;
2024-10-08 19:50:22 +08:00
if (selectUnit.gameunitType != EGameUnitType.Character
&& selectUnit.gameunitType != EGameUnitType.Vehicle) return;
2024-12-03 19:10:28 +08:00
selectUnit.controlData.markVehicle = null;
selectUnit.controlData.markEnemy = null;
if (selectUnit.transData.cellIndex == cellIndex) return;
var units = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
for (int i = 0; i < units.Count; i++)
{
var unit = units[i];
if (unit.commonData.troopId == selectUnit.commonData.troopId
&& unit.gameunitType == EGameUnitType.Vehicle)
{
2024-10-08 19:50:22 +08:00
if (selectUnit.gameunitType == EGameUnitType.Vehicle)
{
// 载具不能上载具
return;
}
// 点击的己方单位, 这里必然是载具
var vehicle = unit as NormalVehicle;
if (vehicle == null)
{
DebugUtil.LogError("不是载具");
return;
}
if (vehicle.CheckCanUp(selectUnit))
{
selectUnit.controlData.markVehicle = vehicle;
}
return;
}
2024-12-03 19:10:28 +08:00
// 如果是敌人, 则设置敌人目标
if (Level.IsEnemy(unit, selectUnit) && unit.statusData.CheckCanBeTarget(selectUnit, -1))
{
2025-02-12 19:37:55 +08:00
if (selectUnit.statusData.isPretend)
{
// 取消伪装状态
selectUnit.statusData.isPretend = false;
}
2024-12-03 19:10:28 +08:00
selectUnit.controlData.markEnemy = unit;
selectUnit.controlData.forceUpdateTarget = true;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_AUDIO_CLICK_ATTACK, selectUnit.debugShowInfo.cacheConfigId);
2024-12-03 19:10:28 +08:00
return;
}
}
EventManager.Instance.Send(EventManager.EventName.INFIGHT_AUDIO_CLICK_MOVE, selectUnit.debugShowInfo.cacheConfigId);
2024-12-03 19:10:28 +08:00
}
2024-09-30 18:06:04 +08:00
private void _TrySelect(int cellIndex)
{
2023-08-22 19:08:45 +08:00
var unitList = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
2024-09-30 18:06:04 +08:00
if (unitList.Count <= 0)
{
// 没有单位,不做任何操作
return;
}
for (int i = 0; i < unitList.Count; i++)
{
var unit = unitList[i];
if (unit.commonData.canSelect)
{
// 只选中可控制单位
_level.SelectUnit(unit);
return;
}
}
2023-08-22 19:08:45 +08:00
}
private void _GetOutVehicle(int cellIndex)
2023-10-19 15:00:19 +08:00
{
var character = AreaManager.instance.readyAreaManager.readyUnit as Character.Character;
if (character == null)
2023-10-19 15:00:19 +08:00
{
DebugUtil.LogError("不是角色");
2023-10-19 15:00:19 +08:00
return;
}
if (character.fullFSM.currentState.id != EFullState.OnVehicle)
2023-10-19 15:00:19 +08:00
{
DebugUtil.LogError("不在载具上");
2023-10-19 15:00:19 +08:00
return;
}
if (AreaManager.instance.readyAreaManager.CheckIsInReadyArea(cellIndex))
2023-10-19 15:00:19 +08:00
{
DebugUtil.Log("在准备区域内, 下车");
character.transData.disableEvent = true;
character.transData.pos =
LevelManager.Instance.CurrentLevel.Map.TGSCellIndex2WorldPosition(cellIndex);
character.transData.disableEvent = false;
character.DownVehicle();
2023-10-19 15:00:19 +08:00
}
else
{
DebugUtil.Log("不在准备区域内, 关闭准备区域");
AreaManager.instance.readyAreaManager.HideReadyArea();
}
_level.SetPause(_oldPauseStatus);
2024-01-04 15:32:00 +08:00
}
2023-08-22 19:08:45 +08:00
}
}