Files
OldThink/Content.Server/NPC/Systems/NPCCombatSystem.Melee.cs

119 lines
3.9 KiB
C#
Raw Permalink Normal View History

using System.Numerics;
using Content.Server.NPC.Components;
using Content.Shared.CombatMode;
2022-12-12 14:33:43 +11:00
using Content.Shared.NPC;
2022-09-30 14:39:48 +10:00
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
using Robust.Shared.Random;
namespace Content.Server.NPC.Systems;
public sealed partial class NPCCombatSystem
{
2022-09-30 14:39:48 +10:00
private const float TargetMeleeLostRange = 14f;
private void InitializeMelee()
{
SubscribeLocalEvent<NPCMeleeCombatComponent, ComponentStartup>(OnMeleeStartup);
SubscribeLocalEvent<NPCMeleeCombatComponent, ComponentShutdown>(OnMeleeShutdown);
}
private void OnMeleeShutdown(EntityUid uid, NPCMeleeCombatComponent component, ComponentShutdown args)
{
if (TryComp<CombatModeComponent>(uid, out var combatMode))
{
_combat.SetInCombatMode(uid, false, combatMode);
}
2022-09-30 14:39:48 +10:00
_steering.Unregister(uid);
}
private void OnMeleeStartup(EntityUid uid, NPCMeleeCombatComponent component, ComponentStartup args)
{
if (TryComp<CombatModeComponent>(uid, out var combatMode))
{
_combat.SetInCombatMode(uid, true, combatMode);
}
}
private void UpdateMelee(float frameTime)
{
var combatQuery = GetEntityQuery<CombatModeComponent>();
var xformQuery = GetEntityQuery<TransformComponent>();
var physicsQuery = GetEntityQuery<PhysicsComponent>();
var curTime = _timing.CurTime;
var query = EntityQueryEnumerator<NPCMeleeCombatComponent, ActiveNPCComponent>();
while (query.MoveNext(out var uid, out var comp, out _))
{
if (!combatQuery.TryGetComponent(uid, out var combat) || !combat.IsInCombatMode)
{
RemComp<NPCMeleeCombatComponent>(uid);
continue;
}
Attack(uid, comp, curTime, physicsQuery, xformQuery);
}
}
private void Attack(EntityUid uid, NPCMeleeCombatComponent component, TimeSpan curTime, EntityQuery<PhysicsComponent> physicsQuery, EntityQuery<TransformComponent> xformQuery)
{
component.Status = CombatStatus.Normal;
if (!_melee.TryGetWeapon(uid, out var weaponUid, out var weapon))
{
component.Status = CombatStatus.NoWeapon;
return;
}
if (!xformQuery.TryGetComponent(uid, out var xform) ||
2022-09-30 14:39:48 +10:00
!xformQuery.TryGetComponent(component.Target, out var targetXform))
{
2022-09-30 14:39:48 +10:00
component.Status = CombatStatus.TargetUnreachable;
return;
}
2022-09-30 14:39:48 +10:00
if (!xform.Coordinates.TryDistance(EntityManager, targetXform.Coordinates, out var distance))
{
component.Status = CombatStatus.TargetUnreachable;
return;
}
2022-09-30 14:39:48 +10:00
if (distance > TargetMeleeLostRange)
{
component.Status = CombatStatus.TargetUnreachable;
return;
}
if (TryComp<NPCSteeringComponent>(uid, out var steering) &&
steering.Status == SteeringStatus.NoPath)
{
component.Status = CombatStatus.TargetUnreachable;
return;
}
2023-05-02 04:57:11 +10:00
// TODO: When I get parallel operators move this as NPC combat shouldn't be handling this.
_steering.Register(uid, new EntityCoordinates(component.Target, Vector2.Zero), steering);
2022-09-30 14:39:48 +10:00
if (distance > weapon.Range)
{
component.Status = CombatStatus.TargetOutOfRange;
return;
}
if (weapon.NextAttack > curTime || weapon.NextMobAttack > curTime || !Enabled) // WD EDIT
return;
if (_random.Prob(component.MissChance) &&
physicsQuery.TryGetComponent(component.Target, out var targetPhysics) &&
targetPhysics.LinearVelocity.LengthSquared() != 0f)
{
_melee.AttemptLightAttackMiss(uid, weaponUid, weapon, targetXform.Coordinates.Offset(_random.NextVector2(0.5f)));
}
else
{
_melee.AttemptLightAttack(uid, weaponUid, weapon, component.Target);
}
}
}