Component.cs 554 B

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