123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using MongoDB.Bson.Serialization.Attributes;
- using System;
- namespace KYFramework
- {
- [BsonIgnoreExtraElements]
- public abstract class Component : Object , IDisposable
- {
- [BsonIgnore]
- public long InstanceId { get; set; }
- [BsonIgnore]
- protected bool isFromPool;
- [BsonIgnore]
- public bool IsFromPool
- {
- get
- {
- return isFromPool;
- }
- set
- {
- isFromPool = value;
- if (!this.isFromPool) return;
- if(InstanceId == 0)
- InstanceId = IdGenerater.GenerateInstanceId();
- }
- }
- [BsonIgnore]
- public bool IsDisposed
- {
- get
- {
- return this.InstanceId == 0;
- }
- }
- private Component parent;
- [BsonIgnore]
- public Component Parent
- {
- get
- {
- return this.parent;
- }
- set
- {
- parent = value;
- }
- }
-
- public T GetParent<T>() where T : Component
- {
- return this.Parent as T;
- }
- [BsonIgnore]
- public Entity Entity
- {
- get
- {
- return this.Parent as Entity;
- }
- }
- protected Component()
- {
- this.InstanceId = IdGenerater.GenerateInstanceId();
- }
- public virtual void Dispose()
- {
- if (this.IsDisposed)
- {
- return;
- }
- // 触发Destroy事件
- Game.EventSystem.Destroy(this);
- Game.EventSystem.Remove(this.InstanceId);
- this.InstanceId = 0;
- if (this.IsFromPool)
- {
- Game.ObjectPool.Recycle(this);
- }
- else
- {
-
- }
- }
- public override void EndInit()
- {
- Game.EventSystem.Deserialize(this);
- }
- public override string ToString()
- {
- return MongoHelper.ToJson(this);
- }
- }
- }
|