525 lines
19 KiB
C#
525 lines
19 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
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();
|
||
}
|
||
|
||
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;
|
||
}
|
||
var map = _level.Map;
|
||
if (map == null) return;
|
||
var unitList = AreaManager.instance.GetUnitsByCellIndex(cellIndex);
|
||
OnHit(unitList, cellIndex);
|
||
//_SendDragSuccess();
|
||
}
|
||
|
||
private void OnHit(List<GameUnit> units,
|
||
int cellIndex)
|
||
{
|
||
if (units.Count <= 0)
|
||
{
|
||
var map = _level.Map;
|
||
var worldPosition = map.TerrainGridSystem.CellGetPosition(cellIndex);
|
||
var teamSelectID = TeamManager.Instance.SelectID;
|
||
|
||
|
||
if (ValueValidator.IsIdValid(teamSelectID))
|
||
{
|
||
var unit = GameUnitManager.instance.totalUnits.Search(teamSelectID);
|
||
// if (TeamManager.Instance.IsInLevel(teamSelectID))
|
||
// {
|
||
// if (unit.gameunitType == EGameUnitType.Character)
|
||
// {
|
||
// if (unit.statusData.isOnVehicle)
|
||
// {
|
||
// //下车
|
||
// _GetOutVehicle(teamSelectID, cellIndex);
|
||
// return;
|
||
// }
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// _FightInBattle(map, cellIndex, worldPosition);
|
||
// return;
|
||
// }
|
||
if (GameUnitManager.instance.prepareUnits.Contains(teamSelectID))
|
||
{
|
||
//在车上不算在准备席,只是UI在备战席
|
||
_FightInBattle(map, cellIndex, worldPosition);
|
||
return;
|
||
}
|
||
|
||
if (GameUnitManager.instance.infightUnits.Contains(teamSelectID))
|
||
{
|
||
if (unit.gameunitType == EGameUnitType.Character)
|
||
{
|
||
if (unit.statusData.isOnVehicle)
|
||
{
|
||
//下车
|
||
_GetOutVehicle(teamSelectID, cellIndex);
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
var selectUnit = _level.selectUnit;
|
||
if (selectUnit == null)
|
||
{
|
||
// 进行选中
|
||
for (int i = 0; i < units.Count; i++)
|
||
{
|
||
var unit = units[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);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
return;
|
||
}
|
||
|
||
var isLightUp = BlackAreaManager.instance.CheckIsInLightArea(cellIndex);
|
||
if (units.Count <= 0 || !isLightUp)
|
||
{
|
||
// 进行移动
|
||
selectUnit.MoveTo(cellIndex, true);
|
||
if (selectUnit.debugShowInfo.cacheConfigId != 0)
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.INFIGHT_AUDIO_CLICK_MOVE, selectUnit.debugShowInfo.cacheConfigId);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 如果当前选择目标是己方英雄
|
||
switch (selectUnit.gameunitType)
|
||
{
|
||
case EGameUnitType.Character:
|
||
_HandleCharacterSelect(units, selectUnit as Character.Character);
|
||
break;
|
||
case EGameUnitType.Vehicle:
|
||
_HandleVehicleSelect(units, selectUnit as NormalVehicle);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
_SendDragCancelEvent();
|
||
}
|
||
|
||
private void _HandleVehicleSelect(List<GameUnit> units,
|
||
NormalVehicle vehicle)
|
||
{
|
||
// 设置目标
|
||
for (int i = 0; i < units.Count; i++)
|
||
{
|
||
var unit = units[i];
|
||
|
||
// 如果是敌人,则设置目标
|
||
if (Level.IsEnemy(vehicle, unit))
|
||
{
|
||
vehicle.MoveTo(unit);
|
||
return;
|
||
}
|
||
|
||
// 如果是己方单位,则选中
|
||
if (unit.commonData.canSelect)
|
||
{
|
||
// 只选中可控制单位
|
||
_level.SelectUnit(unit);
|
||
return;
|
||
}
|
||
|
||
}
|
||
|
||
// 如果走到这里,只能进行移动操作
|
||
vehicle.MoveTo(units[0].transData.cellIndex, true);
|
||
}
|
||
|
||
private void _HandleCharacterSelect(List<GameUnit> units,
|
||
Character.Character character)
|
||
{
|
||
for (int i = 0; i < units.Count; i++)
|
||
{
|
||
var unit = units[i];
|
||
|
||
// 如果是敌人,则设置目标
|
||
if (Level.IsEnemy(character, unit))
|
||
{
|
||
character.MoveTo(unit);
|
||
|
||
if (character.debugShowInfo.cacheConfigId != 0)
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.INFIGHT_AUDIO_CLICK_ATTACK, character.debugShowInfo.cacheConfigId);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// 目前不需要该段逻辑
|
||
// 如果是载具,则向载具移动
|
||
if (unit.gameunitType == EGameUnitType.Vehicle)
|
||
{
|
||
// 需要紧挨着载具
|
||
var vehicle = unit as NormalVehicle;
|
||
if (vehicle == null) continue;
|
||
var vehicleCellIndex = vehicle.transData.cellIndex;
|
||
var characterCellIndex = character.transData.cellIndex;
|
||
var distance = _level.Map.TerrainGridSystem.CellGetHexagonDistance(vehicleCellIndex, characterCellIndex);
|
||
if (distance <= 1)
|
||
{
|
||
// 在附近,则移动到载具
|
||
if (vehicle.CheckCanUp(character))
|
||
{
|
||
if (GuideManager.Instance.IsGuiding)
|
||
EventManager.Instance.Send(EventManager.EventName.Guide_JoinVehicle);
|
||
|
||
character.UpVehicle(vehicle);
|
||
}
|
||
else
|
||
{
|
||
character.MoveTo(vehicle.transData.cellIndex, true);
|
||
}
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 如果是己方单位,则选中
|
||
if (unit.commonData.canSelect)
|
||
{
|
||
_level.SelectUnit(unit);
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 如果走到这里,只能进行移动操作
|
||
character.MoveTo(units[0].transData.cellIndex, true);
|
||
}
|
||
|
||
//战斗中上阵
|
||
private void _PutUnitInLevel(Map map,
|
||
int cellIndex,
|
||
Vector3 worldPosition)
|
||
{
|
||
if (map == null)
|
||
{
|
||
return;
|
||
}
|
||
var teamSelectID = TeamManager.Instance.SelectID;
|
||
if (ValueValidator.IsIdValid(teamSelectID))
|
||
{
|
||
if (TeamManager.Instance.IsInLevel(teamSelectID))
|
||
{
|
||
var unit = GameUnitManager.instance.infightUnits.Search(teamSelectID);
|
||
if (unit.gameunitType == EGameUnitType.Character)
|
||
{
|
||
if (unit.statusData.isOnVehicle)
|
||
{
|
||
//下车
|
||
_GetOutVehicle(teamSelectID, cellIndex);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_FightInBattle(map, cellIndex, worldPosition);
|
||
}
|
||
}
|
||
}
|
||
|
||
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 async 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 async 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);
|
||
}
|
||
}
|
||
}
|