Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
using Content.Server.Interfaces.GameObjects.Components.Movement;
|
||||
using Robust.Server.AI;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Movement
|
||||
{
|
||||
[RegisterComponent]
|
||||
[RegisterComponent, ComponentReference(typeof(IMoverComponent))]
|
||||
public class AiControllerComponent : Component, IMoverComponent
|
||||
{
|
||||
private string _logicName;
|
||||
@@ -13,15 +17,37 @@ namespace Content.Server.GameObjects.Components.Movement
|
||||
|
||||
public override string Name => "AiController";
|
||||
|
||||
public string LogicName => _logicName;
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public string LogicName
|
||||
{
|
||||
get => _logicName;
|
||||
set
|
||||
{
|
||||
_logicName = value;
|
||||
Processor = null;
|
||||
}
|
||||
}
|
||||
|
||||
public AiLogicProcessor Processor { get; set; }
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float VisionRadius
|
||||
{
|
||||
get => _visionRadius;
|
||||
set => _visionRadius = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
// This component requires a physics component.
|
||||
if (!Owner.HasComponent<PhysicsComponent>())
|
||||
Owner.AddComponent<PhysicsComponent>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
@@ -29,5 +55,34 @@ namespace Content.Server.GameObjects.Components.Movement
|
||||
serializer.DataField(ref _logicName, "logic", null);
|
||||
serializer.DataField(ref _visionRadius, "vision", 8.0f);
|
||||
}
|
||||
|
||||
/// <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; set; }
|
||||
|
||||
public GridCoordinates LastPosition { get; set; }
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float StepSoundDistance { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.Components.Movement;
|
||||
using Content.Server.Interfaces.GameObjects.Components.Movement;
|
||||
using Robust.Server.AI;
|
||||
using Robust.Server.Interfaces.Console;
|
||||
using Robust.Server.Interfaces.Player;
|
||||
using Robust.Server.Interfaces.Timing;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Reflection;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
@@ -12,17 +16,23 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
internal class AiSystem : EntitySystem
|
||||
{
|
||||
private readonly Dictionary<string, Type> _processorTypes = new Dictionary<string, Type>();
|
||||
private IPauseManager _pauseManager;
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IPauseManager _pauseManager;
|
||||
[Dependency] private readonly IDynamicTypeFactory _typeFactory;
|
||||
[Dependency] private readonly IReflectionManager _reflectionManager;
|
||||
#pragma warning restore 649
|
||||
|
||||
public AiSystem()
|
||||
private readonly Dictionary<string, Type> _processorTypes = new Dictionary<string, Type>();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
// register entity query
|
||||
EntityQuery = new TypeEntityQuery(typeof(AiControllerComponent));
|
||||
_pauseManager = IoCManager.Resolve<IPauseManager>();
|
||||
|
||||
var reflectionMan = IoCManager.Resolve<IReflectionManager>();
|
||||
var processors = reflectionMan.GetAllChildren<AiLogicProcessor>();
|
||||
var processors = _reflectionManager.GetAllChildren<AiLogicProcessor>();
|
||||
foreach (var processor in processors)
|
||||
{
|
||||
var att = (AiLogicProcessorAttribute)Attribute.GetCustomAttribute(processor, typeof(AiLogicProcessorAttribute));
|
||||
@@ -33,6 +43,7 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
var entities = EntityManager.GetEntities(EntityQuery);
|
||||
@@ -61,11 +72,45 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
if (_processorTypes.TryGetValue(name, out var type))
|
||||
{
|
||||
return (AiLogicProcessor)Activator.CreateInstance(type);
|
||||
return (AiLogicProcessor)_typeFactory.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));
|
||||
}
|
||||
|
||||
private class AddAiCommand : IClientCommand
|
||||
{
|
||||
public string Command => "addai";
|
||||
public string Description => "Add an ai component with a given processor to an entity.";
|
||||
public string Help => "addai <processorId> <entityId>";
|
||||
public void Execute(IConsoleShell shell, IPlayerSession player, string[] args)
|
||||
{
|
||||
if(args.Length != 2)
|
||||
{
|
||||
shell.SendText(player, "Wrong number of args.");
|
||||
return;
|
||||
}
|
||||
|
||||
var processorId = args[0];
|
||||
var entId = new EntityUid(int.Parse(args[1]));
|
||||
var ent = IoCManager.Resolve<IEntityManager>().GetEntity(entId);
|
||||
|
||||
if (ent.HasComponent<AiControllerComponent>())
|
||||
{
|
||||
shell.SendText(player, "Entity already has an AI component.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ent.HasComponent<IMoverComponent>())
|
||||
{
|
||||
ent.RemoveComponent<IMoverComponent>();
|
||||
}
|
||||
|
||||
var comp = ent.AddComponent<AiControllerComponent>();
|
||||
comp.LogicName = processorId;
|
||||
shell.SendText(player, "AI component added.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ using Robust.Shared.Players;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Content.Server.GameObjects.Components.Sound;
|
||||
using Content.Shared.GameObjects.Components.Inventory;
|
||||
using Robust.Shared.Log;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
@@ -45,7 +46,7 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
EntityQuery = new TypeEntityQuery(typeof(PlayerInputMoverComponent));
|
||||
EntityQuery = new TypeEntityQuery(typeof(IMoverComponent));
|
||||
|
||||
var moveUpCmdHandler = InputCmdHandler.FromDelegate(
|
||||
session => HandleDirChange(session, Direction.North, true),
|
||||
@@ -116,14 +117,14 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var mover = entity.GetComponent<PlayerInputMoverComponent>();
|
||||
var mover = entity.GetComponent<IMoverComponent>();
|
||||
var physics = entity.GetComponent<PhysicsComponent>();
|
||||
|
||||
UpdateKinematics(entity.Transform, mover, physics);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateKinematics(ITransformComponent transform, PlayerInputMoverComponent mover, PhysicsComponent physics)
|
||||
private void UpdateKinematics(ITransformComponent transform, IMoverComponent mover, PhysicsComponent physics)
|
||||
{
|
||||
if (mover.VelocityDir.LengthSquared < 0.001 || !ActionBlockerSystem.CanMove(mover.Owner))
|
||||
{
|
||||
@@ -234,9 +235,17 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
}
|
||||
|
||||
// Ok well we know the position of the
|
||||
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
|
||||
var file = _footstepRandom.Pick(soundCollection.PickFiles);
|
||||
_audioSystem.Play(file, coordinates);
|
||||
try
|
||||
{
|
||||
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
|
||||
var file = _footstepRandom.Pick(soundCollection.PickFiles);
|
||||
_audioSystem.Play(file, coordinates);
|
||||
}
|
||||
catch (UnknownPrototypeException)
|
||||
{
|
||||
// Shouldn't crash over a sound
|
||||
Logger.ErrorS("sound", $"Unable to find sound collection for {soundCollectionName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user