Explorar o código

增加buff模块

tanghai %!s(int64=12) %!d(string=hai) anos
pai
achega
607a054ccd

+ 0 - 3
CSharp/CSharp.sln

@@ -341,7 +341,4 @@ Global
 		{C4E7A34A-095C-4983-AB63-FC2D20CD6824} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{DD6F4735-E8E2-4F10-AE2B-434AB3A40236} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 	EndGlobalSection
-	GlobalSection(Performance) = preSolution
-		HasPerformanceSessions = true
-	EndGlobalSection
 EndGlobal

+ 8 - 0
CSharp/Game/Component/Buff.cs

@@ -0,0 +1,8 @@
+
+namespace Component
+{
+	public class Buff: Object
+	{
+		public int Type { get; set; }
+	}
+}

+ 105 - 0
CSharp/Game/Component/BuffManager.cs

@@ -0,0 +1,105 @@
+using System.Collections.Generic;
+using System.ComponentModel;
+using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Component
+{
+	public class BuffManager: ISupportInitialize
+	{
+		public HashSet<Buff> Buffs { get; private set; }
+
+		[BsonIgnore]
+		public Dictionary<ObjectId, Buff> BuffGuidDict { get; private set; }
+
+		[BsonIgnore]
+		public Dictionary<int, Buff> BuffTypeDict { get; private set; }
+
+		void ISupportInitialize.BeginInit()
+		{
+		}
+
+		void ISupportInitialize.EndInit()
+		{
+			foreach (var buff in Buffs)
+			{
+				this.BuffGuidDict.Add(buff.Id, buff);
+			}
+
+			foreach (var buff in Buffs)
+			{
+				this.BuffTypeDict.Add(buff.Type, buff);
+			}
+		}
+
+		public bool Add(Buff buff)
+		{
+			if (!this.Buffs.Contains(buff))
+			{
+				return false;
+			}
+
+			if (!this.BuffGuidDict.ContainsKey(buff.Id))
+			{
+				return false;
+			}
+
+			if (!this.BuffTypeDict.ContainsKey(buff.Type))
+			{
+				return false;
+			}
+
+			this.Buffs.Add(buff);
+			this.BuffGuidDict.Add(buff.Id, buff);
+			this.BuffTypeDict.Add(buff.Type, buff);
+
+			return true;
+		}
+
+		public Buff Get(ObjectId guid)
+		{
+			if (!this.BuffGuidDict.ContainsKey(guid))
+			{
+				return null;
+			}
+
+			return this.BuffGuidDict[guid];
+		}
+
+		public Buff Get(int type)
+		{
+			if (!this.BuffTypeDict.ContainsKey(type))
+			{
+				return null;
+			}
+
+			return this.BuffTypeDict[type];
+		}
+
+		private bool Remove(Buff buff)
+		{
+			if (buff == null)
+			{
+				return false;
+			}
+
+			this.Buffs.Remove(buff);
+			this.BuffGuidDict.Remove(buff.Id);
+			this.BuffTypeDict.Remove(buff.Type);
+
+			return true;
+		}
+
+		public bool Remove(ObjectId guid)
+		{
+			var buff = this.Get(guid);
+			return this.Remove(buff);
+		}
+
+		public bool Remove(int type)
+		{
+			var buff = this.Get(type);
+			return this.Remove(buff);
+		}
+	}
+}

+ 2 - 0
CSharp/Game/Component/Component.csproj

@@ -50,6 +50,8 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Buff.cs" />
+    <Compile Include="BuffManager.cs" />
     <Compile Include="Object.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 5 - 0
CSharp/Game/Component/Object.cs

@@ -40,5 +40,10 @@ namespace Component
 		{
 			return this.Dict.ContainsKey(key);
 		}
+
+		public bool Remove(string key)
+		{
+			return this.Dict.Remove(key);
+		}
 	}
 }

+ 38 - 0
CSharp/Game/ComponentTest/BuffManagerTest.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComponentTest
+{
+	/// <summary>
+	/// BuffManagerTest
+	/// </summary>
+	[TestClass]
+	public class BuffManagerTest
+	{
+		public BuffManagerTest()
+		{
+		}
+
+		private TestContext testContextInstance;
+
+		public TestContext TestContext
+		{
+			get
+			{
+				return testContextInstance;
+			}
+			set
+			{
+				testContextInstance = value;
+			}
+		}
+
+		[TestMethod]
+		public void TestMethod1()
+		{
+
+		}
+	}
+}

+ 1 - 0
CSharp/Game/ComponentTest/ComponentTest.csproj

@@ -60,6 +60,7 @@
     </Otherwise>
   </Choose>
   <ItemGroup>
+    <Compile Include="BuffManagerTest.cs" />
     <Compile Include="ObjectTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 0 - 14
CSharp/Game/ComponentTest/ObjectTest.cs

@@ -1,23 +1,14 @@
 using System;
-using System.Collections.Generic;
 using Helper;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
-using MongoDB.Bson;
 using Object = Component.Object;
 
 namespace ObjectTest
 {
-	class Buff: Object
-	{
-	}
-
 	class Player: Object
 	{
-		public Dictionary<ObjectId, Buff> Buffs { get; private set; }
-
 		public Player()
 		{
-			this.Buffs = new Dictionary<ObjectId, Buff>();
 		}
 	}
 
@@ -29,11 +20,6 @@ namespace ObjectTest
 		{
 			var player = new Player();
 			player["health"] = 10;
-			for (int i = 0; i < 1; ++i)
-			{
-				var buff = new Buff();
-				player.Buffs.Add(buff.Id, buff);
-			}
 
 			string json = MongoHelper.ToJson(player);
 			Console.WriteLine(json);