zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Module/Numeric/NumericWatcherComponent.cs

74 lines
1.8 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
namespace ET
{
public class NumericWatcherComponentAwakeSystem : AwakeSystem<NumericWatcherComponent>
{
public override void Awake(NumericWatcherComponent self)
{
self.Awake();
}
}
public class NumericWatcherComponentLoadSystem : LoadSystem<NumericWatcherComponent>
{
public override void Load(NumericWatcherComponent self)
{
self.Load();
}
}
/// <summary>
/// 监视数值变化组件,分发监听
/// </summary>
public class NumericWatcherComponent : Entity
{
public static NumericWatcherComponent Instance;
private Dictionary<NumericType, List<INumericWatcher>> allWatchers;
public void Awake()
{
Instance = this;
this.Load();
}
public void Load()
{
this.allWatchers = new Dictionary<NumericType, List<INumericWatcher>>();
HashSet<Type> types = Game.EventSystem.GetTypes(typeof(NumericWatcherAttribute));
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(NumericWatcherAttribute), false);
foreach (object attr in attrs)
{
NumericWatcherAttribute numericWatcherAttribute = (NumericWatcherAttribute)attr;
INumericWatcher obj = (INumericWatcher)Activator.CreateInstance(type);
if (!this.allWatchers.ContainsKey(numericWatcherAttribute.NumericType))
{
this.allWatchers.Add(numericWatcherAttribute.NumericType, new List<INumericWatcher>());
}
this.allWatchers[numericWatcherAttribute.NumericType].Add(obj);
}
}
}
public async ETTask Run(NumericType numericType, Entity id,float old,float value)
{
2021-04-18 15:54:51 +08:00
if (!id) return;
2021-04-08 20:09:59 +08:00
List<INumericWatcher> list;
if (!this.allWatchers.TryGetValue(numericType, out list))
{
return;
}
foreach (INumericWatcher numericWatcher in list)
{
await numericWatcher.Run(id,old, value);
}
}
}
}