2021-04-19 09:52:40 +02:00
|
|
|
using System;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Client.Parallax.Managers;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Client.Graphics;
|
2021-03-09 04:33:41 -06:00
|
|
|
using Robust.Shared.Enums;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Maths;
|
|
|
|
|
using Robust.Shared.Prototypes;
|
2018-11-30 21:54:30 +01:00
|
|
|
|
|
|
|
|
namespace Content.Client.Parallax
|
|
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class ParallaxOverlay : Overlay
|
2018-11-30 21:54:30 +01:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IParallaxManager _parallaxManager = default!;
|
|
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
2018-11-30 21:54:30 +01:00
|
|
|
|
|
|
|
|
private const float Slowness = 0.5f;
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private Texture? _parallaxTexture;
|
2018-11-30 21:54:30 +01:00
|
|
|
|
2021-04-19 09:52:40 +02:00
|
|
|
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowWorld;
|
2020-07-31 14:01:34 -07:00
|
|
|
private readonly ShaderInstance _shader;
|
2018-11-30 21:54:30 +01:00
|
|
|
|
2021-03-09 04:33:41 -06:00
|
|
|
public ParallaxOverlay()
|
2018-11-30 21:54:30 +01:00
|
|
|
{
|
|
|
|
|
IoCManager.InjectDependencies(this);
|
2020-07-31 14:01:34 -07:00
|
|
|
_shader = _prototypeManager.Index<ShaderPrototype>("unshaded").Instance();
|
2018-11-30 21:54:30 +01:00
|
|
|
|
|
|
|
|
if (_parallaxManager.ParallaxTexture == null)
|
|
|
|
|
{
|
|
|
|
|
_parallaxManager.OnTextureLoaded += texture => _parallaxTexture = texture;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_parallaxTexture = _parallaxManager.ParallaxTexture;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 09:52:40 +02:00
|
|
|
protected override void Draw(in OverlayDrawArgs args)
|
2018-11-30 21:54:30 +01:00
|
|
|
{
|
2021-04-19 09:52:40 +02:00
|
|
|
if (_parallaxTexture == null || args.Viewport.Eye == null)
|
2018-11-30 21:54:30 +01:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 09:52:40 +02:00
|
|
|
var screenHandle = args.WorldHandle;
|
|
|
|
|
screenHandle.UseShader(_shader);
|
2019-07-06 19:20:20 +02:00
|
|
|
|
2021-04-19 09:52:40 +02:00
|
|
|
var (sizeX, sizeY) = _parallaxTexture.Size / (float) EyeManager.PixelsPerMeter;
|
|
|
|
|
var (posX, posY) = args.Viewport.Eye.Position;
|
|
|
|
|
var o = new Vector2(posX * Slowness, posY * Slowness);
|
2018-11-30 21:54:30 +01:00
|
|
|
|
2021-04-19 09:52:40 +02:00
|
|
|
// Remove offset so we can floor.
|
2021-11-03 11:27:39 +11:00
|
|
|
var (l, b) = args.WorldAABB.BottomLeft - o;
|
2021-04-19 09:52:40 +02:00
|
|
|
|
|
|
|
|
// Floor to background size.
|
|
|
|
|
l = sizeX * MathF.Floor(l / sizeX);
|
|
|
|
|
b = sizeY * MathF.Floor(b / sizeY);
|
|
|
|
|
|
|
|
|
|
// Re-offset.
|
|
|
|
|
l += o.X;
|
|
|
|
|
b += o.Y;
|
|
|
|
|
|
2021-11-03 11:27:39 +11:00
|
|
|
for (var x = l; x < args.WorldAABB.Right; x += sizeX)
|
2021-04-19 09:52:40 +02:00
|
|
|
{
|
2021-11-03 11:27:39 +11:00
|
|
|
for (var y = b; y < args.WorldAABB.Top; y += sizeY)
|
2021-04-19 09:52:40 +02:00
|
|
|
{
|
|
|
|
|
screenHandle.DrawTexture(_parallaxTexture, (x, y));
|
2020-01-26 05:02:12 -08:00
|
|
|
}
|
|
|
|
|
}
|
2018-11-30 21:54:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|