From f73824adcb2804c60283aada633dd8dd662ab138 Mon Sep 17 00:00:00 2001 From: Acruid Date: Wed, 8 Jan 2020 15:17:00 -0800 Subject: [PATCH] Prevents the MoverSystem from overwriting the MoverComponent on an entity. Adds the new ShuttleControllerComponent, a custom IMoverComponent that moves the parent grid when controlled by a mind. --- .../Movement/AiControllerComponent.cs | 2 + .../Movement/ShuttleControllerComponent.cs | 130 ++++++++++++++++++ .../GameObjects/EntitySystems/MoverSystem.cs | 15 +- .../Components/Movement/IMoverComponent.cs | 12 +- .../Prototypes/Entities/buildings/shuttle.yml | 5 +- RobustToolbox | 2 +- 6 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs diff --git a/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs b/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs index 06ccbc734d..d48a78b198 100644 --- a/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs +++ b/Content.Server/GameObjects/Components/Movement/AiControllerComponent.cs @@ -84,5 +84,7 @@ namespace Content.Server.GameObjects.Components.Movement [ViewVariables(VVAccess.ReadWrite)] public float StepSoundDistance { get; set; } + + public void SetVelocityDirection(Direction direction, bool enabled) { } } } diff --git a/Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs b/Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs new file mode 100644 index 0000000000..27e72eeddd --- /dev/null +++ b/Content.Server/GameObjects/Components/Movement/ShuttleControllerComponent.cs @@ -0,0 +1,130 @@ +using Content.Server.GameObjects.Components.Mobs; +using Content.Server.Interfaces.GameObjects.Components.Movement; +using Robust.Server.GameObjects; +using Robust.Server.GameObjects.Components.Container; +using Robust.Server.Interfaces.GameObjects; +using Robust.Server.Interfaces.Player; +using Robust.Shared.GameObjects; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Interfaces.Map; +using Robust.Shared.Interfaces.Network; +using Robust.Shared.Interfaces.Timing; +using Robust.Shared.IoC; +using Robust.Shared.Log; +using Robust.Shared.Map; +using Robust.Shared.Maths; +using Robust.Shared.ViewVariables; + +namespace Content.Server.GameObjects.Components.Movement +{ + [RegisterComponent] + [ComponentReference(typeof(IMoverComponent))] + class ShuttleControllerComponent : Component, IMoverComponent + { + [Dependency] IMapManager mapManager; + [Dependency] IEntityManager entityManager; + [Dependency] IGameTiming gameTiming; + + private bool _movingUp; + private bool _movingDown; + private bool _movingLeft; + private bool _movingRight; + + public override string Name => "ShuttleController"; + + [ViewVariables(VVAccess.ReadWrite)] + public float WalkMoveSpeed { get; set; } = 8; + public float SprintMoveSpeed { get; set; } + public bool Sprinting { get; set; } + public Vector2 VelocityDir { get; } + public GridCoordinates LastPosition { get; set; } + public float StepSoundDistance { get; set; } + + public void SetVelocityDirection(Direction direction, bool enabled) + { + var gridId = Owner.Transform.GridID; + + if (mapManager.TryGetGrid(gridId, out var grid) && entityManager.TryGetEntity(grid.GridEntityId, out var gridEntity)) + { + //TODO: Switch to shuttle component + if (!gridEntity.TryGetComponent(out PhysicsComponent physComp)) + { + physComp = gridEntity.AddComponent(); + physComp.Mass = 1; + } + + physComp.LinearVelocity = CalcNewVelocity(direction, enabled) * WalkMoveSpeed; + } + } + + private Vector2 CalcNewVelocity(Direction direction, bool enabled) + { + switch (direction) + { + case Direction.East: + _movingRight = enabled; + break; + case Direction.North: + _movingUp = enabled; + break; + case Direction.West: + _movingLeft = enabled; + break; + case Direction.South: + _movingDown = enabled; + break; + } + + // key directions are in screen coordinates + // _moveDir is in world coordinates + // if the camera is moved, this needs to be changed + + var x = 0; + x -= _movingLeft ? 1 : 0; + x += _movingRight ? 1 : 0; + + var y = 0; + y -= _movingDown ? 1 : 0; + y += _movingUp ? 1 : 0; + + var result = new Vector2(x, y); + + // can't normalize zero length vector + if (result.LengthSquared > 1.0e-6) + result = result.Normalized; + + return result; + } + + /// + public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null, IComponent component = null) + { + base.HandleMessage(message, netChannel, component); + + if(netChannel != null) + return; + + switch (message) + { + case ContainerContentsModifiedMessage contents: + if(contents.Entity.TryGetComponent(out MindComponent mindComp)) + ContentsChanged(contents.Entity, mindComp, contents.Removed); + break; + } + } + + private void ContentsChanged(IEntity entity, MindComponent mindComp, in bool removed) + { + Logger.DebugS("shuttle", $"Pilot={entity.Name}, removed={removed}"); + + if (!removed) + { + mindComp.Mind.Visit(Owner); + } + else + { + mindComp.Mind.UnVisit(); + } + } + } +} diff --git a/Content.Server/GameObjects/EntitySystems/MoverSystem.cs b/Content.Server/GameObjects/EntitySystems/MoverSystem.cs index b416e32b18..1c3b608770 100644 --- a/Content.Server/GameObjects/EntitySystems/MoverSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/MoverSystem.cs @@ -15,6 +15,7 @@ using Robust.Shared.GameObjects; using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Systems; using Robust.Shared.Input; +using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects.Components; using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Random; @@ -81,16 +82,18 @@ namespace Content.Server.GameObjects.EntitySystems private static void PlayerAttached(object sender, PlayerAttachSystemMessage ev) { - if (ev.Entity.HasComponent()) + if (!ev.Entity.HasComponent()) { - ev.Entity.RemoveComponent(); + ev.Entity.AddComponent(); } - ev.Entity.AddComponent(); } private static void PlayerDetached(object sender, PlayerDetachedSystemMessage ev) { - ev.Entity.RemoveComponent(); + if(ev.Entity.HasComponent()) + { + ev.Entity.RemoveComponent(); + } } /// @@ -174,7 +177,7 @@ namespace Content.Server.GameObjects.EntitySystems private static void HandleDirChange(ICommonSession session, Direction dir, bool state) { - if(!TryGetAttachedComponent(session as IPlayerSession, out PlayerInputMoverComponent moverComp)) + if(!TryGetAttachedComponent(session as IPlayerSession, out IMoverComponent moverComp)) return; moverComp.SetVelocityDirection(dir, state); @@ -189,7 +192,7 @@ namespace Content.Server.GameObjects.EntitySystems } private static bool TryGetAttachedComponent(IPlayerSession session, out T component) - where T: Component + where T: IComponent { component = default; diff --git a/Content.Server/Interfaces/GameObjects/Components/Movement/IMoverComponent.cs b/Content.Server/Interfaces/GameObjects/Components/Movement/IMoverComponent.cs index a8b902f26c..d1f5cb9597 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Movement/IMoverComponent.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Movement/IMoverComponent.cs @@ -1,4 +1,5 @@ -using Robust.Shared.Interfaces.GameObjects; +using Content.Server.GameObjects.Components.Movement; +using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Map; using Robust.Shared.Maths; @@ -31,5 +32,14 @@ namespace Content.Server.Interfaces.GameObjects.Components.Movement GridCoordinates LastPosition { get; set; } float StepSoundDistance { get; set; } + + /// + /// Toggles one of the four cardinal directions. Each of the four directions are + /// composed into a single direction vector, . Enabling + /// opposite directions will cancel each other out, resulting in no direction. + /// + /// Direction to toggle. + /// If the direction is active. + void SetVelocityDirection(Direction direction, bool enabled); } } diff --git a/Resources/Prototypes/Entities/buildings/shuttle.yml b/Resources/Prototypes/Entities/buildings/shuttle.yml index c879bfc02b..05561c64dc 100644 --- a/Resources/Prototypes/Entities/buildings/shuttle.yml +++ b/Resources/Prototypes/Entities/buildings/shuttle.yml @@ -22,4 +22,7 @@ - type: Damageable - type: Destructible - thresholdvalue: 100 \ No newline at end of file + thresholdvalue: 100 + + - type: Physics + - type: ShuttleController \ No newline at end of file diff --git a/RobustToolbox b/RobustToolbox index 8d30bcb041..150c899f6f 160000 --- a/RobustToolbox +++ b/RobustToolbox @@ -1 +1 @@ -Subproject commit 8d30bcb041a815a5a740d2a56d5007bf5ed2c105 +Subproject commit 150c899f6ffd405517348093a3d2371d8fe15dde