Component.cs 828 B

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