提交 e9865b97 编写于 作者: T tanghai

1.做了一个结构上的大改动:Entity改成继承于Component,这样一来Entity也是组件,组件从此可以有能力再挂组件。这么做的原因是:entita...

1.做了一个结构上的大改动:Entity改成继承于Component,这样一来Entity也是组件,组件从此可以有能力再挂组件。这么做的原因是:entitas的ecs组件颗粒度太小,如果et按照目前的架构,实现ecs机制会导致组件要化分的非常细,如果改成entity也是组件,组件也可以挂组件,那么组件可以划分的可大可小,大组件又可以包括小组件,这样就会十分灵活。
2.Component有个Parent字段,当作为组件时Parent是组件所有者,当entity作为child时,Parent可以作为父亲,这个字段可以复用
上级 553aae86
......@@ -31,8 +31,8 @@ namespace Hotfix
{
self.entityActorHandler = new CommonEntityActorHandler();
self.queue = new EQueue<ActorMessageInfo>();
self.actorId = self.Entity.Id;
Game.Scene.GetComponent<ActorManagerComponent>().Add(self.Entity);
self.actorId = self.Parent.Id;
Game.Scene.GetComponent<ActorManagerComponent>().Add(self.Parent);
self.HandleAsync();
}
......@@ -40,8 +40,8 @@ namespace Hotfix
{
self.entityActorHandler = iEntityActorHandler;
self.queue = new EQueue<ActorMessageInfo>();
self.actorId = self.Entity.Id;
Game.Scene.GetComponent<ActorManagerComponent>().Add(self.Entity);
self.actorId = self.Parent.Id;
Game.Scene.GetComponent<ActorManagerComponent>().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)
{
......
......@@ -29,7 +29,7 @@ namespace Hotfix
self.Awake(NetworkProtocol.TCP);
self.MessagePacker = new MongoPacker();
self.MessageDispatcher = new InnerMessageDispatcher();
self.AppType = self.GetComponent<StartConfigComponent>().StartConfig.AppType;
self.AppType = self.Parent.GetComponent<StartConfigComponent>().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<StartConfigComponent>().StartConfig.AppType;
self.AppType = self.Parent.GetComponent<StartConfigComponent>().StartConfig.AppType;
}
public static void Update(this NetInnerComponent self)
......
......@@ -15,7 +15,7 @@ namespace Hotfix
{
public static void Start(this RealmGateAddressComponent component)
{
StartConfig[] startConfigs = component.GetComponent<StartConfigComponent>().GetAll();
StartConfig[] startConfigs = component.Parent.GetComponent<StartConfigComponent>().GetAll();
foreach (StartConfig config in startConfigs)
{
if (!config.AppType.Is(AppType.Gate))
......
......@@ -5,6 +5,7 @@ namespace Model
[BsonKnownTypes(typeof(AConfigComponent))]
[BsonKnownTypes(typeof(UnitGateComponent))]
[BsonKnownTypes(typeof(NumericComponent))]
[BsonKnownTypes(typeof(Entity))]
public partial class Component
{
}
......
......@@ -32,7 +32,7 @@ namespace Model
public void Load()
{
AppType appType = this.GetComponent<StartConfigComponent>().StartConfig.AppType;
AppType appType = this.Parent.GetComponent<StartConfigComponent>().StartConfig.AppType;
Log.Info("apptype: " + appType);
this.handlers = new Dictionary<Type, IMActorHandler>();
......
......@@ -224,7 +224,7 @@ namespace Model
tasks = new EQueue<LocationTask>();
this.taskQueues[key] = tasks;
}
task.Scene = this.GetEntity<Scene>();
task.Scene = this.GetParent<Scene>();
tasks.Enqueue(task);
}
......
......@@ -31,7 +31,7 @@ namespace Model
public void Load()
{
AppType appType = this.GetComponent<StartConfigComponent>().StartConfig.AppType;
AppType appType = this.Parent.GetComponent<StartConfigComponent>().StartConfig.AppType;
this.handlers = new Dictionary<Type, List<IMHandler>>();
......
......@@ -58,7 +58,7 @@ namespace Model
}
NumericDic[numericType] = value;
Game.Scene.GetComponent<EventComponent>().Run(EventIdType.NumbericChange, this.Entity.Id, numericType, value);
Game.Scene.GetComponent<EventComponent>().Run(EventIdType.NumbericChange, this.Parent.Id, numericType, value);
}
}
......
......@@ -52,7 +52,7 @@ namespace Model
this.status = LockStatus.LockRequesting;
// 真身直接本地请求锁,镜像需要调用Rpc获取锁
MasterComponent masterComponent = this.GetComponent<MasterComponent>();
MasterComponent masterComponent = this.Parent.GetComponent<MasterComponent>();
if (masterComponent != null)
{
await masterComponent.Lock(this.address);
......@@ -82,7 +82,7 @@ namespace Model
{
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(this.address);
string serverAddress = Game.Scene.GetComponent<StartConfigComponent>().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<G2G_LockResponse>(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}");
}
}
......
......@@ -6,7 +6,6 @@ namespace Model
{
[ProtoContract]
[BsonKnownTypes(typeof(AFrameMessage))]
[BsonKnownTypes(typeof(FrameMessage))]
public abstract partial class AActorMessage : AMessage
{
}
......
......@@ -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<T>() where T : Entity
public T GetParent<T>() where T : Entity
{
return this.Entity as T;
return this.Parent as T;
}
protected Component()
{
this.Id = 1;
}
public T GetComponent<T>() where T : Component
{
return this.Entity.GetComponent<T>();
}
public override void Dispose()
{
......@@ -31,8 +26,6 @@ namespace Model
}
base.Dispose();
this.Entity?.RemoveComponent(this.GetType());
}
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@
namespace Model
{
[BsonKnownTypes(typeof(AConfigComponent))]
[BsonKnownTypes(typeof(Entity))]
public partial class Component
{
}
......
......@@ -5,7 +5,7 @@
public static T Create<T>(Entity entity) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer);
return disposer;
}
......@@ -13,7 +13,7 @@
public static T Create<T, A>(Entity entity, A a) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer, a);
return disposer;
}
......@@ -21,7 +21,7 @@
public static T Create<T, A, B>(Entity entity, A a, B b) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer, a, b);
return disposer;
}
......@@ -29,7 +29,7 @@
public static T Create<T, A, B, C>(Entity entity, A a, B b, C c) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer, a, b, c);
return disposer;
}
......
......@@ -3,7 +3,6 @@ using MongoDB.Bson.Serialization.Attributes;
namespace Model
{
[BsonKnownTypes(typeof(Entity))]
[BsonKnownTypes(typeof(Component))]
public abstract class Disposer : Object, IDisposable
{
......
......@@ -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<Component> components = new HashSet<Component>();
private HashSet<Component> components;
[BsonIgnore]
private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
private Dictionary<Type, Component> componentDict;
protected Entity()
{
this.Id = IdGenerater.GenerateId();
this.components = new HashSet<Component>();
this.componentDict = new Dictionary<Type, Component>();
}
protected Entity(long id)
{
this.Id = id;
this.components = new HashSet<Component>();
this.componentDict = new Dictionary<Type, Component>();
}
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);
}
}
......
......@@ -31,7 +31,7 @@ namespace Model
public void Awake()
{
Animator animator = this.GetEntity<Unit>().GameObject.GetComponent<Animator>();
Animator animator = this.GetParent<Unit>().GameObject.GetComponent<Animator>();
if (animator == null)
{
......
......@@ -118,7 +118,7 @@ namespace Model
private Node CreateTreeNode(NodeProto proto)
{
Node node = this.CreateOneNode(proto);
node.EndInit(this.GetEntity<Scene>());
node.EndInit(this.GetParent<Scene>());
if (proto.Children == null)
{
......
......@@ -208,6 +208,8 @@ namespace Model
}
base.Dispose();
this.Parent?.RemoveComponent<BundleDownloaderComponent>();
}
}
}
......@@ -93,7 +93,7 @@ namespace Model
public void Awake()
{
this.mainSpeed = this.AddSpeed(new Vector3());
this.animatorComponent = this.GetComponent<AnimatorComponent>();
this.animatorComponent = this.Parent.GetComponent<AnimatorComponent>();
}
public void Update()
......@@ -110,7 +110,7 @@ namespace Model
return;
}
Unit unit = this.GetEntity<Unit>();
Unit unit = this.GetParent<Unit>();
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<Unit>().Rotation = v;
this.GetParent<Unit>().Rotation = v;
}
public void MoveToDest(Vector3 dest, float speedValue)
{
if ((dest - this.GetEntity<Unit>().Position).magnitude < 0.1f)
if ((dest - this.GetParent<Unit>().Position).magnitude < 0.1f)
{
this.IsArrived = true;
return;
}
this.IsArrived = false;
this.hasDest = true;
Vector3 speed = dest - this.GetEntity<Unit>().Position;
Vector3 speed = dest - this.GetParent<Unit>().Position;
speed = speed.normalized * speedValue;
this.MainSpeed = speed;
this.Dest = dest;
......@@ -201,7 +201,7 @@ namespace Model
/// </summary>
public void Turn2D(Vector3 dir, float turnTime = 0.1f)
{
Vector3 nexpos = this.GetEntity<Unit>().GameObject.transform.position + dir;
Vector3 nexpos = this.GetParent<Unit>().GameObject.transform.position + dir;
Turn(nexpos, turnTime);
}
......@@ -210,10 +210,10 @@ namespace Model
/// </summary>
public void Turn(Vector3 target, float turnTime = 0.1f)
{
Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetEntity<Unit>().Position, target);
Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent<Unit>().Position, target);
this.To = quaternion;
this.From = this.GetEntity<Unit>().Rotation;
this.From = this.GetParent<Unit>().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<Unit>().Rotation;
this.From = this.GetParent<Unit>().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<Unit>().Rotation;
this.From = this.GetParent<Unit>().Rotation;
this.t = 0;
this.TurnTime = turnTime;
}
public void TurnImmediately(Quaternion quaternion)
{
this.GetEntity<Unit>().Rotation = quaternion;
this.GetParent<Unit>().Rotation = quaternion;
}
public void TurnImmediately(Vector3 target)
{
Vector3 nowPos = this.GetEntity<Unit>().Position;
Vector3 nowPos = this.GetParent<Unit>().Position;
if (nowPos == target)
{
return;
}
Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetEntity<Unit>().Position, target);
this.GetEntity<Unit>().Rotation = quaternion;
Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent<Unit>().Position, target);
this.GetParent<Unit>().Rotation = quaternion;
}
public void TurnImmediately(float angle)
{
Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle);
this.GetEntity<Unit>().Rotation = quaternion;
this.GetParent<Unit>().Rotation = quaternion;
}
public override void Dispose()
......
......@@ -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<EventComponent>().Run(EventIdType.NumbericChange, this.Entity.Id, numericType, final);
Game.Scene.GetComponent<EventComponent>().Run(EventIdType.NumbericChange, this.Parent.Id, numericType, final);
}
}
}
\ No newline at end of file
......@@ -93,7 +93,7 @@ namespace Model
{
try
{
UI ui = UiTypes[type].Create(this.GetEntity<Scene>(), type, Root);
UI ui = UiTypes[type].Create(this.GetParent<Scene>(), type, Root);
uis.Add(type, ui);
// 设置canvas
......
......@@ -124,7 +124,7 @@ namespace Model
try
{
op = (Opcode)opcode;
Type messageType = this.network.Entity.GetComponent<OpcodeTypeComponent>().GetType(op);
Type messageType = this.network.Parent.GetComponent<OpcodeTypeComponent>().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<OpcodeTypeComponent>().GetOpcode(message.GetType());
Opcode opcode = this.network.Parent.GetComponent<OpcodeTypeComponent>().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<NetInnerComponent>().Get(this.RemoteAddress);
Session session = this.network.Parent.GetComponent<NetInnerComponent>().Get(this.RemoteAddress);
session.RunDecompressedBytes(op, messageBytes, 0, messageBytes.Length);
return;
}
......
......@@ -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)
......
......@@ -9,7 +9,7 @@ namespace Model
public void Awake()
{
UILoadingComponent self = this.Get();
self.text = self.GetEntity<UI>().GameObject.Get<GameObject>("Text").GetComponent<Text>();
self.text = self.GetParent<UI>().GameObject.Get<GameObject>("Text").GetComponent<Text>();
}
public async void Start()
......
......@@ -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<T>() where T : Entity
public T GetParent<T>() 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<T>() where T : Component
{
return this.Entity.GetComponent<T>();
}
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
......@@ -7,7 +7,7 @@ namespace Hotfix
public static T Create<T>(Entity entity) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer);
return disposer;
}
......@@ -15,7 +15,7 @@ namespace Hotfix
public static T Create<T, A>(Entity entity, A a) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer, a);
return disposer;
}
......@@ -23,7 +23,7 @@ namespace Hotfix
public static T Create<T, A, B>(Entity entity, A a, B b) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer, a, b);
return disposer;
}
......@@ -31,7 +31,7 @@ namespace Hotfix
public static T Create<T, A, B, C>(Entity entity, A a, B b, C c) where T : Component
{
T disposer = ObjectPool.Instance.Fetch<T>();
disposer.Entity = entity;
disposer.Parent = entity;
ObjectEvents.Instance.Awake(disposer, a, b, c);
return disposer;
}
......
......@@ -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<Component> components = new HashSet<Component>();
private readonly HashSet<Component> components;
[BsonIgnore]
private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
private readonly Dictionary<Type, Component> componentDict;
protected Entity()
{
this.Id = IdGenerater.GenerateId();
this.components = new HashSet<Component>();
this.componentDict = new Dictionary<Type, Component>();
}
protected Entity(long id)
{
this.Id = id;
this.components = new HashSet<Component>();
this.componentDict = new Dictionary<Type, Component>();
}
public override void Dispose()
......
......@@ -91,7 +91,7 @@ namespace Hotfix
{
try
{
UI ui = UiTypes[type].Create(this.GetEntity<Scene>(), type, Root);
UI ui = UiTypes[type].Create(this.GetParent<Scene>(), type, Root);
uis.Add(type, ui);
// 设置canvas
......
......@@ -21,7 +21,7 @@ namespace Hotfix
public void Awake()
{
ReferenceCollector rc = this.GetEntity<UI>().GameObject.GetComponent<ReferenceCollector>();
ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
GameObject sendBtn = rc.Get<GameObject>("Send");
GameObject sendRpcBtn = rc.Get<GameObject>("SendRpc");
sendBtn.GetComponent<Button>().onClick.Add(this.OnSend);
......
......@@ -21,7 +21,7 @@ namespace Hotfix
public void Awake()
{
ReferenceCollector rc = this.GetEntity<UI>().GameObject.GetComponent<ReferenceCollector>();
ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
loginBtn = rc.Get<GameObject>("LoginBtn");
loginBtn.GetComponent<Button>().onClick.Add(OnLogin);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册