Component.cs 733 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. namespace Base
  2. {
  3. /// <summary>
  4. /// Component的Id与Owner Entity Id一样
  5. /// </summary>
  6. public abstract class Component: Object
  7. {
  8. public Entity Owner { get; set; }
  9. public T GetOwner<T>() where T: Entity
  10. {
  11. T owner = this.Owner as T;
  12. if (owner == null)
  13. {
  14. Log.Error($"Owner类型是{this.Owner.GetType().Name}, 无法转成: {typeof(T).Name}");
  15. }
  16. return owner;
  17. }
  18. protected Component()
  19. {
  20. ObjectManager.Add(this);
  21. }
  22. protected Component(long id): base(id)
  23. {
  24. ObjectManager.Add(this);
  25. }
  26. public T GetComponent<T>() where T: Component
  27. {
  28. return this.Owner.GetComponent<T>();
  29. }
  30. public override void Dispose()
  31. {
  32. base.Dispose();
  33. ObjectManager.Remove(this.Id);
  34. }
  35. }
  36. }