Files
tbd-station-14/Content.Server/Gatherable/GatherableSystem.cs
Nemanja a1a8f04036 Decouple interactions from hands, cleanup old events, add new fears (#28393)
* ok basic shit

* second part

* pretend it isn't real it can't hurt you.

* 👁️ 👁️

* shadowcommander review
2024-05-31 13:26:19 -07:00

84 lines
2.7 KiB
C#

using Content.Server.Destructible;
using Content.Server.Gatherable.Components;
using Content.Shared.Interaction;
using Content.Shared.Tag;
using Content.Shared.Weapons.Melee.Events;
using Robust.Server.GameObjects;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Gatherable;
public sealed partial class GatherableSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly DestructibleSystem _destructible = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
[Dependency] private readonly TransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GatherableComponent, ActivateInWorldEvent>(OnActivate);
SubscribeLocalEvent<GatherableComponent, AttackedEvent>(OnAttacked);
InitializeProjectile();
}
private void OnAttacked(Entity<GatherableComponent> gatherable, ref AttackedEvent args)
{
if (gatherable.Comp.ToolWhitelist?.IsValid(args.Used, EntityManager) != true)
return;
Gather(gatherable, args.User);
}
private void OnActivate(Entity<GatherableComponent> gatherable, ref ActivateInWorldEvent args)
{
if (args.Handled || !args.Complex)
return;
if (gatherable.Comp.ToolWhitelist?.IsValid(args.User, EntityManager) != true)
return;
Gather(gatherable, args.User);
args.Handled = true;
}
public void Gather(EntityUid gatheredUid, EntityUid? gatherer = null, GatherableComponent? component = null)
{
if (!Resolve(gatheredUid, ref component))
return;
if (TryComp<SoundOnGatherComponent>(gatheredUid, out var soundComp))
{
_audio.PlayPvs(soundComp.Sound, Transform(gatheredUid).Coordinates);
}
// Complete the gathering process
_destructible.DestroyEntity(gatheredUid);
// Spawn the loot!
if (component.Loot == null)
return;
var pos = _transform.GetMapCoordinates(gatheredUid);
foreach (var (tag, table) in component.Loot)
{
if (tag != "All")
{
if (gatherer != null && !_tagSystem.HasTag(gatherer.Value, tag))
continue;
}
var getLoot = _proto.Index(table);
var spawnLoot = getLoot.GetSpawns(_random);
var spawnPos = pos.Offset(_random.NextVector2(component.GatherOffset));
Spawn(spawnLoot[0], spawnPos);
}
}
}