Files
tbd-station-14/Content.Server/Physics/Controllers/RandomWalkController.cs
Chief-Engineer 485a2fd432 Add GHOST GANG! (#13734)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2023-05-28 19:21:06 +10:00

92 lines
3.9 KiB
C#

using Robust.Server.GameObjects;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Controllers;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Content.Server.Physics.Components;
using Content.Shared.Follower.Components;
using Content.Shared.Throwing;
namespace Content.Server.Physics.Controllers;
/// <summary>
/// The entity system responsible for managing <see cref="RandomWalkComponent"/>s.
/// Handles updating the direction they move in when their cooldown elapses.
/// </summary>
internal sealed class RandomWalkController : VirtualController
{
#region Dependencies
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly PhysicsSystem _physics = default!;
#endregion Dependencies
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RandomWalkComponent, ComponentStartup>(OnRandomWalkStartup);
}
/// <summary>
/// Updates the cooldowns of all random walkers.
/// If each of them is off cooldown it updates their velocity and resets its cooldown.
/// </summary>
/// <param name="prediction">??? Not documented anywhere I can see ???</param> // TODO: Document this.
/// <param name="frameTime">The amount of time that has elapsed since the last time random walk cooldowns were updated.</param>
public override void UpdateBeforeSolve(bool prediction, float frameTime)
{
base.UpdateBeforeSolve(prediction, frameTime);
foreach(var (randomWalk, physics) in EntityManager.EntityQuery<RandomWalkComponent, PhysicsComponent>())
{
if (EntityManager.HasComponent<ActorComponent>(randomWalk.Owner)
|| EntityManager.HasComponent<ThrownItemComponent>(randomWalk.Owner)
|| EntityManager.HasComponent<FollowerComponent>(randomWalk.Owner))
continue;
var curTime = _timing.CurTime;
if (randomWalk.NextStepTime <= curTime)
Update(randomWalk.Owner, randomWalk, physics);
}
}
/// <summary>
/// Updates the direction and speed a random walker is moving at.
/// Also resets the random walker's cooldown.
/// </summary>
/// <param name="randomWalk">The random walker state.</param>
/// <param name="physics">The physics body associated with the random walker.</param>
public void Update(EntityUid uid, RandomWalkComponent? randomWalk = null, PhysicsComponent? physics = null)
{
if(!Resolve(uid, ref randomWalk))
return;
var curTime = _timing.CurTime;
randomWalk.NextStepTime = curTime + TimeSpan.FromSeconds(_random.NextDouble(randomWalk.MinStepCooldown.TotalSeconds, randomWalk.MaxStepCooldown.TotalSeconds));
if(!Resolve(randomWalk.Owner, ref physics))
return;
var pushAngle = _random.NextAngle();
var pushStrength = _random.NextFloat(randomWalk.MinSpeed, randomWalk.MaxSpeed);
_physics.SetLinearVelocity(uid, physics.LinearVelocity * randomWalk.AccumulatorRatio, body: physics);
_physics.ApplyLinearImpulse(uid, pushAngle.ToVec() * (pushStrength * physics.Mass), body: physics);
}
/// <summary>
/// Syncs up a random walker step timing when the component starts up.
/// </summary>
/// <param name="uid">The uid of the random walker to start up.</param>
/// <param name="comp">The state of the random walker to start up.</param>
/// <param name="args">The startup prompt arguments.</param>
private void OnRandomWalkStartup(EntityUid uid, RandomWalkComponent comp, ComponentStartup args)
{
if (comp.StepOnStartup)
Update(uid, comp);
else
comp.NextStepTime = _timing.CurTime + TimeSpan.FromSeconds(_random.NextDouble(comp.MinStepCooldown.TotalSeconds, comp.MaxStepCooldown.TotalSeconds));
}
}