- add: TelescopeSystem. (#273)
This commit is contained in:
78
Content.Shared/_White/Telescope/SharedTelescopeSystem.cs
Normal file
78
Content.Shared/_White/Telescope/SharedTelescopeSystem.cs
Normal 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;
|
||||
}
|
||||
19
Content.Shared/_White/Telescope/TelescopeComponent.cs
Normal file
19
Content.Shared/_White/Telescope/TelescopeComponent.cs
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user