2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.CombatMode;
|
|
|
|
|
using Content.Server.Hands.Components;
|
|
|
|
|
using Content.Server.Interaction;
|
|
|
|
|
using Content.Server.Weapon.Melee.Components;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-06-18 22:52:44 +10:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
|
2020-06-25 01:43:58 +10:00
|
|
|
namespace Content.Server.AI.Operators.Combat.Melee
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class SwingMeleeWeaponOperator : AiOperator
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
[Dependency] private readonly IEntityManager _entMan = default!;
|
|
|
|
|
|
2020-11-21 14:02:00 +01:00
|
|
|
private readonly float _burstTime;
|
2020-06-18 22:52:44 +10:00
|
|
|
private float _elapsedTime;
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
private readonly EntityUid _owner;
|
|
|
|
|
private readonly EntityUid _target;
|
2020-06-18 22:52:44 +10:00
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public SwingMeleeWeaponOperator(EntityUid owner, EntityUid target, float burstTime = 1.0f)
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
2021-12-08 17:04:21 +01:00
|
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
|
|
2020-06-18 22:52:44 +10:00
|
|
|
_owner = owner;
|
|
|
|
|
_target = target;
|
|
|
|
|
_burstTime = burstTime;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 01:24:03 +11:00
|
|
|
public override bool Startup()
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
2021-01-11 01:24:03 +11:00
|
|
|
if (!base.Startup())
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-07-06 14:27:03 -07:00
|
|
|
|
2021-12-08 17:04:21 +01:00
|
|
|
if (!_entMan.TryGetComponent(_owner, out CombatModeComponent? combatModeComponent))
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!combatModeComponent.IsInCombatMode)
|
|
|
|
|
{
|
|
|
|
|
combatModeComponent.IsInCombatMode = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-07-06 14:27:03 -07:00
|
|
|
|
2021-01-11 01:24:03 +11:00
|
|
|
public override bool Shutdown(Outcome outcome)
|
2020-06-25 01:43:58 +10:00
|
|
|
{
|
2021-01-11 01:24:03 +11:00
|
|
|
if (!base.Shutdown(outcome))
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-12-08 17:04:21 +01:00
|
|
|
if (_entMan.TryGetComponent(_owner, out CombatModeComponent? combatModeComponent))
|
2020-06-25 01:43:58 +10:00
|
|
|
{
|
|
|
|
|
combatModeComponent.IsInCombatMode = false;
|
|
|
|
|
}
|
2021-01-11 01:24:03 +11:00
|
|
|
|
|
|
|
|
return true;
|
2020-06-25 01:43:58 +10:00
|
|
|
}
|
2020-06-18 22:52:44 +10:00
|
|
|
|
|
|
|
|
public override Outcome Execute(float frameTime)
|
|
|
|
|
{
|
|
|
|
|
if (_burstTime <= _elapsedTime)
|
|
|
|
|
{
|
|
|
|
|
return Outcome.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 01:38:35 -08:00
|
|
|
if (!_entMan.TryGetComponent(_owner, out HandsComponent? hands) || hands.GetActiveHandItem == null)
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
|
|
|
|
return Outcome.Failed;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 01:38:35 -08:00
|
|
|
var meleeWeapon = hands.GetActiveHandItem.Owner;
|
2021-12-08 17:04:21 +01:00
|
|
|
_entMan.TryGetComponent(meleeWeapon, out MeleeWeaponComponent? meleeWeaponComponent);
|
2020-06-18 22:52:44 +10:00
|
|
|
|
2021-12-08 17:04:21 +01:00
|
|
|
if ((_entMan.GetComponent<TransformComponent>(_target).Coordinates.Position - _entMan.GetComponent<TransformComponent>(_owner).Coordinates.Position).Length >
|
2021-03-16 15:50:20 +01:00
|
|
|
meleeWeaponComponent?.Range)
|
2020-06-18 22:52:44 +10:00
|
|
|
{
|
|
|
|
|
return Outcome.Failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var interactionSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<InteractionSystem>();
|
|
|
|
|
|
2021-12-08 17:04:21 +01:00
|
|
|
interactionSystem.AiUseInteraction(_owner, _entMan.GetComponent<TransformComponent>(_target).Coordinates, _target);
|
2020-06-18 22:52:44 +10:00
|
|
|
_elapsedTime += frameTime;
|
|
|
|
|
return Outcome.Continuing;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|