NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Unit/GameUnitManager.cs

488 lines
16 KiB
C#
Raw Normal View History

2023-12-14 14:42:27 +08:00
using System;
2023-12-25 19:00:08 +08:00
using System.Collections.Generic;
2023-12-14 14:42:27 +08:00
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.Character;
using Gameplay.Character.Utils;
using Gameplay.Level;
2023-12-19 15:01:19 +08:00
using Gameplay.PlayerSkill.Data;
using Gameplay.Summon;
2023-12-14 14:42:27 +08:00
using Gameplay.Unit.Data;
2023-12-14 15:31:46 +08:00
using Gameplay.Vehicle;
2023-12-14 14:42:27 +08:00
using Gameplay.Vehicle.Impl;
2024-01-03 13:04:36 +08:00
using PhxhSDK;
2023-12-19 18:07:31 +08:00
using UnityEngine;
2023-12-14 14:42:27 +08:00
namespace Gameplay.Unit
{
public class GameUnitManager
{
/// <summary>
/// 备战席的单位
/// </summary>
public UnitContainer prepareUnits = new UnitContainer();
/// <summary>
/// 战斗中的单位
/// </summary>
2023-12-25 19:00:08 +08:00
public UnitContainerWithTroop infightUnits = new UnitContainerWithTroop();
2023-12-14 14:42:27 +08:00
/// <summary>
/// 已死亡的单位
/// 注意这里不会有载具单位,载具单位死亡之后仍在存在于战斗中,只是处于损坏状态,可以被技能修复
/// </summary>
public UnitContainer deadUnits = new UnitContainer();
/// <summary>
/// 所有的单位,无论是否在备战席、战斗中、死亡,都能在这里找到
/// </summary>
public UnitContainer totalUnits = new UnitContainer();
public CharacterManager characterManager;
2023-12-14 15:31:46 +08:00
public VehicleManager vehicleManager;
public SummonManager summonManager;
2023-12-14 14:42:27 +08:00
private bool _needUpdateTargets;
public static GameUnitManager instance { get; private set; }
public bool enableLog = false;
2024-02-20 13:45:32 +08:00
private Dictionary<string, int> _loadPathRecord = new Dictionary<string, int>();
2023-12-14 14:42:27 +08:00
public static void CreateInstance()
{
instance = new GameUnitManager();
instance.Init();
}
private GameUnitManager()
{
characterManager = new CharacterManager();
2023-12-14 15:31:46 +08:00
vehicleManager = new VehicleManager();
2023-12-14 19:42:05 +08:00
summonManager = new SummonManager();
2023-12-14 14:42:27 +08:00
}
public void Init()
{
_needUpdateTargets = true;
_RegistEvents();
}
2024-02-20 13:45:32 +08:00
public void RecordLoadPath(string path)
{
if (_loadPathRecord.ContainsKey(path))
{
_loadPathRecord[path]++;
}
else
{
_loadPathRecord.Add(path, 1);
}
}
2023-12-14 14:42:27 +08:00
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);
2023-12-14 14:42:27 +08:00
}
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;
2023-12-14 14:42:27 +08:00
}
private void _OnCharacterDead(GameUnit deadCharacter)
{
_needUpdateTargets = true;
}
private void _OnAnyCharacterCellIndexChange(GameUnit noUse)
{
_needUpdateTargets = true;
}
2024-01-03 13:04:36 +08:00
// 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);
// }
// }
2023-12-14 14:42:27 +08:00
2023-12-14 16:30:55 +08:00
private void _PrepareUnit(GameUnit unit)
{
prepareUnits.Add(unit);
totalUnits.Add(unit);
if (enableLog)
{
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 已进入备战席");
}
2023-12-14 16:30:55 +08:00
}
2024-01-10 12:57:10 +08:00
public int GetTroopUnitAliveCount(int troopID)
{
var unitList = infightUnits.GetListByTroop(troopID);
if (unitList == null)
return 0;
var result = 0;
foreach (var unit in unitList)
{
if (!unit.statusData.isDead)
result++;
}
return result;
}
2023-12-14 19:42:05 +08:00
public async UniTask<Character.Character> PrepareNpc(LevelData.NPCInfo npcInfo,
2023-12-14 14:42:27 +08:00
int troopId)
{
var nGuid = GUIDManager.GenGuid();
var dataConfig = CharacterDataHelper.CombineConfigData(npcInfo.id, npcInfo.level);
2023-12-29 17:00:23 +08:00
2023-12-14 14:42:27 +08:00
var createUnit = new Character.Character(nGuid, dataConfig, troopId);
2024-01-03 13:04:36 +08:00
if (!AssetManager.Instance.CanLocateAsset<GameObject>(createUnit.runtimeData.configData.modelPath, out _))
{
return null;
}
2024-02-20 13:45:32 +08:00
await createUnit.CreateAsync();
2023-12-14 15:38:17 +08:00
await createUnit.Prepare();
2023-12-14 16:30:55 +08:00
_PrepareUnit(createUnit);
2023-12-14 14:42:27 +08:00
return createUnit;
}
2023-12-14 19:42:05 +08:00
public async UniTask<Character.Character> PrepareCharacter(CharacterDataInfo cInfo,
2023-12-14 14:42:27 +08:00
int troopId)
{
var nGuid = GUIDManager.GenGuid();
var dataConfig = CharacterDataHelper.CombineConfigData(cInfo);
var createUnit = new Character.Character(nGuid, dataConfig, troopId);
2024-01-03 13:04:36 +08:00
if (!AssetManager.Instance.CanLocateAsset<GameObject>(createUnit.runtimeData.configData.modelPath, out _))
{
return null;
}
2024-02-20 13:45:32 +08:00
await createUnit.CreateAsync();
2023-12-14 15:38:17 +08:00
await createUnit.Prepare();
2023-12-14 16:30:55 +08:00
_PrepareUnit(createUnit);
2023-12-14 14:42:27 +08:00
return createUnit;
}
2023-12-14 19:42:05 +08:00
public async UniTask<NormalVehicle> PrepareVehicle(VehicleCultivateData vInfo, int troopId)
2023-12-14 14:58:07 +08:00
{
var nGuid = GUIDManager.GenGuid();
var dataConfig = VehicleDataHelper.CombineConfigData(vInfo);
var createUnit = new NormalVehicle(nGuid, dataConfig, troopId);
2024-02-20 13:45:32 +08:00
await createUnit.CreateAsync();
2023-12-14 15:38:17 +08:00
await createUnit.Prepare();
2023-12-14 16:30:55 +08:00
_PrepareUnit(createUnit);
2023-12-14 14:58:07 +08:00
return createUnit;
}
public BaseSummon PrepareSummon(int summondId, GameUnit creator)
{
var createSummon = SummonFactory.CreateSummon(summondId, creator);
2023-12-14 16:30:55 +08:00
_PrepareUnit(createSummon);
return createSummon;
}
2023-12-14 14:42:27 +08:00
// 将单位移动到备战席
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();
}
2023-12-14 16:30:55 +08:00
2023-12-19 13:12:21 +08:00
EventManager.Instance.Send(EventManager.EventName.INFIGHT_UNIT_REMOVE, unit);
// 单独给UI那边发一个事件,用于更新UI
2023-12-19 13:12:21 +08:00
EventManager.Instance.Send(EventManager.EventName.INFIGHT_BACK_PREPARE, unit);
_RemoveFromFightHandles(unit);
if (enableLog)
{
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 从战斗列表回到备战席");
}
2023-12-20 14:32:55 +08:00
_needUpdateTargets = true;
2023-12-14 14:42:27 +08:00
}
// 将到位移动到战斗中,并指定位置
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);
2023-12-14 19:00:39 +08:00
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;
2023-12-14 19:00:39 +08:00
}
if (enableLog)
{
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 已进入战斗序列");
}
2023-12-20 14:32:55 +08:00
_needUpdateTargets = true;
2023-12-14 14:42:27 +08:00
}
public void EnterBattle()
{
2023-12-14 18:36:48 +08:00
_needUpdateTargets = true;
2023-12-14 14:42:27 +08:00
for (int i = 0; i < infightUnits.count; i++)
{
var unit = infightUnits.GetByIndex(i);
unit.EnterBattle();
}
}
2023-12-19 15:01:19 +08:00
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()} 已复活");
}
2023-12-20 14:32:55 +08:00
2023-12-19 15:01:19 +08:00
}
else
{
DebugUtil.LogError("设计上只支持角色复活");
}
}
2023-12-14 14:42:27 +08:00
public void LogicUpdate(float dt)
{
if (_needUpdateTargets)
{
_needUpdateTargets = false;
_UpdateTargets();
}
for (int i = 0; i < infightUnits.count; i++)
{
var unit = infightUnits.GetByIndex(i);
2023-12-14 15:31:46 +08:00
2023-12-14 14:42:27 +08:00
unit.LogicUpdate(dt);
if (unit.needRemove)
{
2023-12-14 19:00:39 +08:00
_RemoveFromFight(unit);
2023-12-14 14:42:27 +08:00
i--;
}
}
}
2023-12-14 19:00:39 +08:00
private void _RemoveFromFight(GameUnit unit)
{
if (enableLog)
{
DebugUtil.LogG($"单位:{unit.GetUnitDesc()} 已移除");
}
2023-12-14 19:00:39 +08:00
unit.ExitBattle();
infightUnits.Remove(unit);
2023-12-14 19:10:53 +08:00
2023-12-19 13:12:21 +08:00
EventManager.Instance.Send(EventManager.EventName.INFIGHT_UNIT_REMOVE, unit);
_RemoveFromFightHandles(unit);
2023-12-20 14:32:55 +08:00
2023-12-26 13:24:21 +08:00
var deadToList = _CheckDeadToList(unit);
if (deadToList)
{
deadUnits.Add(unit);
}
else
{
// 销毁
unit.Dispose();
}
2023-12-20 14:32:55 +08:00
_needUpdateTargets = true;
}
2023-12-26 13:24:21 +08:00
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)
{
2023-12-14 19:10:53 +08:00
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;
2023-12-14 19:10:53 +08:00
}
2023-12-14 19:00:39 +08:00
}
2023-12-14 14:42:27 +08:00
private void _UpdateTargets()
{
2023-12-14 15:31:46 +08:00
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;
}
}
2023-12-14 14:42:27 +08:00
}
public void Dispose()
{
_UnRegistEvents();
2023-12-14 18:36:48 +08:00
for (int i = 0; i < infightUnits.count; i++)
{
var unit = infightUnits.GetByIndex(i);
unit.ExitBattle();
2023-12-14 19:10:53 +08:00
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();
2023-12-14 18:36:48 +08:00
}
2024-02-20 13:45:32 +08:00
// unload
foreach (var kv in _loadPathRecord)
{
var path = kv.Key;
var count = kv.Value;
for (int i = 0; i < count; i++)
{
AssetManager.Instance.Unload(path);
}
}
2023-12-14 14:42:27 +08:00
instance = null;
}
2023-12-25 19:00:08 +08:00
2023-12-14 14:42:27 +08:00
}
}