Browse Source

初步增加了gameobject

tanghai 12 years ago
parent
commit
beea8c9e96

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

@@ -41,11 +41,13 @@
   <ItemGroup>
     <Compile Include="Buff.cs" />
     <Compile Include="BuffManager.cs" />
+    <Compile Include="GameObject.cs" />
     <Compile Include="ConfigAttribute.cs" />
     <Compile Include="ConfigCategory.cs" />
     <Compile Include="ConfigManager.cs" />
     <Compile Include="EventAttribute.cs" />
     <Compile Include="EventType.cs" />
+    <Compile Include="GameObjectManager.cs" />
     <Compile Include="HandlerAttribute.cs" />
     <Compile Include="IConfigInitialize.cs" />
     <Compile Include="IEvent.cs" />

+ 1 - 1
CSharp/Game/Component/EventAttribute.cs

@@ -6,6 +6,6 @@ namespace Component
 	public class EventAttribute : Attribute
 	{
 		public EventType Type { get; set; }
-		public short Number { get; set; }
+		public int Number { get; set; }
 	}
 }

+ 18 - 0
CSharp/Game/Component/GameObject.cs

@@ -0,0 +1,18 @@
+
+namespace Component
+{
+	public class GameObject: Object
+	{
+		private readonly BuffManager buffManager = new BuffManager();
+
+		public int Type { get; set; }
+
+		public BuffManager BuffManager
+		{
+			get
+			{
+				return this.buffManager;
+			}
+		}
+	}
+}

+ 70 - 0
CSharp/Game/Component/GameObjectManager.cs

@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using MongoDB.Bson;
+
+namespace Component
+{
+	public class GameObjectManager
+	{
+		private readonly Dictionary<ObjectId, GameObject> gameObjects = 
+				new Dictionary<ObjectId, GameObject>();
+
+		private readonly Dictionary<int, Dictionary<ObjectId, GameObject>> typeGameObjects = 
+				new Dictionary<int, Dictionary<ObjectId, GameObject>>();
+
+		public void Add(GameObject gameObject)
+		{
+			this.gameObjects.Add(gameObject.Id, gameObject);
+			if (!this.typeGameObjects.ContainsKey(gameObject.Type))
+			{
+				this.typeGameObjects.Add(gameObject.Type, new Dictionary<ObjectId, GameObject>());
+			}
+			this.typeGameObjects[gameObject.Type].Add(gameObject.Id, gameObject);
+		}
+
+		public GameObject Get(ObjectId id)
+		{
+			GameObject gameObject = null;
+			this.gameObjects.TryGetValue(id, out gameObject);
+			return gameObject;
+		}
+
+		public GameObject[] GetOneType(int type)
+		{
+			Dictionary<ObjectId, GameObject> oneTypeGameObjects = null;
+			if (!this.typeGameObjects.TryGetValue(type, out oneTypeGameObjects))
+			{
+				return new GameObject[0];
+			}
+			return oneTypeGameObjects.Values.ToArray();
+		}
+
+		public bool Remove(GameObject gameObject)
+		{
+			if (gameObject == null)
+			{
+				throw new ArgumentNullException("gameObject");
+			}
+			if (!this.gameObjects.Remove(gameObject.Id))
+			{
+				return false;
+			}
+			if (!this.typeGameObjects[gameObject.Type].Remove(gameObject.Id))
+			{
+				return false;
+			}
+			return true;
+		}
+
+		public bool Remove(ObjectId id)
+		{
+			GameObject gameObject = this.Get(id);
+			if (gameObject == null)
+			{
+				return false;
+			}
+			return this.Remove(gameObject);
+		}
+	}
+}

+ 1 - 1
CSharp/Game/Component/HandlerAttribute.cs

@@ -5,6 +5,6 @@ namespace Component
 	[AttributeUsage(AttributeTargets.Class)]
 	public class HandlerAttribute : Attribute
 	{
-		public short Opcode { get; set; }
+		public int Opcode { get; set; }
 	}
 }

+ 7 - 7
CSharp/Game/Component/LogicManager.cs

@@ -10,9 +10,9 @@ namespace Component
     {
 		private static readonly LogicManager instance = new LogicManager();
 
-		private Dictionary<short, IHandler> handlers;
+		private Dictionary<int, IHandler> handlers;
 
-		private Dictionary<EventType, SortedDictionary<short, IEvent>> events;
+		private Dictionary<EventType, SortedDictionary<int, IEvent>> events;
 
 		public static LogicManager Instance
 		{
@@ -34,7 +34,7 @@ namespace Component
 			Type[] types = assembly.GetTypes();
 
 			// 加载封包处理器
-			this.handlers = new Dictionary<short, IHandler>();
+			this.handlers = new Dictionary<int, IHandler>();
 			foreach (var type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(HandlerAttribute), false);
@@ -43,7 +43,7 @@ namespace Component
 					continue;
 				}
 				var handler = (IHandler)Activator.CreateInstance(type);
-				short opcode = ((HandlerAttribute)attrs[0]).Opcode;
+				int opcode = ((HandlerAttribute)attrs[0]).Opcode;
 				if (this.handlers.ContainsKey(opcode))
 				{
 					throw new Exception(string.Format(
@@ -53,7 +53,7 @@ namespace Component
 			}
 
 			// 加载事件处理器
-			this.events = new Dictionary<EventType, SortedDictionary<short, IEvent>>();
+			this.events = new Dictionary<EventType, SortedDictionary<int, IEvent>>();
 			foreach (var type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
@@ -66,7 +66,7 @@ namespace Component
 				var eventNumber = ((EventAttribute)attrs[0]).Number;
 				if (!this.events.ContainsKey(eventType))
 				{
-					this.events[eventType] = new SortedDictionary<short, IEvent>();
+					this.events[eventType] = new SortedDictionary<int, IEvent>();
 				}
 				if (this.events[eventType].ContainsKey(eventNumber))
 				{
@@ -104,7 +104,7 @@ namespace Component
 
 		public void Trigger(MessageEnv messageEnv, EventType type)
 		{
-			SortedDictionary<short, IEvent> iEventDict = null;
+			SortedDictionary<int, IEvent> iEventDict = null;
 			if (!this.events.TryGetValue(type, out iEventDict))
 			{
 				return;

+ 10 - 0
CSharp/Game/World/World.cs

@@ -11,6 +11,8 @@ namespace World
 
 		private readonly ConfigManager configManager = ConfigManager.Instance;
 
+		private readonly GameObjectManager gameObjectManager = new GameObjectManager();
+
 		public static World Instance
 		{
 			get
@@ -34,5 +36,13 @@ namespace World
 				return this.configManager;
 			}
 		}
+
+		public GameObjectManager GameObjectManager
+		{
+			get
+			{
+				return this.gameObjectManager;
+			}
+		}
 	}
 }

+ 2 - 1
CSharp/Game/WorldTest/WorldTest.cs

@@ -1,4 +1,5 @@
-using System.Threading;
+using System;
+using System.Threading;
 using Component;
 using Helper;
 using Microsoft.VisualStudio.TestTools.UnitTesting;