Component.cs 756 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }
  16. protected Component(long id): base(id)
  17. {
  18. }
  19. public T GetComponent<T>() where T : Component
  20. {
  21. return this.Owner.GetComponent<T>();
  22. }
  23. public override void Dispose()
  24. {
  25. if (this.Id == 0)
  26. {
  27. return;
  28. }
  29. base.Dispose();
  30. if (this.Owner.Id != 0)
  31. {
  32. this.Owner?.RemoveComponent(this.GetType());
  33. }
  34. }
  35. public override void EndInit()
  36. {
  37. base.EndInit();
  38. ObjectEvents.Instance.Add(this);
  39. }
  40. }
  41. }