Bullet flyby sounds (#8317)

This commit is contained in:
metalgearsloth
2022-05-21 18:04:47 +10:00
committed by GitHub
parent 850eebcabc
commit 4e2b94199e
16 changed files with 181 additions and 4 deletions

View File

@@ -0,0 +1,26 @@
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Weapons.Ranged;
/// <summary>
/// Plays a sound when its non-hard fixture collides with a player.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class FlyBySoundComponent : Component
{
/// <summary>
/// Probability that the sound plays
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("prob")]
public float Prob = 0.75f;
[ViewVariables(VVAccess.ReadWrite), DataField("sound")]
public SoundSpecifier Sound = new SoundCollectionSpecifier("BulletMiss")
{
Params = AudioParams.Default.WithVolume(5f),
};
[ViewVariables, DataField("range")] public float Range = 1.5f;
}

View File

@@ -0,0 +1,75 @@
using Content.Shared.Physics;
using Content.Shared.Sound;
using Robust.Shared.GameStates;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Serialization;
namespace Content.Shared.Weapons.Ranged;
public abstract class SharedFlyBySoundSystem : EntitySystem
{
[Dependency] private readonly FixtureSystem _fixtures = default!;
public const string FlyByFixture = "fly-by";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FlyBySoundComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<FlyBySoundComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<FlyBySoundComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<FlyBySoundComponent, ComponentShutdown>(OnShutdown);
}
private void OnStartup(EntityUid uid, FlyBySoundComponent component, ComponentStartup args)
{
if (!TryComp<PhysicsComponent>(uid, out var body)) return;
var shape = new PhysShapeCircle()
{
Radius = component.Range,
};
var fixture = new Fixture(body, shape)
{
Hard = false,
ID = FlyByFixture,
CollisionLayer = (int) CollisionGroup.MobMask,
};
_fixtures.TryCreateFixture(body, fixture);
}
private void OnShutdown(EntityUid uid, FlyBySoundComponent component, ComponentShutdown args)
{
if (!TryComp<PhysicsComponent>(uid, out var body)) return;
_fixtures.DestroyFixture(body, FlyByFixture);
}
private void OnHandleState(EntityUid uid, FlyBySoundComponent component, ref ComponentHandleState args)
{
if (args.Current is not FlyBySoundComponentState state) return;
component.Sound = state.Sound;
component.Range = state.Range;
}
private void OnGetState(EntityUid uid, FlyBySoundComponent component, ref ComponentGetState args)
{
args.State = new FlyBySoundComponentState()
{
Sound = component.Sound,
Range = component.Range,
};
}
[Serializable, NetSerializable]
private sealed class FlyBySoundComponentState : ComponentState
{
public SoundSpecifier Sound = default!;
public float Range;
}
}