Component.cs 664 B

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