Slight trigger refactor (#4320)

* Trigger tweaks

* Finish trigger refactor

* Clusterflash too
This commit is contained in:
metalgearsloth
2021-07-25 20:09:08 +10:00
committed by GitHub
parent 8cb5df2a56
commit ced39d4b2e
19 changed files with 211 additions and 179 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Content.Server.Flash.Components;
using Content.Server.Throwing;
using Content.Shared.Explosion;
using Content.Shared.Interaction;
@@ -58,7 +59,8 @@ namespace Content.Server.Explosion.Components
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs args)
{
if (_grenadesContainer.ContainedEntities.Count >= _maxGrenades || !args.Using.HasComponent<FlashExplosiveComponent>())
if (_grenadesContainer.ContainedEntities.Count >= _maxGrenades ||
!args.Using.HasComponent<FlashOnTriggerComponent>())
return false;
_grenadesContainer.Insert(args.Using);
@@ -70,7 +72,7 @@ namespace Content.Server.Explosion.Components
{
base.Initialize();
_grenadesContainer = ContainerHelpers.EnsureContainer<Container>(Owner, "cluster-flash");
_grenadesContainer = Owner.EnsureContainer<Container>("cluster-flash");
}
@@ -117,10 +119,7 @@ namespace Content.Server.Explosion.Components
if (grenade.Deleted)
return;
if (grenade.TryGetComponent(out OnUseTimerTriggerComponent? useTimer))
{
useTimer.Trigger(eventArgs.User);
}
EntitySystem.Get<TriggerSystem>().Trigger(grenade, eventArgs.User);
});
}

View File

@@ -0,0 +1,13 @@
using Robust.Shared.GameObjects;
namespace Content.Server.Explosion.Components
{
/// <summary>
/// Will delete the attached entity upon a <see cref="TriggerEvent"/>.
/// </summary>
[RegisterComponent]
public class DeleteOnTriggerComponent : Component
{
public override string Name => "DeleteOnTrigger";
}
}

View File

@@ -0,0 +1,13 @@
using Robust.Shared.GameObjects;
namespace Content.Server.Explosion.Components
{
/// <summary>
/// Explode using the entity's <see cref="ExplosiveComponent"/> if Triggered.
/// </summary>
[RegisterComponent]
public class ExplodeOnTriggerComponent : Component
{
public override string Name => "ExplodeOnTrigger";
}
}

View File

@@ -1,11 +1,13 @@
using Content.Shared.Acts;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Explosion.Components
{
/// <summary>
/// Specifies an explosion range should this entity be exploded.
/// </summary>
[RegisterComponent]
public class ExplosiveComponent : Component, ITimerTrigger, IDestroyAct
public class ExplosiveComponent : Component
{
public override string Name => "Explosive";
@@ -18,31 +20,6 @@ namespace Content.Server.Explosion.Components
[DataField("flashRange")]
public int FlashRange;
public bool Exploding { get; private set; } = false;
public bool Explosion()
{
if (Exploding)
{
return false;
}
else
{
Exploding = true;
Owner.SpawnExplosion(DevastationRange, HeavyImpactRange, LightImpactRange, FlashRange);
Owner.QueueDelete();
return true;
}
}
bool ITimerTrigger.Trigger(TimerTriggerEventArgs eventArgs)
{
return Explosion();
}
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
{
Explosion();
}
public bool Exploding { get; set; } = false;
}
}

View File

@@ -1,27 +0,0 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Physics.Collision;
using Robust.Shared.Physics.Dynamics;
namespace Content.Server.Explosion.Components
{
[RegisterComponent]
public class ExplosiveProjectileComponent : Component, IStartCollide
{
public override string Name => "ExplosiveProjectile";
protected override void Initialize()
{
base.Initialize();
Owner.EnsureComponent<ExplosiveComponent>();
}
void IStartCollide.CollideWith(Fixture ourFixture, Fixture otherFixture, in Manifold manifold)
{
if (Owner.TryGetComponent(out ExplosiveComponent? explosive))
{
explosive.Explosion();
}
}
}
}

View File

@@ -1,61 +0,0 @@
using Content.Server.Flash.Components;
using Content.Server.Storage.Components;
using Content.Shared.Acts;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Explosion.Components
{
/// <summary>
/// When triggered will flash in an area around the object and destroy itself
/// </summary>
[RegisterComponent]
public class FlashExplosiveComponent : Component, ITimerTrigger, IDestroyAct
{
public override string Name => "FlashExplosive";
[DataField("range")]
private float _range = 7.0f;
[DataField("duration")]
private float _duration = 8.0f;
[DataField("sound")]
private string _sound = "/Audio/Effects/flash_bang.ogg";
[DataField("deleteOnFlash")]
private bool _deleteOnFlash = true;
public bool Explode()
{
// If we're in a locker or whatever then can't flash anything
Owner.TryGetContainer(out var container);
if (container == null || !container.Owner.HasComponent<EntityStorageComponent>())
{
FlashableComponent.FlashAreaHelper(Owner, _range, _duration);
}
if (_sound != null)
{
SoundSystem.Play(Filter.Pvs(Owner), _sound, Owner.Transform.Coordinates);
}
if (_deleteOnFlash && !Owner.Deleted)
{
Owner.Delete();
}
return true;
}
bool ITimerTrigger.Trigger(TimerTriggerEventArgs eventArgs)
{
return Explode();
}
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
{
Explode();
}
}
}

View File

@@ -15,12 +15,13 @@ namespace Content.Server.Explosion.Components
[DataField("delay")]
private float _delay = 0f;
// TODO: Need to split this out so it's a generic "OnUseTimerTrigger" component.
public void Trigger(IEntity user)
{
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
appearance.SetData(TriggerVisuals.VisualState, TriggerVisualState.Primed);
EntitySystem.Get<TriggerSystem>().HandleTimerTrigger(TimeSpan.FromSeconds(_delay), user, Owner);
EntitySystem.Get<TriggerSystem>().HandleTimerTrigger(TimeSpan.FromSeconds(_delay), Owner, user);
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)

View File

@@ -0,0 +1,20 @@
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
{
public override string Name => "SoundOnTrigger";
[ViewVariables(VVAccess.ReadWrite)]
[DataField("sound")]
public SoundSpecifier? Sound { get; set; } = null;
}
}

View File

@@ -0,0 +1,10 @@
using Robust.Shared.GameObjects;
namespace Content.Server.Explosion.Components
{
[RegisterComponent]
public class TriggerOnCollideComponent : Component
{
public override string Name => "TriggerOnCollide";
}
}

View File

@@ -274,10 +274,12 @@ namespace Content.Server.Explosion
public static void SpawnExplosion(this IEntity entity, int devastationRange = 0, int heavyImpactRange = 0,
int lightImpactRange = 0, int flashRange = 0)
{
// TODO: Need to refactor this stufferino
// If you want to directly set off the explosive
if (!entity.Deleted && entity.TryGetComponent(out ExplosiveComponent? explosive) && !explosive.Exploding)
{
explosive.Explosion();
EntitySystem.Get<TriggerSystem>().Explode(entity.Uid, explosive);
}
else
{

View File

@@ -1,53 +1,118 @@
using System;
using System.Linq;
using Content.Server.Explosion.Components;
using Content.Server.Flash.Components;
using Content.Shared.Acts;
using Content.Shared.Audio;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Server.Explosion
{
/// <summary>
/// This interface gives components behavior when being "triggered" by timer or other conditions
/// Raised whenever something is Triggered on the entity.
/// </summary>
public interface ITimerTrigger
public class TriggerEvent : HandledEntityEventArgs
{
/// <summary>
/// Called when one object is triggering some event
/// </summary>
bool Trigger(TimerTriggerEventArgs eventArgs);
}
public IEntity Triggered { get; }
public IEntity? User { get; }
public class TimerTriggerEventArgs : EventArgs
{
public TimerTriggerEventArgs(IEntity user, IEntity source)
public TriggerEvent(IEntity triggered, IEntity? user = null)
{
Triggered = triggered;
User = user;
Source = source;
}
public IEntity User { get; set; }
public IEntity Source { get; set; }
}
[UsedImplicitly]
public sealed class TriggerSystem : EntitySystem
{
public void HandleTimerTrigger(TimeSpan delay, IEntity user, IEntity trigger)
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TriggerOnCollideComponent, StartCollideEvent>(HandleCollide);
SubscribeLocalEvent<DeleteOnTriggerComponent, TriggerEvent>(HandleDeleteTrigger);
SubscribeLocalEvent<SoundOnTriggerComponent, TriggerEvent>(HandleSoundTrigger);
SubscribeLocalEvent<ExplodeOnTriggerComponent, TriggerEvent>(HandleExplodeTrigger);
SubscribeLocalEvent<FlashOnTriggerComponent, TriggerEvent>(HandleFlashTrigger);
SubscribeLocalEvent<ExplosiveComponent, DestructionEventArgs>(HandleDestruction);
}
#region Explosions
private void HandleDestruction(EntityUid uid, ExplosiveComponent component, DestructionEventArgs args)
{
Explode(uid, component);
}
private void HandleExplodeTrigger(EntityUid uid, ExplodeOnTriggerComponent component, TriggerEvent args)
{
if (!ComponentManager.TryGetComponent(uid, out ExplosiveComponent? explosiveComponent)) return;
Explode(uid, explosiveComponent);
}
// You really shouldn't call this directly (TODO Change that when ExplosionHelper gets changed).
public void Explode(EntityUid uid, ExplosiveComponent component)
{
if (component.Exploding)
{
return;
}
component.Exploding = true;
component.Owner.SpawnExplosion(component.DevastationRange, component.HeavyImpactRange, component.LightImpactRange, component.FlashRange);
EntityManager.QueueDeleteEntity(uid);
}
#endregion
#region Flash
private void HandleFlashTrigger(EntityUid uid, FlashOnTriggerComponent component, TriggerEvent args)
{
if (component.Flashed) return;
FlashableComponent.FlashAreaHelper(component.Owner, component.Range, component.Duration);
component.Flashed = true;
}
#endregion
private void HandleSoundTrigger(EntityUid uid, SoundOnTriggerComponent component, TriggerEvent args)
{
if (component.Sound == null) return;
SoundSystem.Play(Filter.Pvs(component.Owner), component.Sound.GetSound(), AudioHelpers.WithVariation(0.01f));
}
private void HandleDeleteTrigger(EntityUid uid, DeleteOnTriggerComponent component, TriggerEvent args)
{
EntityManager.QueueDeleteEntity(uid);
}
private void HandleCollide(EntityUid uid, TriggerOnCollideComponent component, StartCollideEvent args)
{
Trigger(component.Owner);
}
public void Trigger(IEntity trigger, IEntity? user = null)
{
var triggerEvent = new TriggerEvent(trigger, user);
EntityManager.EventBus.RaiseLocalEvent(trigger.Uid, triggerEvent);
}
public void HandleTimerTrigger(TimeSpan delay, IEntity triggered, IEntity? user = null)
{
if (delay.TotalSeconds <= 0)
{
Trigger(triggered, user);
return;
}
Timer.Spawn(delay, () =>
{
var timerTriggerEventArgs = new TimerTriggerEventArgs(user, trigger);
var timerTriggers = trigger.GetAllComponents<ITimerTrigger>().ToList();
foreach (var timerTrigger in timerTriggers)
{
if (timerTrigger.Trigger(timerTriggerEventArgs))
{
// If an IOnTimerTrigger returns a status completion we finish our trigger
return;
}
}
Trigger(triggered, user);
});
}
}