Files
crystall-punk-14/Content.Server/Gatherable/GatherableSystem.cs

89 lines
2.9 KiB
C#
Raw Permalink Normal View History

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