Footsteps cleanup (#3840)

* Disable footsteps in space

* Just clean up footsteps
This commit is contained in:
metalgearsloth
2021-04-21 20:14:40 +10:00
committed by GitHub
parent afdd08c600
commit fa2360ee02

View File

@@ -1,5 +1,6 @@
#nullable enable #nullable enable
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.Actions;
using Content.Server.GameObjects.Components.GUI; using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage; using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.Movement; using Content.Server.GameObjects.Components.Movement;
@@ -22,6 +23,7 @@ using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Random; using Robust.Shared.Random;
using Robust.Shared.Utility;
namespace Content.Server.Physics.Controllers namespace Content.Server.Physics.Controllers
{ {
@@ -98,60 +100,59 @@ namespace Content.Server.Physics.Controllers
protected override void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover) protected override void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover)
{ {
if (!mover.Owner.HasTag("FootstepSound")) return;
var transform = mover.Owner.Transform; var transform = mover.Owner.Transform;
var coordinates = transform.Coordinates;
var gridId = coordinates.GetGridId(EntityManager);
var distanceNeeded = mover.Sprinting ? StepSoundMoveDistanceRunning : StepSoundMoveDistanceWalking;
// Handle footsteps. // Handle footsteps.
if (_mapManager.GridExists(mobMover.LastPosition.GetGridId(EntityManager))) if (_mapManager.GridExists(gridId))
{ {
// Can happen when teleporting between grids. // Can happen when teleporting between grids.
if (!transform.Coordinates.TryDistance(EntityManager, mobMover.LastPosition, out var distance)) if (!coordinates.TryDistance(EntityManager, mobMover.LastPosition, out var distance) ||
distance > distanceNeeded)
{ {
mobMover.LastPosition = transform.Coordinates; mobMover.StepSoundDistance = distanceNeeded;
return;
}
mobMover.StepSoundDistance += distance;
}
mobMover.LastPosition = transform.Coordinates;
float distanceNeeded;
if (mover.Sprinting)
{
distanceNeeded = StepSoundMoveDistanceRunning;
}
else
{
distanceNeeded = StepSoundMoveDistanceWalking;
}
if (mobMover.StepSoundDistance > distanceNeeded)
{
mobMover.StepSoundDistance = 0;
if (!mover.Owner.HasTag("FootstepSound") || mover.Owner.Transform.GridID == GridId.Invalid)
{
return;
}
if (mover.Owner.TryGetComponent<InventoryComponent>(out var inventory)
&& inventory.TryGetSlotItem<ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item)
&& item.Owner.TryGetComponent<FootstepModifierComponent>(out var modifier))
{
modifier.PlayFootstep();
} }
else else
{ {
PlayFootstepSound(mover.Owner, mover.Sprinting); 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;
if (mover.Owner.TryGetComponent<InventoryComponent>(out var inventory)
&& inventory.TryGetSlotItem<ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item)
&& item.Owner.TryGetComponent<FootstepModifierComponent>(out var modifier))
{
modifier.PlayFootstep();
}
else
{
PlayFootstepSound(mover.Owner, gridId, coordinates, mover.Sprinting);
}
} }
private void PlayFootstepSound(IEntity mover, bool sprinting) private void PlayFootstepSound(IEntity mover, GridId gridId, EntityCoordinates coordinates, bool sprinting)
{ {
var coordinates = mover.Transform.Coordinates; var grid = _mapManager.GetGrid(gridId);
// Step one: figure out sound collection prototype.
var grid = _mapManager.GetGrid(coordinates.GetGridId(EntityManager));
var tile = grid.GetTileRef(coordinates); var tile = grid.GetTileRef(coordinates);
if (tile.IsSpace(_tileDefinitionManager)) return;
// If the coordinates have a FootstepModifier component // If the coordinates have a FootstepModifier component
// i.e. component that emit sound on footsteps emit that sound // i.e. component that emit sound on footsteps emit that sound
string? soundCollectionName = null; string? soundCollectionName = null;
@@ -177,19 +178,17 @@ namespace Content.Server.Physics.Controllers
soundCollectionName = def.FootstepSounds; soundCollectionName = def.FootstepSounds;
} }
// Ok well we know the position of the if (!_prototypeManager.TryIndex(soundCollectionName, out SoundCollectionPrototype? soundCollection))
try
{ {
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
var file = _robustRandom.Pick(soundCollection.PickFiles);
SoundSystem.Play(Filter.Pvs(coordinates), file, coordinates, sprinting ? AudioParams.Default.WithVolume(0.75f) : null);
}
catch (UnknownPrototypeException)
{
// Shouldn't crash over a sound
Logger.ErrorS("sound", $"Unable to find sound collection for {soundCollectionName}"); Logger.ErrorS("sound", $"Unable to find sound collection for {soundCollectionName}");
return;
} }
}
SoundSystem.Play(
Filter.Pvs(coordinates),
_robustRandom.Pick(soundCollection.PickFiles),
mover,
sprinting ? AudioParams.Default.WithVolume(0.75f) : null);
}
} }
} }