forked from space-wizards/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mineral Scanner (space-wizards#31390)
* Mineral Scanner * doink * review * sunday funday * review and fix bugs i think? * Update MiningOverlay.cs
- Loading branch information
1 parent
582a644
commit 8599251
Showing
25 changed files
with
568 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System.Numerics; | ||
using Content.Shared.Mining.Components; | ||
using Robust.Client.GameObjects; | ||
using Robust.Client.Graphics; | ||
using Robust.Client.Player; | ||
using Robust.Shared.Enums; | ||
using Robust.Shared.Timing; | ||
using Robust.Shared.Utility; | ||
|
||
namespace Content.Client.Mining; | ||
|
||
public sealed class MiningOverlay : Overlay | ||
{ | ||
[Dependency] private readonly IEntityManager _entityManager = default!; | ||
[Dependency] private readonly IGameTiming _timing = default!; | ||
[Dependency] private readonly IPlayerManager _player = default!; | ||
private readonly EntityLookupSystem _lookup; | ||
private readonly SpriteSystem _sprite; | ||
private readonly TransformSystem _xform; | ||
|
||
private readonly EntityQuery<SpriteComponent> _spriteQuery; | ||
private readonly EntityQuery<TransformComponent> _xformQuery; | ||
|
||
public override OverlaySpace Space => OverlaySpace.WorldSpace; | ||
public override bool RequestScreenTexture => false; | ||
|
||
private readonly HashSet<Entity<MiningScannerViewableComponent>> _viewableEnts = new(); | ||
|
||
public MiningOverlay() | ||
{ | ||
IoCManager.InjectDependencies(this); | ||
|
||
_lookup = _entityManager.System<EntityLookupSystem>(); | ||
_sprite = _entityManager.System<SpriteSystem>(); | ||
_xform = _entityManager.System<TransformSystem>(); | ||
|
||
_spriteQuery = _entityManager.GetEntityQuery<SpriteComponent>(); | ||
_xformQuery = _entityManager.GetEntityQuery<TransformComponent>(); | ||
} | ||
|
||
protected override void Draw(in OverlayDrawArgs args) | ||
{ | ||
var handle = args.WorldHandle; | ||
|
||
if (_player.LocalEntity is not { } localEntity || | ||
!_entityManager.TryGetComponent<MiningScannerViewerComponent>(localEntity, out var viewerComp)) | ||
return; | ||
|
||
if (viewerComp.LastPingLocation == null) | ||
return; | ||
|
||
var scaleMatrix = Matrix3Helpers.CreateScale(Vector2.One); | ||
|
||
_viewableEnts.Clear(); | ||
_lookup.GetEntitiesInRange(viewerComp.LastPingLocation.Value, viewerComp.ViewRange, _viewableEnts); | ||
foreach (var ore in _viewableEnts) | ||
{ | ||
if (!_xformQuery.TryComp(ore, out var xform) || | ||
!_spriteQuery.TryComp(ore, out var sprite)) | ||
continue; | ||
|
||
if (xform.MapID != args.MapId || !sprite.Visible) | ||
continue; | ||
|
||
if (!sprite.LayerMapTryGet(MiningScannerVisualLayers.Overlay, out var idx)) | ||
continue; | ||
var layer = sprite[idx]; | ||
|
||
if (layer.ActualRsi?.Path == null || layer.RsiState.Name == null) | ||
continue; | ||
|
||
var gridRot = xform.GridUid == null ? 0 : _xformQuery.CompOrNull(xform.GridUid.Value)?.LocalRotation ?? 0; | ||
var rotationMatrix = Matrix3Helpers.CreateRotation(gridRot); | ||
|
||
var worldMatrix = Matrix3Helpers.CreateTranslation(_xform.GetWorldPosition(xform)); | ||
var scaledWorld = Matrix3x2.Multiply(scaleMatrix, worldMatrix); | ||
var matty = Matrix3x2.Multiply(rotationMatrix, scaledWorld); | ||
handle.SetTransform(matty); | ||
|
||
var spriteSpec = new SpriteSpecifier.Rsi(layer.ActualRsi.Path, layer.RsiState.Name); | ||
var texture = _sprite.GetFrame(spriteSpec, TimeSpan.FromSeconds(layer.AnimationTime)); | ||
|
||
var animTime = (viewerComp.NextPingTime - _timing.CurTime).TotalSeconds; | ||
|
||
|
||
var alpha = animTime < viewerComp.AnimationDuration | ||
? 0 | ||
: (float) Math.Clamp((animTime - viewerComp.AnimationDuration) / viewerComp.AnimationDuration, 0f, 1f); | ||
var color = Color.White.WithAlpha(alpha); | ||
|
||
handle.DrawTexture(texture, -(Vector2) texture.Size / 2f / EyeManager.PixelsPerMeter, layer.Rotation, modulate: color); | ||
|
||
} | ||
handle.SetTransform(Matrix3x2.Identity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Content.Shared.Mining.Components; | ||
using Robust.Client.Graphics; | ||
using Robust.Client.Player; | ||
using Robust.Shared.Player; | ||
|
||
namespace Content.Client.Mining; | ||
|
||
/// <summary> | ||
/// This handles the lifetime of the <see cref="MiningOverlay"/> for a given entity. | ||
/// </summary> | ||
public sealed class MiningOverlaySystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IPlayerManager _player = default!; | ||
[Dependency] private readonly IOverlayManager _overlayMan = default!; | ||
|
||
private MiningOverlay _overlay = default!; | ||
|
||
/// <inheritdoc/> | ||
public override void Initialize() | ||
{ | ||
SubscribeLocalEvent<MiningScannerViewerComponent, ComponentInit>(OnInit); | ||
SubscribeLocalEvent<MiningScannerViewerComponent, ComponentShutdown>(OnShutdown); | ||
SubscribeLocalEvent<MiningScannerViewerComponent, LocalPlayerAttachedEvent>(OnPlayerAttached); | ||
SubscribeLocalEvent<MiningScannerViewerComponent, LocalPlayerDetachedEvent>(OnPlayerDetached); | ||
|
||
_overlay = new(); | ||
} | ||
|
||
private void OnPlayerAttached(Entity<MiningScannerViewerComponent> ent, ref LocalPlayerAttachedEvent args) | ||
{ | ||
_overlayMan.AddOverlay(_overlay); | ||
} | ||
|
||
private void OnPlayerDetached(Entity<MiningScannerViewerComponent> ent, ref LocalPlayerDetachedEvent args) | ||
{ | ||
_overlayMan.RemoveOverlay(_overlay); | ||
} | ||
|
||
private void OnInit(Entity<MiningScannerViewerComponent> ent, ref ComponentInit args) | ||
{ | ||
if (_player.LocalEntity == ent) | ||
{ | ||
_overlayMan.AddOverlay(_overlay); | ||
} | ||
} | ||
|
||
private void OnShutdown(Entity<MiningScannerViewerComponent> ent, ref ComponentShutdown args) | ||
{ | ||
if (_player.LocalEntity == ent) | ||
{ | ||
_overlayMan.RemoveOverlay(_overlay); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Content.Shared/Mining/Components/MiningScannerComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Robust.Shared.GameStates; | ||
|
||
namespace Content.Shared.Mining.Components; | ||
|
||
/// <summary> | ||
/// This is a component that, when held in the inventory or pocket of a player, gives the the MiningOverlay. | ||
/// </summary> | ||
[RegisterComponent, NetworkedComponent, Access(typeof(MiningScannerSystem))] | ||
public sealed partial class MiningScannerComponent : Component | ||
{ | ||
[DataField] | ||
public float Range = 5; | ||
} |
13 changes: 13 additions & 0 deletions
13
Content.Shared/Mining/Components/MiningScannerViewableComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Robust.Shared.GameStates; | ||
using Robust.Shared.Serialization; | ||
|
||
namespace Content.Shared.Mining.Components; | ||
|
||
[RegisterComponent, NetworkedComponent, Access(typeof(MiningScannerSystem))] | ||
public sealed partial class MiningScannerViewableComponent : Component; | ||
|
||
[Serializable, NetSerializable] | ||
public enum MiningScannerVisualLayers : byte | ||
{ | ||
Overlay | ||
} |
36 changes: 36 additions & 0 deletions
36
Content.Shared/Mining/Components/MiningScannerViewerComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Robust.Shared.Audio; | ||
using Robust.Shared.GameStates; | ||
using Robust.Shared.Map; | ||
|
||
namespace Content.Shared.Mining.Components; | ||
|
||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause, Access(typeof(MiningScannerSystem))] | ||
public sealed partial class MiningScannerViewerComponent : Component | ||
{ | ||
[DataField, ViewVariables(VVAccess.ReadOnly), AutoNetworkedField] | ||
public float ViewRange; | ||
|
||
[DataField, AutoNetworkedField] | ||
public float AnimationDuration = 1.5f; | ||
|
||
[DataField, AutoNetworkedField] | ||
public TimeSpan PingDelay = TimeSpan.FromSeconds(5); | ||
|
||
[DataField, AutoNetworkedField, AutoPausedField] | ||
public TimeSpan NextPingTime = TimeSpan.MaxValue; | ||
|
||
[DataField] | ||
public EntityCoordinates? LastPingLocation; | ||
|
||
[DataField, AutoNetworkedField] | ||
public SoundSpecifier? PingSound = new SoundPathSpecifier("/Audio/Machines/sonar-ping.ogg") | ||
{ | ||
Params = new AudioParams | ||
{ | ||
Volume = -3, | ||
} | ||
}; | ||
|
||
[DataField] | ||
public bool QueueRemoval; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Content.Shared.Inventory; | ||
using Content.Shared.Item.ItemToggle.Components; | ||
using Content.Shared.Mining.Components; | ||
using Robust.Shared.Audio.Systems; | ||
using Robust.Shared.Containers; | ||
using Robust.Shared.Network; | ||
using Robust.Shared.Timing; | ||
|
||
namespace Content.Shared.Mining; | ||
|
||
public sealed class MiningScannerSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IGameTiming _timing = default!; | ||
[Dependency] private readonly INetManager _net = default!; | ||
[Dependency] private readonly SharedAudioSystem _audio = default!; | ||
[Dependency] private readonly SharedContainerSystem _container = default!; | ||
[Dependency] private readonly InventorySystem _inventory = default!; | ||
|
||
/// <inheritdoc/> | ||
public override void Initialize() | ||
{ | ||
SubscribeLocalEvent<MiningScannerComponent, EntGotInsertedIntoContainerMessage>(OnInserted); | ||
SubscribeLocalEvent<MiningScannerComponent, EntGotRemovedFromContainerMessage>(OnRemoved); | ||
SubscribeLocalEvent<MiningScannerComponent, ItemToggledEvent>(OnToggled); | ||
} | ||
|
||
private void OnInserted(Entity<MiningScannerComponent> ent, ref EntGotInsertedIntoContainerMessage args) | ||
{ | ||
UpdateViewerComponent(args.Container.Owner); | ||
} | ||
|
||
private void OnRemoved(Entity<MiningScannerComponent> ent, ref EntGotRemovedFromContainerMessage args) | ||
{ | ||
UpdateViewerComponent(args.Container.Owner); | ||
} | ||
|
||
private void OnToggled(Entity<MiningScannerComponent> ent, ref ItemToggledEvent args) | ||
{ | ||
if (_container.TryGetContainingContainer((ent.Owner, null, null), out var container)) | ||
UpdateViewerComponent(container.Owner); | ||
} | ||
|
||
public void UpdateViewerComponent(EntityUid uid) | ||
{ | ||
Entity<MiningScannerComponent>? scannerEnt = null; | ||
|
||
var ents = _inventory.GetHandOrInventoryEntities(uid); | ||
foreach (var ent in ents) | ||
{ | ||
if (!TryComp<MiningScannerComponent>(ent, out var scannerComponent) || | ||
!TryComp<ItemToggleComponent>(ent, out var toggle)) | ||
continue; | ||
|
||
if (!toggle.Activated) | ||
continue; | ||
|
||
if (scannerEnt == null || scannerComponent.Range > scannerEnt.Value.Comp.Range) | ||
scannerEnt = (ent, scannerComponent); | ||
} | ||
|
||
if (_net.IsServer) | ||
{ | ||
if (scannerEnt == null) | ||
{ | ||
if (TryComp<MiningScannerViewerComponent>(uid, out var viewer)) | ||
viewer.QueueRemoval = true; | ||
} | ||
else | ||
{ | ||
var viewer = EnsureComp<MiningScannerViewerComponent>(uid); | ||
viewer.ViewRange = scannerEnt.Value.Comp.Range; | ||
viewer.QueueRemoval = false; | ||
viewer.NextPingTime = _timing.CurTime + viewer.PingDelay; | ||
Dirty(uid, viewer); | ||
} | ||
} | ||
} | ||
|
||
public override void Update(float frameTime) | ||
{ | ||
base.Update(frameTime); | ||
|
||
var query = EntityQueryEnumerator<MiningScannerViewerComponent, TransformComponent>(); | ||
while (query.MoveNext(out var uid, out var viewer, out var xform)) | ||
{ | ||
if (viewer.QueueRemoval) | ||
{ | ||
RemCompDeferred(uid, viewer); | ||
continue; | ||
} | ||
|
||
if (_timing.CurTime < viewer.NextPingTime) | ||
continue; | ||
|
||
viewer.NextPingTime = _timing.CurTime + viewer.PingDelay; | ||
viewer.LastPingLocation = xform.Coordinates; | ||
if (_net.IsClient && _timing.IsFirstTimePredicted) | ||
_audio.PlayEntity(viewer.PingSound, uid, uid); | ||
} | ||
} | ||
} |
Oops, something went wrong.