Component.cs 635 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using MongoDB.Bson.Serialization.Attributes;
  2. namespace Base
  3. {
  4. /// <summary>
  5. /// Component的Id与Owner Entity Id一样
  6. /// </summary>
  7. public abstract class Component : Object
  8. {
  9. [BsonIgnore]
  10. public Entity Owner { get; set; }
  11. protected Component()
  12. {
  13. ObjectManager.Add(this);
  14. }
  15. protected Component(long id): base(id)
  16. {
  17. ObjectManager.Add(this);
  18. }
  19. public T GetComponent<T>() where T: Component
  20. {
  21. return this.Owner.GetComponent<T>();
  22. }
  23. public override void Dispose()
  24. {
  25. if (this.Id == 0)
  26. {
  27. return;
  28. }
  29. long id = this.Id;
  30. base.Dispose();
  31. ObjectManager.Remove(id);
  32. }
  33. }
  34. }