Files
tbd-station-14/Content.Server/Physics/Controllers/RandomWalkController.cs
TemporalOroboros 9a72b05a50 Splits the singularity into its component parts + ECS singularity + Support for singularities in containers. (#12132)
* InitialCommit (Broken)

* Fixes compile errors

* PR comments. More doc comments. Fixes

* Makes a singularity/event horizon without radiation/physics a valid state to be in

* VV 'fake' setters, fixes the visualizer, fixes the singularity trying to eat itself instead of nearby things.

* Removes unused dependency from Content.Client.GravityWellSystem

* Testing containment and fake VV setters for SingularityGeneratorComponent

* Fixes gravity wells (broken due to LookupFlags.None). Adds recursive Event Horizon consumption

* Fix merge skew

* Fixes for the master merge

* Fix engine commit

* Dirty is obsolete

* Switch over dirty

* Fix requested changes

* ambiant -> ambient

* Moves EventHorionComponent to Shared

* Proper container handling

* Fixes master merge. Fixes post insertion assertions for singularities. Extends proper container handling to gravity wells and the distortion shader.

* Better support for admemes throwing singularities.

* Moves update timing from accumulators to target times

* Update doc comments
2022-12-19 20:47:15 -06:00

90 lines
3.7 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.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))
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(physics, physics.LinearVelocity * randomWalk.AccumulatorRatio);
_physics.ApplyLinearImpulse(physics, pushAngle.ToVec() * (pushStrength * physics.Mass));
}
/// <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));
}
}