-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add magnet pickup system from Frontier
- Loading branch information
Velcroboy
committed
Mar 10, 2024
1 parent
67191e7
commit d6f3e1b
Showing
6 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Content.Shared/Frontier/Storage/Components/MaterialReclaimerMagnetPickupComponent.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,20 @@ | ||
namespace Content.Server.Storage.Components; | ||
|
||
/// <summary> | ||
/// Applies an ongoing pickup area around the attached entity. | ||
/// </summary> | ||
[RegisterComponent] | ||
public sealed partial class MaterialReclaimerMagnetPickupComponent : Component | ||
{ | ||
[ViewVariables(VVAccess.ReadWrite), DataField("nextScan")] | ||
public TimeSpan NextScan = TimeSpan.Zero; | ||
|
||
[ViewVariables(VVAccess.ReadWrite), DataField("range")] | ||
public float Range = 1f; | ||
|
||
/// <summary> | ||
/// Frontier - Is the magnet currently enabled? | ||
/// </summary> | ||
[ViewVariables(VVAccess.ReadWrite), DataField("magnetEnabled")] | ||
public bool MagnetEnabled = false; | ||
} |
20 changes: 20 additions & 0 deletions
20
Content.Shared/Frontier/Storage/Components/MaterialStorageMagnetPickupComponent.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,20 @@ | ||
namespace Content.Server.Storage.Components; | ||
|
||
/// <summary> | ||
/// Applies an ongoing pickup area around the attached entity. | ||
/// </summary> | ||
[RegisterComponent] | ||
public sealed partial class MaterialStorageMagnetPickupComponent : Component | ||
{ | ||
[ViewVariables(VVAccess.ReadWrite), DataField("nextScan")] | ||
public TimeSpan NextScan = TimeSpan.Zero; | ||
|
||
[ViewVariables(VVAccess.ReadWrite), DataField("range")] | ||
public float Range = 1f; | ||
|
||
/// <summary> | ||
/// Frontier - Is the magnet currently enabled? | ||
/// </summary> | ||
[ViewVariables(VVAccess.ReadWrite), DataField("magnetEnabled")] | ||
public bool MagnetEnabled = false; | ||
} |
118 changes: 118 additions & 0 deletions
118
Content.Shared/Frontier/Storage/EntitySystems/MaterialReclaimerMagnetPickupSystem.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,118 @@ | ||
using Content.Server.Storage.Components; | ||
using Content.Shared.Materials; | ||
using Robust.Shared.Physics.Components; | ||
using Robust.Shared.Timing; | ||
using Content.Shared.Examine; // Frontier | ||
using Content.Shared.Hands.Components; // Frontier | ||
using Content.Shared.Verbs; // Frontier | ||
using Robust.Shared.Utility; // Frontier | ||
|
||
namespace Content.Shared.Storage.EntitySystems; | ||
|
||
/// <summary> | ||
/// <see cref="MaterialReclaimerMagnetPickupComponent"/> | ||
/// </summary> | ||
public sealed class MaterialReclaimerMagnetPickupSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IGameTiming _timing = default!; | ||
[Dependency] private readonly EntityLookupSystem _lookup = default!; | ||
[Dependency] private readonly SharedMaterialReclaimerSystem _storage = default!; | ||
|
||
private static readonly TimeSpan ScanDelay = TimeSpan.FromSeconds(1); | ||
|
||
private EntityQuery<PhysicsComponent> _physicsQuery; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
_physicsQuery = GetEntityQuery<PhysicsComponent>(); | ||
SubscribeLocalEvent<MaterialReclaimerMagnetPickupComponent, MapInitEvent>(OnMagnetMapInit); | ||
SubscribeLocalEvent<MaterialReclaimerMagnetPickupComponent, EntityUnpausedEvent>(OnMagnetUnpaused); | ||
SubscribeLocalEvent<MaterialReclaimerMagnetPickupComponent, ExaminedEvent>(OnExamined); // Frontier | ||
SubscribeLocalEvent<MaterialReclaimerMagnetPickupComponent, GetVerbsEvent<AlternativeVerb>>(AddToggleMagnetVerb); // Frontier | ||
} | ||
|
||
private void OnMagnetUnpaused(EntityUid uid, MaterialReclaimerMagnetPickupComponent component, ref EntityUnpausedEvent args) | ||
{ | ||
component.NextScan += args.PausedTime; | ||
} | ||
|
||
private void OnMagnetMapInit(EntityUid uid, MaterialReclaimerMagnetPickupComponent component, MapInitEvent args) | ||
{ | ||
component.NextScan = _timing.CurTime + TimeSpan.FromSeconds(1); // Need to add 1 sec to fix a weird time bug with it that make it never start the magnet | ||
} | ||
|
||
// Frontier, used to add the magnet toggle to the context menu | ||
private void AddToggleMagnetVerb(EntityUid uid, MaterialReclaimerMagnetPickupComponent component, GetVerbsEvent<AlternativeVerb> args) | ||
{ | ||
if (!args.CanAccess || !args.CanInteract) | ||
return; | ||
|
||
if (!HasComp<HandsComponent>(args.User)) | ||
return; | ||
|
||
AlternativeVerb verb = new() | ||
{ | ||
Act = () => | ||
{ | ||
ToggleMagnet(uid, component); | ||
}, | ||
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/Spare/poweronoff.svg.192dpi.png")), | ||
Text = Loc.GetString("magnet-pickup-component-toggle-verb"), | ||
Priority = 3 | ||
}; | ||
|
||
args.Verbs.Add(verb); | ||
} | ||
|
||
// Frontier, used to show the magnet state on examination | ||
private void OnExamined(EntityUid uid, MaterialReclaimerMagnetPickupComponent component, ExaminedEvent args) | ||
{ | ||
args.PushMarkup(Loc.GetString("magnet-pickup-component-on-examine-main", | ||
("stateText", Loc.GetString(component.MagnetEnabled | ||
? "magnet-pickup-component-magnet-on" | ||
: "magnet-pickup-component-magnet-off")))); | ||
} | ||
|
||
// Frontier, used to toggle the magnet on the ore bag/box | ||
public bool ToggleMagnet(EntityUid uid, MaterialReclaimerMagnetPickupComponent comp) | ||
{ | ||
var query = EntityQueryEnumerator<MaterialReclaimerMagnetPickupComponent>(); | ||
comp.MagnetEnabled = !comp.MagnetEnabled; | ||
|
||
return comp.MagnetEnabled; | ||
} | ||
|
||
public override void Update(float frameTime) | ||
{ | ||
base.Update(frameTime); | ||
var query = EntityQueryEnumerator<MaterialReclaimerMagnetPickupComponent, MaterialReclaimerComponent, TransformComponent>(); | ||
var currentTime = _timing.CurTime; | ||
|
||
while (query.MoveNext(out var uid, out var comp, out var storage, out var xform)) | ||
{ | ||
if (comp.NextScan < currentTime) | ||
continue; | ||
|
||
comp.NextScan += ScanDelay; | ||
|
||
// Frontier - magnet disabled | ||
if (!comp.MagnetEnabled) | ||
continue; | ||
|
||
var parentUid = xform.ParentUid; | ||
|
||
foreach (var near in _lookup.GetEntitiesInRange(uid, comp.Range, LookupFlags.Dynamic | LookupFlags.Sundries)) | ||
{ | ||
if (!_physicsQuery.TryGetComponent(near, out var physics) || physics.BodyStatus != BodyStatus.OnGround) | ||
continue; | ||
|
||
if (near == parentUid) | ||
continue; | ||
|
||
if (!_storage.TryStartProcessItem(uid, near)) | ||
continue; | ||
} | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
Content.Shared/Frontier/Storage/EntitySystems/MaterialStorageMagnetPickupSystem.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,118 @@ | ||
using Content.Server.Storage.Components; | ||
using Content.Shared.Materials; | ||
using Robust.Shared.Physics.Components; | ||
using Robust.Shared.Timing; | ||
using Content.Shared.Examine; // Frontier | ||
using Content.Shared.Hands.Components; // Frontier | ||
using Content.Shared.Verbs; // Frontier | ||
using Robust.Shared.Utility; // Frontier | ||
|
||
namespace Content.Shared.Storage.EntitySystems; | ||
|
||
/// <summary> | ||
/// <see cref="MaterialStorageMagnetPickupComponent"/> | ||
/// </summary> | ||
public sealed class MaterialStorageMagnetPickupSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IGameTiming _timing = default!; | ||
[Dependency] private readonly EntityLookupSystem _lookup = default!; | ||
[Dependency] private readonly SharedMaterialStorageSystem _storage = default!; | ||
|
||
private static readonly TimeSpan ScanDelay = TimeSpan.FromSeconds(1); | ||
|
||
private EntityQuery<PhysicsComponent> _physicsQuery; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
_physicsQuery = GetEntityQuery<PhysicsComponent>(); | ||
SubscribeLocalEvent<MaterialStorageMagnetPickupComponent, MapInitEvent>(OnMagnetMapInit); | ||
SubscribeLocalEvent<MaterialStorageMagnetPickupComponent, EntityUnpausedEvent>(OnMagnetUnpaused); | ||
SubscribeLocalEvent<MaterialStorageMagnetPickupComponent, ExaminedEvent>(OnExamined); // Frontier | ||
SubscribeLocalEvent<MaterialStorageMagnetPickupComponent, GetVerbsEvent<AlternativeVerb>>(AddToggleMagnetVerb); // Frontier | ||
} | ||
|
||
private void OnMagnetUnpaused(EntityUid uid, MaterialStorageMagnetPickupComponent component, ref EntityUnpausedEvent args) | ||
{ | ||
component.NextScan += args.PausedTime; | ||
} | ||
|
||
private void OnMagnetMapInit(EntityUid uid, MaterialStorageMagnetPickupComponent component, MapInitEvent args) | ||
{ | ||
component.NextScan = _timing.CurTime + TimeSpan.FromSeconds(1); // Need to add 1 sec to fix a weird time bug with it that make it never start the magnet | ||
} | ||
|
||
// Frontier, used to add the magnet toggle to the context menu | ||
private void AddToggleMagnetVerb(EntityUid uid, MaterialStorageMagnetPickupComponent component, GetVerbsEvent<AlternativeVerb> args) | ||
{ | ||
if (!args.CanAccess || !args.CanInteract) | ||
return; | ||
|
||
if (!HasComp<HandsComponent>(args.User)) | ||
return; | ||
|
||
AlternativeVerb verb = new() | ||
{ | ||
Act = () => | ||
{ | ||
ToggleMagnet(uid, component); | ||
}, | ||
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/Spare/poweronoff.svg.192dpi.png")), | ||
Text = Loc.GetString("magnet-pickup-component-toggle-verb"), | ||
Priority = 3 | ||
}; | ||
|
||
args.Verbs.Add(verb); | ||
} | ||
|
||
// Frontier, used to show the magnet state on examination | ||
private void OnExamined(EntityUid uid, MaterialStorageMagnetPickupComponent component, ExaminedEvent args) | ||
{ | ||
args.PushMarkup(Loc.GetString("magnet-pickup-component-on-examine-main", | ||
("stateText", Loc.GetString(component.MagnetEnabled | ||
? "magnet-pickup-component-magnet-on" | ||
: "magnet-pickup-component-magnet-off")))); | ||
} | ||
|
||
// Frontier, used to toggle the magnet on the ore bag/box | ||
public bool ToggleMagnet(EntityUid uid, MaterialStorageMagnetPickupComponent comp) | ||
{ | ||
var query = EntityQueryEnumerator<MaterialStorageMagnetPickupComponent>(); | ||
comp.MagnetEnabled = !comp.MagnetEnabled; | ||
|
||
return comp.MagnetEnabled; | ||
} | ||
|
||
public override void Update(float frameTime) | ||
{ | ||
base.Update(frameTime); | ||
var query = EntityQueryEnumerator<MaterialStorageMagnetPickupComponent, MaterialStorageComponent, TransformComponent>(); | ||
var currentTime = _timing.CurTime; | ||
|
||
while (query.MoveNext(out var uid, out var comp, out var storage, out var xform)) | ||
{ | ||
if (comp.NextScan < currentTime) | ||
continue; | ||
|
||
comp.NextScan += ScanDelay; | ||
|
||
// Frontier - magnet disabled | ||
if (!comp.MagnetEnabled) | ||
continue; | ||
|
||
var parentUid = xform.ParentUid; | ||
|
||
foreach (var near in _lookup.GetEntitiesInRange(uid, comp.Range, LookupFlags.Dynamic | LookupFlags.Sundries)) | ||
{ | ||
if (!_physicsQuery.TryGetComponent(near, out var physics) || physics.BodyStatus != BodyStatus.OnGround) | ||
continue; | ||
|
||
if (near == parentUid) | ||
continue; | ||
|
||
if (!_storage.TryInsertMaterialEntity(uid, near, uid, storage)) | ||
continue; | ||
} | ||
} | ||
} | ||
} |
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