Files
OldThink/Content.Client/Parallax/ParallaxOverlay.cs

65 lines
2.3 KiB
C#
Raw Normal View History

using Content.Client.Interfaces.Parallax;
using Robust.Client.Graphics;
using Robust.Client.Graphics.Drawing;
using Robust.Client.Graphics.Overlays;
using Robust.Client.Graphics.Shaders;
using Robust.Client.Interfaces.Graphics.ClientEye;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
2018-11-30 21:54:30 +01:00
namespace Content.Client.Parallax
{
public class ParallaxOverlay : Overlay
{
#pragma warning disable 649
[Dependency] private readonly IParallaxManager _parallaxManager;
[Dependency] private readonly IEyeManager _eyeManager;
[Dependency] private readonly IPrototypeManager _prototypeManager;
#pragma warning restore 649
public override bool AlwaysDirty => true;
private const float Slowness = 0.5f;
private Texture _parallaxTexture;
public override OverlaySpace Space => OverlaySpace.ScreenSpaceBelowWorld;
public ParallaxOverlay() : base(nameof(ParallaxOverlay))
{
IoCManager.InjectDependencies(this);
Shader = _prototypeManager.Index<ShaderPrototype>("unshaded").Instance();
if (_parallaxManager.ParallaxTexture == null)
{
_parallaxManager.OnTextureLoaded += texture => _parallaxTexture = texture;
}
else
{
_parallaxTexture = _parallaxManager.ParallaxTexture;
}
}
2019-07-06 19:20:20 +02:00
protected override void Draw(DrawingHandleBase handle)
2018-11-30 21:54:30 +01:00
{
if (_parallaxTexture == null)
{
return;
}
2019-07-06 19:20:20 +02:00
var screenHandle = (DrawingHandleScreen) handle;
2018-11-30 21:54:30 +01:00
var (sizeX, sizeY) = _parallaxTexture.Size;
var (posX, posY) = _eyeManager.ScreenToMap(Vector2.Zero).Position;
2018-11-30 21:54:30 +01:00
var (ox, oy) = (Vector2i) new Vector2(-posX / Slowness, posY / Slowness);
ox = MathHelper.Mod(ox, sizeX);
oy = MathHelper.Mod(oy, sizeY);
2019-07-06 19:20:20 +02:00
screenHandle.DrawTexture(_parallaxTexture, new Vector2(ox, oy));
screenHandle.DrawTexture(_parallaxTexture, new Vector2(ox - sizeX, oy));
screenHandle.DrawTexture(_parallaxTexture, new Vector2(ox, oy - sizeY));
screenHandle.DrawTexture(_parallaxTexture, new Vector2(ox - sizeX, oy - sizeY));
2018-11-30 21:54:30 +01:00
}
}
}