| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using MongoDB.Bson.Serialization.Attributes;
- namespace ETModel
- {
- [BsonIgnoreExtraElements]
- public abstract class Component : Object, IDisposable
- {
- [BsonIgnore]
- public long InstanceId { get; private set; }
- [BsonIgnore]
- private bool isFromPool;
- [BsonIgnore]
- public bool IsFromPool
- {
- get
- {
- return this.isFromPool;
- }
- set
- {
- this.isFromPool = value;
- if (!this.isFromPool)
- {
- return;
- }
- this.InstanceId = IdGenerater.GenerateId();
- Game.EventSystem.Add(this);
- }
- }
- [BsonIgnore]
- public bool IsDisposed
- {
- get
- {
- return this.InstanceId == 0;
- }
- }
-
- [BsonIgnore]
- public Component Parent { get; set; }
- 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.GenerateId();
- }
- 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);
- }
- }
- public override void EndInit()
- {
- Game.EventSystem.Deserialize(this);
- }
-
- public override string ToString()
- {
- return MongoHelper.ToJson(this);
- }
- }
- }
|