GatherableSystem/Component (#8041)

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Chris V
2022-05-12 05:53:31 -07:00
committed by GitHub
parent 0de06cb9c0
commit 6cb2a01723
12 changed files with 244 additions and 192 deletions

View File

@@ -0,0 +1,33 @@
using Content.Shared.EntityList;
using Content.Shared.Whitelist;
namespace Content.Server.Gatherable.Components;
[RegisterComponent]
[Friend(typeof(GatherableSystem))]
public sealed class GatherableComponent : Component
{
/// <summary>
/// Whitelist for specifying the kind of tools can be used on a resource
/// Supports multiple tags.
/// </summary>
[ViewVariables]
[DataField("whitelist", required: true)]
public EntityWhitelist? ToolWhitelist;
/// <summary>
/// YAML example below
/// (Tag1, Tag2, LootTableID1, LootTableID2 are placeholders for example)
/// --------------------
/// useMappedLoot: true
/// whitelist:
/// tags:
/// - Tag1
/// - Tag2
/// mappedLoot:
/// Tag1: LootTableID1
/// Tag2: LootTableID2
/// </summary>
[DataField("loot")]
public Dictionary<string, string>? MappedLoot = new();
}

View File

@@ -0,0 +1,44 @@
using System.Threading;
using Content.Shared.Damage;
using Content.Shared.Sound;
namespace Content.Server.Gatherable.Components
{
/// <summary>
/// When interacting with an <see cref="GatherableComponent"/> allows it to spawn entities.
/// </summary>
[RegisterComponent]
public sealed class GatheringToolComponent : Component
{
/// <summary>
/// Sound that is made once you completed gathering
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("sound")]
public SoundSpecifier GatheringSound { get; set; } = new SoundPathSpecifier("/Audio/Items/Mining/pickaxe.ogg");
/// <summary>
/// This directly plugs into the time delay for gathering.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("gatheringTime")]
public float GatheringTime { get; set; } = 1f;
/// <summary>
/// What damage should be given to objects when
/// gathered using this tool? (0 for infinite gathering)
/// </summary>
[DataField("damage", required: true)]
public DamageSpecifier Damage { get; set; } = default!;
/// <summary>
/// How many entities can this tool gather from at once?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("maxEntities")]
public int MaxGatheringEntities = 1;
[ViewVariables]
public readonly Dictionary<EntityUid, CancellationTokenSource> GatheringEntities = new();
}
}

View File

@@ -0,0 +1,111 @@
using System.Threading;
using Content.Server.DoAfter;
using Content.Server.Gatherable.Components;
using Content.Shared.Damage;
using Content.Shared.EntityList;
using Content.Shared.Interaction;
using Content.Shared.Tag;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Gatherable;
public sealed class GatherableSystem : EntitySystem
{
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly IRobustRandom _random = null!;
[Dependency] private readonly TagSystem _tagSystem = Get<TagSystem>();
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GatherableComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<GatheringDoafterCancel>(OnDoafterCancel);
SubscribeLocalEvent<GatherableComponent, GatheringDoafterSuccess>(OnDoafterSuccess);
}
private void OnInteractUsing(EntityUid uid, GatherableComponent component, InteractUsingEvent args)
{
if (!TryComp<GatheringToolComponent>(args.Used, out var tool) ||
component.ToolWhitelist?.IsValid(args.Used) == false ||
tool.GatheringEntities.TryGetValue(uid, out var cancelToken))
return;
// Can't gather too many entities at once.
if (tool.MaxGatheringEntities < tool.GatheringEntities.Count + 1)
return;
cancelToken = new CancellationTokenSource();
tool.GatheringEntities[uid] = cancelToken;
var doAfter = new DoAfterEventArgs(args.User, tool.GatheringTime, cancelToken.Token, uid)
{
BreakOnDamage = true,
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
MovementThreshold = 0.25f,
BroadcastCancelledEvent = new GatheringDoafterCancel { Tool = args.Used, Resource = uid },
TargetFinishedEvent = new GatheringDoafterSuccess { Tool = args.Used, Resource = uid, Player = args.User }
};
_doAfterSystem.DoAfter(doAfter);
}
private void OnDoafterSuccess(EntityUid uid, GatherableComponent component, GatheringDoafterSuccess ev)
{
if (!TryComp(ev.Tool, out GatheringToolComponent? tool))
return;
// Complete the gathering process
_damageableSystem.TryChangeDamage(ev.Resource, tool.Damage);
SoundSystem.Play(Filter.Pvs(ev.Resource, entityManager: EntityManager), tool.GatheringSound.GetSound(), ev.Resource);
tool.GatheringEntities.Remove(ev.Resource);
// Spawn the loot!
if (component.MappedLoot == null) return;
var playerPos = Transform(ev.Player).MapPosition;
foreach (var (tag, table) in component.MappedLoot)
{
if (tag != "All")
{
if (!_tagSystem.HasTag(tool.Owner, tag)) continue;
}
var getLoot = _prototypeManager.Index<EntityLootTablePrototype>(table);
var spawnLoot = getLoot.GetSpawns();
var spawnPos = playerPos.Offset(_random.NextVector2(0.3f));
Spawn(spawnLoot[0], spawnPos);
}
}
private void OnDoafterCancel(GatheringDoafterCancel ev)
{
if (!TryComp<GatheringToolComponent>(ev.Tool, out var tool))
return;
tool.GatheringEntities.Remove(ev.Resource);
}
private sealed class GatheringDoafterCancel : EntityEventArgs
{
public EntityUid Tool;
public EntityUid Resource;
}
private sealed class GatheringDoafterSuccess : EntityEventArgs
{
public EntityUid Tool;
public EntityUid Resource;
public EntityUid Player;
}
}