Make recyclers great again (#6653)
Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -1,24 +1,20 @@
|
||||
using System;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.Recycling.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[RegisterComponent, Friend(typeof(RecyclerSystem))]
|
||||
public sealed class RecyclableComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The prototype that will be spawned on recycle.
|
||||
/// </summary>
|
||||
[DataField("prototype")] private string? _prototype;
|
||||
[DataField("prototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))] public string? Prototype;
|
||||
|
||||
/// <summary>
|
||||
/// The amount of things that will be spawned on recycle.
|
||||
/// </summary>
|
||||
[DataField("amount")] private int _amount = 1;
|
||||
[DataField("amount")] public int Amount = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Whether this is "safe" to recycle or not.
|
||||
@@ -26,19 +22,5 @@ namespace Content.Server.Recycling.Components
|
||||
/// </summary>
|
||||
[DataField("safe")]
|
||||
public bool Safe { get; set; } = true;
|
||||
|
||||
public void Recycle(float efficiency = 1f)
|
||||
{
|
||||
if(!string.IsNullOrEmpty(_prototype))
|
||||
{
|
||||
for (var i = 0; i < Math.Max(_amount * efficiency, 1); i++)
|
||||
{
|
||||
_entMan.SpawnEntity(_prototype, _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
_entMan.QueueDeleteEntity(Owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,8 @@ using Content.Server.Popups;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Recycling;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Recycling.Components
|
||||
{
|
||||
@@ -23,6 +18,10 @@ namespace Content.Server.Recycling.Components
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("enabled")]
|
||||
public bool Enabled = true;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not sentient beings will be recycled
|
||||
/// </summary>
|
||||
@@ -47,13 +46,15 @@ namespace Content.Server.Recycling.Components
|
||||
|
||||
SuicideKind ISuicideAct.Suicide(EntityUid victim, IChatManager chat)
|
||||
{
|
||||
if (_entMan.TryGetComponent(victim, out ActorComponent? actor) && actor.PlayerSession.ContentData()?.Mind is {} mind)
|
||||
if (_entMan.TryGetComponent(victim, out ActorComponent? actor) &&
|
||||
actor.PlayerSession.ContentData()?.Mind is { } mind)
|
||||
{
|
||||
EntitySystem.Get<GameTicker>().OnGhostAttempt(mind, false);
|
||||
mind.OwnedEntity?.PopupMessage(Loc.GetString("recycler-component-suicide-message"));
|
||||
}
|
||||
|
||||
victim.PopupMessageOtherClients(Loc.GetString("recycler-component-suicide-message-others", ("victim",victim)));
|
||||
victim.PopupMessageOtherClients(Loc.GetString("recycler-component-suicide-message-others",
|
||||
("victim", victim)));
|
||||
|
||||
if (_entMan.TryGetComponent<SharedBodyComponent?>(victim, out var body))
|
||||
{
|
||||
@@ -61,8 +62,16 @@ namespace Content.Server.Recycling.Components
|
||||
}
|
||||
|
||||
EntitySystem.Get<RecyclerSystem>().Bloodstain(this);
|
||||
|
||||
return SuicideKind.Bloodloss;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default sound to play when recycling
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("sound")]
|
||||
public SoundSpecifier? Sound = new SoundPathSpecifier("/Audio/Effects/saw.ogg");
|
||||
|
||||
// Ratelimit sounds to avoid spam
|
||||
public TimeSpan LastSound;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,52 +1,93 @@
|
||||
using Content.Server.Audio;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Recycling.Components;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Recycling;
|
||||
using Content.Shared.Emag.Systems;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Content.Shared.Recycling;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Recycling
|
||||
{
|
||||
internal sealed class RecyclerSystem : EntitySystem
|
||||
public sealed class RecyclerSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly AmbientSoundSystem _ambience = default!;
|
||||
[Dependency] private readonly TagSystem _tags = default!;
|
||||
|
||||
private const float RecyclerSoundCooldown = 0.8f;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<RecyclerComponent, StartCollideEvent>(HandleCollide);
|
||||
SubscribeLocalEvent<RecyclerComponent, StartCollideEvent>(OnCollide);
|
||||
SubscribeLocalEvent<RecyclerComponent, GotEmaggedEvent>(OnEmagged);
|
||||
}
|
||||
|
||||
private void HandleCollide(EntityUid uid, RecyclerComponent component, StartCollideEvent args)
|
||||
public void EnableRecycler(RecyclerComponent component)
|
||||
{
|
||||
if (component.Enabled) return;
|
||||
|
||||
component.Enabled = true;
|
||||
_ambience.SetAmbience(component.Owner, true);
|
||||
}
|
||||
|
||||
public void DisableRecycler(RecyclerComponent component)
|
||||
{
|
||||
if (!component.Enabled) return;
|
||||
|
||||
component.Enabled = false;
|
||||
_ambience.SetAmbience(component.Owner, false);
|
||||
}
|
||||
|
||||
private void OnCollide(EntityUid uid, RecyclerComponent component, StartCollideEvent args)
|
||||
{
|
||||
if (component.Enabled && args.OurFixture.ID != "brrt") return;
|
||||
|
||||
Recycle(component, args.OtherFixture.Body.Owner);
|
||||
}
|
||||
|
||||
private void Recycle(RecyclerComponent component, EntityUid entity)
|
||||
{
|
||||
// TODO: Prevent collision with recycled items
|
||||
RecyclableComponent? recyclable = null;
|
||||
|
||||
// Can only recycle things that are recyclable... And also check the safety of the thing to recycle.
|
||||
if (!EntityManager.TryGetComponent(entity, out RecyclableComponent? recyclable) || !recyclable.Safe && component.Safe) return;
|
||||
if (!_tags.HasTag(entity, "Recyclable") &&
|
||||
(!TryComp(entity, out recyclable) || !recyclable.Safe && component.Safe))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Prevent collision with recycled items
|
||||
|
||||
// Mobs are a special case!
|
||||
if (CanGib(component, entity))
|
||||
{
|
||||
EntityManager.GetComponent<SharedBodyComponent>(entity).Gib(true);
|
||||
Comp<SharedBodyComponent>(entity).Gib(true);
|
||||
Bloodstain(component);
|
||||
return;
|
||||
}
|
||||
|
||||
recyclable.Recycle(component.Efficiency);
|
||||
if (recyclable == null)
|
||||
QueueDel(entity);
|
||||
else
|
||||
Recycle(recyclable, component.Efficiency);
|
||||
|
||||
if (component.Sound != null && (_timing.CurTime - component.LastSound).TotalSeconds > RecyclerSoundCooldown)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(component.Owner, entityManager: EntityManager), component.Sound.GetSound(), component.Owner, AudioHelpers.WithVariation(0.01f).WithVolume(-3));
|
||||
component.LastSound = _timing.CurTime;
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanGib(RecyclerComponent component, EntityUid entity)
|
||||
{
|
||||
// We suppose this entity has a Recyclable component.
|
||||
return EntityManager.HasComponent<SharedBodyComponent>(entity) && !component.Safe &&
|
||||
EntityManager.TryGetComponent(component.Owner, out ApcPowerReceiverComponent? receiver) && receiver.Powered;
|
||||
// TODO: Power needs a helper for this jeez
|
||||
return HasComp<SharedBodyComponent>(entity) && !component.Safe &&
|
||||
TryComp<ApcPowerReceiverComponent>(component.Owner, out var receiver) && receiver.Powered;
|
||||
}
|
||||
|
||||
public void Bloodstain(RecyclerComponent component)
|
||||
@@ -57,13 +98,26 @@ namespace Content.Server.Recycling
|
||||
}
|
||||
}
|
||||
|
||||
private void Recycle(RecyclableComponent component, float efficiency = 1f)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(component.Prototype))
|
||||
{
|
||||
var xform = Transform(component.Owner);
|
||||
|
||||
for (var i = 0; i < Math.Max(component.Amount * efficiency, 1); i++)
|
||||
{
|
||||
Spawn(component.Prototype, xform.Coordinates);
|
||||
}
|
||||
}
|
||||
|
||||
QueueDel(component.Owner);
|
||||
}
|
||||
|
||||
private void OnEmagged(EntityUid uid, RecyclerComponent component, GotEmaggedEvent args)
|
||||
{
|
||||
if (component.Safe == true)
|
||||
{
|
||||
component.Safe = false;
|
||||
args.Handled = true;
|
||||
}
|
||||
if (!component.Safe) return;
|
||||
component.Safe = false;
|
||||
args.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user