Files
OldThink/Content.Server/Gatherable/GatherableSystem.cs

83 lines
2.6 KiB
C#
Raw Normal View History

using Content.Server.Destructible;
using Content.Server.Gatherable.Components;
using Content.Shared.EntityList;
using Content.Shared.Interaction;
using Content.Shared.Tag;
using Content.Shared.Weapons.Melee.Events;
2023-05-11 23:19:08 +10:00
using Robust.Shared.Audio;
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 _prototypeManager = 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!;
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(EntityUid uid, GatherableComponent component, AttackedEvent args)
{
if (component.ToolWhitelist?.IsValid(args.Used, EntityManager) != true)
return;
Gather(uid, args.User, component);
}
private void OnActivate(EntityUid uid, GatherableComponent component, ActivateInWorldEvent args)
{
if (component.ToolWhitelist?.IsValid(args.User, EntityManager) != true)
return;
Gather(uid, args.User, component);
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!
2022-10-20 09:16:29 -04:00
if (component.MappedLoot == null)
return;
var pos = Transform(gatheredUid).MapPosition;
foreach (var (tag, table) in component.MappedLoot)
{
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 = _prototypeManager.Index<EntityLootTablePrototype>(table);
var spawnLoot = getLoot.GetSpawns(_random);
2023-05-11 23:19:08 +10:00
var spawnPos = pos.Offset(_random.NextVector2(0.3f));
Spawn(spawnLoot[0], spawnPos);
}
}
}