435 lines
14 KiB
C#
435 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using Framework;
|
|
using Gameplay.Character;
|
|
using Gameplay.Character.Utils;
|
|
using Gameplay.Level;
|
|
using Gameplay.PlayerSkill.Data;
|
|
using Gameplay.Summon;
|
|
using Gameplay.Unit.Data;
|
|
using Gameplay.Vehicle;
|
|
using Gameplay.Vehicle.Impl;
|
|
using UnityEngine;
|
|
namespace Gameplay.Unit
|
|
{
|
|
public class GameUnitManager
|
|
{
|
|
|
|
/// <summary>
|
|
/// 备战席的单位
|
|
/// </summary>
|
|
public UnitContainer prepareUnits = new UnitContainer();
|
|
|
|
/// <summary>
|
|
/// 战斗中的单位
|
|
/// </summary>
|
|
public UnitContainerWithTroop infightUnits = new UnitContainerWithTroop();
|
|
|
|
/// <summary>
|
|
/// 已死亡的单位
|
|
/// 注意这里不会有载具单位,载具单位死亡之后仍在存在于战斗中,只是处于损坏状态,可以被技能修复
|
|
/// </summary>
|
|
public UnitContainer deadUnits = new UnitContainer();
|
|
|
|
/// <summary>
|
|
/// 所有的单位,无论是否在备战席、战斗中、死亡,都能在这里找到
|
|
/// </summary>
|
|
public UnitContainer totalUnits = new UnitContainer();
|
|
|
|
public CharacterManager characterManager;
|
|
public VehicleManager vehicleManager;
|
|
public SummonManager summonManager;
|
|
|
|
private bool _needUpdateTargets;
|
|
|
|
public static GameUnitManager instance { get; private set; }
|
|
|
|
public bool enableLog = false;
|
|
|
|
public static void CreateInstance()
|
|
{
|
|
instance = new GameUnitManager();
|
|
instance.Init();
|
|
}
|
|
|
|
private GameUnitManager()
|
|
{
|
|
characterManager = new CharacterManager();
|
|
vehicleManager = new VehicleManager();
|
|
summonManager = new SummonManager();
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
_needUpdateTargets = true;
|
|
|
|
_RegistEvents();
|
|
}
|
|
|
|
private void _RegistEvents()
|
|
{
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_UNIT_CELLINDEX_CHANGE, (Action<GameUnit>)_OnAnyCharacterCellIndexChange);
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_UP_VEHICLE, (Action<GameUnit>)_OnAnyCharacterCellIndexChange);
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_DOWN_VEHICLE, (Action<GameUnit>)_OnAnyCharacterCellIndexChange);
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_UNIT_DEAD, (Action<GameUnit>)_OnCharacterDead);
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_UNIT_READY, (Action<GameUnit>)_OnUnitEnterBattle);
|
|
}
|
|
|
|
private void _UnRegistEvents()
|
|
{
|
|
EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_UNIT_CELLINDEX_CHANGE, (Action<GameUnit>)_OnAnyCharacterCellIndexChange);
|
|
EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_UP_VEHICLE, (Action<GameUnit>)_OnAnyCharacterCellIndexChange);
|
|
EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_DOWN_VEHICLE, (Action<GameUnit>)_OnAnyCharacterCellIndexChange);
|
|
EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_UNIT_DEAD, (Action<GameUnit>)_OnCharacterDead);
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_UNIT_READY, (Action<GameUnit>)_OnUnitEnterBattle);
|
|
}
|
|
|
|
private void _OnUnitEnterBattle(GameUnit unit)
|
|
{
|
|
_needUpdateTargets = true;
|
|
}
|
|
|
|
private void _OnCharacterDead(GameUnit deadCharacter)
|
|
{
|
|
_needUpdateTargets = true;
|
|
}
|
|
|
|
private void _OnAnyCharacterCellIndexChange(GameUnit noUse)
|
|
{
|
|
_needUpdateTargets = true;
|
|
}
|
|
|
|
public async UniTask PrepareUnits()
|
|
{
|
|
var selectTeam = TeamEditManager.Instance.GetSelectTeam();
|
|
var cIds = selectTeam.GetCharacterIDs();
|
|
for (int i = 0; i < cIds.Length; i++)
|
|
{
|
|
var cId = cIds[i];
|
|
var cInfo = CharacterDataInfoManager.Instance.GetCharacterInfo(cId);
|
|
if (cInfo == null)
|
|
{
|
|
DebugUtil.LogError("不存在的角色id: {0}", cId);
|
|
continue;
|
|
}
|
|
await PrepareCharacter(cInfo, ETroopsId.Self);
|
|
}
|
|
|
|
var vIds = selectTeam.GetVehicleIDs();
|
|
for (int i = 0; i < vIds.Length; i++)
|
|
{
|
|
var vId = vIds[i];
|
|
var vInfo = VehicleCultivateDataManager.Instance.GetVehicleCultivateData(vId);
|
|
if (vInfo == null)
|
|
{
|
|
DebugUtil.LogError("不存在的载具id: {0}", vId);
|
|
continue;
|
|
}
|
|
await PrepareVehicle(vInfo, ETroopsId.Self);
|
|
}
|
|
}
|
|
|
|
private void _PrepareUnit(GameUnit unit)
|
|
{
|
|
prepareUnits.Add(unit);
|
|
totalUnits.Add(unit);
|
|
|
|
if (enableLog)
|
|
{
|
|
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 已进入备战席");
|
|
}
|
|
|
|
}
|
|
|
|
public async UniTask<Character.Character> PrepareNpc(LevelData.NPCInfo npcInfo,
|
|
int troopId)
|
|
{
|
|
var nGuid = GUIDManager.GenGuid();
|
|
var dataConfig = CharacterDataHelper.CombineConfigData(npcInfo.id, npcInfo.level);
|
|
var createUnit = new Character.Character(nGuid, dataConfig, troopId);
|
|
await createUnit.LoadAsync();
|
|
await createUnit.Prepare();
|
|
|
|
_PrepareUnit(createUnit);
|
|
return createUnit;
|
|
}
|
|
|
|
public async UniTask<Character.Character> PrepareCharacter(CharacterDataInfo cInfo,
|
|
int troopId)
|
|
{
|
|
var nGuid = GUIDManager.GenGuid();
|
|
var dataConfig = CharacterDataHelper.CombineConfigData(cInfo);
|
|
var createUnit = new Character.Character(nGuid, dataConfig, troopId);
|
|
await createUnit.LoadAsync();
|
|
await createUnit.Prepare();
|
|
|
|
_PrepareUnit(createUnit);
|
|
return createUnit;
|
|
}
|
|
|
|
public async UniTask<NormalVehicle> PrepareVehicle(VehicleCultivateData vInfo, int troopId)
|
|
{
|
|
var nGuid = GUIDManager.GenGuid();
|
|
var dataConfig = VehicleDataHelper.CombineConfigData(vInfo);
|
|
var createUnit = new NormalVehicle(nGuid, dataConfig, troopId);
|
|
await createUnit.Create();
|
|
await createUnit.Prepare();
|
|
|
|
_PrepareUnit(createUnit);
|
|
return createUnit;
|
|
}
|
|
|
|
public BaseSummon PrepareSummon(int summondId, GameUnit creator)
|
|
{
|
|
var createSummon = SummonFactory.CreateSummon(summondId, creator);
|
|
|
|
_PrepareUnit(createSummon);
|
|
|
|
return createSummon;
|
|
}
|
|
|
|
// 将单位移动到备战席
|
|
public void MoveUnitToPrepare(uint unitGuid)
|
|
{
|
|
var unit = infightUnits.Search(unitGuid);
|
|
if (unit == null)
|
|
{
|
|
DebugUtil.LogError("不存在的单位:{0}", unitGuid);
|
|
return;
|
|
}
|
|
// 判断是否已经开展
|
|
if (LevelManager.Instance.CurrentLevel.isInBattle)
|
|
{
|
|
unit.ExitBattle();
|
|
}
|
|
unit.BackPrepare();
|
|
// 转移
|
|
infightUnits.Remove(unit);
|
|
prepareUnits.Add(unit);
|
|
|
|
// 如果有选中,则取消选中-修复bug:选中单位回到备战席后,选中状态仍然存在
|
|
if (LevelManager.Instance.CurrentLevel.selectUnit == unit)
|
|
{
|
|
LevelManager.Instance.CurrentLevel.UnSelectUnit();
|
|
}
|
|
|
|
EventManager.Instance.Send(EventManager.EventName.INFIGHT_UNIT_REMOVE, unit);
|
|
|
|
// 单独给UI那边发一个事件,用于更新UI
|
|
EventManager.Instance.Send(EventManager.EventName.INFIGHT_BACK_PREPARE, unit);
|
|
_RemoveFromFightHandles(unit);
|
|
|
|
if (enableLog)
|
|
{
|
|
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 从战斗列表回到备战席");
|
|
}
|
|
|
|
_needUpdateTargets = true;
|
|
}
|
|
|
|
// 将到位移动到战斗中,并指定位置
|
|
public void MoveUnitToFight(uint unitGuid,
|
|
int cellIndex,
|
|
float yaw)
|
|
{
|
|
var unit = prepareUnits.Search(unitGuid);
|
|
if (unit == null)
|
|
{
|
|
DebugUtil.LogError("不存在的单位:{0}", unitGuid);
|
|
return;
|
|
}
|
|
// 设置位置
|
|
unit.SetPosAndYaw(cellIndex, yaw);
|
|
unit.Ready();
|
|
// 判断是否已经开战
|
|
if (LevelManager.Instance.CurrentLevel.isInBattle)
|
|
{
|
|
unit.EnterBattle();
|
|
}
|
|
// 转移
|
|
prepareUnits.Remove(unit);
|
|
infightUnits.Add(unit);
|
|
|
|
switch (unit.gameunitType)
|
|
{
|
|
case EGameUnitType.Character:
|
|
if (unit is Character.Character character)
|
|
characterManager.AddCharacter(character);
|
|
break;
|
|
case EGameUnitType.Vehicle:
|
|
if (unit is NormalVehicle vehicle)
|
|
vehicleManager.AddVehicle(vehicle);
|
|
break;
|
|
}
|
|
|
|
if (enableLog)
|
|
{
|
|
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 已进入战斗序列");
|
|
}
|
|
|
|
_needUpdateTargets = true;
|
|
}
|
|
|
|
public void EnterBattle()
|
|
{
|
|
_needUpdateTargets = true;
|
|
for (int i = 0; i < infightUnits.count; i++)
|
|
{
|
|
var unit = infightUnits.GetByIndex(i);
|
|
unit.EnterBattle();
|
|
}
|
|
}
|
|
|
|
public void ReviveUnit(uint unitId, UnitReviveData reviveData)
|
|
{
|
|
var unit = deadUnits.Search(unitId);
|
|
if (unit == null)
|
|
{
|
|
DebugUtil.LogError("不存在的单位:{0}", unitId);
|
|
return;
|
|
}
|
|
if (unit is Character.Character character)
|
|
{
|
|
character.Revive(reviveData);
|
|
|
|
// 转移
|
|
deadUnits.Remove(unit);
|
|
prepareUnits.Add(unit);
|
|
|
|
// 发送事件
|
|
EventManager.Instance.Send(EventManager.EventName.INFIGHT_REBORN_UNIT, unit);
|
|
|
|
if (enableLog)
|
|
{
|
|
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 已复活");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError("设计上只支持角色复活");
|
|
}
|
|
}
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
if (_needUpdateTargets)
|
|
{
|
|
_needUpdateTargets = false;
|
|
_UpdateTargets();
|
|
}
|
|
|
|
for (int i = 0; i < infightUnits.count; i++)
|
|
{
|
|
var unit = infightUnits.GetByIndex(i);
|
|
|
|
unit.LogicUpdate(dt);
|
|
|
|
if (unit.needRemove)
|
|
{
|
|
_RemoveFromFight(unit);
|
|
i--;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void _RemoveFromFight(GameUnit unit)
|
|
{
|
|
if (enableLog)
|
|
{
|
|
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 已移除");
|
|
}
|
|
unit.ExitBattle();
|
|
infightUnits.Remove(unit);
|
|
|
|
EventManager.Instance.Send(EventManager.EventName.INFIGHT_UNIT_REMOVE, unit);
|
|
_RemoveFromFightHandles(unit);
|
|
|
|
var deadToList = _CheckDeadToList(unit);
|
|
if (deadToList)
|
|
{
|
|
deadUnits.Add(unit);
|
|
}
|
|
else
|
|
{
|
|
// 销毁
|
|
unit.Dispose();
|
|
}
|
|
|
|
_needUpdateTargets = true;
|
|
}
|
|
|
|
private bool _CheckDeadToList(GameUnit unit)
|
|
{
|
|
// 目前只有己方角色才会进入死亡列表
|
|
if (unit.gameunitType == EGameUnitType.Character)
|
|
{
|
|
if (unit is Character.Character character && character.commonData.troopId == ETroopsId.Self)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void _RemoveFromFightHandles(GameUnit unit)
|
|
{
|
|
switch (unit.gameunitType)
|
|
{
|
|
case EGameUnitType.Character:
|
|
if (unit is Character.Character character)
|
|
characterManager.RemoveCharacter(character);
|
|
break;
|
|
case EGameUnitType.Vehicle:
|
|
if (unit is NormalVehicle vehicle)
|
|
vehicleManager.RemoveVehicle(vehicle);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void _UpdateTargets()
|
|
{
|
|
for (int i = 0; i < infightUnits.count; i++)
|
|
{
|
|
var unit = infightUnits.GetByIndex(i);
|
|
switch (unit.gameunitType)
|
|
{
|
|
case EGameUnitType.Character:
|
|
if (unit is Character.Character character)
|
|
character.UpdateTargets();
|
|
break;
|
|
case EGameUnitType.Vehicle:
|
|
if (unit is NormalVehicle vehicle)
|
|
vehicle.UpdateTargets();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_UnRegistEvents();
|
|
for (int i = 0; i < infightUnits.count; i++)
|
|
{
|
|
var unit = infightUnits.GetByIndex(i);
|
|
unit.ExitBattle();
|
|
unit.Dispose();
|
|
}
|
|
for (int i = 0; i < prepareUnits.count; i++)
|
|
{
|
|
var unit = prepareUnits.GetByIndex(i);
|
|
unit.Dispose();
|
|
}
|
|
for (int i = 0; i < deadUnits.count; i++)
|
|
{
|
|
var unit = deadUnits.GetByIndex(i);
|
|
unit.Dispose();
|
|
}
|
|
instance = null;
|
|
}
|
|
|
|
}
|
|
}
|