Jetpacks (#9023)
* Movement acceleration * tweaks * Weightless refactor coz fuck it * CCVars * weightless movement tweaks * Some cleanup while I'm here * dorkpacks * thanks fork * fixes * zoomies * toggles * hmm * yamls * b * so true * Effects refactor * namespace * review
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using Content.Shared.Movement.Components;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Movement.Systems;
|
||||
|
||||
public sealed class MovementIgnoreGravitySystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<MovementIgnoreGravityComponent, ComponentGetState>(GetState);
|
||||
SubscribeLocalEvent<MovementIgnoreGravityComponent, ComponentHandleState>(HandleState);
|
||||
}
|
||||
|
||||
private void HandleState(EntityUid uid, MovementIgnoreGravityComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Next is null)
|
||||
return;
|
||||
|
||||
component.Weightless = ((MovementIgnoreGravityComponentState) args.Next).Weightless;
|
||||
}
|
||||
|
||||
private void GetState(EntityUid uid, MovementIgnoreGravityComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new MovementIgnoreGravityComponentState(component);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Movement.Systems
|
||||
{
|
||||
public sealed class MovementSpeedModifierSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<MovementSpeedModifierComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<MovementSpeedModifierComponent, ComponentHandleState>(OnHandleState);
|
||||
}
|
||||
|
||||
private void OnGetState(EntityUid uid, MovementSpeedModifierComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new MovementSpeedModifierComponentState
|
||||
{
|
||||
BaseWalkSpeed = component.BaseWalkSpeed,
|
||||
BaseSprintSpeed = component.BaseSprintSpeed,
|
||||
WalkSpeedModifier = component.WalkSpeedModifier,
|
||||
SprintSpeedModifier = component.SprintSpeedModifier
|
||||
};
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, MovementSpeedModifierComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not MovementSpeedModifierComponentState state) return;
|
||||
component.BaseWalkSpeed = state.BaseWalkSpeed;
|
||||
component.BaseSprintSpeed = state.BaseSprintSpeed;
|
||||
component.WalkSpeedModifier = state.WalkSpeedModifier;
|
||||
component.SprintSpeedModifier = state.SprintSpeedModifier;
|
||||
}
|
||||
|
||||
public void RefreshMovementSpeedModifiers(EntityUid uid, MovementSpeedModifierComponent? move = null)
|
||||
{
|
||||
if (!Resolve(uid, ref move, false))
|
||||
return;
|
||||
|
||||
var ev = new RefreshMovementSpeedModifiersEvent();
|
||||
RaiseLocalEvent(uid, ev);
|
||||
|
||||
move.WalkSpeedModifier = ev.WalkSpeedModifier;
|
||||
move.SprintSpeedModifier = ev.SprintSpeedModifier;
|
||||
|
||||
Dirty(move);
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
private sealed class MovementSpeedModifierComponentState : ComponentState
|
||||
{
|
||||
public float BaseWalkSpeed;
|
||||
public float BaseSprintSpeed;
|
||||
public float WalkSpeedModifier;
|
||||
public float SprintSpeedModifier;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised on an entity to determine its new movement speed. Any system that wishes to change movement speed
|
||||
/// should hook into this event and set it then. If you want this event to be raised,
|
||||
/// call <see cref="MovementSpeedModifierSystem.RefreshMovementSpeedModifiers"/>.
|
||||
/// </summary>
|
||||
public sealed class RefreshMovementSpeedModifiersEvent : EntityEventArgs, IInventoryRelayEvent
|
||||
{
|
||||
public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
|
||||
|
||||
public float WalkSpeedModifier { get; private set; } = 1.0f;
|
||||
public float SprintSpeedModifier { get; private set; } = 1.0f;
|
||||
|
||||
public void ModifySpeed(float walk, float sprint)
|
||||
{
|
||||
WalkSpeedModifier *= walk;
|
||||
SprintSpeedModifier *= sprint;
|
||||
}
|
||||
}
|
||||
}
|
||||
124
Content.Shared/Movement/Systems/SharedJetpackSystem.cs
Normal file
124
Content.Shared/Movement/Systems/SharedJetpackSystem.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using Content.Shared.Actions;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Movement.Events;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Movement.Systems;
|
||||
|
||||
public abstract class SharedJetpackSystem : EntitySystem
|
||||
{
|
||||
[Dependency] protected readonly SharedContainerSystem Container = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<JetpackComponent, GetItemActionsEvent>(OnJetpackGetAction);
|
||||
SubscribeLocalEvent<JetpackComponent, DroppedEvent>(OnJetpackDropped);
|
||||
SubscribeLocalEvent<JetpackComponent, ToggleJetpackEvent>(OnJetpackToggle);
|
||||
SubscribeLocalEvent<JetpackUserComponent, CanWeightlessMoveEvent>(OnJetpackUserCanWeightless);
|
||||
SubscribeLocalEvent<JetpackUserComponent, MobMovementProfileEvent>(OnJetpackUserMovement);
|
||||
}
|
||||
|
||||
private void OnJetpackDropped(EntityUid uid, JetpackComponent component, DroppedEvent args)
|
||||
{
|
||||
SetEnabled(component, false, args.User);
|
||||
}
|
||||
|
||||
private void OnJetpackUserMovement(EntityUid uid, JetpackUserComponent component, ref MobMovementProfileEvent args)
|
||||
{
|
||||
// Only overwrite jetpack movement if they're offgrid.
|
||||
if (args.Override || !args.Weightless) return;
|
||||
|
||||
args.Override = true;
|
||||
args.Acceleration = component.Acceleration;
|
||||
args.WeightlessModifier = component.WeightlessModifier;
|
||||
args.Friction = component.Friction;
|
||||
}
|
||||
|
||||
private void OnJetpackUserCanWeightless(EntityUid uid, JetpackUserComponent component, ref CanWeightlessMoveEvent args)
|
||||
{
|
||||
args.CanMove = true;
|
||||
}
|
||||
|
||||
private void SetupUser(EntityUid uid, JetpackComponent component)
|
||||
{
|
||||
var user = EnsureComp<JetpackUserComponent>(uid);
|
||||
user.Acceleration = component.Acceleration;
|
||||
user.Friction = component.Friction;
|
||||
user.WeightlessModifier = component.WeightlessModifier;
|
||||
}
|
||||
|
||||
private void OnJetpackToggle(EntityUid uid, JetpackComponent component, ToggleJetpackEvent args)
|
||||
{
|
||||
if (args.Handled) return;
|
||||
|
||||
SetEnabled(component, !IsEnabled(uid));
|
||||
}
|
||||
|
||||
private void OnJetpackGetAction(EntityUid uid, JetpackComponent component, GetItemActionsEvent args)
|
||||
{
|
||||
args.Actions.Add(component.ToggleAction);
|
||||
}
|
||||
|
||||
private bool IsEnabled(EntityUid uid)
|
||||
{
|
||||
return HasComp<ActiveJetpackComponent>(uid);
|
||||
}
|
||||
|
||||
public void SetEnabled(JetpackComponent component, bool enabled, EntityUid? user = null)
|
||||
{
|
||||
if (IsEnabled(component.Owner) == enabled ||
|
||||
enabled && !CanEnable(component)) return;
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
EnsureComp<ActiveJetpackComponent>(component.Owner);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemComp<ActiveJetpackComponent>(component.Owner);
|
||||
}
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
Container.TryGetContainingContainer(component.Owner, out var container);
|
||||
user = container?.Owner;
|
||||
}
|
||||
|
||||
// Can't activate if no one's using.
|
||||
if (user == null && enabled) return;
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
SetupUser(user.Value, component);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemComp<JetpackUserComponent>(user.Value);
|
||||
}
|
||||
}
|
||||
|
||||
TryComp<AppearanceComponent>(component.Owner, out var appearance);
|
||||
appearance?.SetData(JetpackVisuals.Enabled, enabled);
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
protected abstract bool CanEnable(JetpackComponent component);
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
protected sealed class JetpackComponentState : ComponentState
|
||||
{
|
||||
public bool Enabled;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum JetpackVisuals : byte
|
||||
{
|
||||
Enabled,
|
||||
}
|
||||
118
Content.Shared/Movement/Systems/SharedMoverController.Input.cs
Normal file
118
Content.Shared/Movement/Systems/SharedMoverController.Input.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Movement.Events;
|
||||
using Content.Shared.Vehicle.Components;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Input;
|
||||
using Robust.Shared.Input.Binding;
|
||||
using Robust.Shared.Players;
|
||||
|
||||
namespace Content.Shared.Movement.Systems
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles converting inputs into movement.
|
||||
/// </summary>
|
||||
public abstract partial class SharedMoverController
|
||||
{
|
||||
private void InitializeInput()
|
||||
{
|
||||
var moveUpCmdHandler = new MoverDirInputCmdHandler(this, Direction.North);
|
||||
var moveLeftCmdHandler = new MoverDirInputCmdHandler(this, Direction.West);
|
||||
var moveRightCmdHandler = new MoverDirInputCmdHandler(this, Direction.East);
|
||||
var moveDownCmdHandler = new MoverDirInputCmdHandler(this, Direction.South);
|
||||
|
||||
CommandBinds.Builder
|
||||
.Bind(EngineKeyFunctions.MoveUp, moveUpCmdHandler)
|
||||
.Bind(EngineKeyFunctions.MoveLeft, moveLeftCmdHandler)
|
||||
.Bind(EngineKeyFunctions.MoveRight, moveRightCmdHandler)
|
||||
.Bind(EngineKeyFunctions.MoveDown, moveDownCmdHandler)
|
||||
.Bind(EngineKeyFunctions.Walk, new WalkInputCmdHandler(this))
|
||||
.Register<SharedMoverController>();
|
||||
}
|
||||
|
||||
private void ShutdownInput()
|
||||
{
|
||||
CommandBinds.Unregister<SharedMoverController>();
|
||||
}
|
||||
|
||||
private void HandleDirChange(ICommonSession? session, Direction dir, ushort subTick, bool state)
|
||||
{
|
||||
if (!TryComp<IMoverComponent>(session?.AttachedEntity, out var moverComp))
|
||||
return;
|
||||
|
||||
var owner = session?.AttachedEntity;
|
||||
|
||||
if (owner != null && session != null)
|
||||
{
|
||||
EntityManager.EventBus.RaiseLocalEvent(owner.Value, new RelayMoveInputEvent(session), true);
|
||||
|
||||
// For stuff like "Moving out of locker" or the likes
|
||||
if (owner.Value.IsInContainer() &&
|
||||
(!EntityManager.TryGetComponent(owner.Value, out MobStateComponent? mobState) ||
|
||||
mobState.IsAlive()))
|
||||
{
|
||||
var relayMoveEvent = new RelayMovementEntityEvent(owner.Value);
|
||||
EntityManager.EventBus.RaiseLocalEvent(EntityManager.GetComponent<TransformComponent>(owner.Value).ParentUid, relayMoveEvent, true);
|
||||
}
|
||||
// Pass the rider's inputs to the vehicle (the rider itself is on the ignored list in C.S/MoverController.cs)
|
||||
if (TryComp<RiderComponent>(owner.Value, out var rider) && rider.Vehicle != null && rider.Vehicle.HasKey)
|
||||
{
|
||||
if (TryComp<IMoverComponent>(rider.Vehicle.Owner, out var vehicleMover))
|
||||
{
|
||||
vehicleMover.SetVelocityDirection(dir, subTick, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
moverComp.SetVelocityDirection(dir, subTick, state);
|
||||
}
|
||||
|
||||
private void HandleRunChange(ICommonSession? session, ushort subTick, bool walking)
|
||||
{
|
||||
if (!TryComp<IMoverComponent>(session?.AttachedEntity, out var moverComp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
moverComp.SetSprinting(subTick, walking);
|
||||
}
|
||||
|
||||
private sealed class MoverDirInputCmdHandler : InputCmdHandler
|
||||
{
|
||||
private SharedMoverController _controller;
|
||||
private readonly Direction _dir;
|
||||
|
||||
public MoverDirInputCmdHandler(SharedMoverController controller, Direction dir)
|
||||
{
|
||||
_controller = controller;
|
||||
_dir = dir;
|
||||
}
|
||||
|
||||
public override bool HandleCmdMessage(ICommonSession? session, InputCmdMessage message)
|
||||
{
|
||||
if (message is not FullInputCmdMessage full) return false;
|
||||
|
||||
_controller.HandleDirChange(session, _dir, message.SubTick, full.State == BoundKeyState.Down);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class WalkInputCmdHandler : InputCmdHandler
|
||||
{
|
||||
private SharedMoverController _controller;
|
||||
|
||||
public WalkInputCmdHandler(SharedMoverController controller)
|
||||
{
|
||||
_controller = controller;
|
||||
}
|
||||
|
||||
public override bool HandleCmdMessage(ICommonSession? session, InputCmdMessage message)
|
||||
{
|
||||
if (message is not FullInputCmdMessage full) return false;
|
||||
|
||||
_controller.HandleRunChange(session, full.SubTick, full.State == BoundKeyState.Down);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
|
||||
namespace Content.Shared.Movement.Systems;
|
||||
|
||||
public abstract partial class SharedMoverController
|
||||
{
|
||||
private bool _pushingEnabled;
|
||||
|
||||
private void InitializePushing()
|
||||
{
|
||||
_configManager.OnValueChanged(CCVars.MobPushing, SetPushing, true);
|
||||
}
|
||||
|
||||
private void SetPushing(bool value)
|
||||
{
|
||||
if (_pushingEnabled == value) return;
|
||||
|
||||
_pushingEnabled = value;
|
||||
|
||||
if (_pushingEnabled)
|
||||
{
|
||||
_physics.KinematicControllerCollision += OnMobCollision;
|
||||
}
|
||||
else
|
||||
{
|
||||
_physics.KinematicControllerCollision -= OnMobCollision;
|
||||
}
|
||||
}
|
||||
|
||||
private void ShutdownPushing()
|
||||
{
|
||||
if (_pushingEnabled)
|
||||
_physics.KinematicControllerCollision -= OnMobCollision;
|
||||
|
||||
_configManager.UnsubValueChanged(CCVars.MobPushing, SetPushing);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fake pushing for player collisions.
|
||||
/// </summary>
|
||||
private void OnMobCollision(Fixture ourFixture, Fixture otherFixture, float frameTime, Vector2 worldNormal)
|
||||
{
|
||||
if (!_pushingEnabled) return;
|
||||
|
||||
var otherBody = otherFixture.Body;
|
||||
|
||||
if (otherBody.BodyType != BodyType.Dynamic || !otherFixture.Hard) return;
|
||||
|
||||
if (!EntityManager.TryGetComponent(ourFixture.Body.Owner, out IMobMoverComponent? mobMover) || worldNormal == Vector2.Zero) return;
|
||||
|
||||
otherBody.ApplyLinearImpulse(-worldNormal * mobMover.PushStrength * frameTime);
|
||||
}
|
||||
}
|
||||
453
Content.Shared/Movement/Systems/SharedMoverController.cs
Normal file
453
Content.Shared/Movement/Systems/SharedMoverController.cs
Normal file
@@ -0,0 +1,453 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
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.Movement.Events;
|
||||
using Content.Shared.Pulling.Components;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Controllers;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Movement.Systems
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles player and NPC mob movement.
|
||||
/// NPCs are handled server-side only.
|
||||
/// </summary>
|
||||
public abstract partial class SharedMoverController : VirtualController
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _configManager = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = 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;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.MinimumFrictionSpeed"/>
|
||||
/// </summary>
|
||||
private float _minimumFrictionSpeed;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.StopSpeed"/>
|
||||
/// </summary>
|
||||
private float _stopSpeed;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.MobAcceleration"/>
|
||||
/// </summary>
|
||||
private float _mobAcceleration;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.MobFriction"/>
|
||||
/// </summary>
|
||||
private float _frictionVelocity;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.MobWeightlessAcceleration"/>
|
||||
/// </summary>
|
||||
private float _mobWeightlessAcceleration;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.MobWeightlessFriction"/>
|
||||
/// </summary>
|
||||
private float _weightlessFrictionVelocity;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.MobWeightlessFrictionNoInput"/>
|
||||
/// </summary>
|
||||
private float _weightlessFrictionVelocityNoInput;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="CCVars.MobWeightlessModifier"/>
|
||||
/// </summary>
|
||||
private float _mobWeightlessModifier;
|
||||
|
||||
private bool _relativeMovement;
|
||||
|
||||
/// <summary>
|
||||
/// Cache the mob movement calculation to re-use elsewhere.
|
||||
/// </summary>
|
||||
public Dictionary<EntityUid, bool> UsedMobMovement = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
InitializeInput();
|
||||
InitializePushing();
|
||||
// Hello
|
||||
_configManager.OnValueChanged(CCVars.RelativeMovement, SetRelativeMovement, true);
|
||||
_configManager.OnValueChanged(CCVars.MinimumFrictionSpeed, SetMinimumFrictionSpeed, true);
|
||||
_configManager.OnValueChanged(CCVars.MobFriction, SetFrictionVelocity, true);
|
||||
_configManager.OnValueChanged(CCVars.MobWeightlessFriction, SetWeightlessFrictionVelocity, true);
|
||||
_configManager.OnValueChanged(CCVars.StopSpeed, SetStopSpeed, true);
|
||||
_configManager.OnValueChanged(CCVars.MobAcceleration, SetMobAcceleration, true);
|
||||
_configManager.OnValueChanged(CCVars.MobWeightlessAcceleration, SetMobWeightlessAcceleration, true);
|
||||
_configManager.OnValueChanged(CCVars.MobWeightlessFrictionNoInput, SetWeightlessFrictionNoInput, true);
|
||||
_configManager.OnValueChanged(CCVars.MobWeightlessModifier, SetMobWeightlessModifier, true);
|
||||
UpdatesBefore.Add(typeof(SharedTileFrictionController));
|
||||
}
|
||||
|
||||
private void SetRelativeMovement(bool value) => _relativeMovement = value;
|
||||
private void SetMinimumFrictionSpeed(float value) => _minimumFrictionSpeed = value;
|
||||
private void SetStopSpeed(float value) => _stopSpeed = value;
|
||||
private void SetFrictionVelocity(float value) => _frictionVelocity = value;
|
||||
private void SetWeightlessFrictionVelocity(float value) => _weightlessFrictionVelocity = value;
|
||||
private void SetMobAcceleration(float value) => _mobAcceleration = value;
|
||||
private void SetMobWeightlessAcceleration(float value) => _mobWeightlessAcceleration = value;
|
||||
private void SetWeightlessFrictionNoInput(float value) => _weightlessFrictionVelocityNoInput = value;
|
||||
private void SetMobWeightlessModifier(float value) => _mobWeightlessModifier = value;
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
ShutdownInput();
|
||||
ShutdownPushing();
|
||||
_configManager.UnsubValueChanged(CCVars.RelativeMovement, SetRelativeMovement);
|
||||
_configManager.UnsubValueChanged(CCVars.MinimumFrictionSpeed, SetMinimumFrictionSpeed);
|
||||
_configManager.UnsubValueChanged(CCVars.StopSpeed, SetStopSpeed);
|
||||
_configManager.UnsubValueChanged(CCVars.MobFriction, SetFrictionVelocity);
|
||||
_configManager.UnsubValueChanged(CCVars.MobWeightlessFriction, SetWeightlessFrictionVelocity);
|
||||
_configManager.UnsubValueChanged(CCVars.MobAcceleration, SetMobAcceleration);
|
||||
_configManager.UnsubValueChanged(CCVars.MobWeightlessAcceleration, SetMobWeightlessAcceleration);
|
||||
_configManager.UnsubValueChanged(CCVars.MobWeightlessFrictionNoInput, SetWeightlessFrictionNoInput);
|
||||
_configManager.UnsubValueChanged(CCVars.MobWeightlessModifier, SetMobWeightlessModifier);
|
||||
}
|
||||
|
||||
public override void UpdateAfterSolve(bool prediction, float frameTime)
|
||||
{
|
||||
base.UpdateAfterSolve(prediction, frameTime);
|
||||
UsedMobMovement.Clear();
|
||||
}
|
||||
|
||||
protected Angle GetParentGridAngle(TransformComponent xform, IMoverComponent mover)
|
||||
{
|
||||
if (!_mapManager.TryGetGrid(xform.GridUid, out var grid))
|
||||
return mover.LastGridAngle;
|
||||
|
||||
return grid.WorldRotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic kinematic mover for entities.
|
||||
/// </summary>
|
||||
protected void HandleKinematicMovement(IMoverComponent mover, PhysicsComponent physicsComponent)
|
||||
{
|
||||
var (walkDir, sprintDir) = mover.VelocityDir;
|
||||
|
||||
var transform = EntityManager.GetComponent<TransformComponent>(mover.Owner);
|
||||
var parentRotation = GetParentGridAngle(transform, mover);
|
||||
|
||||
// Regular movement.
|
||||
// Target velocity.
|
||||
var total = walkDir * mover.CurrentWalkSpeed + sprintDir * mover.CurrentSprintSpeed;
|
||||
|
||||
var worldTotal = _relativeMovement ? parentRotation.RotateVec(total) : total;
|
||||
|
||||
if (transform.GridUid != null)
|
||||
mover.LastGridAngle = parentRotation;
|
||||
|
||||
if (worldTotal != Vector2.Zero)
|
||||
transform.LocalRotation = transform.GridUid != null
|
||||
? total.ToWorldAngle()
|
||||
: worldTotal.ToWorldAngle();
|
||||
|
||||
_physics.SetLinearVelocity(physicsComponent, worldTotal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Movement while considering actionblockers, weightlessness, etc.
|
||||
/// </summary>
|
||||
protected void HandleMobMovement(
|
||||
IMoverComponent mover,
|
||||
PhysicsComponent physicsComponent,
|
||||
IMobMoverComponent mobMover,
|
||||
TransformComponent xform,
|
||||
float frameTime)
|
||||
{
|
||||
DebugTools.Assert(!UsedMobMovement.ContainsKey(mover.Owner));
|
||||
|
||||
if (!UseMobMovement(mover, physicsComponent))
|
||||
{
|
||||
UsedMobMovement[mover.Owner] = false;
|
||||
return;
|
||||
}
|
||||
|
||||
UsedMobMovement[mover.Owner] = true;
|
||||
var weightless = mover.Owner.IsWeightless(physicsComponent, mapManager: _mapManager, entityManager: EntityManager);
|
||||
var (walkDir, sprintDir) = mover.VelocityDir;
|
||||
var touching = false;
|
||||
|
||||
// Handle wall-pushes.
|
||||
if (weightless)
|
||||
{
|
||||
if (xform.GridUid != null)
|
||||
touching = true;
|
||||
|
||||
if (!touching)
|
||||
{
|
||||
var ev = new CanWeightlessMoveEvent();
|
||||
RaiseLocalEvent(xform.Owner, ref ev);
|
||||
// No gravity: is our entity touching anything?
|
||||
touching = ev.CanMove || IsAroundCollider(_physics, xform, mobMover, physicsComponent);
|
||||
}
|
||||
|
||||
if (!touching)
|
||||
{
|
||||
if (xform.GridUid != null)
|
||||
mover.LastGridAngle = GetParentGridAngle(xform, mover);
|
||||
}
|
||||
}
|
||||
|
||||
// Regular movement.
|
||||
// Target velocity.
|
||||
// This is relative to the map / grid we're on.
|
||||
var total = walkDir * mover.CurrentWalkSpeed + sprintDir * mover.CurrentSprintSpeed;
|
||||
var parentRotation = GetParentGridAngle(xform, mover);
|
||||
var worldTotal = _relativeMovement ? parentRotation.RotateVec(total) : total;
|
||||
|
||||
DebugTools.Assert(MathHelper.CloseToPercent(total.Length, worldTotal.Length));
|
||||
|
||||
var velocity = physicsComponent.LinearVelocity;
|
||||
float friction;
|
||||
float weightlessModifier;
|
||||
float accel;
|
||||
|
||||
if (weightless)
|
||||
{
|
||||
if (worldTotal != Vector2.Zero && touching)
|
||||
friction = _weightlessFrictionVelocity;
|
||||
else
|
||||
friction = _weightlessFrictionVelocityNoInput;
|
||||
|
||||
weightlessModifier = _mobWeightlessModifier;
|
||||
accel = _mobWeightlessAcceleration;
|
||||
}
|
||||
else
|
||||
{
|
||||
friction = _frictionVelocity;
|
||||
weightlessModifier = 1f;
|
||||
accel = _mobAcceleration;
|
||||
}
|
||||
|
||||
var profile = new MobMovementProfileEvent(
|
||||
touching,
|
||||
weightless,
|
||||
friction,
|
||||
weightlessModifier,
|
||||
accel);
|
||||
|
||||
RaiseLocalEvent(xform.Owner, ref profile);
|
||||
|
||||
if (profile.Override)
|
||||
{
|
||||
friction = profile.Friction;
|
||||
weightlessModifier = profile.WeightlessModifier;
|
||||
accel = profile.Acceleration;
|
||||
}
|
||||
|
||||
Friction(frameTime, friction, ref velocity);
|
||||
|
||||
if (xform.GridUid != EntityUid.Invalid)
|
||||
mover.LastGridAngle = parentRotation;
|
||||
|
||||
if (worldTotal != Vector2.Zero)
|
||||
{
|
||||
// This should have its event run during island solver soooo
|
||||
xform.DeferUpdates = true;
|
||||
xform.LocalRotation = xform.GridUid != null
|
||||
? total.ToWorldAngle()
|
||||
: worldTotal.ToWorldAngle();
|
||||
xform.DeferUpdates = false;
|
||||
|
||||
if (TryGetSound(mover, mobMover, xform, out var variation, out var sound))
|
||||
{
|
||||
SoundSystem.Play(sound,
|
||||
GetSoundPlayers(mover.Owner),
|
||||
mover.Owner, AudioHelpers.WithVariation(variation).WithVolume(FootstepVolume));
|
||||
}
|
||||
}
|
||||
|
||||
worldTotal *= weightlessModifier;
|
||||
|
||||
if (!weightless || touching)
|
||||
Accelerate(ref velocity, in worldTotal, accel, frameTime);
|
||||
|
||||
_physics.SetLinearVelocity(physicsComponent, velocity);
|
||||
}
|
||||
|
||||
private void Friction(float frameTime, float friction, ref Vector2 velocity)
|
||||
{
|
||||
var speed = velocity.Length;
|
||||
|
||||
if (speed < _minimumFrictionSpeed) return;
|
||||
|
||||
var drop = 0f;
|
||||
|
||||
var control = MathF.Max(_stopSpeed, speed);
|
||||
drop += control * friction * frameTime;
|
||||
|
||||
var newSpeed = MathF.Max(0f, speed - drop);
|
||||
|
||||
if (newSpeed.Equals(speed)) return;
|
||||
|
||||
newSpeed /= speed;
|
||||
velocity *= newSpeed;
|
||||
}
|
||||
|
||||
private void Accelerate(ref Vector2 currentVelocity, in Vector2 velocity, float accel, float frameTime)
|
||||
{
|
||||
var wishDir = velocity != Vector2.Zero ? velocity.Normalized : Vector2.Zero;
|
||||
var wishSpeed = velocity.Length;
|
||||
|
||||
var currentSpeed = Vector2.Dot(currentVelocity, wishDir);
|
||||
var addSpeed = wishSpeed - currentSpeed;
|
||||
|
||||
if (addSpeed <= 0f) return;
|
||||
|
||||
var accelSpeed = accel * frameTime * wishSpeed;
|
||||
accelSpeed = MathF.Min(accelSpeed, addSpeed);
|
||||
|
||||
currentVelocity += wishDir * accelSpeed;
|
||||
}
|
||||
|
||||
public bool UseMobMovement(EntityUid uid)
|
||||
{
|
||||
return UsedMobMovement.TryGetValue(uid, out var used) && used;
|
||||
}
|
||||
|
||||
protected bool UseMobMovement(IMoverComponent mover, PhysicsComponent body)
|
||||
{
|
||||
return mover.CanMove &&
|
||||
body.BodyStatus == BodyStatus.OnGround &&
|
||||
HasComp<MobStateComponent>(body.Owner) &&
|
||||
// If we're being pulled then don't mess with our velocity.
|
||||
(!TryComp(body.Owner, out SharedPullableComponent? pullable) || !pullable.BeingPulled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used for weightlessness to determine if we are near a wall.
|
||||
/// </summary>
|
||||
private bool IsAroundCollider(SharedPhysicsSystem broadPhaseSystem, TransformComponent transform, IMobMoverComponent mover, IPhysBody collider)
|
||||
{
|
||||
var enlargedAABB = collider.GetWorldAABB().Enlarged(mover.GrabRange);
|
||||
|
||||
foreach (var otherCollider in broadPhaseSystem.GetCollidingEntities(transform.MapID, enlargedAABB))
|
||||
{
|
||||
if (otherCollider == collider) continue; // Don't try to push off of yourself!
|
||||
|
||||
// Only allow pushing off of anchored things that have collision.
|
||||
if (otherCollider.BodyType != BodyType.Static ||
|
||||
!otherCollider.CanCollide ||
|
||||
((collider.CollisionMask & otherCollider.CollisionLayer) == 0 &&
|
||||
(otherCollider.CollisionMask & collider.CollisionLayer) == 0) ||
|
||||
(TryComp(otherCollider.Owner, out SharedPullableComponent? pullable) && pullable.BeingPulled))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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.GetGridUid(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 != null);
|
||||
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!.Value, coordinates, out variation, out sound);
|
||||
}
|
||||
|
||||
private bool TryGetFootstepSound(EntityUid 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
72
Content.Shared/Movement/Systems/SlowContactsSystem.cs
Normal file
72
Content.Shared/Movement/Systems/SlowContactsSystem.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Content.Shared.Movement.Components;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
|
||||
namespace Content.Shared.Movement.Systems;
|
||||
|
||||
public sealed class SlowContactsSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
|
||||
[Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!;
|
||||
|
||||
private readonly Dictionary<EntityUid, int> _statusCapableInContact = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<SlowContactsComponent, StartCollideEvent>(OnEntityEnter);
|
||||
SubscribeLocalEvent<SlowContactsComponent, EndCollideEvent>(OnEntityExit);
|
||||
SubscribeLocalEvent<SlowsOnContactComponent, RefreshMovementSpeedModifiersEvent>(MovementSpeedCheck);
|
||||
}
|
||||
|
||||
private void MovementSpeedCheck(EntityUid uid, SlowsOnContactComponent component, RefreshMovementSpeedModifiersEvent args)
|
||||
{
|
||||
if (!_statusCapableInContact.ContainsKey(uid) || _statusCapableInContact[uid] <= 0)
|
||||
return;
|
||||
|
||||
if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent))
|
||||
return;
|
||||
|
||||
var walkSpeed = 1.0f;
|
||||
var sprintSpeed = 1.0f;
|
||||
|
||||
foreach (var colliding in _physics.GetCollidingEntities(physicsComponent))
|
||||
{
|
||||
var ent = colliding.Owner;
|
||||
if (!EntityManager.TryGetComponent<SlowContactsComponent>(ent, out var slowContactsComponent))
|
||||
continue;
|
||||
|
||||
walkSpeed = Math.Min(walkSpeed, slowContactsComponent.WalkSpeedModifier);
|
||||
sprintSpeed = Math.Min(sprintSpeed, slowContactsComponent.SprintSpeedModifier);
|
||||
}
|
||||
|
||||
args.ModifySpeed(walkSpeed, sprintSpeed);
|
||||
}
|
||||
|
||||
private void OnEntityExit(EntityUid uid, SlowContactsComponent component, EndCollideEvent args)
|
||||
{
|
||||
var otherUid = args.OtherFixture.Body.Owner;
|
||||
if (!EntityManager.HasComponent<MovementSpeedModifierComponent>(otherUid)
|
||||
|| !EntityManager.HasComponent<SlowsOnContactComponent>(otherUid))
|
||||
return;
|
||||
if (!_statusCapableInContact.ContainsKey(otherUid))
|
||||
Logger.ErrorS("slowscontacts", $"The entity {otherUid} left a body ({uid}) it was never in.");
|
||||
_statusCapableInContact[otherUid]--;
|
||||
if (_statusCapableInContact[otherUid] == 0)
|
||||
EntityManager.RemoveComponentDeferred<SlowsOnContactComponent>(otherUid);
|
||||
_speedModifierSystem.RefreshMovementSpeedModifiers(otherUid);
|
||||
|
||||
}
|
||||
|
||||
private void OnEntityEnter(EntityUid uid, SlowContactsComponent component, StartCollideEvent args)
|
||||
{
|
||||
var otherUid = args.OtherFixture.Body.Owner;
|
||||
if (!EntityManager.HasComponent<MovementSpeedModifierComponent>(otherUid))
|
||||
return;
|
||||
if (!_statusCapableInContact.ContainsKey(otherUid))
|
||||
_statusCapableInContact[otherUid] = 0;
|
||||
_statusCapableInContact[otherUid]++;
|
||||
|
||||
EnsureComp<SlowsOnContactComponent>(otherUid);
|
||||
_speedModifierSystem.RefreshMovementSpeedModifiers(otherUid);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user