Adds portable flasher (#4523)

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
ScalyChimp
2022-02-06 15:59:41 +00:00
committed by GitHub
parent ff8ebf1896
commit 3c9328ff99
14 changed files with 441 additions and 27 deletions

View File

@@ -0,0 +1,64 @@
using Content.Server.Explosion.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
using System.Collections.Generic;
namespace Content.Server.Explosion.Components
{
/// <summary>
/// Raises a <see cref="TriggerEvent"/> whenever an entity collides with a fixture attached to the owner of this component.
/// </summary>
[RegisterComponent]
public sealed class TriggerOnProximityComponent : Component
{
public const string FixtureID = "trigger-on-proximity-fixture";
public HashSet<PhysicsComponent> Colliding = new();
[DataField("shape", required: true)]
public IPhysShape Shape { get; set; } = new PhysShapeCircle {Radius = 2};
/// <summary>
/// How long the the proximity trigger animation plays for.
/// </summary>
[ViewVariables]
[DataField("animationDuration")]
public float AnimationDuration = 0.3f;
/// <summary>
/// Whether the entity needs to be anchored for the proximity to work.
/// </summary>
[ViewVariables]
[DataField("requiresAnchored")]
public bool RequiresAnchored { get; set; } = true;
[ViewVariables]
[DataField("enabled")]
public bool Enabled = true;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("cooldown")]
public float Cooldown { get; set; } = 5f;
/// <summary>
/// How much cooldown has elapsed (if relevant).
/// </summary>
public float Accumulator = 0f;
/// <summary>
/// What speed should the other object be moving at to trigger the proximity fixture?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("triggerSpeed")]
public float TriggerSpeed { get; set; } = 3.5f;
/// <summary>
/// If this proximity is triggered should we continually repeat it?
/// </summary>
[DataField("repeating")]
internal bool Repeating = true;
}
}

View File

@@ -0,0 +1,158 @@
using System.Collections.Generic;
using Content.Server.Explosion.Components;
using Content.Shared.Physics;
using Content.Shared.Trigger;
using Robust.Shared.GameObjects;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Utility;
namespace Content.Server.Explosion.EntitySystems;
public sealed partial class TriggerSystem
{
/// <summary>
/// Anything that has stuff touching it (to check speed) or is on cooldown.
/// </summary>
private HashSet<TriggerOnProximityComponent> _activeProximities = new();
private void InitializeProximity()
{
SubscribeLocalEvent<TriggerOnProximityComponent, StartCollideEvent>(OnProximityStartCollide);
SubscribeLocalEvent<TriggerOnProximityComponent, EndCollideEvent>(OnProximityEndCollide);
SubscribeLocalEvent<TriggerOnProximityComponent, ComponentStartup>(OnProximityStartup);
SubscribeLocalEvent<TriggerOnProximityComponent, ComponentShutdown>(OnProximityShutdown);
SubscribeLocalEvent<TriggerOnProximityComponent, AnchorStateChangedEvent>(OnProximityAnchor);
}
private void OnProximityAnchor(EntityUid uid, TriggerOnProximityComponent component, ref AnchorStateChangedEvent args)
{
component.Enabled = !component.RequiresAnchored ||
args.Anchored;
SetProximityAppearance(uid, component);
if (!component.Enabled)
{
_activeProximities.Remove(component);
component.Colliding.Clear();
}
// Re-check for contacts as we cleared them.
else if (TryComp<PhysicsComponent>(uid, out var body))
{
_broadphase.RegenerateContacts(body);
}
}
private void OnProximityShutdown(EntityUid uid, TriggerOnProximityComponent component, ComponentShutdown args)
{
_activeProximities.Remove(component);
component.Colliding.Clear();
}
private void OnProximityStartup(EntityUid uid, TriggerOnProximityComponent component, ComponentStartup args)
{
component.Enabled = !component.RequiresAnchored ||
EntityManager.GetComponent<TransformComponent>(uid).Anchored;
SetProximityAppearance(uid, component);
if (!TryComp<PhysicsComponent>(uid, out var body)) return;
_fixtures.CreateFixture(body, new Fixture(body, component.Shape)
{
// TODO: Should probably have these settable via datafield but I'm lazy and it's a pain
CollisionLayer = (int) (CollisionGroup.MobImpassable | CollisionGroup.SmallImpassable | CollisionGroup.VaultImpassable), Hard = false, ID = TriggerOnProximityComponent.FixtureID
});
}
private void OnProximityStartCollide(EntityUid uid, TriggerOnProximityComponent component, StartCollideEvent args)
{
if (args.OurFixture.ID != TriggerOnProximityComponent.FixtureID) return;
_activeProximities.Add(component);
component.Colliding.Add(args.OtherFixture.Body);
}
private void OnProximityEndCollide(EntityUid uid, TriggerOnProximityComponent component, EndCollideEvent args)
{
if (args.OurFixture.ID != TriggerOnProximityComponent.FixtureID) return;
component.Colliding.Remove(args.OtherFixture.Body);
if (component.Colliding.Count == 0)
_activeProximities.Remove(component);
}
private void SetProximityAppearance(EntityUid uid, TriggerOnProximityComponent component)
{
if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearanceComponent))
{
appearanceComponent.SetData(ProximityTriggerVisualState.State, component.Enabled ? ProximityTriggerVisuals.Inactive : ProximityTriggerVisuals.Off);
}
}
private void Activate(TriggerOnProximityComponent component)
{
DebugTools.Assert(component.Enabled);
if (!component.Repeating)
{
component.Enabled = false;
_activeProximities.Remove(component);
component.Colliding.Clear();
}
else
{
component.Accumulator = component.Cooldown;
}
SetProximityAppearance(component.Owner, component);
Trigger(component.Owner);
}
private void UpdateProximity(float frameTime)
{
var toRemove = new RemQueue<TriggerOnProximityComponent>();
foreach (var comp in _activeProximities)
{
if (!comp.Enabled)
{
toRemove.Add(comp);
continue;
}
MetaDataComponent? metadata = null;
if (Deleted(comp.Owner, metadata))
{
toRemove.Add(comp);
continue;
}
if (Paused(comp.Owner, metadata)) continue;
comp.Accumulator -= frameTime;
if (comp.Accumulator > 0f) continue;
// Alright now that we have no cd check everything in range.
foreach (var colliding in comp.Colliding)
{
if (Deleted(colliding.Owner)) continue;
if (colliding.LinearVelocity.Length < comp.TriggerSpeed) continue;
// Trigger!
Activate(comp);
break;
}
}
foreach (var prox in toRemove)
{
_activeProximities.Remove(prox);
}
}
}

View File

@@ -6,25 +6,29 @@ using Content.Server.Doors.Systems;
using Content.Server.Explosion.Components;
using Content.Server.Flash;
using Content.Server.Flash.Components;
using Content.Server.Projectiles.Components;
using Content.Shared.Audio;
using Content.Shared.Database;
using Content.Shared.Doors;
using Content.Shared.Throwing;
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.Trigger;
using Timer = Robust.Shared.Timing.Timer;
using Content.Shared.Physics;
using System.Collections.Generic;
namespace Content.Server.Explosion.EntitySystems
{
/// <summary>
/// Raised whenever something is Triggered on the entity.
/// </summary>
public class TriggerEvent : HandledEntityEventArgs
public sealed class TriggerEvent : HandledEntityEventArgs
{
public EntityUid Triggered { get; }
public EntityUid? User { get; }
@@ -40,16 +44,19 @@ namespace Content.Server.Explosion.EntitySystems
public sealed partial class TriggerSystem : EntitySystem
{
[Dependency] private readonly ExplosionSystem _explosions = default!;
[Dependency] private readonly FixtureSystem _fixtures = default!;
[Dependency] private readonly FlashSystem _flashSystem = default!;
[Dependency] private readonly DoorSystem _sharedDoorSystem = default!;
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
public override void Initialize()
{
base.Initialize();
InitializeProximity();
InitializeOnUse();
SubscribeLocalEvent<TriggerOnCollideComponent, StartCollideEvent>(HandleCollide);
SubscribeLocalEvent<TriggerOnCollideComponent, StartCollideEvent>(OnTriggerCollide);
SubscribeLocalEvent<DeleteOnTriggerComponent, TriggerEvent>(HandleDeleteTrigger);
SubscribeLocalEvent<SoundOnTriggerComponent, TriggerEvent>(HandleSoundTrigger);
@@ -88,11 +95,8 @@ namespace Content.Server.Explosion.EntitySystems
#region Flash
private void HandleFlashTrigger(EntityUid uid, FlashOnTriggerComponent component, TriggerEvent args)
{
if (component.Flashed) return;
// TODO Make flash durations sane ffs.
_flashSystem.FlashArea(uid, args.User, component.Range, component.Duration * 1000f);
component.Flashed = true;
}
#endregion
@@ -112,15 +116,9 @@ namespace Content.Server.Explosion.EntitySystems
_sharedDoorSystem.TryToggleDoor(uid);
}
private void HandleCollide(EntityUid uid, TriggerOnCollideComponent component, StartCollideEvent args)
private void OnTriggerCollide(EntityUid uid, TriggerOnCollideComponent component, StartCollideEvent args)
{
EntityUid? user = null;
if (EntityManager.TryGetComponent(uid, out ProjectileComponent projectile))
user = projectile.Shooter;
else if (EntityManager.TryGetComponent(uid, out ThrownItemComponent thrown))
user = thrown.Thrower;
Trigger(component.Owner, user);
Trigger(component.Owner);
}
@@ -144,5 +142,12 @@ namespace Content.Server.Explosion.EntitySystems
Trigger(triggered, user);
});
}
public override void Update(float frameTime)
{
base.Update(frameTime);
UpdateProximity(frameTime);
}
}
}