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

164 lines
6.1 KiB
C#
Raw Normal View History

#nullable enable
using Content.Server.GameObjects.Components.GUI;
2020-07-07 00:23:41 +02:00
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.Mobs;
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;
using Content.Shared.GameObjects.Components.Tag;
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.Shared.Audio;
using Robust.Shared.GameObjects;
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
2019-04-04 16:18:43 +02:00
{
[UsedImplicitly]
internal class MoverSystem : SharedMoverSystem
2019-04-04 16:18:43 +02:00
{
2020-06-24 02:21:20 +02:00
[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
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;
/// <inheritdoc />
2019-04-04 16:18:43 +02:00
public override void Initialize()
{
2020-06-24 02:21:20 +02:00
base.Initialize();
SubscribeLocalEvent<PlayerDetachedSystemMessage>(PlayerDetached);
Physics (#3452) * Content side new physics structure * BroadPhase outline done * But we need to fix WorldAABB * Fix static pvs AABB * Fix import * Rando fixes * B is for balloon * Change human mob hitbox to circle * Decent movement * Start adding friction to player controller I think it's the best way to go about it to keep other objects somewhat consistent for physics. * This baby can fit so many physics bugs in it. * Slight mob mover optimisations. * Player mover kinda works okay. * Beginnings of testbed * More testbed * Circlestack bed * Namespaces * BB fixes * Pull WorldAABB * Joint pulling * Semi-decent movement I guess. * Pulling better * Bullet controller + old movement * im too dumb for this shit * Use kinematic mob controller again It's probably for the best TBH * Stashed shitcode * Remove SlipController * In which movement code is entirely refactored * Singularity fix * Fix ApplyLinearImpulse * MoveRelay fix * Fix door collisions * Disable subfloor collisions Saves on broadphase a fair bit * Re-implement ClimbController * Zumzum's pressure * Laggy item throwing * Minor atmos change * Some caching * Optimise controllers * Optimise CollideWith to hell and back * Re-do throwing and tile friction * Landing too * Optimise controllers * Move CCVars and other stuff swept is beautiful * Cleanup a bunch of controllers * Fix shooting and high pressure movement controller * Flashing improvements * Stuff and things * Combat collisions * Combat mode collisions * Pulling distance joint again * Cleanup physics interfaces * More like scuffedularity * Shit's fucked * Haha tests go green * Bigmoneycrab Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-01 03:11:29 +11:00
_audioSystem = EntitySystemManager.GetEntitySystem<AudioSystem>();
Physics (#3452) * Content side new physics structure * BroadPhase outline done * But we need to fix WorldAABB * Fix static pvs AABB * Fix import * Rando fixes * B is for balloon * Change human mob hitbox to circle * Decent movement * Start adding friction to player controller I think it's the best way to go about it to keep other objects somewhat consistent for physics. * This baby can fit so many physics bugs in it. * Slight mob mover optimisations. * Player mover kinda works okay. * Beginnings of testbed * More testbed * Circlestack bed * Namespaces * BB fixes * Pull WorldAABB * Joint pulling * Semi-decent movement I guess. * Pulling better * Bullet controller + old movement * im too dumb for this shit * Use kinematic mob controller again It's probably for the best TBH * Stashed shitcode * Remove SlipController * In which movement code is entirely refactored * Singularity fix * Fix ApplyLinearImpulse * MoveRelay fix * Fix door collisions * Disable subfloor collisions Saves on broadphase a fair bit * Re-implement ClimbController * Zumzum's pressure * Laggy item throwing * Minor atmos change * Some caching * Optimise controllers * Optimise CollideWith to hell and back * Re-do throwing and tile friction * Landing too * Optimise controllers * Move CCVars and other stuff swept is beautiful * Cleanup a bunch of controllers * Fix shooting and high pressure movement controller * Flashing improvements * Stuff and things * Combat collisions * Combat mode collisions * Pulling distance joint again * Cleanup physics interfaces * More like scuffedularity * Shit's fucked * Haha tests go green * Bigmoneycrab Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-01 03:11:29 +11:00
UpdatesBefore.Add(typeof(PhysicsSystem));
}
Physics (#3452) * Content side new physics structure * BroadPhase outline done * But we need to fix WorldAABB * Fix static pvs AABB * Fix import * Rando fixes * B is for balloon * Change human mob hitbox to circle * Decent movement * Start adding friction to player controller I think it's the best way to go about it to keep other objects somewhat consistent for physics. * This baby can fit so many physics bugs in it. * Slight mob mover optimisations. * Player mover kinda works okay. * Beginnings of testbed * More testbed * Circlestack bed * Namespaces * BB fixes * Pull WorldAABB * Joint pulling * Semi-decent movement I guess. * Pulling better * Bullet controller + old movement * im too dumb for this shit * Use kinematic mob controller again It's probably for the best TBH * Stashed shitcode * Remove SlipController * In which movement code is entirely refactored * Singularity fix * Fix ApplyLinearImpulse * MoveRelay fix * Fix door collisions * Disable subfloor collisions Saves on broadphase a fair bit * Re-implement ClimbController * Zumzum's pressure * Laggy item throwing * Minor atmos change * Some caching * Optimise controllers * Optimise CollideWith to hell and back * Re-do throwing and tile friction * Landing too * Optimise controllers * Move CCVars and other stuff swept is beautiful * Cleanup a bunch of controllers * Fix shooting and high pressure movement controller * Flashing improvements * Stuff and things * Combat collisions * Combat mode collisions * Pulling distance joint again * Cleanup physics interfaces * More like scuffedularity * Shit's fucked * Haha tests go green * Bigmoneycrab Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-01 03:11:29 +11:00
public override void Update(float frameTime)
{
foreach (var (moverComponent, collidableComponent) in EntityManager.ComponentManager
.EntityQuery<IMoverComponent, IPhysicsComponent>(false))
Physics (#3452) * Content side new physics structure * BroadPhase outline done * But we need to fix WorldAABB * Fix static pvs AABB * Fix import * Rando fixes * B is for balloon * Change human mob hitbox to circle * Decent movement * Start adding friction to player controller I think it's the best way to go about it to keep other objects somewhat consistent for physics. * This baby can fit so many physics bugs in it. * Slight mob mover optimisations. * Player mover kinda works okay. * Beginnings of testbed * More testbed * Circlestack bed * Namespaces * BB fixes * Pull WorldAABB * Joint pulling * Semi-decent movement I guess. * Pulling better * Bullet controller + old movement * im too dumb for this shit * Use kinematic mob controller again It's probably for the best TBH * Stashed shitcode * Remove SlipController * In which movement code is entirely refactored * Singularity fix * Fix ApplyLinearImpulse * MoveRelay fix * Fix door collisions * Disable subfloor collisions Saves on broadphase a fair bit * Re-implement ClimbController * Zumzum's pressure * Laggy item throwing * Minor atmos change * Some caching * Optimise controllers * Optimise CollideWith to hell and back * Re-do throwing and tile friction * Landing too * Optimise controllers * Move CCVars and other stuff swept is beautiful * Cleanup a bunch of controllers * Fix shooting and high pressure movement controller * Flashing improvements * Stuff and things * Combat collisions * Combat mode collisions * Pulling distance joint again * Cleanup physics interfaces * More like scuffedularity * Shit's fucked * Haha tests go green * Bigmoneycrab Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-01 03:11:29 +11:00
{
var entity = moverComponent.Owner;
UpdateKinematics(entity.Transform, moverComponent, collidableComponent);
2019-04-04 16:18:43 +02:00
}
}
private void PlayerDetached(PlayerDetachedSystemMessage ev)
2020-06-24 02:21:20 +02:00
{
if (ev.Entity.TryGetComponent(out IPhysicsComponent? physics) &&
physics.TryGetController(out MoverController controller) &&
!ev.Entity.IsWeightless())
{
controller.StopMoving();
}
2020-06-24 02:21:20 +02:00
}
2020-05-23 01:23:36 +02:00
protected override void HandleFootsteps(IMoverComponent mover)
2020-06-24 02:21:20 +02:00
{
var transform = mover.Owner.Transform;
// Handle footsteps.
if (_mapManager.GridExists(mover.LastPosition.GetGridId(EntityManager)))
2020-06-24 02:21:20 +02:00
{
// Can happen when teleporting between grids.
if (!transform.Coordinates.TryDistance(EntityManager, mover.LastPosition, out var distance))
{
mover.LastPosition = transform.Coordinates;
return;
}
mover.StepSoundDistance += distance;
2020-05-02 15:02:52 +01:00
}
2020-03-30 01:15:23 +02:00
mover.LastPosition = transform.Coordinates;
2020-06-24 02:21:20 +02:00
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
}
if (mover.StepSoundDistance > distanceNeeded)
2020-05-23 17:18:32 +02:00
{
mover.StepSoundDistance = 0;
2020-05-23 17:18:32 +02:00
if (!mover.Owner.HasTag("FootstepSound"))
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
{
PlayFootstepSound(transform.Coordinates, mover.Sprinting);
}
}
2019-04-04 16:18:43 +02:00
}
2019-04-05 02:04:34 +02:00
private void PlayFootstepSound(EntityCoordinates coordinates, bool sprinting)
2019-04-05 02:04:34 +02:00
{
// Step one: figure out sound collection prototype.
var grid = _mapManager.GetGrid(coordinates.GetGridId(EntityManager));
var tile = grid.GetTileRef(coordinates);
2019-04-05 02:04:34 +02:00
// If the coordinates have a FootstepModifier component
// i.e. component that emit sound on footsteps emit that sound
string? soundCollectionName = null;
foreach (var maybeFootstep in grid.GetSnapGridCell(tile.GridIndices, SnapGridOffset.Center))
2019-04-05 02:04:34 +02:00
{
if (maybeFootstep.Owner.TryGetComponent(out FootstepModifierComponent? footstep))
2019-04-05 02:04:34 +02:00
{
soundCollectionName = footstep._soundCollectionName;
2019-04-05 02:04:34 +02:00
break;
}
}
// if there is no FootstepModifierComponent, determine sound based on tiles
if (soundCollectionName == null)
2019-04-05 02:04:34 +02:00
{
// 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, sprinting ? AudioParams.Default.WithVolume(0.75f) : null);
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
}
}