Kaynağa Gözat

增加一个KV组件,可以用来保存KeyValue数据

tanghai 9 yıl önce
ebeveyn
işleme
5d98c2ccd6

+ 63 - 0
Unity/Assets/Scripts/Component/KVComponent.cs

@@ -0,0 +1,63 @@
+using System.Collections.Generic;
+using System.Linq;
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Base
+{
+	/// <summary>
+	/// Key Value组件用于保存一些数据
+	/// </summary>
+    public class KVComponent<T> : Component<T> where T: Entity<T>
+    {
+		[BsonElement]
+		private readonly Dictionary<string, object> kv = new Dictionary<string, object>();
+		
+		public void Add(string key, object value)
+		{
+			this.kv.Add(key, value);
+		}
+		
+		public void Remove(string key)
+		{
+			this.kv.Remove(key);
+		}
+
+		public K Get<K>(string key)
+		{
+			object k;
+			if (!this.kv.TryGetValue(key, out k))
+			{
+				return default(K);
+			}
+			return (K)k;
+		}
+
+		public override void Dispose()
+		{
+			if (this.Id == 0)
+			{
+				return;
+			}
+
+			base.Dispose();
+		}
+    }
+
+	public static class KVHelper
+	{
+		public static void Add<T>(this Entity<T> entity, string key, T value) where T : Entity<T>
+		{
+			entity.GetComponent<KVComponent<T>>().Add(key, value);
+		}
+
+		public static void Remove<T>(this Entity<T> entity, string key) where T : Entity<T>
+		{
+			entity.GetComponent<KVComponent<T>>().Remove(key);
+		}
+
+		public static void Get<T, K>(this Entity<T> entity, string key) where T : Entity<T>
+		{
+			entity.GetComponent<KVComponent<T>>().Get<K>(key);
+		}
+	}
+}

+ 24 - 19
Unity/Assets/Scripts/Component/LevelComponent.cs

@@ -12,13 +12,12 @@ namespace Base
 		[BsonIgnore]
 		public T Parent { get; private set; }
 
-		private List<T> children;
-
-		[BsonElement, BsonIgnoreIfNull]
-		private Dictionary<long, T> idChildren;
-
-		[BsonElement, BsonIgnoreIfNull]
-		private Dictionary<string, T> nameChildren;
+		[BsonElement]
+		private readonly List<T> children = new List<T>();
+		
+		private readonly Dictionary<long, T> idChildren = new Dictionary<long, T>();
+		
+		private readonly Dictionary<string, T> nameChildren = new Dictionary<string, T>();
 
 		[BsonIgnore]
 		public int Count
@@ -32,12 +31,6 @@ namespace Base
 		public void Add(T t)
 		{
 			t.GetComponent<LevelComponent<T>>().Parent = this.Owner;
-			if (this.idChildren == null)
-			{
-				this.children = new List<T>();
-				this.idChildren = new Dictionary<long, T>();
-				this.nameChildren = new Dictionary<string, T>();
-			}
 			this.children.Add(t);
 			this.idChildren.Add(t.Id, t);
 			this.nameChildren.Add(t.Name, t);
@@ -52,12 +45,6 @@ namespace Base
 		{
 			this.idChildren.Remove(t.Id);
 			this.nameChildren.Remove(t.Name);
-			if (this.idChildren.Count == 0)
-			{
-				this.children = null;
-				this.idChildren = null;
-				this.nameChildren = null;
-			}
 			t.Dispose();
 		}
 
@@ -98,4 +85,22 @@ namespace Base
 			this.Parent?.GetComponent<LevelComponent<T>>().Remove(this.Id);
 		}
     }
+
+	public static class LevelHelper
+	{
+		public static void AddChild<T>(this Entity<T> entity, T t) where T : Entity<T>
+		{
+			entity.GetComponent<LevelComponent<T>>().Add(t);
+		}
+
+		public static void RemoveChild<T>(this Entity<T> entity, long id) where T : Entity<T>
+		{
+			entity.GetComponent<LevelComponent<T>>().Remove(id);
+		}
+
+		public static void RemoveChild<T>(this Entity<T> entity, string name) where T : Entity<T>
+		{
+			entity.GetComponent<LevelComponent<T>>().Remove(name);
+		}
+	}
 }

+ 1 - 0
Unity/Controller/Controller.csproj

@@ -72,6 +72,7 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
+    <Folder Include="Helper\" />
     <Folder Include="Logic\" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

+ 1 - 0
Unity/Unity.CSharp.csproj

@@ -131,6 +131,7 @@
     <Compile Include="Assets\Scripts\Base\ReferenceCollector.cs" />
     <Compile Include="Assets\Scripts\Base\TryLocker.cs" />
     <Compile Include="Assets\Scripts\Component\Game.cs" />
+    <Compile Include="Assets\Scripts\Component\KVComponent.cs" />
     <Compile Include="Assets\Scripts\Component\Scene.cs" />
     <Compile Include="Assets\Scripts\Component\Scene\EventComponent.cs" />
     <Compile Include="Assets\Scripts\Component\Scene\GlobalConfigComponent.cs" />