Files
OldThink/Content.Server/GameObjects/EntitySystems/MoverSystem.cs

201 lines
7.0 KiB
C#
Raw Normal View History

#nullable enable
using Content.Server.GameObjects;
using Content.Server.GameObjects.Components;
using Content.Server.GameObjects.Components.Mobs;
2019-04-05 02:04:34 +02:00
using Content.Server.GameObjects.Components.Movement;
using Content.Server.GameObjects.Components.Sound;
2019-04-05 02:04:34 +02:00
using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Inventory;
using Content.Shared.GameObjects.Components.Movement;
2020-06-24 02:21:20 +02:00
using Content.Shared.GameObjects.EntitySystems;
2019-04-05 02:04:34 +02:00
using Content.Shared.Maps;
2020-05-23 01:23:36 +02:00
using Content.Shared.Physics;
2019-04-04 16:18:43 +02:00
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.Timing;
2020-05-02 15:02:52 +01:00
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
2019-04-04 16:18:43 +02:00
namespace Content.Server.GameObjects.EntitySystems
{
[UsedImplicitly]
2020-06-24 02:21:20 +02:00
internal class MoverSystem : SharedMoverSystem
2019-04-04 16:18:43 +02:00
{
#pragma warning disable 649
2020-06-24 02:21:20 +02:00
[Dependency] private readonly IPauseManager _pauseManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
2019-04-04 16:18:43 +02:00
#pragma warning restore 649
2020-06-24 02:21:20 +02:00
private AudioSystem _audioSystem = default!;
2019-04-05 02:04:34 +02:00
private const float StepSoundMoveDistanceRunning = 2;
private const float StepSoundMoveDistanceWalking = 1.5f;
2019-04-04 16:18:43 +02:00
/// <inheritdoc />
public override void Initialize()
{
2020-06-24 02:21:20 +02:00
base.Initialize();
2019-04-04 16:18:43 +02:00
SubscribeLocalEvent<PlayerAttachSystemMessage>(PlayerAttached);
SubscribeLocalEvent<PlayerDetachedSystemMessage>(PlayerDetached);
2019-04-05 02:04:34 +02:00
_audioSystem = EntitySystemManager.GetEntitySystem<AudioSystem>();
2020-06-24 02:21:20 +02:00
UpdatesBefore.Add(typeof(PhysicsSystem));
2019-04-04 16:18:43 +02:00
}
public override void Update(float frameTime)
{
foreach (var entity in RelevantEntities)
{
if (_pauseManager.IsEntityPaused(entity))
{
continue;
}
2020-06-24 02:21:20 +02:00
var mover = entity.GetComponent<IMoverComponent>();
var physics = entity.GetComponent<PhysicsComponent>();
2020-05-02 15:02:52 +01:00
if (entity.TryGetComponent<CollidableComponent>(out var collider))
{
UpdateKinematics(entity.Transform, mover, physics, collider);
}
else
{
UpdateKinematics(entity.Transform, mover, physics);
}
2019-04-04 16:18:43 +02:00
}
}
protected override void SetController(PhysicsComponent physics)
2019-04-04 16:18:43 +02:00
{
physics.SetController<MoverController>();
2020-06-24 02:21:20 +02:00
}
private static void PlayerAttached(PlayerAttachSystemMessage ev)
{
if (!ev.Entity.HasComponent<IMoverComponent>())
2020-05-23 01:23:36 +02:00
{
2020-06-24 02:21:20 +02:00
ev.Entity.AddComponent<PlayerInputMoverComponent>();
2020-05-23 01:23:36 +02:00
}
2020-06-24 02:21:20 +02:00
}
2020-05-23 01:23:36 +02:00
2020-06-24 02:21:20 +02:00
private static void PlayerDetached(PlayerDetachedSystemMessage ev)
{
if (ev.Entity.HasComponent<PlayerInputMoverComponent>())
2020-05-02 15:02:52 +01:00
{
2020-06-24 02:21:20 +02:00
ev.Entity.RemoveComponent<PlayerInputMoverComponent>();
}
if (ev.Entity.TryGetComponent(out PhysicsComponent physics))
{
(physics.Controller as MoverController)?.StopMoving();
}
2020-06-24 02:21:20 +02:00
}
2020-05-23 01:23:36 +02:00
2020-06-24 02:21:20 +02:00
protected override void HandleFootsteps(IMoverComponent mover)
{
var transform = mover.Owner.Transform;
// Handle footsteps.
if (_mapManager.GridExists(mover.LastPosition.GridID))
{
// Can happen when teleporting between grids.
var distance = transform.GridPosition.Distance(_mapManager, mover.LastPosition);
mover.StepSoundDistance += distance;
2020-05-02 15:02:52 +01:00
}
2020-03-30 01:15:23 +02:00
2020-06-24 02:21:20 +02:00
mover.LastPosition = transform.GridPosition;
float distanceNeeded;
if (mover.Sprinting)
2020-05-23 01:23:36 +02:00
{
2020-06-24 02:21:20 +02:00
distanceNeeded = StepSoundMoveDistanceRunning;
2019-04-04 16:18:43 +02:00
}
else
{
2020-06-24 02:21:20 +02:00
distanceNeeded = StepSoundMoveDistanceWalking;
2019-04-04 16:18:43 +02:00
}
2020-06-24 02:21:20 +02:00
if (mover.StepSoundDistance > distanceNeeded)
2020-05-23 17:18:32 +02:00
{
2020-06-24 02:21:20 +02:00
mover.StepSoundDistance = 0;
2020-05-23 17:18:32 +02:00
2020-06-24 02:21:20 +02:00
if (!mover.Owner.HasComponent<FootstepSoundComponent>())
2020-05-23 17:18:32 +02:00
{
2020-06-24 02:21:20 +02:00
return;
2020-05-23 17:18:32 +02:00
}
2020-06-24 02:21:20 +02:00
if (mover.Owner.TryGetComponent<InventoryComponent>(out var inventory)
&& inventory.TryGetSlotItem<ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item)
&& item.Owner.TryGetComponent<FootstepModifierComponent>(out var modifier))
2020-05-23 17:18:32 +02:00
{
2020-06-24 02:21:20 +02:00
modifier.PlayFootstep();
2020-05-23 17:18:32 +02:00
}
2020-06-24 02:21:20 +02:00
else
{
2020-06-24 02:21:20 +02:00
PlayFootstepSound(transform.GridPosition);
}
}
2019-04-04 16:18:43 +02:00
}
2019-04-05 02:04:34 +02:00
private void PlayFootstepSound(GridCoordinates coordinates)
{
// Step one: figure out sound collection prototype.
var grid = _mapManager.GetGrid(coordinates.GridID);
var tile = grid.GetTileRef(coordinates);
2019-04-05 02:04:34 +02:00
// If the coordinates have a catwalk, it's always catwalk.
string soundCollectionName;
var catwalk = false;
foreach (var maybeCatwalk in grid.GetSnapGridCell(tile.GridIndices, SnapGridOffset.Center))
2019-04-05 02:04:34 +02:00
{
if (maybeCatwalk.Owner.HasComponent<CatwalkComponent>())
{
catwalk = true;
break;
}
}
if (catwalk)
{
// Catwalk overrides tile sound.s
soundCollectionName = "footstep_catwalk";
}
else
{
// Walking on a tile.
2020-06-24 02:21:20 +02:00
var def = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
2019-04-05 02:04:34 +02:00
if (def.FootstepSounds == null)
{
// Nothing to play, oh well.
return;
}
2020-06-24 02:21:20 +02:00
2019-04-05 02:04:34 +02:00
soundCollectionName = def.FootstepSounds;
}
// Ok well we know the position of the
2019-08-10 22:17:49 +10:00
try
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
var file = _robustRandom.Pick(soundCollection.PickFiles);
_audioSystem.PlayAtCoords(file, coordinates);
2019-08-10 22:17:49 +10:00
}
catch (UnknownPrototypeException)
{
// Shouldn't crash over a sound
Logger.ErrorS("sound", $"Unable to find sound collection for {soundCollectionName}");
}
2019-04-05 02:04:34 +02:00
}
2019-04-04 16:18:43 +02:00
}
}