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

471 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using AOT.PostProcess.BlackArea;
using BehaviorDesigner.Runtime.Tasks;
using Code.Scripts.Gameplay.Enviorment;
using Framework;
using Gameplay.Area;
using Gameplay.Buff;
using Gameplay.Bullet;
using Gameplay.Character;
using Gameplay.Fight.Capture;
using Gameplay.Fight.Flag;
using Gameplay.Guide;
using Gameplay.Pause;
using Gameplay.PostProcess.BlackArea;
using Gameplay.Skill;
using Gameplay.Summon;
using Gameplay.Unit;
using Gameplay.Unit.Data;
using Gameplay.Vehicle;
using Gameplay.Vehicle.Impl;
using Gameplay.Vehicle.TireMark;
using PhxhSDK;
using TGS;
using UnityEngine;
using Utils.Bounds;
namespace Gameplay.Level
{
public class BattleState : LevelBaseState
{
private bool _canUpdate;
protected override void _OnEnter()
{
_canUpdate = false;
AreaManager.instance.Init();
TireMarkManager.CreateInstance();
TireMarkManager.instance.Init();
BulletManager.CreateInstance();
BuffManager.instance.Init();
SkillManager.instance.Init();
BulletManager.instance.Init();
PauseManager.CreateInstance();
PauseManager.instance.Init();
EnviormentManager.CreateInstance();
EnviormentManager.instance.Init();
GameUnitManager.instance.EnterBattle();
// 这个必须在GameUnitManager.instance.EnterBattle()之后
// 因为他会给unit增加buff
BoundsManager.instance.Init();
_level.LevelTime = 0;
_level.isInBattle = true;
EventManager.Instance.Send(EventManager.EventName.LevelBattleBegin);
EventManager.Instance.Register(EventManager.EventName.ToFightModelDragEnd, (Action<Vector2>)_OnModelDragEnd);
_canUpdate = true;
var cameraInfo = _level.Map.MapData.cameraMarginInfo;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().rightMargin = cameraInfo.rightMarginInFight;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().leftMargin = cameraInfo.leftMarginInFight;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().topMargin = cameraInfo.topMarginInFight;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().bottomMargin = cameraInfo.bottomMarginInFight;
CameraManager.Instance.MainCamera.GetComponent<CameraController>().AdjustSelf();
LevelManager.Instance.CurrentLevel.SetSpeed(ELevelSpeed.DEFAULT_SPEED);
}
protected override void _OnUpdate(float deltaTime)
{
if (!_canUpdate) return;
_level.LevelTime += deltaTime;
SkillManager.instance.LogicUpdate(deltaTime);
BulletManager.instance.LogicUpdate(deltaTime);
GameUnitManager.instance.LogicUpdate(deltaTime);
BuffManager.instance.LogicUpdate(deltaTime);
AreaManager.instance.LogicUpdate(deltaTime);
PauseManager.instance.LogicUpdate(deltaTime);
TireMarkManager.instance.LogicUpdate(deltaTime);
CaptureManager.instance.LogicUpdate(deltaTime);
BlackAreaManager.instance.LogicUpdate(deltaTime);
EventManager.Instance.Send(EventManager.EventName.LevelTimeIncrease,deltaTime);
}
protected override void _OnExit()
{
_level.isInBattle = false;
BulletManager.instance.Dispose();
BoundsManager.instance.Dispose();
TireMarkManager.instance.Dispose();
EnviormentManager.instance.Dispose();
EventManager.Instance.Unregister(EventManager.EventName.ToFightModelDragEnd, (Action<Vector2>)_OnModelDragEnd);
}
public BattleState(Level level) : base(level)
{
}
protected override void _OnClick(Vector2 screenPosition)
{
var clickCellIndex = GroundHitHelper.CalcHitCellIndex(screenPosition,_level.Map.TerrainGridSystem);
if (clickCellIndex < 0)
{
if (GuideManager.Instance.IsGuiding && GuideManager.Instance.IsControlFloor)
return;
_SendDragCancelEvent();
return;
}
CheckCellClick(_level.Map.TerrainGridSystem, clickCellIndex, 0);
}
protected override void _OnCellClick(TerrainGridSystem tgs,
int cellIndex,
int buttonIndex)
{
if (InputManager.Instance.HandledThisFrame())
{
_SendDragCancelEvent();
return;
}
if (AreaManager.instance.readyAreaManager.isShow)
{
// 上阵模式
_HandleUpUnit(cellIndex);
return;
}
var selectUnit = _level.selectUnit;
if (selectUnit == null)
{
// 没有选中单位,进行选中操作
_TrySelect(cellIndex);
}
else
{
// 有选中单位,进行移动操作
_TryMove(cellIndex);
}
// var unitList = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
// OnHit(unitList, cellIndex);
//_SendDragSuccess();
}
private void _HandleUpUnit(int cellIndex)
{
// 不是准备区域
var map = _level.Map;
var worldPos = AreaManager.instance.GetCellPosByIndex(cellIndex);
var teamSelectID = TeamManager.Instance.SelectID;
var unit = GameUnitManager.instance.totalUnits.Search(teamSelectID);
if (unit == null)
{
_SendDragCancelEvent();
return;
}
if (GameUnitManager.instance.infightUnits.Contains(teamSelectID))
{
if (unit.gameunitType == EGameUnitType.Character)
{
if (unit.statusData.isOnVehicle)
{
//下车
_GetOutVehicle(teamSelectID, cellIndex);
}
}
}
else
{
_FightInBattle(map, cellIndex, worldPos);
}
}
private void _TryMove(int cellIndex)
{
if (!AreaManager.instance.moveAreaView.CheckIsInArea(cellIndex))
{
// 不在区域内,提示操作无效
UITipsManager.ShowTips("无法移动到该位置");
return;
}
var selectUnit = _level.selectUnit;
if (selectUnit.controlData.targetCellIndex == -1)
{
if (selectUnit.transData.cellIndex == cellIndex)
{
// 没有移动
if (!selectUnit.unitUIData.isContinueLock)
{
// 取消选中
_level.UnSelectUnit();
}
return;
}
}
else
{
if (selectUnit.controlData.targetCellIndex == cellIndex)
{
// 没有移动
if (!selectUnit.unitUIData.isContinueLock)
{
// 取消选中
_level.UnSelectUnit();
}
return;
}
}
selectUnit.MoveTo(cellIndex, true);
_CheckAfterMove(cellIndex);
if (!selectUnit.unitUIData.isContinueLock)
{
// 取消选中
_level.UnSelectUnit();
}
}
private void _CheckAfterMove(int cellIndex)
{
var selectUnit = _level.selectUnit;
if (selectUnit.gameunitType != EGameUnitType.Character
&& selectUnit.gameunitType != EGameUnitType.Vehicle) 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)
{
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;
}
else
{
// 如果是敌人, 则设置敌人目标
if (unit.statusData.CheckCanBeTarget(selectUnit, -1))
{
selectUnit.controlData.markEnemy = unit;
selectUnit.controlData.forceUpdateTarget = true;
return;
}
}
}
// 走到这里,说明没有目标,清空标记
selectUnit.controlData.markVehicle = null;
selectUnit.controlData.markEnemy = null;
}
private void _TrySelect(int cellIndex)
{
var unitList = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
if (unitList.Count <= 0)
{
// 没有单位,不做任何操作
return;
}
for (int i = 0; i < unitList.Count; i++)
{
var unit = unitList[i];
if (unit.commonData.canSelect)
{
// 只选中可控制单位
_level.SelectUnit(unit);
if (unit.debugShowInfo.cacheConfigId != 0)
{
EventManager.Instance.Send(EventManager.EventName.INFIGHT_AUDIO_SELECT_UNIT, unit.debugShowInfo.cacheConfigId);
}
return;
}
}
}
private void _FightInBattle(Map map,
int cellIndex,
Vector3 worldPosition)
{
if (TeamManager.Instance.IsFightListLimit(TeamManager.Instance.SelectID))
{
//TODO chenyq 需要添加到stringconfig
_SendDragCancelEvent();
LevelManager.Instance.CurrentLevel.UnSelectUnit();
UITipsManager.ShowTips("上阵人数达到上限!");
return;
}
// 不是准备区域
if (!AreaManager.instance.readyAreaManager.CheckIsInReadyArea(cellIndex))
{
_SendDragCancelEvent();
return;
}
var crossLevel = 0;
var unitType = TeamManager.Instance.GetUnitType(TeamManager.Instance.SelectID);
switch (unitType)
{
case SingleTeamCharacterInfo.CharacType.Hero:
var characterDataInfo = TeamManager.Instance.GetCharacterDataInfo(TeamManager.Instance.SelectID)
.GetConfigInfo();
crossLevel = characterDataInfo.Cfg.MoveCrossLevel;
break;
case SingleTeamCharacterInfo.CharacType.Vehicle:
var vehicleInfo = TeamManager.Instance.GetCharacterDataInfo(TeamManager.Instance.SelectID)
.GetVehicleInfo();
crossLevel = vehicleInfo.Cfg.MoveCrossLevel;
break;
default:
DebugUtil.LogError("暂未处理的类型:{0}", unitType);
return;
}
// 检查通行级别是否可以放置
var blockData = map.GetBlockData(cellIndex);
if (!MapUtils.CanPass(crossLevel, blockData.GetTerrainTypeProperty()))
{
_SendDragCancelEvent();
return;
}
var yaw = LevelUtils.SetCharaterToward(_level.GetLevelData().preparingRegionToward);
switch (unitType)
{
case SingleTeamCharacterInfo.CharacType.Hero:
_CreateCharacter(TeamManager.Instance.SelectID, worldPosition, yaw);
break;
case SingleTeamCharacterInfo.CharacType.Vehicle:
_CreateVehicle(TeamManager.Instance.SelectID, worldPosition, yaw);
break;
}
_level.SelectUnit(TeamManager.Instance.SelectID);
_SendDragSuccess();
//_level.SetSpeed(ELevelSpeed.Normal);
}
private void _CreateCharacter(uint id,
Vector3 position,
float yaw)
{
var map = _level.Map;
if (map == null) return;
// var characterInfo = CharacterDataInfoManager.Instance.GetCharacterInfo(TeamManager.Instance.SelectID);
// if (characterInfo == null)
// {
// return;
// }
// var character = await GameUnitManager.instance.PrepareCharacter(characterInfo, ETroopsId.Self);
// if (character == null)
// {
// DebugUtil.LogError("创建角色失败");
// return;
// }
if (!map.WorldPosition2TGSCellIndex(position, out var cellIndex))
{
DebugUtil.LogError("无法获取到单元格索引");
return;
}
GameUnitManager.instance.MoveUnitToFight(id, cellIndex, yaw);
}
private void _CreateVehicle(uint id,
Vector3 pos,
float yaw)
{
// var vInfo = VehicleCultivateDataManager.Instance.GetVehicleCultivateData(id);
// var vehicle = await GameUnitManager.instance.PrepareVehicle(vInfo, ETroopsId.Self);
// if (vehicle == null)
// {
// DebugUtil.LogError("创建载具失败");
// return;
// }
if (!_level.Map.WorldPosition2TGSCellIndex(pos, out var cellIndex))
{
DebugUtil.LogError("无法获取到cellIndex: {0}", pos);
return;
}
GameUnitManager.instance.MoveUnitToFight(id, cellIndex, yaw);
}
private void _GetOutVehicle(uint uid,
int cellIndex)
{
if (GameUnitManager.instance.infightUnits.Search(uid) is Character.Character gameUnit)
{
var character = gameUnit;
if (character.fullFSM.currentState.id != EFullState.OnVehicle)
{
return;
}
if (AreaManager.instance.readyAreaManager.CheckIsInReadyArea(cellIndex))
{
character.transData.disableEvent = true;
character.transData.pos =
LevelManager.Instance.CurrentLevel.Map.TGSCellIndex2WorldPosition(cellIndex);
character.transData.disableEvent = false;
character.DownVehicle();
_SendDragSuccess();
return;
}
_SendDragCancelEvent();
}
}
private void _OnModelDragEnd(Vector2 screenPos)
{
_OnClick(screenPos);
}
private void _SendDragCancelEvent()
{
if (TeamManager.IsHideAllUI)
{
TeamManager.IsHideAllUI = false;
TeamManager.Instance.CloseFightPosterUI();
}
EventManager.Instance.Send(EventManager.EventName.ToFightModelDragSuc, false);
}
private void _SendDragSuccess()
{
if (TeamManager.IsHideAllUI)
{
TeamManager.IsHideAllUI = false;
TeamManager.Instance.CloseFightPosterUI();
}
EventManager.Instance.Send(EventManager.EventName.ToFightModelDragSuc, true);
}
}
}