62
Content.Shared/RCD/Systems/RCDAmmoSystem.cs
Normal file
62
Content.Shared/RCD/Systems/RCDAmmoSystem.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Content.Shared.Charges.Components;
|
||||
using Content.Shared.Charges.Systems;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.RCD.Components;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.RCD.Systems;
|
||||
|
||||
public sealed class RCDAmmoSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedChargesSystem _charges = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<RCDAmmoComponent, ExaminedEvent>(OnExamine);
|
||||
SubscribeLocalEvent<RCDAmmoComponent, AfterInteractEvent>(OnAfterInteract);
|
||||
}
|
||||
|
||||
private void OnExamine(EntityUid uid, RCDAmmoComponent comp, ExaminedEvent args)
|
||||
{
|
||||
if (!args.IsInDetailsRange)
|
||||
return;
|
||||
|
||||
var examineMessage = Loc.GetString("rcd-ammo-component-on-examine", ("charges", comp.Charges));
|
||||
args.PushText(examineMessage);
|
||||
}
|
||||
|
||||
private void OnAfterInteract(EntityUid uid, RCDAmmoComponent comp, AfterInteractEvent args)
|
||||
{
|
||||
if (args.Handled || !args.CanReach || !_timing.IsFirstTimePredicted)
|
||||
return;
|
||||
|
||||
if (args.Target is not {Valid: true} target ||
|
||||
!HasComp<RCDComponent>(target) ||
|
||||
!TryComp<LimitedChargesComponent>(target, out var charges))
|
||||
return;
|
||||
|
||||
var user = args.User;
|
||||
args.Handled = true;
|
||||
var count = Math.Min(charges.MaxCharges - charges.Charges, comp.Charges);
|
||||
if (count <= 0)
|
||||
{
|
||||
_popup.PopupClient(Loc.GetString("rcd-ammo-component-after-interact-full"), target, user);
|
||||
return;
|
||||
}
|
||||
|
||||
_popup.PopupClient(Loc.GetString("rcd-ammo-component-after-interact-refilled"), target, user);
|
||||
_charges.AddCharges(target, count, charges);
|
||||
comp.Charges -= count;
|
||||
Dirty(comp);
|
||||
|
||||
// prevent having useless ammo with 0 charges
|
||||
if (comp.Charges <= 0)
|
||||
QueueDel(uid);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user