Shooting NPCs and more (#18042)

* Add pirate shooting

* Shooting working

* Basics working

* Refactor time

* More conversion

* Update primitives

* Update yml

* weh

* Building again

* Draft

* weh

* b

* Start shutdown

* Starting to take form

* Code side done

* is it worky

* Fix prototypes

* stuff

* Shitty working

* Juke events working

* Even more cleanup

* RTX

* Fix interaction combat mode and compquery

* GetAmmoCount relays

* Fix rotation speed

* Juke fixes

* fixes

* weh

* The collision avoidance never ends

* Fixes

* Pause support

* framework

* lazy

* Fix idling

* Fix drip

* goobed

* Fix takeover shutdown bug

* Merge fixes

* shitter

* Fix carpos
This commit is contained in:
metalgearsloth
2023-08-02 10:48:56 +10:00
committed by GitHub
parent 018e465fad
commit c31c848afd
103 changed files with 2089 additions and 810 deletions

View File

@@ -18,66 +18,6 @@ public sealed partial class NPCCombatSystem
{
SubscribeLocalEvent<NPCMeleeCombatComponent, ComponentStartup>(OnMeleeStartup);
SubscribeLocalEvent<NPCMeleeCombatComponent, ComponentShutdown>(OnMeleeShutdown);
SubscribeLocalEvent<NPCMeleeCombatComponent, NPCSteeringEvent>(OnMeleeSteering);
}
private void OnMeleeSteering(EntityUid uid, NPCMeleeCombatComponent component, ref NPCSteeringEvent args)
{
args.Steering.CanSeek = true;
if (TryComp<MeleeWeaponComponent>(component.Weapon, out var weapon))
{
var cdRemaining = weapon.NextAttack - _timing.CurTime;
var attackCooldown = TimeSpan.FromSeconds(1f / _melee.GetAttackRate(component.Weapon, uid, weapon));
// Might as well get in range.
if (cdRemaining < attackCooldown * 0.45f)
return;
if (!_physics.TryGetNearestPoints(uid, component.Target, out var pointA, out var pointB))
return;
var obstacleDirection = pointB - args.WorldPosition;
// If they're moving away then pursue anyway.
// If just hit then always back up a bit.
if (cdRemaining < attackCooldown * 0.90f &&
TryComp<PhysicsComponent>(component.Target, out var targetPhysics) &&
Vector2.Dot(targetPhysics.LinearVelocity, obstacleDirection) > 0f)
{
return;
}
if (cdRemaining < TimeSpan.FromSeconds(1f / _melee.GetAttackRate(component.Weapon, uid, weapon)) * 0.45f)
return;
var idealDistance = weapon.Range * 4f;
var obstacleDistance = obstacleDirection.Length();
if (obstacleDistance > idealDistance || obstacleDistance == 0f)
{
// Don't want to get too far.
return;
}
args.Steering.CanSeek = false;
obstacleDirection = args.OffsetRotation.RotateVec(obstacleDirection);
var norm = obstacleDirection.Normalized();
var weight = (obstacleDistance <= args.AgentRadius
? 1f
: (idealDistance - obstacleDistance) / idealDistance);
for (var i = 0; i < SharedNPCSteeringSystem.InterestDirections; i++)
{
var result = -Vector2.Dot(norm, NPCSteeringSystem.Directions[i]) * weight;
if (result < 0f)
continue;
args.Interest[i] = MathF.Max(args.Interest[i], result);
}
}
}
private void OnMeleeShutdown(EntityUid uid, NPCMeleeCombatComponent component, ComponentShutdown args)
@@ -87,7 +27,7 @@ public sealed partial class NPCCombatSystem
_combat.SetInCombatMode(uid, false, combatMode);
}
_steering.Unregister(component.Owner);
_steering.Unregister(uid);
}
private void OnMeleeStartup(EntityUid uid, NPCMeleeCombatComponent component, ComponentStartup args)
@@ -96,9 +36,6 @@ public sealed partial class NPCCombatSystem
{
_combat.SetInCombatMode(uid, true, combatMode);
}
// TODO: Cleanup later, just looking for parity for now.
component.Weapon = uid;
}
private void UpdateMelee(float frameTime)
@@ -107,11 +44,10 @@ public sealed partial class NPCCombatSystem
var xformQuery = GetEntityQuery<TransformComponent>();
var physicsQuery = GetEntityQuery<PhysicsComponent>();
var curTime = _timing.CurTime;
var query = EntityQueryEnumerator<NPCMeleeCombatComponent, ActiveNPCComponent>();
foreach (var (comp, _) in EntityQuery<NPCMeleeCombatComponent, ActiveNPCComponent>())
while (query.MoveNext(out var uid, out var comp, out _))
{
var uid = comp.Owner;
if (!combatQuery.TryGetComponent(uid, out var combat) || !combat.IsInCombatMode)
{
RemComp<NPCMeleeCombatComponent>(uid);
@@ -126,7 +62,7 @@ public sealed partial class NPCCombatSystem
{
component.Status = CombatStatus.Normal;
if (!TryComp<MeleeWeaponComponent>(component.Weapon, out var weapon))
if (!_melee.TryGetWeapon(uid, out var weaponUid, out var weapon))
{
component.Status = CombatStatus.NoWeapon;
return;
@@ -167,12 +103,6 @@ public sealed partial class NPCCombatSystem
return;
}
steering = EnsureComp<NPCSteeringComponent>(uid);
steering.Range = MathF.Max(0.2f, weapon.Range - 0.4f);
// Gets unregistered on component shutdown.
_steering.TryRegister(uid, new EntityCoordinates(component.Target, Vector2.Zero), steering);
if (weapon.NextAttack > curTime || !Enabled)
return;
@@ -180,11 +110,11 @@ public sealed partial class NPCCombatSystem
physicsQuery.TryGetComponent(component.Target, out var targetPhysics) &&
targetPhysics.LinearVelocity.LengthSquared() != 0f)
{
_melee.AttemptLightAttackMiss(uid, component.Weapon, weapon, targetXform.Coordinates.Offset(_random.NextVector2(0.5f)));
_melee.AttemptLightAttackMiss(uid, weaponUid, weapon, targetXform.Coordinates.Offset(_random.NextVector2(0.5f)));
}
else
{
_melee.AttemptLightAttack(uid, component.Weapon, weapon, component.Target);
_melee.AttemptLightAttack(uid, weaponUid, weapon, component.Target);
}
}
}