- 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,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;
}