Component.cs 714 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.ComponentEventManager.Add(this);
  17. }
  18. protected Component(long id): base(id)
  19. {
  20. Game.ComponentEventManager.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.ComponentEventManager.Remove(this);
  34. }
  35. }
  36. }