Component.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using XGame.Framework.Interfaces;
  3. namespace XGame.Framework.Components
  4. {
  5. /// <summary>
  6. /// 逻辑行为组件基类
  7. /// </summary>
  8. /// <typeparam name="TContext"></typeparam>
  9. public abstract class Component<TContext> : IComponent, IReference, IActiveable, IDisposable
  10. {
  11. public TContext Context { get; private set; }
  12. public virtual int Order => 0;
  13. void IComponent.Bind(object handle)
  14. {
  15. Context = (TContext)handle;
  16. }
  17. public bool Active { get; private set; }
  18. void IActiveable.Enable(object intent)
  19. {
  20. if (Active) return;
  21. Active = true;
  22. OnEnable(intent);
  23. }
  24. void IActiveable.Disable()
  25. {
  26. if (!Active) return;
  27. Active = false;
  28. OnDisable();
  29. }
  30. void IReference.Clear()
  31. {
  32. OnDispose();
  33. }
  34. void IDisposable.Dispose()
  35. {
  36. OnDispose();
  37. Context = default;
  38. }
  39. protected virtual void OnEnable(object intent)
  40. {
  41. }
  42. protected virtual void OnDisable()
  43. {
  44. }
  45. /// <summary>
  46. /// 销毁事件回调
  47. /// 缓存池回收也会触发OnDispose,但不真正销毁
  48. /// </summary>
  49. protected abstract void OnDispose();
  50. }
  51. }