Component.cs 728 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 { protected get; set; }
  11. protected T GetOwner<T>() where T: Entity
  12. {
  13. return this.Owner as T;
  14. }
  15. protected Component()
  16. {
  17. ObjectManager.Add(this);
  18. }
  19. protected Component(long id): base(id)
  20. {
  21. ObjectManager.Add(this);
  22. }
  23. protected T GetComponent<T>() where T: Component
  24. {
  25. return this.Owner.GetComponent<T>();
  26. }
  27. public override void Dispose()
  28. {
  29. if (this.Id == 0)
  30. {
  31. return;
  32. }
  33. long id = this.Id;
  34. base.Dispose();
  35. ObjectManager.Remove(id);
  36. }
  37. }
  38. }