From 64672fdc3100fc1abfb4a0021450becb9953d15a Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Thu, 23 Jul 2020 18:33:37 +0200 Subject: [PATCH] Refactor to use the new multiple controller system (#1462) * Transition code to new controller system * Fix shuttles not moving * Fix throwing * Fix guns * Change hands to use physics.Stop() and remove item fumble method --- .../GameObjects/EntitySystems/MoverSystem.cs | 5 -- .../Components/GUI/ServerHandsComponent.cs | 2 +- .../Components/Items/Storage/ItemComponent.cs | 18 ------- .../Movement/ShuttleControllerComponent.cs | 4 +- .../Projectiles/ThrownItemComponent.cs | 25 ++++++--- .../Barrels/ServerRangedBarrelComponent.cs | 8 ++- .../GameObjects/EntitySystems/MoverSystem.cs | 10 ++-- Content.Server/Throw/ThrowHelper.cs | 16 +++--- .../EntitySystems/SharedMoverSystem.cs | 25 +++++---- Content.Shared/Physics/BulletController.cs | 17 +++++++ Content.Shared/Physics/MoverController.cs | 51 ++++--------------- Content.Shared/Physics/ShuttleController.cs | 17 +++++++ Content.Shared/Physics/ThrownController.cs | 17 +++++++ 13 files changed, 114 insertions(+), 101 deletions(-) create mode 100644 Content.Shared/Physics/BulletController.cs create mode 100644 Content.Shared/Physics/ShuttleController.cs create mode 100644 Content.Shared/Physics/ThrownController.cs diff --git a/Content.Client/GameObjects/EntitySystems/MoverSystem.cs b/Content.Client/GameObjects/EntitySystems/MoverSystem.cs index d6d972ae30..e0d161b649 100644 --- a/Content.Client/GameObjects/EntitySystems/MoverSystem.cs +++ b/Content.Client/GameObjects/EntitySystems/MoverSystem.cs @@ -42,10 +42,5 @@ namespace Content.Client.GameObjects.EntitySystems { FrameUpdate(frameTime); } - - protected override void SetController(IPhysicsComponent physics) - { - physics.SetController(); - } } } diff --git a/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs b/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs index 9e48f57663..b0b39cd326 100644 --- a/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs +++ b/Content.Server/GameObjects/Components/GUI/ServerHandsComponent.cs @@ -567,7 +567,7 @@ namespace Content.Server.GameObjects } // set velocity to zero - physics.LinearVelocity = Vector2.Zero; + physics.Stop(); return; } } diff --git a/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs b/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs index 7849847f36..00f1221cec 100644 --- a/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs +++ b/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs @@ -134,24 +134,6 @@ namespace Content.Server.GameObjects.Components return new ItemComponentState(EquippedPrefix); } - public void Fumble() - { - if (Owner.TryGetComponent(out var physicsComponent)) - { - physicsComponent.LinearVelocity += RandomOffset(); - } - } - - private Vector2 RandomOffset() - { - return new Vector2(RandomOffset(), RandomOffset()); - float RandomOffset() - { - var size = 15.0F; - return (_robustRandom.NextFloat() * size) - size / 2; - } - } - public void OnExplosion(ExplosionEventArgs eventArgs) { var sourceLocation = eventArgs.Source; diff --git a/Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs b/Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs index 19fca5c045..700b9e7b53 100644 --- a/Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs +++ b/Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs @@ -1,5 +1,6 @@ using Content.Server.GameObjects.Components.Mobs; using Content.Shared.GameObjects.Components.Movement; +using Content.Shared.Physics; using Robust.Server.GameObjects; using Robust.Server.GameObjects.Components.Container; using Robust.Shared.GameObjects; @@ -70,7 +71,8 @@ namespace Content.Server.GameObjects.Components.Movement collideComp.PhysicsShapes.Add(new PhysShapeGrid(grid)); } - physComp.LinearVelocity = CalcNewVelocity(direction, enabled) * CurrentWalkSpeed; + var controller = physComp.EnsureController(); + controller.Push(CalcNewVelocity(direction, enabled), CurrentWalkSpeed); } } diff --git a/Content.Server/GameObjects/Components/Projectiles/ThrownItemComponent.cs b/Content.Server/GameObjects/Components/Projectiles/ThrownItemComponent.cs index cabb9deb27..d1b9a4d2c0 100644 --- a/Content.Server/GameObjects/Components/Projectiles/ThrownItemComponent.cs +++ b/Content.Server/GameObjects/Components/Projectiles/ThrownItemComponent.cs @@ -32,7 +32,7 @@ namespace Content.Server.GameObjects.Components void ICollideBehavior.CollideWith(IEntity entity) { if (!_shouldCollide) return; - if (entity.TryGetComponent(out CollidableComponent collid)) + if (entity.TryGetComponent(out CollidableComponent collid)) { if (!collid.Hard) // ignore non hard return; @@ -65,10 +65,13 @@ namespace Content.Server.GameObjects.Components { body.PhysicsShapes[0].CollisionMask &= (int) ~CollisionGroup.ThrownItem; - var physics = Owner.GetComponent(); - physics.LinearVelocity = Vector2.Zero; - physics.Status = BodyStatus.OnGround; + if (body.TryGetController(out ThrownController controller)) + { + controller.LinearVelocity = Vector2.Zero; + } + body.Status = BodyStatus.OnGround; + Owner.RemoveComponent(); EntitySystem.Get().LandInteraction(User, Owner, Owner.Transform.GridPosition); } @@ -82,11 +85,14 @@ namespace Content.Server.GameObjects.Components } } - public void StartThrow(Vector2 initialImpulse) + public void StartThrow(Vector2 direction, float speed) { var comp = Owner.GetComponent(); comp.Status = BodyStatus.InAir; - comp.Momentum = initialImpulse; + + var controller = comp.EnsureController(); + controller.Push(direction, speed); + StartStopTimer(); } @@ -110,5 +116,12 @@ namespace Content.Server.GameObjects.Components StopThrow(); } + + public override void Initialize() + { + base.Initialize(); + + Owner.EnsureComponent().EnsureController(); + } } } diff --git a/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs b/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs index e0c5eaf89f..6f7f76cf62 100644 --- a/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs +++ b/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs @@ -7,6 +7,7 @@ using Content.Server.GameObjects.Components.Weapon.Ranged.Ammunition; using Content.Shared.Audio; using Content.Shared.GameObjects.Components.Weapons.Ranged; using Content.Shared.Interfaces.GameObjects.Components; +using Content.Shared.Physics; using Robust.Server.GameObjects.EntitySystems; using Robust.Shared.Audio; using Robust.Shared.GameObjects.Components; @@ -380,7 +381,12 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels var projectileComponent = projectile.GetComponent(); projectileComponent.IgnoreEntity(shooter); - projectile.GetComponent().LinearVelocity = projectileAngle.ToVec() * velocity; + + projectile + .GetComponent() + .EnsureController() + .LinearVelocity = projectileAngle.ToVec() * velocity; + projectile.Transform.LocalRotation = projectileAngle.Theta; } } diff --git a/Content.Server/GameObjects/EntitySystems/MoverSystem.cs b/Content.Server/GameObjects/EntitySystems/MoverSystem.cs index 69bebc4e66..6caaf9523b 100644 --- a/Content.Server/GameObjects/EntitySystems/MoverSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/MoverSystem.cs @@ -78,11 +78,6 @@ namespace Content.Server.GameObjects.EntitySystems } } - protected override void SetController(IPhysicsComponent physics) - { - physics.SetController(); - } - private static void PlayerAttached(PlayerAttachSystemMessage ev) { if (!ev.Entity.HasComponent()) @@ -98,9 +93,10 @@ namespace Content.Server.GameObjects.EntitySystems ev.Entity.RemoveComponent(); } - if (ev.Entity.TryGetComponent(out IPhysicsComponent physics)) + if (ev.Entity.TryGetComponent(out IPhysicsComponent physics) && + physics.TryGetController(out MoverController controller)) { - (physics.Controller as MoverController)?.StopMoving(); + controller.StopMoving(); } } diff --git a/Content.Server/Throw/ThrowHelper.cs b/Content.Server/Throw/ThrowHelper.cs index 4609f69ee3..ba9ce46d7d 100644 --- a/Content.Server/Throw/ThrowHelper.cs +++ b/Content.Server/Throw/ThrowHelper.cs @@ -75,18 +75,14 @@ namespace Content.Server.Throw throwSourceEnt.Transform.LocalRotation = angle.GetCardinalDir().ToAngle(); } - if (!thrownEnt.TryGetComponent(out IPhysicsComponent physComp)) - physComp = thrownEnt.AddComponent(); + // scaling is handled elsewhere, this is just multiplying by 10 independent of timing as a fix until elsewhere values are updated + var spd = throwForce * 10; - var timing = IoCManager.Resolve(); + projComp.StartThrow(angle.ToVec(), spd); - // scaling is handled elsewhere, this is just multiplying by 60 independent of timing as a fix until elsewhere values are updated - var spd = throwForce * 60; - - projComp.StartThrow(angle.ToVec() * spd); - - if (throwSourceEnt != null && throwSourceEnt.TryGetComponent(out var physics) - && physics.Controller is MoverController mover) + if (throwSourceEnt != null && + throwSourceEnt.TryGetComponent(out var physics) && + physics.TryGetController(out MoverController mover)) { var physicsMgr = IoCManager.Resolve(); diff --git a/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs b/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs index 3ced9a135e..75abb9d902 100644 --- a/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs +++ b/Content.Shared/GameObjects/EntitySystems/SharedMoverSystem.cs @@ -57,11 +57,7 @@ namespace Content.Shared.GameObjects.EntitySystems protected void UpdateKinematics(ITransformComponent transform, IMoverComponent mover, IPhysicsComponent physics, ICollidableComponent? collider = null) { - if (physics.Controller == null) - { - // Set up controller - SetController(physics); - } + physics.EnsureController(); var weightless = !transform.Owner.HasComponent() && _physicsManager.IsWeightless(transform.GridPosition); @@ -82,7 +78,10 @@ namespace Content.Shared.GameObjects.EntitySystems var combined = walkDir + sprintDir; if (combined.LengthSquared < 0.001 || !ActionBlockerSystem.CanMove(mover.Owner) && !weightless) { - (physics.Controller as MoverController)?.StopMoving(); + if (physics.TryGetController(out MoverController controller)) + { + controller.StopMoving(); + } } else { @@ -90,7 +89,11 @@ namespace Content.Shared.GameObjects.EntitySystems if (weightless) { - (physics.Controller as MoverController)?.Push(combined, mover.CurrentPushSpeed); + if (physics.TryGetController(out MoverController controller)) + { + controller.Push(combined, mover.CurrentPushSpeed); + } + transform.LocalRotation = walkDir.GetDir().ToAngle(); return; } @@ -98,7 +101,11 @@ namespace Content.Shared.GameObjects.EntitySystems var total = walkDir * mover.CurrentWalkSpeed + sprintDir * mover.CurrentSprintSpeed; //Console.WriteLine($"{walkDir} ({mover.CurrentWalkSpeed}) + {sprintDir} ({mover.CurrentSprintSpeed}): {total}"); - (physics.Controller as MoverController)?.Move(total, 1); + {if (physics.TryGetController(out MoverController controller)) + { + controller.Move(total, 1); + }} + transform.LocalRotation = total.GetDir().ToAngle(); HandleFootsteps(mover); @@ -110,8 +117,6 @@ namespace Content.Shared.GameObjects.EntitySystems } - protected abstract void SetController(IPhysicsComponent physics); - private bool IsAroundCollider(ITransformComponent transform, IMoverComponent mover, ICollidableComponent collider) { diff --git a/Content.Shared/Physics/BulletController.cs b/Content.Shared/Physics/BulletController.cs new file mode 100644 index 0000000000..349b44bf11 --- /dev/null +++ b/Content.Shared/Physics/BulletController.cs @@ -0,0 +1,17 @@ +#nullable enable +using Robust.Shared.GameObjects.Components; +using Robust.Shared.Maths; +using Robust.Shared.Physics; + +namespace Content.Shared.Physics +{ + public class BulletController : VirtualController + { + public override ICollidableComponent? ControlledComponent { protected get; set; } + + public void Push(Vector2 velocityDirection, float speed) + { + LinearVelocity = velocityDirection * speed; + } + } +} diff --git a/Content.Shared/Physics/MoverController.cs b/Content.Shared/Physics/MoverController.cs index fb0b204ec0..bf4372a3ca 100644 --- a/Content.Shared/Physics/MoverController.cs +++ b/Content.Shared/Physics/MoverController.cs @@ -10,60 +10,27 @@ namespace Content.Shared.Physics { public class MoverController : VirtualController { - private Vector2 _velocity; - private ICollidableComponent? _component; - - public Vector2 Velocity - { - get => _velocity; - set => _velocity = value; - } - - public override ICollidableComponent? ControlledComponent - { - set => _component = value; - } - - public MoverController() - { - _velocity = Vector2.Zero; - } + public override ICollidableComponent? ControlledComponent { protected get; set; } public void Move(Vector2 velocityDirection, float speed) { - if (_component?.Owner.HasComponent() == false - && IoCManager.Resolve().IsWeightless(_component.Owner.Transform.GridPosition)) return; + if (ControlledComponent?.Owner.HasComponent() == false + && IoCManager.Resolve().IsWeightless(ControlledComponent.Owner.Transform.GridPosition)) + { + return; + } + Push(velocityDirection, speed); } public void Push(Vector2 velocityDirection, float speed) { - Velocity = velocityDirection * speed; + LinearVelocity = velocityDirection * speed; } public void StopMoving() { - Velocity = Vector2.Zero; - } - - public override void UpdateBeforeProcessing() - { - base.UpdateBeforeProcessing(); - - if (_component == null) - { - return; - } - - if (Velocity == Vector2.Zero) - { - // Try to stop movement - _component.LinearVelocity = Vector2.Zero; - } - else - { - _component.LinearVelocity = Velocity; - } + LinearVelocity = Vector2.Zero; } } } diff --git a/Content.Shared/Physics/ShuttleController.cs b/Content.Shared/Physics/ShuttleController.cs new file mode 100644 index 0000000000..8a95fbec33 --- /dev/null +++ b/Content.Shared/Physics/ShuttleController.cs @@ -0,0 +1,17 @@ +#nullable enable +using Robust.Shared.GameObjects.Components; +using Robust.Shared.Maths; +using Robust.Shared.Physics; + +namespace Content.Shared.Physics +{ + public class ShuttleController : VirtualController + { + public override ICollidableComponent? ControlledComponent { protected get; set; } + + public void Push(Vector2 velocityDirection, float speed) + { + LinearVelocity = velocityDirection * speed; + } + } +} diff --git a/Content.Shared/Physics/ThrownController.cs b/Content.Shared/Physics/ThrownController.cs new file mode 100644 index 0000000000..8b2c39a9df --- /dev/null +++ b/Content.Shared/Physics/ThrownController.cs @@ -0,0 +1,17 @@ +#nullable enable +using Robust.Shared.GameObjects.Components; +using Robust.Shared.Maths; +using Robust.Shared.Physics; + +namespace Content.Shared.Physics +{ + public class ThrownController : VirtualController + { + public override ICollidableComponent? ControlledComponent { protected get; set; } + + public void Push(Vector2 velocityDirection, float speed) + { + LinearVelocity = velocityDirection * speed; + } + } +}