Przeglądaj źródła

服务端增加数值组件,数值组件需要能序列化反序列化,目前反序列化还有点问题

tanghai 8 lat temu
rodzic
commit
7eca7f3e6f

+ 11 - 0
Server/App/Program.cs

@@ -13,6 +13,8 @@ namespace App
 			OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext();
 			SynchronizationContext.SetSynchronizationContext(contex);
 
+			MongoHelper.Init();
+			
 			try
 			{
 				ObjectEvents.Instance.Add("Model", typeof(Game).Assembly);
@@ -39,10 +41,19 @@ namespace App
 				Game.Scene.AddComponent<OpcodeTypeComponent>();
 				Game.Scene.AddComponent<MessageDispatherComponent>();
 
+				Unit unit = new Unit();
+				NumericComponent numericComponent = unit.AddComponent<NumericComponent>();
+				numericComponent.Set(NumericType.Speed, 100);
+
+				Log.Debug(MongoHelper.ToJson(unit));
+				Unit unit2 = MongoHelper.FromJson<Unit>(MongoHelper.ToJson(unit));
+				Log.Debug(MongoHelper.ToJson(unit2));
+
 				// 根据不同的AppType添加不同的组件
 				OuterConfig outerConfig = startConfig.GetComponent<OuterConfig>();
 				InnerConfig innerConfig = startConfig.GetComponent<InnerConfig>();
 				ClientConfig clientConfig = startConfig.GetComponent<ClientConfig>();
+				
 				switch (startConfig.AppType)
 				{
 					case AppType.Manager:

+ 2 - 0
Server/Model/Base/Event/EventIdType.cs

@@ -14,5 +14,7 @@
 		BehaviorTreeConnectState,
 		BehaviorTreeReplaceClick,
 		BehaviorTreeRightDesignerDrag,
+
+		NumbericChange,
 	}
 }

+ 1 - 0
Server/Model/Base/Object/ComponentDB.cs

@@ -4,6 +4,7 @@ namespace Model
 {
 	[BsonKnownTypes(typeof(AConfigComponent))]
 	[BsonKnownTypes(typeof(UnitGateComponent))]
+	[BsonKnownTypes(typeof(NumericComponent))]
 	public abstract class ComponentDB : Component
 	{
 	}

+ 58 - 0
Server/Model/Component/NumericComponent.cs

@@ -0,0 +1,58 @@
+using System.Collections.Generic;
+using MongoDB.Bson.Serialization.Attributes;
+using MongoDB.Bson.Serialization.Options;
+
+namespace Model
+{
+	public class NumericComponent : ComponentDB
+	{
+		[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
+		public readonly Dictionary<NumericType, int> NumericDic = new Dictionary<NumericType, int>();
+
+		public float GetAsFloat(NumericType numericType)
+		{
+			return (float)GetByKey(numericType) / 10000;
+		}
+
+		public int GetAsInt(NumericType numericType)
+		{
+			return this[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(numericType);
+			}
+			set
+			{
+				int v = this.GetByKey(numericType);
+				if (v == value)
+				{
+					return;
+				}
+
+				NumericDic[numericType] = value;
+				Game.Scene.GetComponent<EventComponent>().Run(EventIdType.NumbericChange, this.Entity.Id, numericType, value);
+			}
+		}
+
+		private int GetByKey(NumericType key)
+		{
+			int value;
+			this.NumericDic.TryGetValue(key, out value);
+			return value;
+		}
+	}
+}

+ 2 - 2
Server/Model/Component/StartConfigComponent.cs

@@ -74,9 +74,9 @@ namespace Model
 						this.GateConfigs.Add(startConfig);
 					}
 				}
-				catch (Exception)
+				catch (Exception e)
 				{
-					Log.Error($"config错误: {s2}");
+					Log.Error($"config错误: {s2} {e}");
 				}
 			}
 

+ 3 - 1
Server/Model/Entity/Unit.cs

@@ -1,4 +1,5 @@
 using System.Numerics;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
@@ -20,7 +21,8 @@ namespace Model
 	public sealed class Unit: Entity
 	{
 		public UnitType UnitType { get; private set; }
-
+		
+		[BsonIgnore]
 		public Vector3 Position { get; set; }
 		
 		public void Awake(UnitType unitType)

+ 10 - 0
Server/Model/Other/NumericType.cs

@@ -0,0 +1,10 @@
+namespace Model
+{
+	public enum NumericType
+	{
+		Speed = 1000,
+		SpeedBase = Speed * 10,
+		SpeedAdd = Speed * 10 + 1,
+		SpeedPct = Speed * 10 + 2,
+	}
+}

+ 2 - 2
Unity/Assets/Scripts/Base/Network/TNet/TChannel.cs

@@ -94,7 +94,7 @@ namespace Model
 			this.sendBuffer.SendTo(buffer);
 			if (this.isConnected)
 			{
-				((TService)this.service).Add(this.StartSend);
+				this.StartSend();
 			}
 		}
 
@@ -113,7 +113,7 @@ namespace Model
 			}
 			if (this.isConnected)
 			{
-				((TService)this.service).Add(this.StartSend);
+				this.StartSend();
 			}
 		}
 

+ 6 - 0
Unity/Assets/Scripts/Helper/MongoHelper.cs

@@ -3,11 +3,17 @@ using System.IO;
 using MongoDB.Bson;
 using MongoDB.Bson.IO;
 using MongoDB.Bson.Serialization;
+using MongoDB.Bson.Serialization.Serializers;
 
 namespace Model
 {
 	public static class MongoHelper
 	{
+		public static void Init()
+		{
+			BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
+		}
+
 		public static string ToJson(object obj)
 		{
 			return obj.ToJson();

+ 1 - 10
Unity/Unity.sln

@@ -1,8 +1,6 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 15
-VisualStudioVersion = 15.0.27004.2005
-MinimumVisualStudioVersion = 10.0.40219.1
+# Visual Studio 2017
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Plugins", "Unity.Plugins.csproj", "{D1FDB199-0FB7-099D-3771-C6A942E4E326}"
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity", "Unity.csproj", "{CF118143-7E37-744F-BE45-3F55345FEC40}"
@@ -12,10 +10,6 @@ EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Editor.Plugins", "Unity.Editor.Plugins.csproj", "{81A6E58E-BFF2-F1C8-1C4E-6316985F642C}"
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Hotfix", "Hotfix\Unity.Hotfix.csproj", "{40533600-4E69-4F7D-A924-E1A3B4127255}"
-	ProjectSection(ProjectDependencies) = postProject
-		{CF118143-7E37-744F-BE45-3F55345FEC40} = {CF118143-7E37-744F-BE45-3F55345FEC40}
-		{D1FDB199-0FB7-099D-3771-C6A942E4E326} = {D1FDB199-0FB7-099D-3771-C6A942E4E326}
-	EndProjectSection
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -47,7 +41,4 @@ Global
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 	EndGlobalSection
-	GlobalSection(ExtensibilityGlobals) = postSolution
-		SolutionGuid = {13124EA5-9FAE-4741-9767-8033906DCD75}
-	EndGlobalSection
 EndGlobal