fish-station/Content.Shared/Camera/SharedCameraRecoilSystem.cs
Vigers Ray cef39bc293 Merge remote-tracking branch 'refs/remotes/wizards/master'
# Conflicts:
#	Content.Client/UserInterface/Controls/RadialContainer.cs
#	Content.Server/Lathe/LatheSystem.cs
#	Content.Server/Silicons/Laws/SiliconLawSystem.cs
#	Content.Shared/Inventory/InventorySystem.Relay.cs
#	Content.Shared/Materials/SharedMaterialReclaimerSystem.cs
#	Content.Shared/Medical/Cryogenics/SharedCryoPodSystem.cs
#	Content.Shared/VendingMachines/SharedVendingMachineSystem.cs
#	Content.Shared/Wieldable/SharedWieldableSystem.cs
#	Resources/Locale/en-US/_strings/station-events/events/radiation-storm.ftl
#	Resources/Locale/en-US/_strings/store/uplink-catalog.ftl
#	Resources/Prototypes/Atmospherics/gases.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/heads.yml
#	Resources/Prototypes/Datasets/Names/borg.yml
#	Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml
#	Resources/Prototypes/Entities/Mobs/Player/silicon.yml
#	Resources/Prototypes/Entities/Objects/Misc/subdermal_implants.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Snipers/snipers.yml
#	Resources/Prototypes/Entities/Structures/Specific/Atmospherics/sensor.yml
#	Resources/ServerInfo/Guidebook/Engineering/AME.xml
#	Resources/ServerInfo/Guidebook/Engineering/AirlockSecurity.xml
#	Resources/ServerInfo/Guidebook/Engineering/Atmospherics.xml
#	Resources/ServerInfo/Guidebook/Engineering/Construction.xml
#	Resources/ServerInfo/Guidebook/Engineering/Engineering.xml
#	Resources/ServerInfo/Guidebook/Engineering/Fires.xml
#	Resources/ServerInfo/Guidebook/Engineering/NetworkConfigurator.xml
#	Resources/ServerInfo/Guidebook/Engineering/Networking.xml
#	Resources/ServerInfo/Guidebook/Engineering/Power.xml
#	Resources/ServerInfo/Guidebook/Engineering/RTG.xml
#	Resources/ServerInfo/Guidebook/Engineering/Shuttlecraft.xml
#	Resources/ServerInfo/Guidebook/Engineering/Singularity.xml
#	Resources/ServerInfo/Guidebook/Engineering/TEG.xml
#	Resources/ServerInfo/Guidebook/Security/CriminalRecords.xml
#	Resources/Textures/Clothing/OuterClothing/Vests/hazard.rsi/equipped-OUTERCLOTHING.png
#	Resources/Textures/Clothing/OuterClothing/Vests/hazard.rsi/icon.png
#	Resources/Textures/Clothing/OuterClothing/Vests/hazard.rsi/meta.json
#	Resources/Textures/Objects/Weapons/Guns/Rifles/lecter.rsi/equipped-BACKPACK.png
#	Resources/migration.yml
2025-01-30 11:33:49 +03:00

115 lines
3.6 KiB
C#

using System.Numerics;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using JetBrains.Annotations;
using Robust.Shared.Network;
using Robust.Shared.Serialization;
namespace Content.Shared.Camera;
[UsedImplicitly]
public abstract class SharedCameraRecoilSystem : EntitySystem
{
/// <summary>
/// Maximum rate of magnitude restore towards 0 kick.
/// </summary>
private const float RestoreRateMax = 30f;
/// <summary>
/// Minimum rate of magnitude restore towards 0 kick.
/// </summary>
private const float RestoreRateMin = 0.1f;
/// <summary>
/// Time in seconds since the last kick that lerps RestoreRateMin and RestoreRateMax
/// </summary>
private const float RestoreRateRamp = 4f;
/// <summary>
/// The maximum magnitude of the kick applied to the camera at any point.
/// </summary>
protected const float KickMagnitudeMax = 1f;
[Dependency] private readonly SharedContentEyeSystem _eye = default!;
[Dependency] private readonly INetManager _net = default!;
public override void Initialize()
{
SubscribeLocalEvent<CameraRecoilComponent, GetEyeOffsetEvent>(OnCameraRecoilGetEyeOffset);
}
private void OnCameraRecoilGetEyeOffset(Entity<CameraRecoilComponent> ent, ref GetEyeOffsetEvent args)
{
args.Offset += ent.Comp.BaseOffset + ent.Comp.CurrentKick;
}
/// <summary>
/// Applies explosion/recoil/etc kickback to the view of the entity.
/// </summary>
/// <remarks>
/// If the entity is missing <see cref="CameraRecoilComponent" /> and/or <see cref="EyeComponent" />,
/// this call will have no effect. It is safe to call this function on any entity.
/// </remarks>
public abstract void KickCamera(EntityUid euid, Vector2 kickback, CameraRecoilComponent? component = null);
private void UpdateEyes(float frameTime)
{
var query = AllEntityQuery<CameraRecoilComponent, EyeComponent>();
while (query.MoveNext(out var uid, out var recoil, out var eye))
{
var magnitude = recoil.CurrentKick.Length();
if (magnitude <= 0.005f)
{
recoil.CurrentKick = Vector2.Zero;
}
else // Continually restore camera to 0.
{
var normalized = recoil.CurrentKick.Normalized();
recoil.LastKickTime += frameTime;
var restoreRate = MathHelper.Lerp(RestoreRateMin, RestoreRateMax, Math.Min(1, recoil.LastKickTime / RestoreRateRamp));
var restore = normalized * restoreRate * frameTime;
var (x, y) = recoil.CurrentKick - restore;
if (Math.Sign(x) != Math.Sign(recoil.CurrentKick.X))
x = 0;
if (Math.Sign(y) != Math.Sign(recoil.CurrentKick.Y))
y = 0;
recoil.CurrentKick = new Vector2(x, y);
}
if (recoil.CurrentKick == recoil.LastKick)
continue;
recoil.LastKick = recoil.CurrentKick;
_eye.UpdateEyeOffset((uid, eye));
}
}
public override void Update(float frameTime)
{
if (_net.IsServer)
UpdateEyes(frameTime);
}
public override void FrameUpdate(float frameTime)
{
UpdateEyes(frameTime);
}
}
[Serializable]
[NetSerializable]
public sealed class CameraKickEvent : EntityEventArgs
{
public readonly NetEntity NetEntity;
public readonly Vector2 Recoil;
public CameraKickEvent(NetEntity netEntity, Vector2 recoil)
{
Recoil = recoil;
NetEntity = netEntity;
}
}