Component.cs 625 B

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