using Content.Client.Hands.Systems;
using Content.Shared.CCVar;
using Content.Shared.CombatMode;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.Player;
using Robust.Shared.Configuration;
namespace Content.Client.CombatMode;
public sealed class CombatModeSystem : SharedCombatModeSystem
{
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IEyeManager _eye = default!;
///
/// Raised whenever combat mode changes.
///
public event Action? LocalPlayerCombatModeUpdated;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnHandleState);
_cfg.OnValueChanged(CCVars.CombatModeIndicatorsPointShow, OnShowCombatIndicatorsChanged, true);
}
private void OnHandleState(EntityUid uid, CombatModeComponent component, ref AfterAutoHandleStateEvent args)
{
UpdateHud(uid);
}
public override void Shutdown()
{
_cfg.UnsubValueChanged(CCVars.CombatModeIndicatorsPointShow, OnShowCombatIndicatorsChanged);
_overlayManager.RemoveOverlay();
base.Shutdown();
}
public bool IsInCombatMode()
{
var entity = _playerManager.LocalPlayer?.ControlledEntity;
if (entity == null)
return false;
return IsInCombatMode(entity.Value);
}
public override void SetInCombatMode(EntityUid entity, bool value, CombatModeComponent? component = null)
{
base.SetInCombatMode(entity, value, component);
UpdateHud(entity);
}
private void UpdateHud(EntityUid entity)
{
if (entity != _playerManager.LocalPlayer?.ControlledEntity || !Timing.IsFirstTimePredicted)
{
return;
}
var inCombatMode = IsInCombatMode();
LocalPlayerCombatModeUpdated?.Invoke(inCombatMode);
}
private void OnShowCombatIndicatorsChanged(bool isShow)
{
if (isShow)
{
_overlayManager.AddOverlay(new CombatModeIndicatorsOverlay(
_inputManager,
EntityManager,
_eye,
this,
EntityManager.System()));
}
else
{
_overlayManager.RemoveOverlay();
}
}
}