From e9865b97159e7d7fb11f198acf975551f6c3f7d9 Mon Sep 17 00:00:00 2001 From: tanghai Date: Wed, 3 Jan 2018 18:14:19 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=81=9A=E4=BA=86=E4=B8=80=E4=B8=AA=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=B8=8A=E7=9A=84=E5=A4=A7=E6=94=B9=E5=8A=A8=EF=BC=9A?= =?UTF-8?q?Entity=E6=94=B9=E6=88=90=E7=BB=A7=E6=89=BF=E4=BA=8EComponent?= =?UTF-8?q?=EF=BC=8C=E8=BF=99=E6=A0=B7=E4=B8=80=E6=9D=A5Entity=E4=B9=9F?= =?UTF-8?q?=E6=98=AF=E7=BB=84=E4=BB=B6=EF=BC=8C=E7=BB=84=E4=BB=B6=E4=BB=8E?= =?UTF-8?q?=E6=AD=A4=E5=8F=AF=E4=BB=A5=E6=9C=89=E8=83=BD=E5=8A=9B=E5=86=8D?= =?UTF-8?q?=E6=8C=82=E7=BB=84=E4=BB=B6=E3=80=82=E8=BF=99=E4=B9=88=E5=81=9A?= =?UTF-8?q?=E7=9A=84=E5=8E=9F=E5=9B=A0=E6=98=AF=EF=BC=9Aentitas=E7=9A=84ec?= =?UTF-8?q?s=E7=BB=84=E4=BB=B6=E9=A2=97=E7=B2=92=E5=BA=A6=E5=A4=AA?= =?UTF-8?q?=E5=B0=8F=EF=BC=8C=E5=A6=82=E6=9E=9Cet=E6=8C=89=E7=85=A7?= =?UTF-8?q?=E7=9B=AE=E5=89=8D=E7=9A=84=E6=9E=B6=E6=9E=84=EF=BC=8C=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0ecs=E6=9C=BA=E5=88=B6=E4=BC=9A=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E8=A6=81=E5=8C=96=E5=88=86=E7=9A=84=E9=9D=9E?= =?UTF-8?q?=E5=B8=B8=E7=BB=86=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=94=B9=E6=88=90?= =?UTF-8?q?entity=E4=B9=9F=E6=98=AF=E7=BB=84=E4=BB=B6=EF=BC=8C=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E4=B9=9F=E5=8F=AF=E4=BB=A5=E6=8C=82=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E9=82=A3=E4=B9=88=E7=BB=84=E4=BB=B6=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=88=92=E5=88=86=E7=9A=84=E5=8F=AF=E5=A4=A7=E5=8F=AF=E5=B0=8F?= =?UTF-8?q?=EF=BC=8C=E5=A4=A7=E7=BB=84=E4=BB=B6=E5=8F=88=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=8C=85=E6=8B=AC=E5=B0=8F=E7=BB=84=E4=BB=B6=EF=BC=8C=E8=BF=99?= =?UTF-8?q?=E6=A0=B7=E5=B0=B1=E4=BC=9A=E5=8D=81=E5=88=86=E7=81=B5=E6=B4=BB?= =?UTF-8?q?=E3=80=82=202.Component=E6=9C=89=E4=B8=AAParent=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E5=BD=93=E4=BD=9C=E4=B8=BA=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=97=B6Parent=E6=98=AF=E7=BB=84=E4=BB=B6=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=80=85=EF=BC=8C=E5=BD=93entity=E4=BD=9C=E4=B8=BAchild?= =?UTF-8?q?=E6=97=B6=EF=BC=8CParent=E5=8F=AF=E4=BB=A5=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E7=88=B6=E4=BA=B2=EF=BC=8C=E8=BF=99=E4=B8=AA=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/Hotfix/System/ActorComponentSystem.cs | 10 +++---- .../Hotfix/System/NetInnerComponentSystem.cs | 4 +-- .../System/RealmGateAddressComponentSystem.cs | 2 +- .../Model/Base/Object/ComponentAttribute.cs | 1 + .../ActorMessageDispatherComponent.cs | 2 +- Server/Model/Component/LocationComponent.cs | 2 +- .../Component/MessageDispatherComponent.cs | 2 +- Server/Model/Component/NumericComponent.cs | 2 +- Server/Model/Component/Unit/LockComponent.cs | 6 ++-- .../Scripts/Base/Message/AActorMessage.cs | 1 - Unity/Assets/Scripts/Base/Object/Component.cs | 13 ++------ .../Scripts/Base/Object/ComponentAttribute.cs | 1 + .../Scripts/Base/Object/ComponentFactory.cs | 8 ++--- Unity/Assets/Scripts/Base/Object/Disposer.cs | 1 - Unity/Assets/Scripts/Base/Object/Entity.cs | 15 +++++----- .../Scripts/Component/AnimatorComponent.cs | 2 +- .../Component/BehaviorTreeComponent.cs | 2 +- .../Component/BundleDownloaderComponent.cs | 2 ++ .../Assets/Scripts/Component/MoveComponent.cs | 30 +++++++++---------- .../Scripts/Component/NumericComponent.cs | 2 +- Unity/Assets/Scripts/Component/UIComponent.cs | 2 +- Unity/Assets/Scripts/Entity/Session.cs | 6 ++-- Unity/Assets/Scripts/Entity/UI.cs | 2 ++ .../UILoading/Component/UILoadingComponent.cs | 2 +- Unity/Hotfix/Base/Object/Component.cs | 13 ++------ Unity/Hotfix/Base/Object/ComponentFactory.cs | 8 ++--- Unity/Hotfix/Base/Object/Entity.cs | 13 ++++---- Unity/Hotfix/Component/UIComponent.cs | 2 +- .../UI/UILobby/Component/UILobbyComponent.cs | 2 +- .../UI/UILogin/Component/UILoginComponent.cs | 2 +- 30 files changed, 76 insertions(+), 84 deletions(-) diff --git a/Server/Hotfix/System/ActorComponentSystem.cs b/Server/Hotfix/System/ActorComponentSystem.cs index 84d20d27..92b66999 100644 --- a/Server/Hotfix/System/ActorComponentSystem.cs +++ b/Server/Hotfix/System/ActorComponentSystem.cs @@ -31,8 +31,8 @@ namespace Hotfix { self.entityActorHandler = new CommonEntityActorHandler(); self.queue = new EQueue(); - self.actorId = self.Entity.Id; - Game.Scene.GetComponent().Add(self.Entity); + self.actorId = self.Parent.Id; + Game.Scene.GetComponent().Add(self.Parent); self.HandleAsync(); } @@ -40,8 +40,8 @@ namespace Hotfix { self.entityActorHandler = iEntityActorHandler; self.queue = new EQueue(); - self.actorId = self.Entity.Id; - Game.Scene.GetComponent().Add(self.Entity); + self.actorId = self.Parent.Id; + Game.Scene.GetComponent().Add(self.Parent); self.HandleAsync(); } @@ -102,7 +102,7 @@ namespace Hotfix { return; } - await self.entityActorHandler.Handle(info.Session, self.Entity, info.Message); + await self.entityActorHandler.Handle(info.Session, self.Parent, info.Message); } catch (Exception e) { diff --git a/Server/Hotfix/System/NetInnerComponentSystem.cs b/Server/Hotfix/System/NetInnerComponentSystem.cs index 3a4281c1..dff260d7 100644 --- a/Server/Hotfix/System/NetInnerComponentSystem.cs +++ b/Server/Hotfix/System/NetInnerComponentSystem.cs @@ -29,7 +29,7 @@ namespace Hotfix self.Awake(NetworkProtocol.TCP); self.MessagePacker = new MongoPacker(); self.MessageDispatcher = new InnerMessageDispatcher(); - self.AppType = self.GetComponent().StartConfig.AppType; + self.AppType = self.Parent.GetComponent().StartConfig.AppType; } public static void Awake(this NetInnerComponent self, IPEndPoint ipEndPoint) @@ -37,7 +37,7 @@ namespace Hotfix self.Awake(NetworkProtocol.TCP, ipEndPoint); self.MessagePacker = new MongoPacker(); self.MessageDispatcher = new InnerMessageDispatcher(); - self.AppType = self.GetComponent().StartConfig.AppType; + self.AppType = self.Parent.GetComponent().StartConfig.AppType; } public static void Update(this NetInnerComponent self) diff --git a/Server/Hotfix/System/RealmGateAddressComponentSystem.cs b/Server/Hotfix/System/RealmGateAddressComponentSystem.cs index 96a8a377..17c25481 100644 --- a/Server/Hotfix/System/RealmGateAddressComponentSystem.cs +++ b/Server/Hotfix/System/RealmGateAddressComponentSystem.cs @@ -15,7 +15,7 @@ namespace Hotfix { public static void Start(this RealmGateAddressComponent component) { - StartConfig[] startConfigs = component.GetComponent().GetAll(); + StartConfig[] startConfigs = component.Parent.GetComponent().GetAll(); foreach (StartConfig config in startConfigs) { if (!config.AppType.Is(AppType.Gate)) diff --git a/Server/Model/Base/Object/ComponentAttribute.cs b/Server/Model/Base/Object/ComponentAttribute.cs index 04de2a04..399656c6 100644 --- a/Server/Model/Base/Object/ComponentAttribute.cs +++ b/Server/Model/Base/Object/ComponentAttribute.cs @@ -5,6 +5,7 @@ namespace Model [BsonKnownTypes(typeof(AConfigComponent))] [BsonKnownTypes(typeof(UnitGateComponent))] [BsonKnownTypes(typeof(NumericComponent))] + [BsonKnownTypes(typeof(Entity))] public partial class Component { } diff --git a/Server/Model/Component/ActorMessageDispatherComponent.cs b/Server/Model/Component/ActorMessageDispatherComponent.cs index e271c9a2..c4076ce7 100644 --- a/Server/Model/Component/ActorMessageDispatherComponent.cs +++ b/Server/Model/Component/ActorMessageDispatherComponent.cs @@ -32,7 +32,7 @@ namespace Model public void Load() { - AppType appType = this.GetComponent().StartConfig.AppType; + AppType appType = this.Parent.GetComponent().StartConfig.AppType; Log.Info("apptype: " + appType); this.handlers = new Dictionary(); diff --git a/Server/Model/Component/LocationComponent.cs b/Server/Model/Component/LocationComponent.cs index ea1d5d1c..d560d333 100644 --- a/Server/Model/Component/LocationComponent.cs +++ b/Server/Model/Component/LocationComponent.cs @@ -224,7 +224,7 @@ namespace Model tasks = new EQueue(); this.taskQueues[key] = tasks; } - task.Scene = this.GetEntity(); + task.Scene = this.GetParent(); tasks.Enqueue(task); } diff --git a/Server/Model/Component/MessageDispatherComponent.cs b/Server/Model/Component/MessageDispatherComponent.cs index 146f7cbe..7aeb480f 100644 --- a/Server/Model/Component/MessageDispatherComponent.cs +++ b/Server/Model/Component/MessageDispatherComponent.cs @@ -31,7 +31,7 @@ namespace Model public void Load() { - AppType appType = this.GetComponent().StartConfig.AppType; + AppType appType = this.Parent.GetComponent().StartConfig.AppType; this.handlers = new Dictionary>(); diff --git a/Server/Model/Component/NumericComponent.cs b/Server/Model/Component/NumericComponent.cs index 9a7661a6..ec9b379f 100644 --- a/Server/Model/Component/NumericComponent.cs +++ b/Server/Model/Component/NumericComponent.cs @@ -58,7 +58,7 @@ namespace Model } NumericDic[numericType] = value; - Game.Scene.GetComponent().Run(EventIdType.NumbericChange, this.Entity.Id, numericType, value); + Game.Scene.GetComponent().Run(EventIdType.NumbericChange, this.Parent.Id, numericType, value); } } diff --git a/Server/Model/Component/Unit/LockComponent.cs b/Server/Model/Component/Unit/LockComponent.cs index 3c029bb7..dde9985d 100644 --- a/Server/Model/Component/Unit/LockComponent.cs +++ b/Server/Model/Component/Unit/LockComponent.cs @@ -52,7 +52,7 @@ namespace Model this.status = LockStatus.LockRequesting; // 真身直接本地请求锁,镜像需要调用Rpc获取锁 - MasterComponent masterComponent = this.GetComponent(); + MasterComponent masterComponent = this.Parent.GetComponent(); if (masterComponent != null) { await masterComponent.Lock(this.address); @@ -82,7 +82,7 @@ namespace Model { Session session = Game.Scene.GetComponent().Get(this.address); string serverAddress = Game.Scene.GetComponent().StartConfig.ServerIP; - G2G_LockRequest request = new G2G_LockRequest { Id = this.Entity.Id, Address = serverAddress }; + G2G_LockRequest request = new G2G_LockRequest { Id = this.Parent.Id, Address = serverAddress }; await session.Call(request); this.status = LockStatus.Locked; @@ -95,7 +95,7 @@ namespace Model } catch (Exception e) { - Log.Error($"获取锁失败: {this.address} {this.Entity.Id} {e}"); + Log.Error($"获取锁失败: {this.address} {this.Parent.Id} {e}"); } } diff --git a/Unity/Assets/Scripts/Base/Message/AActorMessage.cs b/Unity/Assets/Scripts/Base/Message/AActorMessage.cs index b1e41681..797d69de 100644 --- a/Unity/Assets/Scripts/Base/Message/AActorMessage.cs +++ b/Unity/Assets/Scripts/Base/Message/AActorMessage.cs @@ -6,7 +6,6 @@ namespace Model { [ProtoContract] [BsonKnownTypes(typeof(AFrameMessage))] - [BsonKnownTypes(typeof(FrameMessage))] public abstract partial class AActorMessage : AMessage { } diff --git a/Unity/Assets/Scripts/Base/Object/Component.cs b/Unity/Assets/Scripts/Base/Object/Component.cs index e78c9533..a1ad3c61 100644 --- a/Unity/Assets/Scripts/Base/Object/Component.cs +++ b/Unity/Assets/Scripts/Base/Object/Component.cs @@ -6,22 +6,17 @@ namespace Model public abstract partial class Component: Disposer { [BsonIgnore] - public Entity Entity { get; set; } + public Entity Parent { get; set; } - public T GetEntity() where T : Entity + public T GetParent() where T : Entity { - return this.Entity as T; + return this.Parent as T; } protected Component() { this.Id = 1; } - - public T GetComponent() where T : Component - { - return this.Entity.GetComponent(); - } public override void Dispose() { @@ -31,8 +26,6 @@ namespace Model } base.Dispose(); - - this.Entity?.RemoveComponent(this.GetType()); } } } \ No newline at end of file diff --git a/Unity/Assets/Scripts/Base/Object/ComponentAttribute.cs b/Unity/Assets/Scripts/Base/Object/ComponentAttribute.cs index d53392c8..36b6cb26 100644 --- a/Unity/Assets/Scripts/Base/Object/ComponentAttribute.cs +++ b/Unity/Assets/Scripts/Base/Object/ComponentAttribute.cs @@ -3,6 +3,7 @@ namespace Model { [BsonKnownTypes(typeof(AConfigComponent))] + [BsonKnownTypes(typeof(Entity))] public partial class Component { } diff --git a/Unity/Assets/Scripts/Base/Object/ComponentFactory.cs b/Unity/Assets/Scripts/Base/Object/ComponentFactory.cs index fd959ee7..5091a5a7 100644 --- a/Unity/Assets/Scripts/Base/Object/ComponentFactory.cs +++ b/Unity/Assets/Scripts/Base/Object/ComponentFactory.cs @@ -5,7 +5,7 @@ public static T Create(Entity entity) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer); return disposer; } @@ -13,7 +13,7 @@ public static T Create(Entity entity, A a) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer, a); return disposer; } @@ -21,7 +21,7 @@ public static T Create(Entity entity, A a, B b) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer, a, b); return disposer; } @@ -29,7 +29,7 @@ public static T Create(Entity entity, A a, B b, C c) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer, a, b, c); return disposer; } diff --git a/Unity/Assets/Scripts/Base/Object/Disposer.cs b/Unity/Assets/Scripts/Base/Object/Disposer.cs index 9fa14e50..b92dc0cd 100644 --- a/Unity/Assets/Scripts/Base/Object/Disposer.cs +++ b/Unity/Assets/Scripts/Base/Object/Disposer.cs @@ -3,7 +3,6 @@ using MongoDB.Bson.Serialization.Attributes; namespace Model { - [BsonKnownTypes(typeof(Entity))] [BsonKnownTypes(typeof(Component))] public abstract class Disposer : Object, IDisposable { diff --git a/Unity/Assets/Scripts/Base/Object/Entity.cs b/Unity/Assets/Scripts/Base/Object/Entity.cs index 24fd578e..c568d91c 100644 --- a/Unity/Assets/Scripts/Base/Object/Entity.cs +++ b/Unity/Assets/Scripts/Base/Object/Entity.cs @@ -6,26 +6,27 @@ using MongoDB.Bson.Serialization.Attributes; namespace Model { [BsonIgnoreExtraElements] - public partial class Entity : Disposer + public partial class Entity : Component { - [BsonIgnore] - public Entity Parent { get; set; } - [BsonElement] [BsonIgnoreIfNull] - private HashSet components = new HashSet(); + private HashSet components; [BsonIgnore] - private Dictionary componentDict = new Dictionary(); + private Dictionary componentDict; protected Entity() { this.Id = IdGenerater.GenerateId(); + this.components = new HashSet(); + this.componentDict = new Dictionary(); } protected Entity(long id) { this.Id = id; + this.components = new HashSet(); + this.componentDict = new Dictionary(); } public override void Dispose() @@ -182,7 +183,7 @@ namespace Model { foreach (Component component in this.components) { - component.Entity = this; + component.Parent = this; this.componentDict.Add(component.GetType(), component); } } diff --git a/Unity/Assets/Scripts/Component/AnimatorComponent.cs b/Unity/Assets/Scripts/Component/AnimatorComponent.cs index b36bedb7..59e500af 100644 --- a/Unity/Assets/Scripts/Component/AnimatorComponent.cs +++ b/Unity/Assets/Scripts/Component/AnimatorComponent.cs @@ -31,7 +31,7 @@ namespace Model public void Awake() { - Animator animator = this.GetEntity().GameObject.GetComponent(); + Animator animator = this.GetParent().GameObject.GetComponent(); if (animator == null) { diff --git a/Unity/Assets/Scripts/Component/BehaviorTreeComponent.cs b/Unity/Assets/Scripts/Component/BehaviorTreeComponent.cs index 22930db5..2a8f7d14 100644 --- a/Unity/Assets/Scripts/Component/BehaviorTreeComponent.cs +++ b/Unity/Assets/Scripts/Component/BehaviorTreeComponent.cs @@ -118,7 +118,7 @@ namespace Model private Node CreateTreeNode(NodeProto proto) { Node node = this.CreateOneNode(proto); - node.EndInit(this.GetEntity()); + node.EndInit(this.GetParent()); if (proto.Children == null) { diff --git a/Unity/Assets/Scripts/Component/BundleDownloaderComponent.cs b/Unity/Assets/Scripts/Component/BundleDownloaderComponent.cs index 07525357..24be94d7 100644 --- a/Unity/Assets/Scripts/Component/BundleDownloaderComponent.cs +++ b/Unity/Assets/Scripts/Component/BundleDownloaderComponent.cs @@ -208,6 +208,8 @@ namespace Model } base.Dispose(); + + this.Parent?.RemoveComponent(); } } } diff --git a/Unity/Assets/Scripts/Component/MoveComponent.cs b/Unity/Assets/Scripts/Component/MoveComponent.cs index 33ee5166..07ee2668 100644 --- a/Unity/Assets/Scripts/Component/MoveComponent.cs +++ b/Unity/Assets/Scripts/Component/MoveComponent.cs @@ -93,7 +93,7 @@ namespace Model public void Awake() { this.mainSpeed = this.AddSpeed(new Vector3()); - this.animatorComponent = this.GetComponent(); + this.animatorComponent = this.Parent.GetComponent(); } public void Update() @@ -110,7 +110,7 @@ namespace Model return; } - Unit unit = this.GetEntity(); + Unit unit = this.GetParent(); Vector3 moveVector3 = this.Speed * Time.deltaTime; if (this.hasDest) @@ -138,19 +138,19 @@ namespace Model this.t += Time.deltaTime; Quaternion v = Quaternion.Slerp(this.From, this.To, this.t / this.TurnTime); - this.GetEntity().Rotation = v; + this.GetParent().Rotation = v; } public void MoveToDest(Vector3 dest, float speedValue) { - if ((dest - this.GetEntity().Position).magnitude < 0.1f) + if ((dest - this.GetParent().Position).magnitude < 0.1f) { this.IsArrived = true; return; } this.IsArrived = false; this.hasDest = true; - Vector3 speed = dest - this.GetEntity().Position; + Vector3 speed = dest - this.GetParent().Position; speed = speed.normalized * speedValue; this.MainSpeed = speed; this.Dest = dest; @@ -201,7 +201,7 @@ namespace Model /// public void Turn2D(Vector3 dir, float turnTime = 0.1f) { - Vector3 nexpos = this.GetEntity().GameObject.transform.position + dir; + Vector3 nexpos = this.GetParent().GameObject.transform.position + dir; Turn(nexpos, turnTime); } @@ -210,10 +210,10 @@ namespace Model /// public void Turn(Vector3 target, float turnTime = 0.1f) { - Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetEntity().Position, target); + Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent().Position, target); this.To = quaternion; - this.From = this.GetEntity().Rotation; + this.From = this.GetParent().Rotation; this.t = 0; this.TurnTime = turnTime; } @@ -227,7 +227,7 @@ namespace Model Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle); this.To = quaternion; - this.From = this.GetEntity().Rotation; + this.From = this.GetParent().Rotation; this.t = 0; this.TurnTime = turnTime; } @@ -235,32 +235,32 @@ namespace Model public void Turn(Quaternion quaternion, float turnTime = 0.1f) { this.To = quaternion; - this.From = this.GetEntity().Rotation; + this.From = this.GetParent().Rotation; this.t = 0; this.TurnTime = turnTime; } public void TurnImmediately(Quaternion quaternion) { - this.GetEntity().Rotation = quaternion; + this.GetParent().Rotation = quaternion; } public void TurnImmediately(Vector3 target) { - Vector3 nowPos = this.GetEntity().Position; + Vector3 nowPos = this.GetParent().Position; if (nowPos == target) { return; } - Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetEntity().Position, target); - this.GetEntity().Rotation = quaternion; + Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent().Position, target); + this.GetParent().Rotation = quaternion; } public void TurnImmediately(float angle) { Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle); - this.GetEntity().Rotation = quaternion; + this.GetParent().Rotation = quaternion; } public override void Dispose() diff --git a/Unity/Assets/Scripts/Component/NumericComponent.cs b/Unity/Assets/Scripts/Component/NumericComponent.cs index 52dee58a..d2b02c65 100644 --- a/Unity/Assets/Scripts/Component/NumericComponent.cs +++ b/Unity/Assets/Scripts/Component/NumericComponent.cs @@ -83,7 +83,7 @@ namespace Model // 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果 // final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100; this.NumericDic[final] = ((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetByKey(pct)) / 100 + this.GetByKey(finalAdd)) * (100 + this.GetByKey(finalPct)) / 100; - Game.Scene.GetComponent().Run(EventIdType.NumbericChange, this.Entity.Id, numericType, final); + Game.Scene.GetComponent().Run(EventIdType.NumbericChange, this.Parent.Id, numericType, final); } } } \ No newline at end of file diff --git a/Unity/Assets/Scripts/Component/UIComponent.cs b/Unity/Assets/Scripts/Component/UIComponent.cs index 8a6332a3..fc58cd71 100644 --- a/Unity/Assets/Scripts/Component/UIComponent.cs +++ b/Unity/Assets/Scripts/Component/UIComponent.cs @@ -93,7 +93,7 @@ namespace Model { try { - UI ui = UiTypes[type].Create(this.GetEntity(), type, Root); + UI ui = UiTypes[type].Create(this.GetParent(), type, Root); uis.Add(type, ui); // 设置canvas diff --git a/Unity/Assets/Scripts/Entity/Session.cs b/Unity/Assets/Scripts/Entity/Session.cs index 0968c02a..b3dbaee3 100644 --- a/Unity/Assets/Scripts/Entity/Session.cs +++ b/Unity/Assets/Scripts/Entity/Session.cs @@ -124,7 +124,7 @@ namespace Model try { op = (Opcode)opcode; - Type messageType = this.network.Entity.GetComponent().GetType(op); + Type messageType = this.network.Parent.GetComponent().GetType(op); message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, count - offset); } catch (Exception e) @@ -332,7 +332,7 @@ namespace Model private void SendMessage(object message) { //Log.Debug($"send: {MongoHelper.ToJson(message)}"); - Opcode opcode = this.network.GetComponent().GetOpcode(message.GetType()); + Opcode opcode = this.network.Parent.GetComponent().GetOpcode(message.GetType()); ushort op = (ushort)opcode; byte[] messageBytes = this.network.MessagePacker.SerializeToByteArray(message); @@ -340,7 +340,7 @@ namespace Model // 如果是allserver,内部消息不走网络,直接转给session,方便调试时看到整体堆栈 if (this.network.AppType == AppType.AllServer) { - Session session = this.network.GetComponent().Get(this.RemoteAddress); + Session session = this.network.Parent.GetComponent().Get(this.RemoteAddress); session.RunDecompressedBytes(op, messageBytes, 0, messageBytes.Length); return; } diff --git a/Unity/Assets/Scripts/Entity/UI.cs b/Unity/Assets/Scripts/Entity/UI.cs index 8fb25962..14f9bb39 100644 --- a/Unity/Assets/Scripts/Entity/UI.cs +++ b/Unity/Assets/Scripts/Entity/UI.cs @@ -58,6 +58,7 @@ namespace Model UnityEngine.Object.Destroy(GameObject); children.Clear(); + this.Parent = null; } public void SetAsFirstSibling() @@ -68,6 +69,7 @@ namespace Model public void Add(UI ui) { this.children.Add(ui.Name, ui); + ui.Parent = this; } public void Remove(string name) diff --git a/Unity/Assets/Scripts/UI/UILoading/Component/UILoadingComponent.cs b/Unity/Assets/Scripts/UI/UILoading/Component/UILoadingComponent.cs index db79013f..f6728265 100644 --- a/Unity/Assets/Scripts/UI/UILoading/Component/UILoadingComponent.cs +++ b/Unity/Assets/Scripts/UI/UILoading/Component/UILoadingComponent.cs @@ -9,7 +9,7 @@ namespace Model public void Awake() { UILoadingComponent self = this.Get(); - self.text = self.GetEntity().GameObject.Get("Text").GetComponent(); + self.text = self.GetParent().GameObject.Get("Text").GetComponent(); } public async void Start() diff --git a/Unity/Hotfix/Base/Object/Component.cs b/Unity/Hotfix/Base/Object/Component.cs index 7bb6be2b..d8cc95c8 100644 --- a/Unity/Hotfix/Base/Object/Component.cs +++ b/Unity/Hotfix/Base/Object/Component.cs @@ -5,11 +5,11 @@ namespace Hotfix public abstract class Component : Disposer { [BsonIgnore] - public Entity Entity { get; set; } + public Entity Parent { get; set; } - public T GetEntity() where T : Entity + public T GetParent() where T : Entity { - return this.Entity as T; + return this.Parent as T; } protected Component() @@ -17,11 +17,6 @@ namespace Hotfix this.Id = 1; } - public T GetComponent() where T : Component - { - return this.Entity.GetComponent(); - } - public override void Dispose() { if (this.Id == 0) @@ -30,8 +25,6 @@ namespace Hotfix } base.Dispose(); - - this.Entity?.RemoveComponent(this.GetType()); } } } \ No newline at end of file diff --git a/Unity/Hotfix/Base/Object/ComponentFactory.cs b/Unity/Hotfix/Base/Object/ComponentFactory.cs index 97bfd4e5..f988b484 100644 --- a/Unity/Hotfix/Base/Object/ComponentFactory.cs +++ b/Unity/Hotfix/Base/Object/ComponentFactory.cs @@ -7,7 +7,7 @@ namespace Hotfix public static T Create(Entity entity) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer); return disposer; } @@ -15,7 +15,7 @@ namespace Hotfix public static T Create(Entity entity, A a) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer, a); return disposer; } @@ -23,7 +23,7 @@ namespace Hotfix public static T Create(Entity entity, A a, B b) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer, a, b); return disposer; } @@ -31,7 +31,7 @@ namespace Hotfix public static T Create(Entity entity, A a, B b, C c) where T : Component { T disposer = ObjectPool.Instance.Fetch(); - disposer.Entity = entity; + disposer.Parent = entity; ObjectEvents.Instance.Awake(disposer, a, b, c); return disposer; } diff --git a/Unity/Hotfix/Base/Object/Entity.cs b/Unity/Hotfix/Base/Object/Entity.cs index cf9a92c1..51d7220f 100644 --- a/Unity/Hotfix/Base/Object/Entity.cs +++ b/Unity/Hotfix/Base/Object/Entity.cs @@ -7,26 +7,27 @@ using MongoDB.Bson.Serialization.Attributes; namespace Hotfix { [BsonIgnoreExtraElements] - public class Entity : Disposer + public class Entity : Component { - [BsonIgnore] - public Entity Parent { get; set; } - [BsonElement] [BsonIgnoreIfNull] - private HashSet components = new HashSet(); + private readonly HashSet components; [BsonIgnore] - private Dictionary componentDict = new Dictionary(); + private readonly Dictionary componentDict; protected Entity() { this.Id = IdGenerater.GenerateId(); + this.components = new HashSet(); + this.componentDict = new Dictionary(); } protected Entity(long id) { this.Id = id; + this.components = new HashSet(); + this.componentDict = new Dictionary(); } public override void Dispose() diff --git a/Unity/Hotfix/Component/UIComponent.cs b/Unity/Hotfix/Component/UIComponent.cs index bf819541..686ca574 100644 --- a/Unity/Hotfix/Component/UIComponent.cs +++ b/Unity/Hotfix/Component/UIComponent.cs @@ -91,7 +91,7 @@ namespace Hotfix { try { - UI ui = UiTypes[type].Create(this.GetEntity(), type, Root); + UI ui = UiTypes[type].Create(this.GetParent(), type, Root); uis.Add(type, ui); // 设置canvas diff --git a/Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs b/Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs index e67e312b..a2e42c8f 100644 --- a/Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs +++ b/Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs @@ -21,7 +21,7 @@ namespace Hotfix public void Awake() { - ReferenceCollector rc = this.GetEntity().GameObject.GetComponent(); + ReferenceCollector rc = this.GetParent().GameObject.GetComponent(); GameObject sendBtn = rc.Get("Send"); GameObject sendRpcBtn = rc.Get("SendRpc"); sendBtn.GetComponent