Component.cs 722 B

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