From 5fcd13d0e3456abde96c927c701b24ad03aea672 Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Wed, 14 Oct 2020 22:37:25 +0200 Subject: [PATCH] Add IEntity.IsWeightless extension method (#2255) Checking manually every time wasn't that cool --- .../Movement/MovementIgnoreGravityComponent.cs | 15 +++++++++++++++ .../EntitySystems/SharedMoverSystem.cs | 3 +-- Content.Shared/Physics/ConveyedController.cs | 3 +-- Content.Shared/Physics/MoverController.cs | 7 +------ 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Content.Shared/GameObjects/Components/Movement/MovementIgnoreGravityComponent.cs b/Content.Shared/GameObjects/Components/Movement/MovementIgnoreGravityComponent.cs index a603b58d84..c69a2d8bf3 100644 --- a/Content.Shared/GameObjects/Components/Movement/MovementIgnoreGravityComponent.cs +++ b/Content.Shared/GameObjects/Components/Movement/MovementIgnoreGravityComponent.cs @@ -1,4 +1,8 @@ +#nullable enable using Robust.Shared.GameObjects; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Interfaces.Physics; +using Robust.Shared.IoC; namespace Content.Shared.GameObjects.Components.Movement { @@ -7,4 +11,15 @@ namespace Content.Shared.GameObjects.Components.Movement { public override string Name => "MovementIgnoreGravity"; } + + public static class GravityExtensions + { + public static bool IsWeightless(this IEntity entity, IPhysicsManager? physicsManager = null) + { + physicsManager ??= IoCManager.Resolve(); + + return !entity.HasComponent() && + physicsManager.IsWeightless(entity.Transform.Coordinates); + } + } } diff --git a/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs b/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs index c8b308cc26..a4782391ea 100644 --- a/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs +++ b/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs @@ -56,8 +56,7 @@ namespace Content.Shared.GameObjects.EntitySystems { physics.EnsureController(); - var weightless = !transform.Owner.HasComponent() && - _physicsManager.IsWeightless(transform.Coordinates); + var weightless = !transform.Owner.IsWeightless(); if (weightless) { diff --git a/Content.Shared/Physics/ConveyedController.cs b/Content.Shared/Physics/ConveyedController.cs index 92d7c565f9..d3a08818ea 100644 --- a/Content.Shared/Physics/ConveyedController.cs +++ b/Content.Shared/Physics/ConveyedController.cs @@ -14,8 +14,7 @@ namespace Content.Shared.Physics public void Move(Vector2 velocityDirection, float speed) { - if (ControlledComponent?.Owner.HasComponent() == false && - IoCManager.Resolve().IsWeightless(ControlledComponent.Owner.Transform.Coordinates)) + if (ControlledComponent?.Owner.IsWeightless() ?? false) { return; } diff --git a/Content.Shared/Physics/MoverController.cs b/Content.Shared/Physics/MoverController.cs index cf0831a5ab..60c6f76ba2 100644 --- a/Content.Shared/Physics/MoverController.cs +++ b/Content.Shared/Physics/MoverController.cs @@ -1,8 +1,6 @@ #nullable enable using Content.Shared.GameObjects.Components.Movement; using Robust.Shared.GameObjects.Components; -using Robust.Shared.Interfaces.Physics; -using Robust.Shared.IoC; using Robust.Shared.Maths; using Robust.Shared.Physics; @@ -10,14 +8,11 @@ namespace Content.Shared.Physics { public class MoverController : VirtualController { - [Dependency] private readonly IPhysicsManager _physicsManager = default!; - public override IPhysicsComponent? ControlledComponent { protected get; set; } public void Move(Vector2 velocityDirection, float speed) { - if (ControlledComponent?.Owner.HasComponent() == false - && _physicsManager.IsWeightless(ControlledComponent.Owner.Transform.Coordinates)) + if (ControlledComponent?.Owner.IsWeightless() ?? false) { return; }