CTT/Server/Hotfix/Game/System/Bag/GoodsEffectComponentSystem.cs

189 lines
7.1 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using Cal.DataTable;
using System;
using System.Collections.Generic;
namespace ET
{
public class GoodsEffectComponentAwakeystem : AwakeSystem<GoodsEffectComponent>
{
public override void Awake(GoodsEffectComponent self)
{
GoodsEffectComponent.Instance = self;
}
}
public class GoodsEffectComponentUpdateSystem : UpdateSystem<GoodsEffectComponent>
{
public override void Update(GoodsEffectComponent self)
{
self.Update();
}
}
public static class GoodsEffectComponentSystem
{
public static string AcquireGoodsEffect(this GoodsEffectComponent self, Unit unit, GoodsBase goodsBase, bool isLock)
{
try
{
2021-04-11 19:50:39 +08:00
GoodsEffectType effect = (GoodsEffectType)goodsBase.EffectType;
2021-04-08 20:09:59 +08:00
Type tempType = self.GetEffectType(effect);
2021-04-11 19:50:39 +08:00
if (self._goodsEffectPoolDic.TryGetValue(tempType, out Queue<GoodsEffect> buffBase))
2021-04-08 20:09:59 +08:00
{
if (buffBase.Count > 0)
{
2021-04-11 19:50:39 +08:00
GoodsEffect tempBuffBase = buffBase.Dequeue();
2021-04-08 20:09:59 +08:00
return tempBuffBase.Init(unit, goodsBase, isLock);
}
}
2021-04-11 19:50:39 +08:00
GoodsEffect temp = (GoodsEffect)Activator.CreateInstance(tempType);
2021-04-08 20:09:59 +08:00
return temp.Init(unit, goodsBase, isLock);
}
catch (Exception e)
{
Log.Error(e);
}
return string.Empty;
}
//!断线重连时调用
public static void ReAcquireGoodsEffect(this GoodsEffectComponent self, Unit unit, GoodsBase goodsBase, long endTime)
{
try
{
2021-04-11 19:50:39 +08:00
GoodsEffectType effect = (GoodsEffectType)goodsBase.EffectType;
2021-04-08 20:09:59 +08:00
Type tempType = self.GetEffectType(effect);
2021-04-11 19:50:39 +08:00
if (self._goodsEffectPoolDic.TryGetValue(tempType, out Queue<GoodsEffect> buffBase))
2021-04-08 20:09:59 +08:00
{
if (buffBase.Count > 0)
{
2021-04-11 19:50:39 +08:00
GoodsEffect tempBuffBase = buffBase.Dequeue();
2021-04-08 20:09:59 +08:00
tempBuffBase.ReInit(unit, goodsBase, endTime);
return;
}
}
2021-04-11 19:50:39 +08:00
GoodsEffect temp = (GoodsEffect)Activator.CreateInstance(tempType);
2021-04-08 20:09:59 +08:00
temp.ReInit(unit, goodsBase, endTime);
}
catch (Exception e)
{
Log.Error(e);
}
}
private static Type GetEffectType(this GoodsEffectComponent self, GoodsEffectType effectType) => effectType switch
{
GoodsEffectType.AddHpOrMp => typeof(AddHpOrMpGoodsEffect),
GoodsEffectType.AddHp => typeof(AddHpGoodsEffect),
GoodsEffectType.AddMp => typeof(AddMpGoodsEffect),
GoodsEffectType.AddGoods => typeof(AddGoodsEffect),
GoodsEffectType.AddEquipOrMaterialBySpecial => typeof(AddEquipOrMaterialBySpecialEffect),
GoodsEffectType.AddSpecialGoods => typeof(AddSpecialGoodsEffect),
GoodsEffectType.BackHome => typeof(BackHomeGoodsEffect),
GoodsEffectType.AddHpBuff => typeof(AddHpBuffGoodsEffect),
GoodsEffectType.AddMpBuff => typeof(AddMpBuffGoodsEffect),
GoodsEffectType.AddBattleExpSpeed => typeof(AddBattleExpSpeedGoodsEffect),
GoodsEffectType.AddIdleExpSpeed => typeof(AddIdleExpSpeedGoodsEffect),
GoodsEffectType.ChatHorn => throw new NotImplementedException(),
2021-04-15 00:12:07 +08:00
GoodsEffectType.AddPet =>typeof(AddPetGoodsEffect),
2021-04-08 20:09:59 +08:00
GoodsEffectType.AddPetExpSpeed => throw new NotImplementedException(),
2021-04-15 00:12:07 +08:00
// GoodsEffectType.AddPetIntimacy => typeof(AddPetIntimacyGoodsEffect),
2021-04-08 20:09:59 +08:00
GoodsEffectType.LearnSkill => typeof(LearnSkillGoodsEffect),
GoodsEffectType.AddEnergy => typeof(AddEnergyGoodsEffect),
GoodsEffectType.AddMainAITime => typeof(AddMainAITimeGoodsEffect),
GoodsEffectType.AddMainVIPAITime => typeof(AddMainVIPAITimeGoodsEffect),
_ => throw new Exception("Goods 类型名错误,检查表格"),
};
public static void AddGoodsBuff(this GoodsEffectComponent self, long id, GoodsEffectType type, GoodsEffect goodsEffect)
{
try
{
2021-04-11 19:50:39 +08:00
if (!self._goodsEffectBuffDic.TryGetValue(id, out Dictionary<int, GoodsEffect> dic))
2021-04-08 20:09:59 +08:00
{
dic = self._goodsEffectBuffDic[id] = new Dictionary<int, GoodsEffect>();
}
dic[(int)type] = goodsEffect;
}
catch (Exception e)
{
Log.Error(e);
}
}
public static GoodsEffect GetGoodsBuffType(this GoodsEffectComponent self, long id, GoodsEffectType type)
{
try
{
2021-04-11 19:50:39 +08:00
if (self._goodsEffectBuffDic.TryGetValue(id, out Dictionary<int, GoodsEffect> dic))
2021-04-08 20:09:59 +08:00
{
2021-04-11 19:50:39 +08:00
if (dic.TryGetValue((int)type, out GoodsEffect goodsEffect))
2021-04-08 20:09:59 +08:00
{
return goodsEffect;
}
}
}
catch (Exception e)
{
Log.Error(e);
}
return null;
}
public static IEnumerable<GoodsEffect> GetAllEffect(this GoodsEffectComponent self, long id)
{
2021-04-11 19:50:39 +08:00
if (self._goodsEffectBuffDic.TryGetValue(id, out Dictionary<int, GoodsEffect> dic))
2021-04-08 20:09:59 +08:00
{
return dic.Values;
}
return null;
}
private static List<ValueTuple<long, int>> _needRemoveList = new List<ValueTuple<long, int>>();
internal static void Update(this GoodsEffectComponent self)
{
try
{
2021-04-11 19:50:39 +08:00
foreach (KeyValuePair<long, Dictionary<int, GoodsEffect>> kv in self._goodsEffectBuffDic)
2021-04-08 20:09:59 +08:00
{
2021-04-11 19:50:39 +08:00
Dictionary<int, GoodsEffect> dic = kv.Value;
foreach (KeyValuePair<int, GoodsEffect> _kv in dic)
2021-04-08 20:09:59 +08:00
{
2021-04-11 19:50:39 +08:00
GoodsEffect effect = _kv.Value;
2021-04-12 23:38:54 +08:00
switch (effect.BuffState)
2021-04-08 20:09:59 +08:00
{
2021-04-12 23:38:54 +08:00
case BuffState.Running:
effect.Execute();
break;
case BuffState.Finished:
_needRemoveList.Add((kv.Key, _kv.Key));
break;
2021-04-08 20:09:59 +08:00
}
}
}
2021-04-12 23:38:54 +08:00
if (_needRemoveList.Count == 0)
{
return;
}
foreach ((long id, int type) in _needRemoveList)
2021-04-08 20:09:59 +08:00
{
2021-04-12 23:38:54 +08:00
self._goodsEffectBuffDic[id].Remove(type);
if (self._goodsEffectBuffDic[id].Count == 0)
2021-04-08 20:09:59 +08:00
{
2021-04-12 23:38:54 +08:00
self._goodsEffectBuffDic.Remove(type);
2021-04-08 20:09:59 +08:00
}
}
2021-04-12 23:38:54 +08:00
_needRemoveList.Clear();
2021-04-08 20:09:59 +08:00
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}