Component.cs 805 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Base;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace Model
  4. {
  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. Game.EntityEventManager.Add(this);
  17. }
  18. protected Component(long id): base(id)
  19. {
  20. Game.EntityEventManager.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. Game.EntityEventManager.Remove(this);
  34. }
  35. public override void EndInit()
  36. {
  37. base.EndInit();
  38. Game.EntityEventManager.Add(this);
  39. }
  40. }
  41. }