2023-02-14 02:32:09 +11:00
|
|
|
using Content.Server.Destructible;
|
2022-05-12 05:53:31 -07:00
|
|
|
using Content.Server.Gatherable.Components;
|
|
|
|
|
using Content.Shared.EntityList;
|
|
|
|
|
using Content.Shared.Interaction;
|
|
|
|
|
using Content.Shared.Tag;
|
2023-08-06 11:23:38 +10:00
|
|
|
using Content.Shared.Weapons.Melee.Events;
|
2023-05-11 23:19:08 +10:00
|
|
|
using Robust.Shared.Audio;
|
2023-11-27 22:12:34 +11:00
|
|
|
using Robust.Shared.Audio.Systems;
|
2022-05-12 05:53:31 -07:00
|
|
|
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
|
2022-05-12 05:53:31 -07:00
|
|
|
{
|
2023-02-14 02:32:09 +11:00
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
2022-10-20 09:16:29 -04:00
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2023-02-14 02:32:09 +11:00
|
|
|
[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!;
|
2022-05-12 05:53:31 -07:00
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2023-08-01 22:10:48 +08:00
|
|
|
SubscribeLocalEvent<GatherableComponent, ActivateInWorldEvent>(OnActivate);
|
2023-08-06 11:23:38 +10:00
|
|
|
SubscribeLocalEvent<GatherableComponent, AttackedEvent>(OnAttacked);
|
2023-05-11 23:19:08 +10:00
|
|
|
InitializeProjectile();
|
2022-05-12 05:53:31 -07:00
|
|
|
}
|
|
|
|
|
|
2023-08-06 11:23:38 +10:00
|
|
|
private void OnAttacked(EntityUid uid, GatherableComponent component, AttackedEvent args)
|
2022-05-12 05:53:31 -07:00
|
|
|
{
|
2023-08-06 11:23:38 +10:00
|
|
|
if (component.ToolWhitelist?.IsValid(args.Used, EntityManager) != true)
|
2022-05-12 05:53:31 -07:00
|
|
|
return;
|
2023-02-14 02:32:09 +11:00
|
|
|
|
2023-08-06 11:23:38 +10:00
|
|
|
Gather(uid, args.User, component);
|
2022-05-12 05:53:31 -07:00
|
|
|
}
|
|
|
|
|
|
2023-08-01 22:10:48 +08:00
|
|
|
private void OnActivate(EntityUid uid, GatherableComponent component, ActivateInWorldEvent args)
|
|
|
|
|
{
|
2023-08-13 17:07:33 +10:00
|
|
|
if (component.ToolWhitelist?.IsValid(args.User, EntityManager) != true)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-08-06 11:23:38 +10:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 05:53:31 -07:00
|
|
|
// Complete the gathering process
|
2023-05-11 23:19:08 +10:00
|
|
|
_destructible.DestroyEntity(gatheredUid);
|
2022-05-12 05:53:31 -07:00
|
|
|
|
|
|
|
|
// Spawn the loot!
|
2022-10-20 09:16:29 -04:00
|
|
|
if (component.MappedLoot == null)
|
|
|
|
|
return;
|
2022-05-12 05:53:31 -07:00
|
|
|
|
2024-02-28 00:51:20 +11:00
|
|
|
var pos = Transform(gatheredUid).MapPosition;
|
2022-05-12 05:53:31 -07:00
|
|
|
|
|
|
|
|
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;
|
2022-05-12 05:53:31 -07:00
|
|
|
}
|
|
|
|
|
var getLoot = _prototypeManager.Index<EntityLootTablePrototype>(table);
|
2023-10-16 16:54:10 +11:00
|
|
|
var spawnLoot = getLoot.GetSpawns(_random);
|
2023-05-11 23:19:08 +10:00
|
|
|
var spawnPos = pos.Offset(_random.NextVector2(0.3f));
|
2022-05-12 05:53:31 -07:00
|
|
|
Spawn(spawnLoot[0], spawnPos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|