ParallaxSystem.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Numerics;
  2. using Content.Client.Parallax.Data;
  3. using Content.Client.Parallax.Managers;
  4. using Content.Shared.Parallax;
  5. using Robust.Client.Graphics;
  6. using Robust.Shared.Map;
  7. using Robust.Shared.Prototypes;
  8. namespace Content.Client.Parallax;
  9. public sealed class ParallaxSystem : SharedParallaxSystem
  10. {
  11. [Dependency] private readonly IMapManager _map = default!;
  12. [Dependency] private readonly IOverlayManager _overlay = default!;
  13. [Dependency] private readonly IParallaxManager _parallax = default!;
  14. [ValidatePrototypeId<ParallaxPrototype>]
  15. private const string Fallback = "Default";
  16. public const int ParallaxZIndex = 0;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. _overlay.AddOverlay(new ParallaxOverlay());
  21. SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnReload);
  22. SubscribeLocalEvent<ParallaxComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
  23. }
  24. private void OnReload(PrototypesReloadedEventArgs obj)
  25. {
  26. if (!obj.WasModified<ParallaxPrototype>())
  27. return;
  28. _parallax.UnloadParallax(Fallback);
  29. _parallax.LoadDefaultParallax();
  30. foreach (var comp in EntityQuery<ParallaxComponent>(true))
  31. {
  32. _parallax.UnloadParallax(comp.Parallax);
  33. _parallax.LoadParallaxByName(comp.Parallax);
  34. }
  35. }
  36. public override void Shutdown()
  37. {
  38. base.Shutdown();
  39. _overlay.RemoveOverlay<ParallaxOverlay>();
  40. }
  41. private void OnAfterAutoHandleState(EntityUid uid, ParallaxComponent component, ref AfterAutoHandleStateEvent args)
  42. {
  43. if (!_parallax.IsLoaded(component.Parallax))
  44. {
  45. _parallax.LoadParallaxByName(component.Parallax);
  46. }
  47. }
  48. public ParallaxLayerPrepared[] GetParallaxLayers(MapId mapId)
  49. {
  50. return _parallax.GetParallaxLayers(GetParallax(_map.GetMapEntityId(mapId)));
  51. }
  52. public string GetParallax(MapId mapId)
  53. {
  54. return GetParallax(_map.GetMapEntityId(mapId));
  55. }
  56. public string GetParallax(EntityUid mapUid)
  57. {
  58. return TryComp<ParallaxComponent>(mapUid, out var parallax) ? parallax.Parallax : Fallback;
  59. }
  60. /// <summary>
  61. /// Draws a texture as parallax in the specified world handle.
  62. /// </summary>
  63. /// <param name="worldHandle"></param>
  64. /// <param name="worldAABB">WorldAABB to use</param>
  65. /// <param name="sprite">Sprite to draw</param>
  66. /// <param name="curTime">Current time, unused if scrolling not set</param>
  67. /// <param name="position">Current position of the parallax</param>
  68. /// <param name="scrolling">How much to scroll the parallax texture per second</param>
  69. /// <param name="scale">Scale of the texture</param>
  70. /// <param name="slowness">How slow the parallax moves compared to position</param>
  71. /// <param name="modulate">Color modulation applied to drawing the texture</param>
  72. public void DrawParallax(
  73. DrawingHandleWorld worldHandle,
  74. Box2 worldAABB,
  75. Texture sprite,
  76. TimeSpan curTime,
  77. Vector2 position,
  78. Vector2 scrolling,
  79. float scale = 1f,
  80. float slowness = 0f,
  81. Color? modulate = null)
  82. {
  83. // Size of the texture in world units.
  84. var size = sprite.Size / (float) EyeManager.PixelsPerMeter * scale;
  85. var scrolled = scrolling * (float) curTime.TotalSeconds;
  86. // Origin - start with the parallax shift itself.
  87. var originBL = position * slowness + scrolled;
  88. // Centre the image.
  89. originBL -= size / 2;
  90. // Remove offset so we can floor.
  91. var flooredBL = worldAABB.BottomLeft - originBL;
  92. // Floor to background size.
  93. flooredBL = (flooredBL / size).Floored() * size;
  94. // Re-offset.
  95. flooredBL += originBL;
  96. for (var x = flooredBL.X; x < worldAABB.Right; x += size.X)
  97. {
  98. for (var y = flooredBL.Y; y < worldAABB.Top; y += size.Y)
  99. {
  100. var box = Box2.FromDimensions(new Vector2(x, y), size);
  101. worldHandle.DrawTextureRect(sprite, box, modulate);
  102. }
  103. }
  104. }
  105. }