NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Buff/BuffManager.cs

262 lines
7.7 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
using System.Collections.Generic;
2024-06-04 14:57:24 +08:00
using Gameplay.Fight.Protect;
2023-10-19 15:00:19 +08:00
using Gameplay.Pool;
2024-03-13 13:15:12 +08:00
using Gameplay.Summon;
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;
namespace Gameplay.Buff
{
using cfg.BuffCfg;
using Framework;
2023-10-19 15:00:19 +08:00
using Bullet;
using Character;
using Skill;
2023-08-22 19:08:45 +08:00
public class BuffManager
{
private static BuffManager _instance;
public static BuffManager instance
{
get
{
// if (_instance == null)
// {
// DebugUtil.LogError("Buff尚未初始化,请先初始化");
// return null;
// }
2023-08-22 19:08:45 +08:00
return _instance;
}
}
public static BuffManager CreateInstance()
{
if (_instance != null)
{
DebugUtil.LogError("请勿重复创建BuffManager");
return _instance;
}
_instance = new BuffManager();
return _instance;
}
2024-02-20 15:17:22 +08:00
private Dictionary<GameUnit, UnitBuffManager> _unitDict;
private List<UnitBuffManager> _unitList;
2023-08-22 19:08:45 +08:00
2024-02-20 15:17:22 +08:00
private Dictionary<int, SideBuffManager> _sideBuffManagerDict;
2023-11-15 15:20:43 +08:00
2023-08-22 19:08:45 +08:00
private BuffManager()
{
_unitDict = new Dictionary<GameUnit, UnitBuffManager>();
_unitList = new List<UnitBuffManager>();
2023-11-15 15:20:43 +08:00
_sideBuffManagerDict = new Dictionary<int, SideBuffManager>();
2023-08-22 19:08:45 +08:00
}
public void Init()
{
2023-10-19 15:00:19 +08:00
DebugUtil.Log("[Init]------BuffManager");
2023-12-19 13:28:41 +08:00
2023-11-16 12:27:59 +08:00
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_READY, _OnUnitReady);
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_REMOVE, _OnUnitRemove);
2023-08-22 19:08:45 +08:00
}
2023-12-19 13:28:41 +08:00
public void Dispose()
2023-10-19 15:00:19 +08:00
{
DebugUtil.Log("[Dispose]------BuffManager");
2023-12-19 13:28:41 +08:00
2023-11-16 12:27:59 +08:00
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_READY, _OnUnitReady);
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_REMOVE, _OnUnitRemove);
2024-03-13 13:15:12 +08:00
2024-02-20 15:17:22 +08:00
_unitDict.Clear();
_unitList.Clear();
_sideBuffManagerDict.Clear();
2024-03-13 13:15:12 +08:00
2024-02-20 15:17:22 +08:00
_unitDict = null;
_unitList = null;
_sideBuffManagerDict = null;
2024-03-13 13:15:12 +08:00
_instance = null;
2023-10-19 15:00:19 +08:00
}
2023-11-16 12:27:59 +08:00
private void _OnUnitReady(GameUnit unit)
{
var troopId = unit.commonData.troopId;
var sideBuffManager = GetSideBuffManager(troopId);
sideBuffManager.AddToUnit(unit);
}
private void _OnUnitRemove(GameUnit unit)
{
var troopId = unit.commonData.troopId;
var sideBuffManager = GetSideBuffManager(troopId);
sideBuffManager.RemoveFromUnit(unit);
}
2023-10-19 15:00:19 +08:00
public static DataBuff GetBuffConfigById(int id)
2023-08-22 19:08:45 +08:00
{
return TableManager.Instance.Tables.BuffConfig.Get(id);
}
2023-10-19 15:00:19 +08:00
public BaseBuff CreateBuffById(GameUnit creator,
2023-08-22 19:08:45 +08:00
int id)
{
var buffConfig = GetBuffConfigById(id);
2024-07-09 18:09:24 +08:00
if (buffConfig == null)
{
return null;
}
return CreateBuffByConfig(creator, buffConfig);
}
public BaseBuff CreateBuffByConfig(GameUnit creator,
DataBuff buffConfig)
{
var buff = BuffFactory.CreateBuff(buffConfig.BuffType);
2024-03-25 19:10:22 +08:00
if (buff == null)
2023-08-22 19:08:45 +08:00
{
2024-03-25 19:10:22 +08:00
return null;
2023-10-19 15:00:19 +08:00
}
2024-03-25 19:10:22 +08:00
buff.Ctor(creator, buffConfig);
2023-08-22 19:08:45 +08:00
return buff;
}
2023-10-19 15:00:19 +08:00
public static void ReturnBuff<T>(T buff) where T : BaseBuff
{
ObjPoolManager.instance.Return(buff);
}
2023-08-22 19:08:45 +08:00
public void AddBuff(GameUnit unit,
BaseBuff buff)
{
if (buff == null)
{
return;
}
2023-08-22 19:08:45 +08:00
switch (unit.gameunitType)
{
case EGameUnitType.Character:
case EGameUnitType.Vehicle:
2024-03-13 13:15:12 +08:00
case EGameUnitType.Summon:
2024-06-04 14:57:24 +08:00
case EGameUnitType.BeProtect:
2023-12-19 13:28:41 +08:00
if (_unitDict.TryGetValue(unit, out var unitManager))
{
unitManager.AddBuff(buff);
}
else
{
// 此unit已经从战场中移除
}
2023-08-22 19:08:45 +08:00
break;
default:
2024-03-13 13:15:12 +08:00
DebugUtil.LogError("暂未实现的gameunit类型:{0}", unit.gameunitType);
2023-08-22 19:08:45 +08:00
break;
}
}
public void InitVehicle(NormalVehicle vehicle)
{
if (_unitDict.ContainsKey(vehicle))
{
DebugUtil.LogError("FATA ERROR: 不应该出现两个相同的vehicle");
return;
}
var unitManager = new UnitBuffManager(vehicle);
_unitDict.Add(vehicle, unitManager);
_unitList.Add(unitManager);
// 双向绑定
vehicle.buffManager = unitManager;
}
2024-06-04 14:57:24 +08:00
public void InitBeProtectUnit(BeProtectUnit beProtectUnit)
{
if (_unitDict.ContainsKey(beProtectUnit))
{
DebugUtil.LogError("FATA ERROR: 不应该出现两个相同的beProtectUnit");
return;
}
var unitManager = new UnitBuffManager(beProtectUnit);
_unitDict.Add(beProtectUnit, unitManager);
_unitList.Add(unitManager);
// 双向绑定
beProtectUnit.buffManager = unitManager;
}
2023-08-22 19:08:45 +08:00
public void InitCharacter(Character c)
{
if (_unitDict.ContainsKey(c))
{
DebugUtil.LogError("FATA ERROR: 不应该出现两个相同的character");
return;
}
var unitManager = new UnitBuffManager(c);
_unitDict.Add(c, unitManager);
_unitList.Add(unitManager);
2023-11-15 16:09:10 +08:00
// 默认全局buff
2023-10-19 15:00:19 +08:00
var autoRecoverMoraleBuff = CreateBuffById(c, InnerBuffID.AUTO_RECOVER_MORALE);
2023-08-22 19:08:45 +08:00
unitManager.AddBuff(autoRecoverMoraleBuff);
// 双向绑定
c.buffManager = unitManager;
}
2024-03-13 13:15:12 +08:00
public void InitSummon(BaseSummon summon)
{
if (_unitDict.ContainsKey(summon))
{
DebugUtil.LogError("FATA ERROR: 不应该出现两个相同的summon");
return;
}
var unitManager = new UnitBuffManager(summon);
_unitDict.Add(summon, unitManager);
_unitList.Add(unitManager);
2024-03-25 19:10:22 +08:00
2024-03-13 13:15:12 +08:00
// 双向绑定
summon.buffManager = unitManager;
}
2023-08-22 19:08:45 +08:00
public void RemoveCharacter(Character c)
{
if (!_unitDict.ContainsKey(c))
{
DebugUtil.LogError("FATA ERROR: 不应该出现不存在的character");
return;
}
var unitManager = _unitDict[c];
_unitDict.Remove(c);
_unitList.Remove(unitManager);
}
2023-11-16 12:27:59 +08:00
public SideBuffManager GetSideBuffManager(int troopId)
{
if (_sideBuffManagerDict.TryGetValue(troopId, out var sideBuffManager))
{
return sideBuffManager;
}
sideBuffManager = new SideBuffManager(troopId);
_sideBuffManagerDict.Add(troopId, sideBuffManager);
return sideBuffManager;
}
2023-08-22 19:08:45 +08:00
public void LogicUpdate(float dt)
{
if (SkillManager.instance.needSkillPaused)
{
return;
}
for (var i = 0; i < _unitList.Count; ++i)
{
var unit = _unitList[i];
unit.LogicUpdate(dt);
}
}
}
}