Component.cs 589 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using MongoDB.Bson.Serialization.Attributes;
  2. namespace Model
  3. {
  4. [BsonIgnoreExtraElements]
  5. public abstract partial class Component: Disposer
  6. {
  7. [BsonIgnore]
  8. public Entity Entity { get; set; }
  9. public T GetEntity<T>() where T : Entity
  10. {
  11. return this.Entity as T;
  12. }
  13. protected Component()
  14. {
  15. this.Id = 1;
  16. }
  17. public T GetComponent<T>() where T : Component
  18. {
  19. return this.Entity.GetComponent<T>();
  20. }
  21. public override void Dispose()
  22. {
  23. if (this.Id == 0)
  24. {
  25. return;
  26. }
  27. base.Dispose();
  28. this.Entity?.RemoveComponent(this.GetType());
  29. }
  30. }
  31. }