Przeglądaj źródła

还是使用可以继承的组件设计,每种类型可以继承于Entity,例如Item,Buff等等,但是他们组件都是通用的,由自己确保组件挂载的正确性

tanghai 9 lat temu
rodzic
commit
054598e54a

+ 11 - 1
Unity/Assets/Scripts/Base/Object/Component.cs

@@ -5,7 +5,17 @@
 	/// </summary>
 	public abstract class Component: Object
 	{
-		public Unit Owner { get; set; }
+		public Entity Owner { get; set; }
+
+		public T GetOwner<T>() where T: Entity
+		{
+			T owner = this.Owner as T;
+			if (owner == null)
+			{
+				Log.Error($"Owner类型是{this.Owner.GetType().Name}, 无法转成: {typeof(T).Name}");
+			}
+			return owner;
+		}
 
 		protected Component()
 		{

+ 5 - 10
Unity/Assets/Scripts/Base/Object/Unit.cs → Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -6,27 +6,22 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Base
 {
-	public sealed class Unit: Object
+	public abstract class Entity: Object
 	{
 		[BsonElement, BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
 		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
-		
-		public Unit()
-		{
-			ObjectManager.Add(this);
-		}
 
-		public Unit(long id): base(id)
+		protected Entity()
 		{
 			ObjectManager.Add(this);
 		}
 
-		public Unit Clone()
+		protected Entity(long id): base(id)
 		{
-			return MongoHelper.FromBson<Unit>(MongoHelper.ToBson(this));
+			ObjectManager.Add(this);
 		}
-
+		
 		public override void Dispose()
 		{
 			if (this.Id == 0)

+ 0 - 0
Unity/Assets/Scripts/Base/Object/Unit.cs.meta → Unity/Assets/Scripts/Base/Object/Entity.cs.meta


+ 21 - 21
Unity/Assets/Scripts/Component/ChildrenComponent.cs

@@ -10,9 +10,9 @@ namespace Base
     public class ChildrenComponent: Component
     {
 		[BsonIgnore]
-		public Unit Parent { get; private set; }
+		public Entity Parent { get; private set; }
 		
-		private readonly Dictionary<long, Unit> idChildren = new Dictionary<long, Unit>();
+		private readonly Dictionary<long, Entity> idChildren = new Dictionary<long, Entity>();
 
 		[BsonIgnore]
 		public int Count
@@ -23,38 +23,38 @@ namespace Base
 			}
 		}
 
-		public void Add(Unit unit)
+		public void Add(Entity entity)
 		{
-			unit.GetComponent<ChildrenComponent>().Parent = this.Owner;
-			this.idChildren.Add(unit.Id, unit);
+			entity.GetComponent<ChildrenComponent>().Parent = this.Owner;
+			this.idChildren.Add(entity.Id, entity);
 		}
 
-		public Unit Get(long id)
+		public Entity Get(long id)
 		{
-			Unit unit = null;
-			this.idChildren.TryGetValue(id, out unit);
-			return unit;
+			Entity entity = null;
+			this.idChildren.TryGetValue(id, out entity);
+			return entity;
 		}
 
-		public Unit[] GetChildren()
+		public Entity[] GetChildren()
 		{
 			return this.idChildren.Values.ToArray();
 		}
 
-		private void Remove(Unit unit)
+		private void Remove(Entity entity)
 		{
-			this.idChildren.Remove(unit.Id);
-			unit.Dispose();
+			this.idChildren.Remove(entity.Id);
+			entity.Dispose();
 		}
 
 		public void Remove(long id)
 		{
-			Unit unit;
-			if (!this.idChildren.TryGetValue(id, out unit))
+			Entity entity;
+			if (!this.idChildren.TryGetValue(id, out entity))
 			{
 				return;
 			}
-			this.Remove(unit);
+			this.Remove(entity);
 		}
 
 		public override void Dispose()
@@ -66,7 +66,7 @@ namespace Base
 
 			base.Dispose();
 
-			foreach (Unit entity in this.idChildren.Values.ToArray())
+			foreach (Entity entity in this.idChildren.Values.ToArray())
 			{
 				entity.Dispose();
 			}
@@ -77,14 +77,14 @@ namespace Base
 
 	public static partial class ChildrenHelper
 	{
-		public static void Add(this Unit unit, Unit child)
+		public static void Add(this Entity entity, Entity child)
 		{
-			unit.GetComponent<ChildrenComponent>().Add(child);
+			entity.GetComponent<ChildrenComponent>().Add(child);
 		}
 
-		public static void Remove(this Unit unit, long id)
+		public static void Remove(this Entity entity, long id)
 		{
-			unit.GetComponent<ChildrenComponent>().Remove(id);
+			entity.GetComponent<ChildrenComponent>().Remove(id);
 		}
 	}
 }

+ 4 - 5
Unity/Assets/Scripts/Component/Game.cs

@@ -2,16 +2,15 @@
 {
 	public sealed class Game
 	{
-		private static Unit game;
+		private static Scene game;
 
-		public static Unit Scene
+		public static Scene Scene
 		{
 			get
 			{
 				if (game == null)
 				{
-					game = new Unit();
-					game.AddComponent<Scene>();
+					game = new Scene("Game", SceneType.Game);
 				}
 				return game;
 			}
@@ -19,7 +18,7 @@
 
 		public static void Close()
 		{
-			Unit scene = game;
+			Entity scene = game;
 			game = null;
 			scene.Dispose();
 		}

+ 9 - 7
Unity/Assets/Scripts/Component/Scene/MessageComponent.cs

@@ -15,16 +15,16 @@ namespace Base
 	}
 
 	[ObjectEvent]
-	public class MessageComponentEvent : ObjectEvent<MessageComponent>, ILoader, IAwake, IUpdate
+	public class MessageComponentEvent : ObjectEvent<MessageComponent>, ILoader, IAwake<SceneType>, IUpdate
 	{
 		public void Load()
 		{
 			this.GetValue().Load();
 		}
 
-		public void Awake()
+		public void Awake(SceneType sceneType)
 		{
-			this.GetValue().Awake();
+			this.GetValue().Awake(sceneType);
 		}
 
 		public void Update()
@@ -38,14 +38,16 @@ namespace Base
 	/// </summary>
 	public class MessageComponent: Component
 	{
+		private SceneType SceneType;
 		private uint RpcId { get; set; }
 		private Dictionary<Opcode, List<Action<byte[], int, int>>> events;
 		private readonly Dictionary<uint, Action<byte[], int, int>> requestCallback = new Dictionary<uint, Action<byte[], int, int>>();
 		private readonly Dictionary<Opcode, Action<byte[], int, int>> waitCallback = new Dictionary<Opcode, Action<byte[], int, int>>();
 		private readonly Dictionary<NetChannelType, AChannel> channels = new Dictionary<NetChannelType, AChannel>();
 		
-		public void Awake()
+		public void Awake(SceneType sceneType)
 		{
+			this.SceneType = sceneType;
 			this.Load();
 		}
 
@@ -66,7 +68,7 @@ namespace Base
 					}
 
 					MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
-					if (messageAttribute.SceneType != this.GetComponent<Scene>().SceneType)
+					if (messageAttribute.SceneType != this.SceneType)
 					{
 						continue;
 					}
@@ -83,7 +85,7 @@ namespace Base
 			}
 		}
 
-		public void Register<T>(Action<Unit, T> action)
+		public void Register<T>(Action<Entity, T> action)
 		{
 			Opcode opcode = EnumHelper.FromString<Opcode>(typeof (T).Name);
 			if (!this.events.ContainsKey(opcode))
@@ -208,7 +210,7 @@ namespace Base
 			List<Action<byte[], int, int>> actions;
 			if (!this.events.TryGetValue(opcode, out actions))
 			{
-				if (this.GetComponent<Scene>().SceneType == SceneType.Game)
+				if (this.SceneType == SceneType.Game)
 				{
 					Log.Error($"消息{opcode}没有处理");
 				}

+ 7 - 1
Unity/Assets/Scripts/Component/Scene/Scene.cs

@@ -13,7 +13,7 @@
 		RobotClient,
 	}
 
-	public sealed class Scene : Component
+	public sealed class Scene: Entity
 	{
 		public Scene Owner { get; set; }
 
@@ -21,6 +21,12 @@
 
 		public SceneType SceneType { get; private set; }
 
+		public Scene(string name, SceneType sceneType)
+		{
+			this.Name = name;
+			this.SceneType = sceneType;
+		}
+
 		public override void Dispose()
 		{
 			if (this.Id == 0)

+ 5 - 6
Unity/Assets/Scripts/Component/Share.cs

@@ -3,18 +3,17 @@
 	/// <summary>
 	/// 游戏和扩展编辑器都需要用到的数据放在这个Scene上面
 	/// </summary>
-	public sealed class Share
+	public sealed class Share: Entity
 	{
-		private static Unit share;
+		private static Scene share;
 
-		public static Unit Scene
+		public static Scene Scene
 		{
 			get
 			{
 				if (share == null)
 				{
-					share = new Unit();
-					share.AddComponent<Scene>();
+					share = new Scene("Share", SceneType.Share);
 					share.AddComponent<EventComponent>();
 					share.AddComponent<LogComponent>();
 					GlobalConfigComponent globalConfigComponent = share.AddComponent<GlobalConfigComponent>();
@@ -26,7 +25,7 @@
 
 		public static void Close()
 		{
-			Unit scene = share;
+			Entity scene = share;
 			share = null;
 			scene?.Dispose();
 		}

+ 2 - 2
Unity/Assets/Scripts/Component/UI/UI.cs

@@ -1,8 +1,8 @@
 namespace Base
 {
-	public sealed class UI: Component
+	public sealed class UI: Entity
 	{
-		public Unit Scene { get; set; }
+		public Entity Scene { get; set; }
 
 		public UIType UIType { get; set; }
 

+ 1 - 1
Unity/Assets/Scripts/Message/AMEvent.cs

@@ -7,6 +7,6 @@
 			component.Register<T>(Run);
 		}
 
-		public abstract void Run(Unit scene, T message);
+		public abstract void Run(Entity scene, T message);
 	}
 }

+ 1 - 1
Unity/Controller/Message/S2C_InitBuffInfoEvent.cs

@@ -5,7 +5,7 @@ namespace Controller
 	[Message(SceneType.Game)]
 	public class S2C_InitBuffInfoEvent: AMEvent<S2C_InitBuffInfo>
 	{
-		public override void Run(Unit scene, S2C_InitBuffInfo buffInfo)
+		public override void Run(Entity scene, S2C_InitBuffInfo buffInfo)
 		{
 			Log.Info(MongoHelper.ToJson(buffInfo));
 		}

+ 3 - 3
Unity/Unity.CSharp.csproj

@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -118,7 +118,7 @@
     <Compile Include="Assets\Scripts\Base\Network\UNet\USocket.cs" />
     <Compile Include="Assets\Scripts\Base\Network\UNet\USocketManager.cs" />
     <Compile Include="Assets\Scripts\Base\Object\Component.cs" />
-    <Compile Include="Assets\Scripts\Base\Object\Unit.cs" />
+    <Compile Include="Assets\Scripts\Base\Object\Entity.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IAwake.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ILoader.cs" />
     <Compile Include="Assets\Scripts\Base\Object\IStart.cs" />
@@ -170,4 +170,4 @@
   </ItemGroup>
   <ItemGroup />
   <Import Project="$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2015\UnityVS.CSharp.targets" />
-</Project>
+</Project>