CatStation/Content.Server/Physics/Controllers/ChasingWalkSystem.cs
Samuka 838984971c
Xenoborg guided missile (#45247)
* update math

* target finder system

* ports

* guns with projectiles

* make sure

* custom sprites for the laser

* add auto link

* add module

* sprite for the missile

* update animation speed

* ops

* make it glow

* overlay sprite

* stuff that doesn't work

* update launcher

* fix it

* make it see from far

* make it work

* sprite for the module

* 30 seconds is too much

* sprite for the range finder

* why was that here?

* update sprite of the missile

* update module icon

* ops

* update missile and missile launcher

* make it look like the launcher item

* remove ammo counter from range finder

* increase to 15 seconds

* no longer show the battery percentage

* use comp

* change summary

* ops

* new line

* make it last longer

* commentary

* acme moment

* fix bug when shooter was also the target and the projectile wouldn't move

* empty commit

* fix test fail

* replace with global behavior

* forgot to remove from the yml

* important

* check to see if the target exists

* fix autorefill bug

* remove unused system

* make it reload a tad slower

---------

Co-authored-by: beck-thompson <beck314159@hotmail.com>
2026-08-16 05:10:57 +00:00

130 lines
5 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 partial class ChasingWalkSystem : VirtualController
{
[Dependency] private IGameTiming _gameTiming = default!;
[Dependency] private IRobustRandom _random = default!;
[Dependency] private SharedTransformSystem _transform = default!;
[Dependency] private EntityLookupSystem _lookup = default!;
[Dependency] private 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)
{
if (component.ChasingComponent.Count <= 0)
return;
//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 currentDirection = _physics.GetLinearVelocity(uid, Vector2.Zero);
var targetVector = pos2 - pos1;
var angleToChange = targetVector.ToAngle() - currentDirection.ToAngle();
// Make sure we rotate to the smallest direction
if (angleToChange > Angle.FromDegrees(180))
angleToChange -= Angle.FromDegrees(360);
else if (angleToChange < Angle.FromDegrees(-180))
angleToChange += Angle.FromDegrees(360);
angleToChange = Math.Clamp(angleToChange,
-component.MaxAngleVectorChangePerImpulse,
component.MaxAngleVectorChangePerImpulse);
var newDirection = currentDirection.ToAngle() + angleToChange;
var speed = !component.StopAtTarget || targetVector.Length() > 0
? newDirection.ToVec() * 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.
if (component.RotateWithImpulse)
{
var ang = speed.ToAngle() + Angle.FromDegrees(90); // we want "Up" to be forward, bullet convention.
_transform.SetWorldRotation(uid, ang + component.RotationAngleOffset);
}
}
}