From 2268fdd24ba35545335584a7c178e0206b5d957c Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 18 Jul 2022 19:49:52 +1000 Subject: [PATCH] Add gravitycomponent support to maps (#9857) * Add gravitycomponent support to maps The alert still needs fixing if you want planets. * move --- .../Friction/SharedTileFrictionController.cs | 10 ++--- Content.Shared/Gravity/SharedGravitySystem.cs | 43 +++++++++++++++++++ .../MovementIgnoreGravityComponent.cs | 7 ++- .../Movement/Systems/SharedMoverController.cs | 4 +- Content.Shared/Throwing/ThrowingSystem.cs | 8 ++-- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/Content.Shared/Friction/SharedTileFrictionController.cs b/Content.Shared/Friction/SharedTileFrictionController.cs index 5246837e8b..fc0696741f 100644 --- a/Content.Shared/Friction/SharedTileFrictionController.cs +++ b/Content.Shared/Friction/SharedTileFrictionController.cs @@ -1,4 +1,5 @@ using Content.Shared.CCVar; +using Content.Shared.Gravity; using Content.Shared.Movement; using Content.Shared.Movement.Components; using Content.Shared.Movement.Systems; @@ -18,6 +19,7 @@ namespace Content.Shared.Friction { [Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!; + [Dependency] private readonly SharedGravitySystem _gravity = default!; protected SharedMoverController Mover = default!; @@ -165,16 +167,14 @@ namespace Content.Shared.Friction [Pure] private float GetTileFriction(PhysicsComponent body, TransformComponent xform) { - var coords = xform.Coordinates; - // TODO: Make IsWeightless event-based; we already have grid traversals tracked so just raise events - if (body.Owner.IsWeightless(body, coords, _mapManager) || + if (_gravity.IsWeightless(body.Owner, body, xform) || !_mapManager.TryGetGrid(xform.GridUid, out var grid)) return 0.0f; - if (!coords.IsValid(EntityManager)) return 0.0f; + if (!xform.Coordinates.IsValid(EntityManager)) return 0.0f; - var tile = grid.GetTileRef(coords); + var tile = grid.GetTileRef(xform.Coordinates); var tileDef = _tileDefinitionManager[tile.Tile.TypeId]; return tileDef.Friction; } diff --git a/Content.Shared/Gravity/SharedGravitySystem.cs b/Content.Shared/Gravity/SharedGravitySystem.cs index bcdce7277f..76aa40a6a9 100644 --- a/Content.Shared/Gravity/SharedGravitySystem.cs +++ b/Content.Shared/Gravity/SharedGravitySystem.cs @@ -1,7 +1,50 @@ +using Content.Shared.Clothing; +using Content.Shared.Inventory; +using Content.Shared.Movement.Components; +using Robust.Shared.Map; +using Robust.Shared.Physics; + namespace Content.Shared.Gravity { public abstract class SharedGravitySystem : EntitySystem { + [Dependency] private readonly IMapManager _mapManager = default!; + [Dependency] private readonly InventorySystem _inventory = default!; + + public bool IsWeightless(EntityUid uid, PhysicsComponent? body = null, TransformComponent? xform = null) + { + Resolve(uid, ref body, false); + + if ((body?.BodyType & (BodyType.Static | BodyType.Kinematic)) != 0) + return false; + + if (TryComp(uid, out var ignoreGravityComponent)) + return ignoreGravityComponent.Weightless; + + if (!Resolve(uid, ref xform)) return true; + + // If grid / map has gravity + if ((TryComp(xform.GridUid, out var gravity) || + TryComp(xform.MapUid, out gravity)) && gravity.Enabled) + return false; + + // On the map then always weightless (unless it has gravity comp obv). + if (!_mapManager.TryGetGrid(xform.GridUid, out var grid)) + return true; + + // Something holding us down + if (_inventory.TryGetSlotEntity(uid, "shoes", out var ent)) + { + if (TryComp(ent, out var boots) && boots.On) + return false; + } + + if (!xform.Coordinates.IsValid(EntityManager)) return true; + + var tile = grid.GetTileRef(xform.Coordinates).Tile; + return tile.IsEmpty; + } + public override void Initialize() { base.Initialize(); diff --git a/Content.Shared/Movement/Components/MovementIgnoreGravityComponent.cs b/Content.Shared/Movement/Components/MovementIgnoreGravityComponent.cs index 2015c5f386..212c420507 100644 --- a/Content.Shared/Movement/Components/MovementIgnoreGravityComponent.cs +++ b/Content.Shared/Movement/Components/MovementIgnoreGravityComponent.cs @@ -31,6 +31,7 @@ namespace Content.Shared.Movement.Components public static class GravityExtensions { + [Obsolete("Use GravitySystem")] public static bool IsWeightless(this EntityUid entity, PhysicsComponent? body = null, EntityCoordinates? coords = null, IMapManager? mapManager = null, IEntityManager? entityManager = null) { entityManager ??= IoCManager.Resolve(); @@ -47,10 +48,12 @@ namespace Content.Shared.Movement.Components var transform = entityManager.GetComponent(entity); var gridId = transform.GridUid; + if ((entityManager.TryGetComponent(transform.GridUid, out var gravity) || + entityManager.TryGetComponent(transform.MapUid, out gravity)) && gravity.Enabled) + return false; + if (gridId == null) { - // Not on a grid = no gravity for now. - // In the future, may want to allow maps to override to always have gravity instead. return true; } diff --git a/Content.Shared/Movement/Systems/SharedMoverController.cs b/Content.Shared/Movement/Systems/SharedMoverController.cs index 4f8d431edd..e9f1116c3c 100644 --- a/Content.Shared/Movement/Systems/SharedMoverController.cs +++ b/Content.Shared/Movement/Systems/SharedMoverController.cs @@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis; using Content.Shared.Audio; using Content.Shared.CCVar; using Content.Shared.Friction; +using Content.Shared.Gravity; using Content.Shared.Inventory; using Content.Shared.Maps; using Content.Shared.MobState.Components; @@ -34,6 +35,7 @@ namespace Content.Shared.Movement.Systems [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!; [Dependency] private readonly InventorySystem _inventory = default!; [Dependency] private readonly SharedContainerSystem _container = default!; + [Dependency] private readonly SharedGravitySystem _gravity = default!; [Dependency] private readonly SharedMobStateSystem _mobState = default!; [Dependency] private readonly TagSystem _tags = default!; @@ -112,7 +114,7 @@ namespace Content.Shared.Movement.Systems } UsedMobMovement[mover.Owner] = true; - var weightless = mover.Owner.IsWeightless(physicsComponent, mapManager: _mapManager, entityManager: EntityManager); + var weightless = _gravity.IsWeightless(mover.Owner, physicsComponent, xform); var (walkDir, sprintDir) = GetVelocityInput(mover); var touching = false; diff --git a/Content.Shared/Throwing/ThrowingSystem.cs b/Content.Shared/Throwing/ThrowingSystem.cs index 54a9713617..86ae9559ce 100644 --- a/Content.Shared/Throwing/ThrowingSystem.cs +++ b/Content.Shared/Throwing/ThrowingSystem.cs @@ -1,3 +1,4 @@ +using Content.Shared.Gravity; using Content.Shared.Interaction; using Content.Shared.Movement.Components; using Content.Shared.Tag; @@ -16,6 +17,7 @@ public sealed class ThrowingSystem : EntitySystem /// public const float FlyTime = 0.15f; + [Dependency] private readonly SharedGravitySystem _gravity = default!; [Dependency] private readonly SharedInteractionSystem _interactionSystem = default!; [Dependency] private readonly ThrownItemSystem _thrownSystem = default!; [Dependency] private readonly TagSystem _tagSystem = default!; @@ -23,7 +25,7 @@ public sealed class ThrowingSystem : EntitySystem /// /// Tries to throw the entity if it has a physics component, otherwise does nothing. /// - /// The entity being thrown. + /// The entity being thrown. /// A vector pointing from the entity to its destination. /// How much the direction vector should be multiplied for velocity. /// @@ -97,10 +99,10 @@ public sealed class ThrowingSystem : EntitySystem if (user != null && pushbackRatio > 0.0f && physicsQuery.Value.TryGetComponent(user.Value, out var userPhysics) && - user.Value.IsWeightless(userPhysics, entityManager: EntityManager)) + _gravity.IsWeightless(user.Value, userPhysics)) { var msg = new ThrowPushbackAttemptEvent(); - RaiseLocalEvent(physics.Owner, msg, false); + RaiseLocalEvent(physics.Owner, msg); if (!msg.Cancelled) userPhysics.ApplyLinearImpulse(-impulseVector * pushbackRatio);