Component.cs 630 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using MongoDB.Bson.Serialization.Attributes;
  2. namespace Model
  3. {
  4. [BsonKnownTypes(typeof(AConfigComponent))]
  5. public abstract 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. }
  16. protected Component(long id): base(id)
  17. {
  18. }
  19. public T GetComponent<T>() where T : Component
  20. {
  21. return this.Entity.GetComponent<T>();
  22. }
  23. public override void Dispose()
  24. {
  25. if (this.Id == 0)
  26. {
  27. return;
  28. }
  29. base.Dispose();
  30. this.Entity?.RemoveComponent(this.GetType());
  31. }
  32. }
  33. }