Files
wwdpublic/Content.Server/Physics/Controllers/ChasingWalkSystem.cs
SimpleStation14 156dfb61a8 Mirror: Code cleanup: Purge obsoleted SharedPhysicsSystem methods (#384)
## Mirror of PR #26287: [Code cleanup: Purge obsoleted
SharedPhysicsSystem
methods](https://github.com/space-wizards/space-station-14/pull/26287)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `964c6d54caae45b205a326143f56d6458a1bbc8a`

PR opened by <img
src="https://avatars.githubusercontent.com/u/85356?v=4" width="16"/><a
href="https://github.com/Tayrtahn"> Tayrtahn</a> at 2024-03-20 13:37:25
UTC

---

PR changed 16 files with 43 additions and 43 deletions.

The PR had the following labels:
- Status: Needs Review


---

<details open="true"><summary><h1>Original Body</h1></summary>

> <!-- Please read these guidelines before opening your PR:
https://docs.spacestation14.io/en/getting-started/pr-guideline -->
> <!-- The text between the arrows are comments - they will not be
visible on your PR. -->
> 
> Requires https://github.com/space-wizards/RobustToolbox/pull/4979
> 
> ## About the PR
> <!-- What did you change in this PR? -->
> Cleans up some obsolete method calls.
> 
> ## Why / Balance / Technical
> <!-- Why was it changed? Link any discussions or issues here. Please
discuss how this would affect game balance. -->
> Cleaning up obsolete Dirty calls in RT required changing the
signatures of some public methods in SharedPhysicsSystem. This updates
the calls to those methods here in Content to use the new signatures
passing in UIDs.
> 
> 


</details>

Co-authored-by: SimpleStation14 <Unknown>
2024-05-20 23:57:56 +01:00

103 lines
4.0 KiB
C#

using System.Linq;
using System.Numerics;
using Content.Server.Physics.Components;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Controllers;
namespace Content.Server.Physics.Controllers;
/// <summary>
/// A system which makes its entity chasing another entity with selected component.
/// </summary>
public sealed class ChasingWalkSystem : VirtualController
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
private readonly HashSet<Entity<IComponent>> _potentialChaseTargets = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ChasingWalkComponent, MapInitEvent>(OnChasingMapInit);
}
private void OnChasingMapInit(EntityUid uid, ChasingWalkComponent component, MapInitEvent args)
{
component.NextImpulseTime = _gameTiming.CurTime;
component.NextChangeVectorTime = _gameTiming.CurTime;
}
public override void UpdateBeforeSolve(bool prediction, float frameTime)
{
base.UpdateBeforeSolve(prediction, frameTime);
var query = EntityQueryEnumerator<ChasingWalkComponent>();
while (query.MoveNext(out var uid, out var chasing))
{
//Set Velocity to Target
if (chasing.NextImpulseTime <= _gameTiming.CurTime)
{
ForceImpulse(uid, chasing);
chasing.NextImpulseTime += TimeSpan.FromSeconds(chasing.ImpulseInterval);
}
//Change Target
if (chasing.NextChangeVectorTime <= _gameTiming.CurTime)
{
ChangeTarget(uid, chasing);
var delay = TimeSpan.FromSeconds(_random.NextFloat(chasing.ChangeVectorMinInterval, chasing.ChangeVectorMaxInterval));
chasing.NextChangeVectorTime += delay;
}
}
}
private void ChangeTarget(EntityUid uid, ChasingWalkComponent component)
{
//We find our coordinates and calculate the radius of the target search.
var xform = Transform(uid);
var range = component.MaxChaseRadius;
var compType = _random.Pick(component.ChasingComponent.Values).Component.GetType();
_potentialChaseTargets.Clear();
_lookup.GetEntitiesInRange(compType, _transform.GetMapCoordinates(xform), range, _potentialChaseTargets, LookupFlags.Uncontained);
//If there are no required components in the radius, don't moving.
if (_potentialChaseTargets.Count <= 0)
return;
//In the case of finding required components, we choose a random one of them and remember its uid.
component.ChasingEntity = _random.Pick(_potentialChaseTargets).Owner;
component.Speed = _random.NextFloat(component.MinSpeed, component.MaxSpeed);
}
//pushing the entity toward its target
private void ForceImpulse(EntityUid uid, ChasingWalkComponent component)
{
if (Deleted(component.ChasingEntity) || component.ChasingEntity == null)
{
ChangeTarget(uid, component);
return;
}
if (!TryComp<PhysicsComponent>(uid, out var physics))
return;
//Calculating direction to the target.
var pos1 = _transform.GetWorldPosition(uid);
var pos2 = _transform.GetWorldPosition(component.ChasingEntity.Value);
var delta = pos2 - pos1;
var speed = delta.Length() > 0 ? delta.Normalized() * component.Speed : Vector2.Zero;
_physics.SetLinearVelocity(uid, speed);
_physics.SetBodyStatus(uid, physics, BodyStatus.InAir); //If this is not done, from the explosion up close, the tesla will "Fall" to the ground, and almost stop moving.
}
}