270 lines
8.0 KiB
C#
270 lines
8.0 KiB
C#
using System.Collections.Generic;
|
|
using Gameplay.Fight.Protect;
|
|
using Gameplay.Pool;
|
|
using Gameplay.Summon;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Unit.Data;
|
|
using Gameplay.Vehicle.Impl;
|
|
|
|
namespace Gameplay.Buff
|
|
{
|
|
using cfg.BuffCfg;
|
|
using Framework;
|
|
using Bullet;
|
|
using Character;
|
|
using Skill;
|
|
|
|
public class BuffManager
|
|
{
|
|
|
|
private static BuffManager _instance;
|
|
|
|
public static BuffManager instance
|
|
{
|
|
get
|
|
{
|
|
// if (_instance == null)
|
|
// {
|
|
// DebugUtil.LogError("Buff尚未初始化,请先初始化");
|
|
// return null;
|
|
// }
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public static BuffManager CreateInstance()
|
|
{
|
|
if (_instance != null)
|
|
{
|
|
DebugUtil.LogError("请勿重复创建BuffManager");
|
|
return _instance;
|
|
}
|
|
_instance = new BuffManager();
|
|
return _instance;
|
|
}
|
|
|
|
private Dictionary<GameUnit, UnitBuffManager> _unitDict;
|
|
private List<UnitBuffManager> _unitList;
|
|
|
|
private Dictionary<int, SideBuffManager> _sideBuffManagerDict;
|
|
|
|
private BuffManager()
|
|
{
|
|
_unitDict = new Dictionary<GameUnit, UnitBuffManager>();
|
|
_unitList = new List<UnitBuffManager>();
|
|
_sideBuffManagerDict = new Dictionary<int, SideBuffManager>();
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
DebugUtil.Log("[Init]------BuffManager");
|
|
|
|
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_READY, _OnUnitReady);
|
|
EventManager.Instance.Register<GameUnit>(EventManager.EventName.INFIGHT_UNIT_REMOVE, _OnUnitRemove);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
DebugUtil.Log("[Dispose]------BuffManager");
|
|
|
|
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_READY, _OnUnitReady);
|
|
EventManager.Instance.Unregister<GameUnit>(EventManager.EventName.INFIGHT_UNIT_REMOVE, _OnUnitRemove);
|
|
|
|
_unitDict.Clear();
|
|
_unitList.Clear();
|
|
_sideBuffManagerDict.Clear();
|
|
|
|
_unitDict = null;
|
|
_unitList = null;
|
|
_sideBuffManagerDict = null;
|
|
|
|
_instance = null;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public static DataBuff GetBuffConfigById(int id)
|
|
{
|
|
return TableManager.Instance.Tables.BuffConfig.Get(id);
|
|
}
|
|
|
|
public BaseBuff CreateBuffById(GameUnit creator,
|
|
int id)
|
|
{
|
|
var buffConfig = GetBuffConfigById(id);
|
|
if (buffConfig == null)
|
|
{
|
|
return null;
|
|
}
|
|
return CreateBuffByConfig(creator, buffConfig);
|
|
}
|
|
|
|
public BaseBuff CreateBuffByConfig(GameUnit creator,
|
|
DataBuff buffConfig)
|
|
{
|
|
var buff = BuffFactory.CreateBuff(buffConfig.BuffType);
|
|
if (buff == null)
|
|
{
|
|
return null;
|
|
}
|
|
buff.Ctor(creator, buffConfig);
|
|
return buff;
|
|
}
|
|
|
|
public static void ReturnBuff<T>(T buff) where T : BaseBuff
|
|
{
|
|
ObjPoolManager.instance.Return(buff);
|
|
}
|
|
|
|
public void AddBuff(GameUnit unit,
|
|
BaseBuff buff)
|
|
{
|
|
if (buff == null)
|
|
{
|
|
return;
|
|
}
|
|
switch (unit.gameunitType)
|
|
{
|
|
case EGameUnitType.Character:
|
|
case EGameUnitType.Vehicle:
|
|
case EGameUnitType.Summon:
|
|
case EGameUnitType.BeProtect:
|
|
if (_unitDict.TryGetValue(unit, out var unitManager))
|
|
{
|
|
unitManager.AddBuff(buff);
|
|
}
|
|
else
|
|
{
|
|
// 此unit已经从战场中移除
|
|
}
|
|
break;
|
|
default:
|
|
DebugUtil.LogError("暂未实现的gameunit类型:{0}", unit.gameunitType);
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
|
|
// 默认全局buff
|
|
var autoRecoverMoraleBuff = CreateBuffById(c, InnerBuffID.AUTO_RECOVER_MORALE);
|
|
unitManager.AddBuff(autoRecoverMoraleBuff);
|
|
|
|
// 双向绑定
|
|
c.buffManager = unitManager;
|
|
}
|
|
|
|
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);
|
|
|
|
// 双向绑定
|
|
summon.buffManager = unitManager;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public SideBuffManager GetSideBuffManager(int troopId)
|
|
{
|
|
if (_sideBuffManagerDict.TryGetValue(troopId, out var sideBuffManager))
|
|
{
|
|
return sideBuffManager;
|
|
}
|
|
sideBuffManager = new SideBuffManager(troopId);
|
|
_sideBuffManagerDict.Add(troopId, sideBuffManager);
|
|
return sideBuffManager;
|
|
}
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
if (SkillManager.instance.needSkillPaused)
|
|
{
|
|
for (var i = 0; i < _unitList.Count; ++i)
|
|
{
|
|
var unit = _unitList[i];
|
|
unit.UpdateOnlyRemoveAdd();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (var i = 0; i < _unitList.Count; ++i)
|
|
{
|
|
var unit = _unitList[i];
|
|
unit.LogicUpdate(dt);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|