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.
This commit is contained in:
Acruid
2020-01-08 15:17:00 -08:00
parent 8a49546add
commit f73824adcb
6 changed files with 157 additions and 9 deletions

View File

@@ -84,5 +84,7 @@ namespace Content.Server.GameObjects.Components.Movement
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public float StepSoundDistance { get; set; } public float StepSoundDistance { get; set; }
public void SetVelocityDirection(Direction direction, bool enabled) { }
} }
} }

View File

@@ -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<PhysicsComponent>();
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;
}
/// <inheritdoc />
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();
}
}
}
}

View File

@@ -15,6 +15,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Input; using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components; using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Random; using Robust.Shared.Interfaces.Random;
@@ -81,16 +82,18 @@ namespace Content.Server.GameObjects.EntitySystems
private static void PlayerAttached(object sender, PlayerAttachSystemMessage ev) private static void PlayerAttached(object sender, PlayerAttachSystemMessage ev)
{ {
if (ev.Entity.HasComponent<IMoverComponent>()) if (!ev.Entity.HasComponent<IMoverComponent>())
{ {
ev.Entity.RemoveComponent<IMoverComponent>(); ev.Entity.AddComponent<PlayerInputMoverComponent>();
} }
ev.Entity.AddComponent<PlayerInputMoverComponent>();
} }
private static void PlayerDetached(object sender, PlayerDetachedSystemMessage ev) private static void PlayerDetached(object sender, PlayerDetachedSystemMessage ev)
{ {
ev.Entity.RemoveComponent<PlayerInputMoverComponent>(); if(ev.Entity.HasComponent<PlayerInputMoverComponent>())
{
ev.Entity.RemoveComponent<PlayerInputMoverComponent>();
}
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -174,7 +177,7 @@ namespace Content.Server.GameObjects.EntitySystems
private static void HandleDirChange(ICommonSession session, Direction dir, bool state) 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; return;
moverComp.SetVelocityDirection(dir, state); moverComp.SetVelocityDirection(dir, state);
@@ -189,7 +192,7 @@ namespace Content.Server.GameObjects.EntitySystems
} }
private static bool TryGetAttachedComponent<T>(IPlayerSession session, out T component) private static bool TryGetAttachedComponent<T>(IPlayerSession session, out T component)
where T: Component where T: IComponent
{ {
component = default; component = default;

View File

@@ -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.Map;
using Robust.Shared.Maths; using Robust.Shared.Maths;
@@ -31,5 +32,14 @@ namespace Content.Server.Interfaces.GameObjects.Components.Movement
GridCoordinates LastPosition { get; set; } GridCoordinates LastPosition { get; set; }
float StepSoundDistance { get; set; } float StepSoundDistance { get; set; }
/// <summary>
/// Toggles one of the four cardinal directions. Each of the four directions are
/// composed into a single direction vector, <see cref="PlayerInputMoverComponent.VelocityDir"/>. Enabling
/// opposite directions will cancel each other out, resulting in no direction.
/// </summary>
/// <param name="direction">Direction to toggle.</param>
/// <param name="enabled">If the direction is active.</param>
void SetVelocityDirection(Direction direction, bool enabled);
} }
} }

View File

@@ -22,4 +22,7 @@
- type: Damageable - type: Damageable
- type: Destructible - type: Destructible
thresholdvalue: 100 thresholdvalue: 100
- type: Physics
- type: ShuttleController