Add Modular grenades (chemnades). (#7138)
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.Audio;
|
||||
|
||||
namespace Content.Server.Explosion.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Component for tracking active trigger timers. A timers can activated by some other component, e.g. <see cref="OnUseTimerTriggerComponent"/>.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class ActiveTimerTriggerComponent : Component
|
||||
{
|
||||
[DataField("timeRemaining")]
|
||||
public float TimeRemaining;
|
||||
|
||||
[DataField("user")]
|
||||
public EntityUid? User;
|
||||
|
||||
[DataField("beepInterval")]
|
||||
public float BeepInterval;
|
||||
|
||||
[DataField("timeUntilBeep")]
|
||||
public float TimeUntilBeep;
|
||||
|
||||
[DataField("beepSound")]
|
||||
public SoundSpecifier? BeepSound;
|
||||
|
||||
[DataField("beepParams")]
|
||||
public AudioParams BeepParams = AudioParams.Default;
|
||||
}
|
||||
@@ -1,11 +1,36 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.Audio;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class OnUseTimerTriggerComponent : Component
|
||||
{
|
||||
[DataField("delay")] public float Delay = 0f;
|
||||
[DataField("delay")]
|
||||
public float Delay = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// If not null, a user can use verbs to configure the delay to one of these options.
|
||||
/// </summary>
|
||||
[DataField("delayOptions")]
|
||||
public List<float>? DelayOptions = null;
|
||||
|
||||
/// <summary>
|
||||
/// If not null, this timer will periodically play this sound wile active.
|
||||
/// </summary>
|
||||
[DataField("beepSound")]
|
||||
public SoundSpecifier? BeepSound;
|
||||
|
||||
/// <summary>
|
||||
/// Time before beeping starts. Defaults to a single beep interval. If set to zero, will emit a beep immediately after use.
|
||||
/// </summary>
|
||||
[DataField("initialBeepDelay")]
|
||||
public float? InitialBeepDelay;
|
||||
|
||||
[DataField("beepInterval")]
|
||||
public float BeepInterval = 1;
|
||||
|
||||
[DataField("beepParams")]
|
||||
public AudioParams BeepParams = AudioParams.Default.WithVolume(-2f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
using Content.Server.Explosion.EntitySystems;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Whenever a <see cref="TriggerEvent"/> is run play a sound in PVS range.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class SoundOnTriggerComponent : Component
|
||||
{
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("sound")]
|
||||
public SoundSpecifier? Sound { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,119 @@
|
||||
using System;
|
||||
using Content.Server.Explosion.Components;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Trigger;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Explosion.EntitySystems;
|
||||
|
||||
public sealed partial class TriggerSystem
|
||||
{
|
||||
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
|
||||
|
||||
private void InitializeOnUse()
|
||||
{
|
||||
SubscribeLocalEvent<OnUseTimerTriggerComponent, UseInHandEvent>(OnTimerUse);
|
||||
SubscribeLocalEvent<OnUseTimerTriggerComponent, ExaminedEvent>(OnExamined);
|
||||
SubscribeLocalEvent<OnUseTimerTriggerComponent, GetVerbsEvent<AlternativeVerb>>(OnGetAltVerbs);
|
||||
}
|
||||
|
||||
private void OnExamined(EntityUid uid, OnUseTimerTriggerComponent component, ExaminedEvent args)
|
||||
{
|
||||
if (args.IsInDetailsRange)
|
||||
args.PushText(Loc.GetString("examine-trigger-timer", ("time", component.Delay)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an alt-click interaction that cycles through delays.
|
||||
/// </summary>
|
||||
private void OnGetAltVerbs(EntityUid uid, OnUseTimerTriggerComponent component, GetVerbsEvent<AlternativeVerb> args)
|
||||
{
|
||||
if (!args.CanInteract || !args.CanAccess)
|
||||
return;
|
||||
|
||||
if (component.DelayOptions == null || component.DelayOptions.Count == 1)
|
||||
return;
|
||||
|
||||
args.Verbs.Add(new AlternativeVerb()
|
||||
{
|
||||
Category = TimerOptions,
|
||||
Text = Loc.GetString("verb-trigger-timer-cycle"),
|
||||
Act = () => CycleDelay(component, args.User),
|
||||
Priority = 1
|
||||
});
|
||||
|
||||
foreach (var option in component.DelayOptions)
|
||||
{
|
||||
if (MathHelper.CloseTo(option, component.Delay))
|
||||
{
|
||||
args.Verbs.Add(new AlternativeVerb()
|
||||
{
|
||||
Category = TimerOptions,
|
||||
Text = Loc.GetString("verb-trigger-timer-set-current", ("time", option)),
|
||||
Disabled = true,
|
||||
Priority = (int) (-100 * option)
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
args.Verbs.Add(new AlternativeVerb()
|
||||
{
|
||||
Category = TimerOptions,
|
||||
Text = Loc.GetString("verb-trigger-timer-set", ("time", option)),
|
||||
Priority = (int) (-100 * option),
|
||||
|
||||
Act = () =>
|
||||
{
|
||||
component.Delay = option;
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-timer-set", ("time", option)), args.User, Filter.Entities(args.User));
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void CycleDelay(OnUseTimerTriggerComponent component, EntityUid user)
|
||||
{
|
||||
if (component.DelayOptions == null || component.DelayOptions.Count == 1)
|
||||
return;
|
||||
|
||||
// This is somewhat inefficient, but its good enough. This is run rarely, and the lists should be short.
|
||||
|
||||
component.DelayOptions.Sort();
|
||||
|
||||
if (component.DelayOptions[^1] <= component.Delay)
|
||||
{
|
||||
component.Delay = component.DelayOptions[0];
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-timer-set", ("time", component.Delay)), user, Filter.Entities(user));
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var option in component.DelayOptions)
|
||||
{
|
||||
if (option > component.Delay)
|
||||
{
|
||||
component.Delay = option;
|
||||
_popupSystem.PopupEntity(Loc.GetString("popup-trigger-timer-set", ("time", option)), user, Filter.Entities(user));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTimerUse(EntityUid uid, OnUseTimerTriggerComponent component, UseInHandEvent args)
|
||||
{
|
||||
if (args.Handled) return;
|
||||
|
||||
Trigger(uid, args.User, component);
|
||||
HandleTimerTrigger(
|
||||
uid,
|
||||
args.User,
|
||||
component.Delay,
|
||||
component.BeepInterval,
|
||||
component.InitialBeepDelay,
|
||||
component.BeepSound,
|
||||
component.BeepParams);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
// TODO: Need to split this out so it's a generic "OnUseTimerTrigger" component.
|
||||
private void Trigger(EntityUid uid, EntityUid user, OnUseTimerTriggerComponent component)
|
||||
{
|
||||
if (TryComp<AppearanceComponent>(uid, out var appearance))
|
||||
appearance.SetData(TriggerVisuals.VisualState, TriggerVisualState.Primed);
|
||||
|
||||
HandleTimerTrigger(TimeSpan.FromSeconds(component.Delay), uid, user);
|
||||
}
|
||||
public static VerbCategory TimerOptions = new("verb-categories-timer", "/Textures/Interface/VerbIcons/clock.svg.192dpi.png");
|
||||
}
|
||||
|
||||
@@ -1,27 +1,17 @@
|
||||
using System;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Doors;
|
||||
using Content.Server.Doors.Components;
|
||||
using Content.Server.Doors.Systems;
|
||||
using Content.Server.Explosion.Components;
|
||||
using Content.Server.Flash;
|
||||
using Content.Server.Flash.Components;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Doors;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Timing;
|
||||
using System.Threading;
|
||||
using Content.Server.Construction.Components;
|
||||
using Content.Shared.Sound;
|
||||
using Content.Shared.Trigger;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
using Content.Shared.Physics;
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Database;
|
||||
|
||||
namespace Content.Server.Explosion.EntitySystems
|
||||
{
|
||||
@@ -48,6 +38,7 @@ namespace Content.Server.Explosion.EntitySystems
|
||||
[Dependency] private readonly FlashSystem _flashSystem = default!;
|
||||
[Dependency] private readonly DoorSystem _sharedDoorSystem = default!;
|
||||
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
|
||||
[Dependency] private readonly AdminLogSystem _logSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -59,7 +50,6 @@ namespace Content.Server.Explosion.EntitySystems
|
||||
SubscribeLocalEvent<TriggerOnCollideComponent, StartCollideEvent>(OnTriggerCollide);
|
||||
|
||||
SubscribeLocalEvent<DeleteOnTriggerComponent, TriggerEvent>(HandleDeleteTrigger);
|
||||
SubscribeLocalEvent<SoundOnTriggerComponent, TriggerEvent>(HandleSoundTrigger);
|
||||
SubscribeLocalEvent<ExplodeOnTriggerComponent, TriggerEvent>(HandleExplodeTrigger);
|
||||
SubscribeLocalEvent<FlashOnTriggerComponent, TriggerEvent>(HandleFlashTrigger);
|
||||
SubscribeLocalEvent<ToggleDoorOnTriggerComponent, TriggerEvent>(HandleDoorTrigger);
|
||||
@@ -100,12 +90,6 @@ namespace Content.Server.Explosion.EntitySystems
|
||||
}
|
||||
#endregion
|
||||
|
||||
private void HandleSoundTrigger(EntityUid uid, SoundOnTriggerComponent component, TriggerEvent args)
|
||||
{
|
||||
if (component.Sound == null) return;
|
||||
SoundSystem.Play(Filter.Pvs(component.Owner), component.Sound.GetSound(), uid);
|
||||
}
|
||||
|
||||
private void HandleDeleteTrigger(EntityUid uid, DeleteOnTriggerComponent component, TriggerEvent args)
|
||||
{
|
||||
EntityManager.QueueDeleteEntity(uid);
|
||||
@@ -128,19 +112,39 @@ namespace Content.Server.Explosion.EntitySystems
|
||||
EntityManager.EventBus.RaiseLocalEvent(trigger, triggerEvent);
|
||||
}
|
||||
|
||||
public void HandleTimerTrigger(TimeSpan delay, EntityUid triggered, EntityUid? user = null)
|
||||
public void HandleTimerTrigger(EntityUid uid, EntityUid? user, float delay , float beepInterval, float? initialBeepDelay, SoundSpecifier? beepSound, AudioParams beepParams)
|
||||
{
|
||||
if (delay.TotalSeconds <= 0)
|
||||
if (delay <= 0)
|
||||
{
|
||||
Trigger(triggered, user);
|
||||
RemComp<ActiveTimerTriggerComponent>(uid);
|
||||
Trigger(uid, user);
|
||||
return;
|
||||
}
|
||||
|
||||
Timer.Spawn(delay, () =>
|
||||
if (HasComp<ActiveTimerTriggerComponent>(uid))
|
||||
return;
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
if (Deleted(triggered)) return;
|
||||
Trigger(triggered, user);
|
||||
});
|
||||
_logSystem.Add(LogType.Trigger,
|
||||
$"{ToPrettyString(user.Value):user} started a {delay} second timer trigger on entity {ToPrettyString(uid):timer}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logSystem.Add(LogType.Trigger,
|
||||
$"{delay} second timer trigger started on entity {ToPrettyString(uid):timer}");
|
||||
}
|
||||
|
||||
var active = AddComp<ActiveTimerTriggerComponent>(uid);
|
||||
active.TimeRemaining = delay;
|
||||
active.User = user;
|
||||
active.BeepParams = beepParams;
|
||||
active.BeepSound = beepSound;
|
||||
active.BeepInterval = beepInterval;
|
||||
active.TimeUntilBeep = initialBeepDelay == null ? active.BeepInterval : initialBeepDelay.Value;
|
||||
|
||||
if (TryComp<AppearanceComponent>(uid, out var appearance))
|
||||
appearance.SetData(TriggerVisuals.VisualState, TriggerVisualState.Primed);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
@@ -148,6 +152,40 @@ namespace Content.Server.Explosion.EntitySystems
|
||||
base.Update(frameTime);
|
||||
|
||||
UpdateProximity(frameTime);
|
||||
UpdateTimer(frameTime);
|
||||
}
|
||||
|
||||
private void UpdateTimer(float frameTime)
|
||||
{
|
||||
HashSet<EntityUid> toRemove = new();
|
||||
foreach (var timer in EntityQuery<ActiveTimerTriggerComponent>())
|
||||
{
|
||||
timer.TimeRemaining -= frameTime;
|
||||
timer.TimeUntilBeep -= frameTime;
|
||||
|
||||
if (timer.TimeRemaining <= 0)
|
||||
{
|
||||
Trigger(timer.Owner, timer.User);
|
||||
toRemove.Add(timer.Owner);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (timer.BeepSound == null || timer.TimeUntilBeep > 0)
|
||||
continue;
|
||||
|
||||
timer.TimeUntilBeep += timer.BeepInterval;
|
||||
var filter = Filter.Pvs(timer.Owner, entityManager: EntityManager);
|
||||
SoundSystem.Play(filter, timer.BeepSound.GetSound(), timer.Owner, timer.BeepParams);
|
||||
}
|
||||
|
||||
foreach (var uid in toRemove)
|
||||
{
|
||||
RemComp<ActiveTimerTriggerComponent>(uid);
|
||||
|
||||
// In case this is a re-usable grenade, un-prime it.
|
||||
if (TryComp<AppearanceComponent>(uid, out var appearance))
|
||||
appearance.SetData(TriggerVisuals.VisualState, TriggerVisualState.Unprimed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user