Kaynağa Gözat

添加一个数值组件,一个数值监听组件

tanghai 8 yıl önce
ebeveyn
işleme
d2f4add035

+ 1 - 0
Unity/Assets/Scripts/Base/Event/EventIdType.cs

@@ -16,5 +16,6 @@
 		BehaviorTreeRightDesignerDrag,
 
 		SessionRecvMessage,
+		NumbericChange,
 	}
 }

+ 10 - 0
Unity/Assets/Scripts/Base/Message/NumericType.cs

@@ -10,5 +10,15 @@
 	    SpeedPct = Speed * 10 + 3,
 	    SpeedFinalAdd = Speed * 10 + 4,
 	    SpeedFinalPct = Speed * 10 + 5,
+
+	    Hp = 1001,
+	    HpBase = Hp * 10 + 1,
+
+	    MaxHp = 1002,
+	    MaxHpBase = MaxHp * 10 + 1,
+	    MaxHpAdd = MaxHp * 10 + 2,
+	    MaxHpPct = MaxHp * 10 + 3,
+	    MaxHpFinalAdd = MaxHp * 10 + 4,
+		MaxHpFinalPct = MaxHp * 10 + 5,
 	}
 }

+ 89 - 0
Unity/Assets/Scripts/Component/NumericComponent.cs

@@ -0,0 +1,89 @@
+using System.Collections.Generic;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class NumericComponentEvent : ObjectEvent<NumericComponent>, IAwake
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+	}
+	
+	public class NumericComponent: Component
+	{
+		public readonly Dictionary<int, int> NumericDic = new Dictionary<int, int>();
+
+		public void Awake()
+		{
+			// 这里初始化base值
+		}
+
+		public float GetAsFloat(NumericType numericType)
+		{
+			return (float)GetByKey((int)numericType) / 10000;
+		}
+
+		public int GetAsInt(NumericType numericType)
+		{
+			return GetByKey((int)numericType);
+		}
+
+		public void Set(NumericType nt, float value)
+		{
+			this[nt] = (int) (value * 10000);
+		}
+
+		public void Set(NumericType nt, int value)
+		{
+			this[nt] = value;
+		}
+
+		public int this[NumericType numericType]
+		{
+			get
+			{
+				return this.GetByKey((int) numericType);
+			}
+			set
+			{
+				int v = this.GetByKey((int) numericType);
+				if (v == value)
+				{
+					return;
+				}
+
+				NumericDic[(int)numericType] = value;
+
+				Update(numericType);
+			}
+		}
+
+		private int GetByKey(int key)
+		{
+			int value = 0;
+			this.NumericDic.TryGetValue(key, out value);
+			return value;
+		}
+
+		public void Update(NumericType numericType)
+		{
+			if (numericType < NumericType.Max)
+			{
+				return;
+			}
+			int final = (int) numericType / 10;
+			int bas = final * 10 + 1; 
+			int add = final * 10 + 2;
+			int pct = final * 10 + 3;
+			int finalAdd = final * 10 + 4;
+			int finalPct = final * 10 + 5;
+
+			// 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果
+			// final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
+			this.NumericDic[final] = ((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetByKey(pct)) / 100 + this.GetByKey(finalAdd)) * (100 + this.GetByKey(finalPct)) / 100;
+			Game.Scene.GetComponent<EventComponent>().Run(EventIdType.NumbericChange, this.Entity.Id, numericType, final);
+		}
+	}
+}

+ 67 - 0
Unity/Assets/Scripts/Component/NumericWatcherComponent.cs

@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class NumericWatcherComponentEvent : ObjectEvent<NumericWatcherComponent>, IAwake, ILoad
+	{
+		public void Awake()
+		{
+			this.Get().Awake();
+		}
+
+		public void Load()
+		{
+			this.Get().Load();
+		}
+	}
+	
+	/// <summary>
+	/// 监视数值变化组件,分发监听
+	/// </summary>
+	public class NumericWatcherComponent : Component
+	{
+		private Dictionary<NumericType, List<INumericWatcher>> allWatchers;
+
+		public void Awake()
+		{
+			this.Load();
+		}
+
+		public void Load()
+		{
+			this.allWatchers = new Dictionary<NumericType, List<INumericWatcher>>();
+
+			Type[] types = DllHelper.GetMonoTypes();
+			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 void Run(NumericType numericType, long id, int value)
+		{
+			List<INumericWatcher> list;
+			if (!this.allWatchers.TryGetValue(numericType, out list))
+			{
+				return;
+			}
+			foreach (INumericWatcher numericWatcher in list)
+			{
+				numericWatcher.Run(id, value);
+			}
+		}
+	}
+}

+ 12 - 0
Unity/Assets/Scripts/Event/NumericChangeEvent_NotifyWatcher.cs

@@ -0,0 +1,12 @@
+namespace Model
+{
+	// 分发数值监听
+	[Event((int)EventIdType.NumbericChange)]
+	public class NumericChangeEvent_NotifyWatcher: IEvent<NumericType, long, int>
+	{
+		public void Run(NumericType numericType, long id, int value)
+		{
+			Game.Scene.GetComponent<NumericWatcherComponent>().Run(numericType, id, value);
+		}
+	}
+}

+ 13 - 0
Unity/Assets/Scripts/Event/NumericWatcher_Hp_ShowUI.cs

@@ -0,0 +1,13 @@
+namespace Model
+{
+	/// <summary>
+	/// 监视hp数值变化,改变血条值
+	/// </summary>
+	[NumericWatcher(NumericType.Hp)]
+	public class NumericWatcher_Hp_ShowUI : INumericWatcher
+	{
+		public void Run(long id, int value)
+		{
+		}
+	}
+}

+ 7 - 0
Unity/Assets/Scripts/Other/INumericWatcher.cs

@@ -0,0 +1,7 @@
+namespace Model
+{
+	public interface INumericWatcher
+	{
+		void Run(long id, int value);
+	}
+}

+ 14 - 0
Unity/Assets/Scripts/Other/NumericWatcherAttribute.cs

@@ -0,0 +1,14 @@
+using System;
+
+namespace Model
+{
+	public class NumericWatcherAttribute : Attribute
+	{
+		public NumericType NumericType { get; }
+
+		public NumericWatcherAttribute(NumericType type)
+		{
+			this.NumericType = type;
+		}
+	}
+}

+ 6 - 0
Unity/Unity.csproj

@@ -531,6 +531,8 @@
     <Compile Include="Assets\Scripts\Component\MoveComponent.cs" />
     <Compile Include="Assets\Scripts\Component\NetOuterComponent.cs" />
     <Compile Include="Assets\Scripts\Component\NetworkComponent.cs" />
+    <Compile Include="Assets\Scripts\Component\NumericComponent.cs" />
+    <Compile Include="Assets\Scripts\Component\NumericWatcherComponent.cs" />
     <Compile Include="Assets\Scripts\Component\OpcodeTypeComponent.cs" />
     <Compile Include="Assets\Scripts\Component\OperaComponent.cs" />
     <Compile Include="Assets\Scripts\Component\PlayerComponent.cs" />
@@ -550,6 +552,8 @@
     <Compile Include="Assets\Scripts\Entity\Session.cs" />
     <Compile Include="Assets\Scripts\Entity\Unit.cs" />
     <Compile Include="Assets\Scripts\Entity\WWWAsync.cs" />
+    <Compile Include="Assets\Scripts\Event\NumericChangeEvent_NotifyWatcher.cs" />
+    <Compile Include="Assets\Scripts\Event\NumericWatcher_Hp_ShowUI.cs" />
     <Compile Include="Assets\Scripts\Factory\PlayerFactory.cs" />
     <Compile Include="Assets\Scripts\Factory\UnitFactory.cs" />
     <Compile Include="Assets\Scripts\Handler\Actor_CreateUnitsHandler.cs" />
@@ -568,8 +572,10 @@
     <Compile Include="Assets\Scripts\Other\Define.cs" />
     <Compile Include="Assets\Scripts\Other\IInstanceMethod.cs" />
     <Compile Include="Assets\Scripts\Other\ILMethod.cs" />
+    <Compile Include="Assets\Scripts\Other\INumericWatcher.cs" />
     <Compile Include="Assets\Scripts\Other\MonoMethod.cs" />
     <Compile Include="Assets\Scripts\Other\MotionType.cs" />
+    <Compile Include="Assets\Scripts\Other\NumericWatcherAttribute.cs" />
     <Compile Include="Assets\Scripts\Other\ReferenceCollector.cs" />
   </ItemGroup>
   <ItemGroup>