Component.cs 848 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using MongoDB.Bson.Serialization.Attributes;
  2. namespace Model
  3. {
  4. [ILBinding]
  5. [BsonKnownTypes(typeof (AConfigComponent))]
  6. public abstract class Component: Disposer
  7. {
  8. [BsonIgnore]
  9. public Entity Owner { get; set; }
  10. public T GetOwner<T>() where T : Entity
  11. {
  12. return this.Owner as T;
  13. }
  14. protected Component()
  15. {
  16. ObjectEvents.Instance.Add(this);
  17. }
  18. protected Component(long id): base(id)
  19. {
  20. ObjectEvents.Instance.Add(this);
  21. }
  22. public T GetComponent<T>() where T : Component
  23. {
  24. return this.Owner.GetComponent<T>();
  25. }
  26. public override void Dispose()
  27. {
  28. if (this.Id == 0)
  29. {
  30. return;
  31. }
  32. base.Dispose();
  33. this.Owner.RemoveComponent(this.GetType());
  34. ObjectEvents.Instance.Remove(this);
  35. }
  36. public override void EndInit()
  37. {
  38. base.EndInit();
  39. ObjectEvents.Instance.Add(this);
  40. }
  41. }
  42. }