- add: TelescopeSystem. (#273)

This commit is contained in:
Aviu00
2024-04-06 21:31:51 +09:00
committed by GitHub
parent 6f6f259e12
commit d8570b8bda
5 changed files with 173 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
using System.Numerics;
using Content.Shared._White.Telescope;
using Content.Shared.Hands.Components;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.Player;
using Robust.Shared.Input;
using Robust.Shared.Timing;
namespace Content.Client._White.Telescope;
public sealed class TelescopeSystem : SharedTelescopeSystem
{
[Dependency] private readonly InputSystem _inputSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IInputManager _input = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!;
public override void Update(float frameTime)
{
base.Update(frameTime);
if (_timing.ApplyingState || !_timing.IsFirstTimePredicted || !_input.MouseScreenPosition.IsValid)
return;
var player = _player.LocalEntity;
if (!TryComp<HandsComponent>(player, out var hands) ||
!TryComp<TelescopeComponent>(hands.ActiveHandEntity, out var telescope) ||
!TryComp<EyeComponent>(player.Value, out var eye) || !TryComp(player.Value, out TransformComponent? xform))
return;
var offset = Vector2.Zero;
if (_inputSystem.CmdStates.GetState(EngineKeyFunctions.UseSecondary) != BoundKeyState.Down)
{
RaisePredictiveEvent(new EyeOffsetChangedEvent
{
Offset = offset
});
return;
}
var mousePos = _input.MouseScreenPosition.Position;
var playerPos = _eyeManager.CoordinatesToScreen(xform.Coordinates).Position;
var diff = mousePos - playerPos;
var len = diff.Length();
if (len > telescope.MaxLength)
{
diff *= telescope.MaxLength / len;
len = telescope.MaxLength;
}
if (len > telescope.MinLength)
{
diff -= diff * telescope.MinLength / len;
offset = new Vector2(diff.X / telescope.Divisor, -diff.Y / telescope.Divisor);
offset = new Angle(-eye.Rotation.Theta).RotateVec(offset);
}
RaisePredictiveEvent(new EyeOffsetChangedEvent
{
Offset = offset
});
}
}

View File

@@ -0,0 +1,5 @@
namespace Content.Server._White.Telescope;
public sealed class TelescopeSystem : EntitySystem
{
}

View File

@@ -0,0 +1,78 @@
using System.Numerics;
using Content.Shared.Camera;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Robust.Shared.Serialization;
namespace Content.Shared._White.Telescope;
public abstract class SharedTelescopeSystem : EntitySystem
{
[Dependency] private readonly SharedEyeSystem _eye = default!;
public override void Initialize()
{
base.Initialize();
SubscribeAllEvent<EyeOffsetChangedEvent>(OnEyeOffsetChanged);
SubscribeLocalEvent<TelescopeComponent, GotUnequippedHandEvent>(OnUnequip);
SubscribeLocalEvent<TelescopeComponent, HandDeselectedEvent>(OnHandDeselected);
SubscribeLocalEvent<TelescopeComponent, ComponentShutdown>(OnShutdown);
}
private void OnShutdown(Entity<TelescopeComponent> ent, ref ComponentShutdown args)
{
if (!TryComp(ent.Comp.LastHoldingEntity, out EyeComponent? eye))
return;
SetOffset((ent.Comp.LastHoldingEntity.Value, eye), Vector2.Zero, ent);
}
private void OnHandDeselected(Entity<TelescopeComponent> ent, ref HandDeselectedEvent args)
{
if (!TryComp(args.User, out EyeComponent? eye))
return;
SetOffset((args.User, eye), Vector2.Zero, ent);
}
private void OnUnequip(Entity<TelescopeComponent> ent, ref GotUnequippedHandEvent args)
{
if (!TryComp(args.User, out EyeComponent? eye))
return;
SetOffset((args.User, eye), Vector2.Zero, ent);
}
private void OnEyeOffsetChanged(EyeOffsetChangedEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not { } ent)
return;
if (!TryComp<HandsComponent>(ent, out var hands) ||
!TryComp<TelescopeComponent>(hands.ActiveHandEntity, out var telescope) ||
!TryComp(ent, out EyeComponent? eye))
return;
SetOffset((ent, eye), msg.Offset, telescope);
}
private void SetOffset(Entity<EyeComponent> ent, Vector2 offset, TelescopeComponent telescope)
{
telescope.LastHoldingEntity = ent;
if (TryComp(ent, out CameraRecoilComponent? recoil))
{
recoil.BaseOffset = offset;
_eye.SetOffset(ent, offset + recoil.CurrentKick, ent);
}
else
_eye.SetOffset(ent, offset, ent);
}
}
[Serializable, NetSerializable]
public sealed class EyeOffsetChangedEvent : EntityEventArgs
{
public Vector2 Offset;
}

View File

@@ -0,0 +1,19 @@
using Robust.Shared.GameStates;
namespace Content.Shared._White.Telescope;
[RegisterComponent, NetworkedComponent]
public sealed partial class TelescopeComponent : Component
{
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float MaxLength = 670f;
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float MinLength = 100f;
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float Divisor = 60f;
[ViewVariables]
public EntityUid? LastHoldingEntity;
}

View File

@@ -67,6 +67,7 @@
- type: Wieldable
wieldTime: 0
forceTwoHanded: True
- type: Telescope
- type: entity
name: musket