Move movement to client.
This commit is contained in:
@@ -110,6 +110,9 @@ namespace Content.Client
|
||||
|
||||
factory.Register<SubFloorHideComponent>();
|
||||
|
||||
factory.RegisterIgnore("AiController");
|
||||
factory.RegisterIgnore("PlayerInputMover");
|
||||
|
||||
IoCManager.Register<IClientNotifyManager, ClientNotifyManager>();
|
||||
IoCManager.Register<ISharedNotifyManager, ClientNotifyManager>();
|
||||
IoCManager.Register<IClientGameTicker, ClientGameTicker>();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Interfaces.GameObjects.Components.Movement;
|
||||
using SS14.Server.AI;
|
||||
using SS14.Server.GameObjects;
|
||||
using SS14.Server.Interfaces.GameObjects;
|
||||
|
||||
@@ -87,6 +87,8 @@
|
||||
<Compile Include="GameObjects\Components\Mobs\DamageThresholdTemplates\HumanTemplate.cs" />
|
||||
<Compile Include="GameObjects\Components\Mobs\MindComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Mobs\SpeciesComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Movement\AiControllerComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Movement\PlayerInputMoverComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Power\LightBulbComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Power\PowerCellComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Power\PowerStorageComponent.cs" />
|
||||
@@ -110,11 +112,13 @@
|
||||
<Compile Include="GameObjects\Components\Weapon\Ranged\RangedWeapon.cs" />
|
||||
<Compile Include="GameObjects\ContainerSlot.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\ActionBlockerSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\AiSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\Click\ExamineSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\Click\InteractionSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\DoorSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\HandHeldLightSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\HandsSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\MoverSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\PowerApcSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\PowerSmesSystem.cs" />
|
||||
<Compile Include="GameObjects\EntitySystems\PowerSystem.cs" />
|
||||
@@ -132,6 +136,7 @@
|
||||
<Compile Include="GameObjects\Components\Damage\DestructibleComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Damage\ResistanceSet.cs" />
|
||||
<Compile Include="GameObjects\Components\Temperature\TemperatureComponent.cs" />
|
||||
<Compile Include="Interfaces\GameObjects\Components\Movement\IMoverComponent.cs" />
|
||||
<Compile Include="Interfaces\GameObjects\IOnDamageBehavior.cs" />
|
||||
<Compile Include="Interfaces\GameTicking\IGameTicker.cs" />
|
||||
<Compile Include="Interfaces\IServerNotifyManager.cs" />
|
||||
@@ -188,5 +193,4 @@
|
||||
<Compile Include="GameObjects\Components\Construction\ConstructorComponent.cs" />
|
||||
<Compile Include="GameObjects\Components\Construction\ConstructionComponent.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
</Project>
|
||||
|
||||
@@ -47,6 +47,8 @@ using Content.Shared.Interfaces;
|
||||
using SS14.Server.Interfaces.ServerStatus;
|
||||
using SS14.Shared.Timing;
|
||||
using Content.Server.GameObjects.Components.Destructible;
|
||||
using Content.Server.GameObjects.Components.Movement;
|
||||
using Content.Server.Interfaces.GameObjects.Components.Movement;
|
||||
|
||||
namespace Content.Server
|
||||
{
|
||||
@@ -143,6 +145,11 @@ namespace Content.Server
|
||||
factory.RegisterIgnore("IconSmooth");
|
||||
factory.RegisterIgnore("SubFloorHide");
|
||||
|
||||
factory.Register<PlayerInputMoverComponent>();
|
||||
factory.RegisterReference<PlayerInputMoverComponent, IMoverComponent>();
|
||||
|
||||
factory.Register<AiControllerComponent>();
|
||||
|
||||
IoCManager.Register<ISharedNotifyManager, ServerNotifyManager>();
|
||||
IoCManager.Register<IServerNotifyManager, ServerNotifyManager>();
|
||||
IoCManager.Register<IGameTicker, GameTicker>();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.GameObjects.Components.Movement;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Shared.GameObjects.Components.Mobs;
|
||||
using SS14.Server.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using Content.Server.Interfaces.GameObjects.Components.Movement;
|
||||
using SS14.Server.AI;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Serialization;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Movement
|
||||
{
|
||||
public class AiControllerComponent : Component, IMoverComponent
|
||||
{
|
||||
private string _logicName;
|
||||
private float _visionRadius;
|
||||
|
||||
public override string Name => "AiController";
|
||||
|
||||
public string LogicName => _logicName;
|
||||
public AiLogicProcessor Processor { get; set; }
|
||||
|
||||
public float VisionRadius
|
||||
{
|
||||
get => _visionRadius;
|
||||
set => _visionRadius = value;
|
||||
}
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
serializer.DataField(ref _logicName, "logic", null);
|
||||
serializer.DataField(ref _visionRadius, "vision", 8.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using Content.Server.Interfaces.GameObjects.Components.Movement;
|
||||
using SS14.Server.GameObjects;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Log;
|
||||
using SS14.Shared.Maths;
|
||||
using SS14.Shared.Serialization;
|
||||
using SS14.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Movement
|
||||
{
|
||||
/// <summary>
|
||||
/// Moves the entity based on input from a KeyBindingInputComponent.
|
||||
/// </summary>
|
||||
public class PlayerInputMoverComponent : Component, IMoverComponent
|
||||
{
|
||||
private bool _movingUp;
|
||||
private bool _movingDown;
|
||||
private bool _movingLeft;
|
||||
private bool _movingRight;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Name => "PlayerInputMover";
|
||||
|
||||
/// <summary>
|
||||
/// Movement speed (m/s) that the entity walks.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float WalkMoveSpeed { get; set; } = 4.0f;
|
||||
|
||||
/// <summary>
|
||||
/// Movement speed (m/s) that the entity sprints.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float SprintMoveSpeed { get; set; } = 10.0f;
|
||||
|
||||
/// <summary>
|
||||
/// Is the entity Sprinting (running)?
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public bool Sprinting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Calculated linear velocity direction of the entity.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public Vector2 VelocityDir { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Blocks entity's movement
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public bool Disabled { get; set; } = false;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnAdd()
|
||||
{
|
||||
// This component requires that the entity has a PhysicsComponent.
|
||||
if (!Owner.HasComponent<PhysicsComponent>())
|
||||
Logger.Error($"[ECS] {Owner.Prototype.Name} - {nameof(PlayerInputMoverComponent)} requires {nameof(PhysicsComponent)}. ");
|
||||
|
||||
base.OnAdd();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
serializer.DataReadWriteFunction("wspd", 4.0f, value => WalkMoveSpeed = value, () => WalkMoveSpeed);
|
||||
serializer.DataReadWriteFunction("rspd", 10.0f, value => SprintMoveSpeed = value, () => SprintMoveSpeed);
|
||||
|
||||
// The velocity and moving directions is usually set from player or AI input,
|
||||
// so we don't want to save/load these derived fields.
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggles one of the four cardinal directions. Each of the four directions are
|
||||
/// composed into a single direction vector, <see cref="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>
|
||||
public void SetVelocityDirection(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;
|
||||
|
||||
VelocityDir = new Vector2(x, y);
|
||||
|
||||
// can't normalize zero length vector
|
||||
if (VelocityDir.LengthSquared > 1.0e-6)
|
||||
VelocityDir = VelocityDir.Normalized;
|
||||
}
|
||||
}
|
||||
}
|
||||
71
Content.Server/GameObjects/EntitySystems/AiSystem.cs
Normal file
71
Content.Server/GameObjects/EntitySystems/AiSystem.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.Components.Movement;
|
||||
using SS14.Server.AI;
|
||||
using SS14.Server.Interfaces.Timing;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.GameObjects.Systems;
|
||||
using SS14.Shared.Interfaces.Reflection;
|
||||
using SS14.Shared.IoC;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
internal class AiSystem : EntitySystem
|
||||
{
|
||||
private readonly Dictionary<string, Type> _processorTypes = new Dictionary<string, Type>();
|
||||
private IPauseManager _pauseManager;
|
||||
|
||||
public AiSystem()
|
||||
{
|
||||
// register entity query
|
||||
EntityQuery = new TypeEntityQuery(typeof(AiControllerComponent));
|
||||
_pauseManager = IoCManager.Resolve<IPauseManager>();
|
||||
|
||||
var reflectionMan = IoCManager.Resolve<IReflectionManager>();
|
||||
var processors = reflectionMan.GetAllChildren<AiLogicProcessor>();
|
||||
foreach (var processor in processors)
|
||||
{
|
||||
var att = (AiLogicProcessorAttribute)Attribute.GetCustomAttribute(processor, typeof(AiLogicProcessorAttribute));
|
||||
if (att != null)
|
||||
{
|
||||
_processorTypes.Add(att.SerializeName, processor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
var entities = EntityManager.GetEntities(EntityQuery);
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
if (_pauseManager.IsEntityPaused(entity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var aiComp = entity.GetComponent<AiControllerComponent>();
|
||||
if (aiComp.Processor == null)
|
||||
{
|
||||
aiComp.Processor = CreateProcessor(aiComp.LogicName);
|
||||
aiComp.Processor.SelfEntity = entity;
|
||||
aiComp.Processor.VisionRadius = aiComp.VisionRadius;
|
||||
}
|
||||
|
||||
var processor = aiComp.Processor;
|
||||
|
||||
processor.Update(frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
private AiLogicProcessor CreateProcessor(string name)
|
||||
{
|
||||
if (_processorTypes.TryGetValue(name, out var type))
|
||||
{
|
||||
return (AiLogicProcessor)Activator.CreateInstance(type);
|
||||
}
|
||||
|
||||
// processor needs to inherit AiLogicProcessor, and needs an AiLogicProcessorAttribute to define the YAML name
|
||||
throw new ArgumentException($"Processor type {name} could not be found.", nameof(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
153
Content.Server/GameObjects/EntitySystems/MoverSystem.cs
Normal file
153
Content.Server/GameObjects/EntitySystems/MoverSystem.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using Content.Server.GameObjects.Components.Movement;
|
||||
using Content.Server.Interfaces.GameObjects.Components.Movement;
|
||||
using JetBrains.Annotations;
|
||||
using SS14.Server.GameObjects;
|
||||
using SS14.Server.GameObjects.EntitySystems;
|
||||
using SS14.Server.Interfaces.Player;
|
||||
using SS14.Server.Interfaces.Timing;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.GameObjects.Systems;
|
||||
using SS14.Shared.Input;
|
||||
using SS14.Shared.Interfaces.GameObjects.Components;
|
||||
using SS14.Shared.IoC;
|
||||
using SS14.Shared.Maths;
|
||||
using SS14.Shared.Players;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal class MoverSystem : EntitySystem
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency]
|
||||
private IPauseManager _pauseManager;
|
||||
#pragma warning restore 649
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
EntityQuery = new TypeEntityQuery(typeof(PlayerInputMoverComponent));
|
||||
|
||||
var moveUpCmdHandler = InputCmdHandler.FromDelegate(
|
||||
session => HandleDirChange(session, Direction.North, true),
|
||||
session => HandleDirChange(session, Direction.North, false));
|
||||
var moveLeftCmdHandler = InputCmdHandler.FromDelegate(
|
||||
session => HandleDirChange(session, Direction.West, true),
|
||||
session => HandleDirChange(session, Direction.West, false));
|
||||
var moveRightCmdHandler = InputCmdHandler.FromDelegate(
|
||||
session => HandleDirChange(session, Direction.East, true),
|
||||
session => HandleDirChange(session, Direction.East, false));
|
||||
var moveDownCmdHandler = InputCmdHandler.FromDelegate(
|
||||
session => HandleDirChange(session, Direction.South, true),
|
||||
session => HandleDirChange(session, Direction.South, false));
|
||||
var runCmdHandler = InputCmdHandler.FromDelegate(
|
||||
session => HandleRunChange(session, true),
|
||||
session => HandleRunChange(session, false));
|
||||
|
||||
var input = EntitySystemManager.GetEntitySystem<InputSystem>();
|
||||
|
||||
input.BindMap.BindFunction(EngineKeyFunctions.MoveUp, moveUpCmdHandler);
|
||||
input.BindMap.BindFunction(EngineKeyFunctions.MoveLeft, moveLeftCmdHandler);
|
||||
input.BindMap.BindFunction(EngineKeyFunctions.MoveRight, moveRightCmdHandler);
|
||||
input.BindMap.BindFunction(EngineKeyFunctions.MoveDown, moveDownCmdHandler);
|
||||
input.BindMap.BindFunction(EngineKeyFunctions.Run, runCmdHandler);
|
||||
|
||||
SubscribeEvent<PlayerAttachSystemMessage>(PlayerAttached);
|
||||
SubscribeEvent<PlayerDetachedSystemMessage>(PlayerDetached);
|
||||
}
|
||||
|
||||
private static void PlayerAttached(object sender, PlayerAttachSystemMessage ev)
|
||||
{
|
||||
if (ev.Entity.HasComponent<IMoverComponent>())
|
||||
{
|
||||
ev.Entity.RemoveComponent<IMoverComponent>();
|
||||
}
|
||||
ev.Entity.AddComponent<PlayerInputMoverComponent>();
|
||||
}
|
||||
|
||||
private static void PlayerDetached(object sender, PlayerDetachedSystemMessage ev)
|
||||
{
|
||||
ev.Entity.RemoveComponent<PlayerInputMoverComponent>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Shutdown()
|
||||
{
|
||||
if (EntitySystemManager.TryGetEntitySystem(out InputSystem input))
|
||||
{
|
||||
input.BindMap.UnbindFunction(EngineKeyFunctions.MoveUp);
|
||||
input.BindMap.UnbindFunction(EngineKeyFunctions.MoveLeft);
|
||||
input.BindMap.UnbindFunction(EngineKeyFunctions.MoveRight);
|
||||
input.BindMap.UnbindFunction(EngineKeyFunctions.MoveDown);
|
||||
input.BindMap.UnbindFunction(EngineKeyFunctions.Run);
|
||||
}
|
||||
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var entity in RelevantEntities)
|
||||
{
|
||||
if (_pauseManager.IsEntityPaused(entity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var mover = entity.GetComponent<PlayerInputMoverComponent>();
|
||||
var physics = entity.GetComponent<PhysicsComponent>();
|
||||
|
||||
UpdateKinematics(entity.Transform, mover, physics);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateKinematics(ITransformComponent transform, PlayerInputMoverComponent mover, PhysicsComponent physics)
|
||||
{
|
||||
if (mover.VelocityDir.LengthSquared < 0.001 || mover.Disabled)
|
||||
{
|
||||
if (physics.LinearVelocity != Vector2.Zero)
|
||||
physics.LinearVelocity = Vector2.Zero;
|
||||
}
|
||||
else
|
||||
{
|
||||
physics.LinearVelocity = mover.VelocityDir * (mover.Sprinting ? mover.SprintMoveSpeed : mover.WalkMoveSpeed);
|
||||
transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle();
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleDirChange(ICommonSession session, Direction dir, bool state)
|
||||
{
|
||||
if(!TryGetAttachedComponent(session as IPlayerSession, out PlayerInputMoverComponent moverComp))
|
||||
return;
|
||||
|
||||
moverComp.SetVelocityDirection(dir, state);
|
||||
}
|
||||
|
||||
private static void HandleRunChange(ICommonSession session, bool running)
|
||||
{
|
||||
if(!TryGetAttachedComponent(session as IPlayerSession, out PlayerInputMoverComponent moverComp))
|
||||
return;
|
||||
|
||||
moverComp.Sprinting = running;
|
||||
}
|
||||
|
||||
private static bool TryGetAttachedComponent<T>(IPlayerSession session, out T component)
|
||||
where T: Component
|
||||
{
|
||||
component = default;
|
||||
|
||||
var ent = session.AttachedEntity;
|
||||
|
||||
if (ent == null || !ent.IsValid())
|
||||
return false;
|
||||
|
||||
if (!ent.TryGetComponent(out T comp))
|
||||
return false;
|
||||
|
||||
component = comp;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Server.Interfaces.GameObjects.Components.Movement
|
||||
{
|
||||
// Does nothing except ensure uniqueness between mover components.
|
||||
// There can only be one.
|
||||
public interface IMoverComponent : IComponent
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Submodule RobustToolbox updated: 9ff6a7ec02...fa15950b91
Reference in New Issue
Block a user