SimpleGameComponent.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended
  4. {
  5. public abstract class SimpleGameComponent : IGameComponent, IUpdateable, IDisposable, IComparable<GameComponent>, IComparable<SimpleGameComponent>
  6. {
  7. private bool _isInitialized;
  8. protected SimpleGameComponent()
  9. {
  10. }
  11. public virtual void Dispose()
  12. {
  13. if (_isInitialized)
  14. {
  15. UnloadContent();
  16. _isInitialized = false;
  17. }
  18. }
  19. private bool _isEnabled = true;
  20. public bool IsEnabled
  21. {
  22. get => _isEnabled;
  23. set
  24. {
  25. if (_isEnabled == value)
  26. return;
  27. _isEnabled = value;
  28. EnabledChanged?.Invoke(this, EventArgs.Empty);
  29. }
  30. }
  31. public virtual void Initialize()
  32. {
  33. if (!_isInitialized)
  34. {
  35. LoadContent();
  36. _isInitialized = true;
  37. }
  38. }
  39. protected virtual void LoadContent() { }
  40. protected virtual void UnloadContent() { }
  41. bool IUpdateable.Enabled => _isEnabled;
  42. private int _updateOrder;
  43. public int UpdateOrder
  44. {
  45. get => _updateOrder;
  46. set
  47. {
  48. if (_updateOrder == value)
  49. return;
  50. _updateOrder = value;
  51. UpdateOrderChanged?.Invoke(this, EventArgs.Empty);
  52. }
  53. }
  54. public event EventHandler<EventArgs> EnabledChanged;
  55. public event EventHandler<EventArgs> UpdateOrderChanged;
  56. public abstract void Update(GameTime gameTime);
  57. public int CompareTo(GameComponent other)
  58. {
  59. return other.UpdateOrder - UpdateOrder;
  60. }
  61. public int CompareTo(SimpleGameComponent other)
  62. {
  63. return other.UpdateOrder - UpdateOrder;
  64. }
  65. }
  66. }