Predicted footstep sounds (#7060)

This commit is contained in:
metalgearsloth
2022-03-11 12:48:03 +11:00
committed by GitHub
parent b9bc1ebebe
commit f1e6a352a8
8 changed files with 168 additions and 177 deletions

View File

@@ -23,7 +23,6 @@ namespace Content.Client.Entry
"EmitSoundOnLand",
"NameIdentifier",
"EmitSoundOnActivate",
"FootstepModifier",
"HeatResistance",
"EntityStorage",
"MeleeWeapon",

View File

@@ -3,15 +3,16 @@ using Content.Shared.Movement;
using Content.Shared.Movement.Components;
using Content.Shared.Pulling.Components;
using Robust.Client.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Client.Physics.Controllers
{
public sealed class MoverController : SharedMoverController
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
public override void UpdateBeforeSolve(bool prediction, float frameTime)
@@ -76,5 +77,15 @@ namespace Content.Client.Physics.Controllers
HandleKinematicMovement(mover, body);
}
protected override Filter GetSoundPlayers(EntityUid mover)
{
return Filter.Local();
}
protected override bool CanSound()
{
return _timing.IsFirstTimePredicted;
}
}
}

View File

@@ -1,28 +0,0 @@
using Content.Shared.Audio;
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Movement.Components
{
/// <summary>
/// Changes footstep sound
/// </summary>
[RegisterComponent]
public sealed class FootstepModifierComponent : Component
{
[DataField("footstepSoundCollection", required: true)]
public SoundSpecifier SoundCollection = default!;
[DataField("variation")]
public float Variation = default;
public void PlayFootstep()
{
SoundSystem.Play(Filter.Pvs(Owner), SoundCollection.GetSound(), IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Coordinates, AudioHelpers.WithVariation(Variation).WithVolume(-2f));
}
}
}

View File

@@ -1,24 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Content.Server.Movement.Components;
using Content.Server.Shuttles.Components;
using Content.Server.Shuttles.EntitySystems;
using Content.Shared.CCVar;
using Content.Shared.Inventory;
using Content.Shared.Item;
using Content.Shared.Maps;
using Content.Shared.Movement;
using Content.Shared.Movement.Components;
using Content.Shared.Shuttles.Components;
using Content.Shared.Tag;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Utility;
@@ -26,24 +11,21 @@ namespace Content.Server.Physics.Controllers
{
public sealed class MoverController : SharedMoverController
{
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly TagSystem _tags = default!;
private const float StepSoundMoveDistanceRunning = 2;
private const float StepSoundMoveDistanceWalking = 1.5f;
private float _shuttleDockSpeedCap;
[Dependency] private readonly ShuttleSystem _shuttle = default!;
[Dependency] private readonly ThrusterSystem _thruster = default!;
private HashSet<EntityUid> _excludedMobs = new();
private Dictionary<ShuttleComponent, List<(PilotComponent, IMoverComponent)>> _shuttlePilots = new();
public override void Initialize()
protected override Filter GetSoundPlayers(EntityUid mover)
{
base.Initialize();
return Filter.Pvs(mover, entityManager: EntityManager).RemoveWhereAttachedEntity(o => o == mover);
}
var configManager = IoCManager.Resolve<IConfigurationManager>();
configManager.OnValueChanged(CCVars.ShuttleDockSpeedCap, value => _shuttleDockSpeedCap = value, true);
protected override bool CanSound()
{
return true;
}
public override void UpdateBeforeSolve(bool prediction, float frameTime)
@@ -91,15 +73,12 @@ namespace Content.Server.Physics.Controllers
pilots.Add((pilot, mover));
}
var shuttleSystem = EntitySystem.Get<ShuttleSystem>();
var thrusterSystem = EntitySystem.Get<ThrusterSystem>();
// Reset inputs for non-piloted shuttles.
foreach (var (shuttle, _) in _shuttlePilots)
{
if (newPilots.ContainsKey(shuttle)) continue;
thrusterSystem.DisableLinearThrusters(shuttle);
_thruster.DisableLinearThrusters(shuttle);
}
_shuttlePilots = newPilots;
@@ -170,8 +149,8 @@ namespace Content.Server.Physics.Controllers
// Handle shuttle movement
if (linearInput.Length.Equals(0f))
{
thrusterSystem.DisableLinearThrusters(shuttle);
body.LinearDamping = shuttleSystem.ShuttleIdleLinearDamping * body.InvMass;
_thruster.DisableLinearThrusters(shuttle);
body.LinearDamping = _shuttle.ShuttleIdleLinearDamping * body.InvMass;
if (body.LinearVelocity.Length < 0.08)
{
body.LinearVelocity = Vector2.Zero;
@@ -204,7 +183,7 @@ namespace Content.Server.Physics.Controllers
if ((dir & dockFlag) == 0x0)
{
thrusterSystem.DisableLinearThrustDirection(shuttle, dir);
_thruster.DisableLinearThrustDirection(shuttle, dir);
continue;
}
@@ -233,7 +212,7 @@ namespace Content.Server.Physics.Controllers
throw new ArgumentOutOfRangeException();
}
thrusterSystem.EnableLinearThrustDirection(shuttle, dir);
_thruster.EnableLinearThrustDirection(shuttle, dir);
var index = (int) Math.Log2((int) dir);
var force = thrustAngle.RotateVec(shuttleNorth) * shuttle.LinearThrust[index] * length;
@@ -241,14 +220,14 @@ namespace Content.Server.Physics.Controllers
totalForce += force;
}
var dragForce = body.LinearVelocity * (totalForce.Length / shuttleSystem.ShuttleMaxLinearSpeed);
var dragForce = body.LinearVelocity * (totalForce.Length / _shuttle.ShuttleMaxLinearSpeed);
body.ApplyLinearImpulse((totalForce - dragForce) * frameTime);
}
if (MathHelper.CloseTo(angularInput, 0f))
{
thrusterSystem.SetAngularThrust(shuttle, false);
body.AngularDamping = shuttleSystem.ShuttleIdleAngularDamping * body.InvI;
_thruster.SetAngularThrust(shuttle, false);
body.AngularDamping = _shuttle.ShuttleIdleAngularDamping * body.InvI;
body.SleepingAllowed = true;
if (Math.Abs(body.AngularVelocity) < 0.01f)
@@ -261,107 +240,17 @@ namespace Content.Server.Physics.Controllers
body.AngularDamping = 0;
body.SleepingAllowed = false;
var maxSpeed = Math.Min(shuttleSystem.ShuttleMaxAngularMomentum * body.InvI, shuttleSystem.ShuttleMaxAngularSpeed);
var maxTorque = body.Inertia * shuttleSystem.ShuttleMaxAngularAcc;
var maxSpeed = Math.Min(_shuttle.ShuttleMaxAngularMomentum * body.InvI, _shuttle.ShuttleMaxAngularSpeed);
var maxTorque = body.Inertia * _shuttle.ShuttleMaxAngularAcc;
var torque = Math.Min(shuttle.AngularThrust, maxTorque);
var dragTorque = body.AngularVelocity * (torque / maxSpeed);
body.ApplyAngularImpulse((-angularInput * torque - dragTorque) * frameTime);
thrusterSystem.SetAngularThrust(shuttle, true);
_thruster.SetAngularThrust(shuttle, true);
}
}
}
protected override void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover)
{
if (!_tags.HasTag(mover.Owner, "FootstepSound")) return;
var transform = EntityManager.GetComponent<TransformComponent>(mover.Owner);
var coordinates = transform.Coordinates;
var gridId = coordinates.GetGridId(EntityManager);
var distanceNeeded = mover.Sprinting ? StepSoundMoveDistanceRunning : StepSoundMoveDistanceWalking;
// Handle footsteps.
if (_mapManager.GridExists(gridId))
{
// Can happen when teleporting between grids.
if (!coordinates.TryDistance(EntityManager, mobMover.LastPosition, out var distance) ||
distance > distanceNeeded)
{
mobMover.StepSoundDistance = distanceNeeded;
}
else
{
mobMover.StepSoundDistance += distance;
}
}
else
{
// In space no one can hear you squeak
return;
}
DebugTools.Assert(gridId != GridId.Invalid);
mobMover.LastPosition = coordinates;
if (mobMover.StepSoundDistance < distanceNeeded) return;
mobMover.StepSoundDistance -= distanceNeeded;
var invSystem = EntitySystem.Get<InventorySystem>();
if (invSystem.TryGetSlotEntity(mover.Owner, "shoes", out var shoes) &&
EntityManager.TryGetComponent<FootstepModifierComponent>(shoes, out var modifier))
{
modifier.PlayFootstep();
}
else
{
PlayFootstepSound(mover.Owner, gridId, coordinates, mover.Sprinting);
}
}
private void PlayFootstepSound(EntityUid mover, GridId gridId, EntityCoordinates coordinates, bool sprinting)
{
var grid = _mapManager.GetGrid(gridId);
var tile = grid.GetTileRef(coordinates);
if (tile.IsSpace(_tileDefinitionManager)) return;
// If the coordinates have a FootstepModifier component
// i.e. component that emit sound on footsteps emit that sound
string? soundToPlay = null;
foreach (var maybeFootstep in grid.GetAnchoredEntities(tile.GridIndices))
{
if (EntityManager.TryGetComponent(maybeFootstep, out FootstepModifierComponent? footstep))
{
soundToPlay = footstep.SoundCollection.GetSound();
break;
}
}
// if there is no FootstepModifierComponent, determine sound based on tiles
if (soundToPlay == null)
{
// Walking on a tile.
var def = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
soundToPlay = def.FootstepSounds?.GetSound();
if (string.IsNullOrEmpty(soundToPlay))
return;
}
if (string.IsNullOrWhiteSpace(soundToPlay))
{
Logger.ErrorS("sound", $"Unable to find sound in {nameof(PlayFootstepSound)}");
return;
}
SoundSystem.Play(
Filter.Pvs(coordinates),
soundToPlay,
EntityManager.GetComponent<TransformComponent>(mover).Coordinates,
sprinting ? AudioParams.Default.WithVolume(0.75f) : null);
}
}
}

View File

@@ -609,10 +609,6 @@ namespace Content.Shared.CCVar
/*
* Shuttles
*/
// Once cruising actually gets implemented I'd likely drop this speed to 3 maybe.
public static readonly CVarDef<float> ShuttleDockSpeedCap =
CVarDef.Create("shuttle.dock_speed_cap", 5f, CVar.SERVERONLY);
public static readonly CVarDef<float> ShuttleMaxLinearSpeed =
CVarDef.Create("shuttle.max_linear_speed", 13f, CVar.SERVERONLY);

View File

@@ -0,0 +1,17 @@
using Content.Shared.Sound;
namespace Content.Shared.Movement.Components
{
/// <summary>
/// Changes footstep sound
/// </summary>
[RegisterComponent]
public sealed class FootstepModifierComponent : Component
{
[DataField("footstepSoundCollection", required: true)]
public SoundSpecifier SoundCollection = default!;
[DataField("variation")]
public float Variation = default;
}
}

View File

@@ -1,18 +1,20 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.ActionBlocker;
using Content.Shared.Audio;
using Content.Shared.CCVar;
using Content.Shared.Friction;
using Content.Shared.Inventory;
using Content.Shared.Maps;
using Content.Shared.MobState.Components;
using Content.Shared.Movement.Components;
using Content.Shared.Pulling.Components;
using JetBrains.Annotations;
using Content.Shared.Tag;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Controllers;
using Robust.Shared.Player;
using Robust.Shared.Utility;
namespace Content.Shared.Movement
@@ -24,9 +26,17 @@ namespace Content.Shared.Movement
public abstract class SharedMoverController : VirtualController
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private ActionBlockerSystem _blocker = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] private readonly InventorySystem _inventory = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly TagSystem _tags = default!;
private const float StepSoundMoveDistanceRunning = 2;
private const float StepSoundMoveDistanceWalking = 1.5f;
private const float FootstepVariation = 0f;
private const float FootstepVolume = 1f;
private bool _relativeMovement;
@@ -155,7 +165,15 @@ namespace Content.Shared.Movement
? total.ToWorldAngle()
: worldTotal.ToWorldAngle();
xform.DeferUpdates = false;
HandleFootsteps(mover, mobMover);
if (TryGetSound(mover, mobMover, xform, out var variation, out var sound))
{
SoundSystem.Play(
GetSoundPlayers(mover.Owner),
sound,
mover.Owner,
AudioHelpers.WithVariation(variation).WithVolume(FootstepVolume));
}
}
_physics.SetLinearVelocity(physicsComponent, worldTotal);
@@ -178,7 +196,7 @@ namespace Content.Shared.Movement
/// <summary>
/// Used for weightlessness to determine if we are near a wall.
/// </summary>
public static bool IsAroundCollider(SharedPhysicsSystem broadPhaseSystem, TransformComponent transform, IMobMoverComponent mover, IPhysBody collider)
private bool IsAroundCollider(SharedPhysicsSystem broadPhaseSystem, TransformComponent transform, IMobMoverComponent mover, IPhysBody collider)
{
var enlargedAABB = collider.GetWorldAABB().Enlarged(mover.GrabRange);
@@ -191,7 +209,7 @@ namespace Content.Shared.Movement
!otherCollider.CanCollide ||
((collider.CollisionMask & otherCollider.CollisionLayer) == 0 &&
(otherCollider.CollisionMask & collider.CollisionLayer) == 0) ||
(IoCManager.Resolve<IEntityManager>().TryGetComponent(otherCollider.Owner, out SharedPullableComponent? pullable) && pullable.BeingPulled))
(TryComp(otherCollider.Owner, out SharedPullableComponent? pullable) && pullable.BeingPulled))
{
continue;
}
@@ -202,7 +220,87 @@ namespace Content.Shared.Movement
return false;
}
// TODO: Need a predicted client version that only plays for our own entity and then have server-side ignore our session (for that entity only)
protected virtual void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover) {}
// TODO: Predicted audio moment.
protected abstract Filter GetSoundPlayers(EntityUid mover);
protected abstract bool CanSound();
private bool TryGetSound(IMoverComponent mover, IMobMoverComponent mobMover, TransformComponent xform, out float variation, [NotNullWhen(true)] out string? sound)
{
sound = null;
variation = 0f;
if (!CanSound() || !_tags.HasTag(mover.Owner, "FootstepSound")) return false;
var coordinates = xform.Coordinates;
var gridId = coordinates.GetGridId(EntityManager);
var distanceNeeded = mover.Sprinting ? StepSoundMoveDistanceRunning : StepSoundMoveDistanceWalking;
// Handle footsteps.
if (_mapManager.GridExists(gridId))
{
// Can happen when teleporting between grids.
if (!coordinates.TryDistance(EntityManager, mobMover.LastPosition, out var distance) ||
distance > distanceNeeded)
{
mobMover.StepSoundDistance = distanceNeeded;
}
else
{
mobMover.StepSoundDistance += distance;
}
}
else
{
// In space no one can hear you squeak
return false;
}
DebugTools.Assert(gridId != GridId.Invalid);
mobMover.LastPosition = coordinates;
if (mobMover.StepSoundDistance < distanceNeeded) return false;
mobMover.StepSoundDistance -= distanceNeeded;
if (_inventory.TryGetSlotEntity(mover.Owner, "shoes", out var shoes) &&
EntityManager.TryGetComponent<FootstepModifierComponent>(shoes, out var modifier))
{
sound = modifier.SoundCollection.GetSound();
variation = modifier.Variation;
return true;
}
return TryGetFootstepSound(gridId, coordinates, out variation, out sound);
}
private bool TryGetFootstepSound(GridId gridId, EntityCoordinates coordinates, out float variation, [NotNullWhen(true)] out string? sound)
{
variation = 0f;
sound = null;
var grid = _mapManager.GetGrid(gridId);
var tile = grid.GetTileRef(coordinates);
if (tile.IsSpace(_tileDefinitionManager)) return false;
// If the coordinates have a FootstepModifier component
// i.e. component that emit sound on footsteps emit that sound
foreach (var maybeFootstep in grid.GetAnchoredEntities(tile.GridIndices))
{
if (EntityManager.TryGetComponent(maybeFootstep, out FootstepModifierComponent? footstep))
{
sound = footstep.SoundCollection.GetSound();
variation = footstep.Variation;
return true;
}
}
// Walking on a tile.
var def = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
sound = def.FootstepSounds?.GetSound();
variation = FootstepVariation;
return !string.IsNullOrEmpty(sound);
}
}
}

View File

@@ -177,6 +177,15 @@ public sealed class TagSystem : EntitySystem
HasTag(component, id);
}
/// <summary>
/// Checks if a tag has been added to an entity.
/// </summary>
public bool HasTag(EntityUid entity, string id, EntityQuery<TagComponent> tagQuery)
{
return tagQuery.TryGetComponent(entity, out var component) &&
HasTag(component, id);
}
/// <summary>
/// Checks if all of the given tags have been added to an entity.
/// </summary>