New indicators for combat mode (#15427)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
87
Content.Client/CombatMode/CombatModeIndicatorsOverlay.cs
Normal file
87
Content.Client/CombatMode/CombatModeIndicatorsOverlay.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Content.Client.Hands.Systems;
|
||||
using Content.Shared.Weapons.Ranged.Components;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.CombatMode;
|
||||
|
||||
/// <summary>
|
||||
/// This shows something like crosshairs for the combat mode next to the mouse cursor.
|
||||
/// For weapons with the gun class, a crosshair of one type is displayed,
|
||||
/// while for all other types of weapons and items in hand, as well as for an empty hand,
|
||||
/// a crosshair of a different type is displayed. These crosshairs simply show the state of combat mode (on|off).
|
||||
/// </summary>
|
||||
public sealed class CombatModeIndicatorsOverlay : Overlay
|
||||
{
|
||||
private readonly IInputManager _inputManager;
|
||||
private readonly IEntityManager _entMan;
|
||||
private readonly IEyeManager _eye;
|
||||
private readonly CombatModeSystem _combat;
|
||||
private readonly HandsSystem _hands = default!;
|
||||
|
||||
private readonly Texture _gunSight;
|
||||
private readonly Texture _meleeSight;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.ScreenSpace;
|
||||
|
||||
public Color MainColor = Color.White.WithAlpha(0.3f);
|
||||
public Color StrokeColor = Color.Black.WithAlpha(0.5f);
|
||||
public float Scale = 0.8f; // 1 is a little big
|
||||
|
||||
public CombatModeIndicatorsOverlay(IInputManager input, IEntityManager entMan,
|
||||
IEyeManager eye, CombatModeSystem combatSys, HandsSystem hands)
|
||||
{
|
||||
_inputManager = input;
|
||||
_entMan = entMan;
|
||||
_eye = eye;
|
||||
_combat = combatSys;
|
||||
_hands = hands;
|
||||
|
||||
var spriteSys = _entMan.EntitySysManager.GetEntitySystem<SpriteSystem>();
|
||||
_gunSight = spriteSys.Frame0(new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Misc/crosshair_pointers.rsi"),
|
||||
"gun_sight"));
|
||||
_meleeSight = spriteSys.Frame0(new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Misc/crosshair_pointers.rsi"),
|
||||
"melee_sight"));
|
||||
}
|
||||
|
||||
protected override bool BeforeDraw(in OverlayDrawArgs args)
|
||||
{
|
||||
if (!_combat.IsInCombatMode())
|
||||
return false;
|
||||
|
||||
return base.BeforeDraw(in args);
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
var mouseScreenPosition = _inputManager.MouseScreenPosition;
|
||||
var mousePosMap = _eye.ScreenToMap(mouseScreenPosition);
|
||||
if (mousePosMap.MapId != args.MapId)
|
||||
return;
|
||||
|
||||
var handEntity = _hands.GetActiveHandEntity();
|
||||
var isHandGunItem = _entMan.HasComponent<GunComponent>(handEntity);
|
||||
|
||||
var mousePos = mouseScreenPosition.Position;
|
||||
var uiScale = (args.ViewportControl as Control)?.UIScale ?? 1f;
|
||||
var limitedScale = uiScale > 1.25f ? 1.25f : uiScale;
|
||||
|
||||
var sight = isHandGunItem ? _gunSight : _meleeSight;
|
||||
DrawSight(sight, args.ScreenHandle, mousePos, limitedScale * Scale);
|
||||
}
|
||||
|
||||
private void DrawSight(Texture sight, DrawingHandleScreen screen, Vector2 centerPos, float scale)
|
||||
{
|
||||
var sightSize = sight.Size * scale;
|
||||
var expandedSize = sightSize + 7f;
|
||||
|
||||
screen.DrawTextureRect(sight,
|
||||
UIBox2.FromDimensions(centerPos - sightSize * 0.5f, sightSize), StrokeColor);
|
||||
screen.DrawTextureRect(sight,
|
||||
UIBox2.FromDimensions(centerPos - expandedSize * 0.5f, expandedSize), MainColor);
|
||||
}
|
||||
}
|
||||
@@ -1,77 +1,102 @@
|
||||
using Content.Client.Hands.Systems;
|
||||
using Content.Shared.CombatMode;
|
||||
using Content.Shared.Targeting;
|
||||
using JetBrains.Annotations;
|
||||
using Content.Shared.CCVar;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Input.Binding;
|
||||
using Robust.Shared.Configuration;
|
||||
|
||||
namespace Content.Client.CombatMode
|
||||
namespace Content.Client.CombatMode;
|
||||
|
||||
public sealed class CombatModeSystem : SharedCombatModeSystem
|
||||
{
|
||||
[UsedImplicitly]
|
||||
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!;
|
||||
public event Action? LocalPlayerCombatModeUpdated;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
base.Initialize();
|
||||
|
||||
public event Action? LocalPlayerCombatModeUpdated;
|
||||
SubscribeLocalEvent<CombatModeComponent, ComponentHandleState>(OnHandleState);
|
||||
|
||||
public override void Initialize()
|
||||
_cfg.OnValueChanged(CCVars.CombatModeIndicatorsPointShow, OnShowCombatIndicatorsChanged, true);
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, CombatModeComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not CombatModeComponentState state)
|
||||
return;
|
||||
|
||||
component.IsInCombatMode = state.IsInCombatMode;
|
||||
component.ActiveZone = state.TargetingZone;
|
||||
UpdateHud(uid);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
_cfg.OnValueChanged(CCVars.CombatModeIndicatorsPointShow, OnShowCombatIndicatorsChanged);
|
||||
_overlayManager.RemoveOverlay<CombatModeIndicatorsOverlay>();
|
||||
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
private void OnTargetingZoneChanged(TargetingZone obj)
|
||||
{
|
||||
EntityManager.RaisePredictiveEvent(new CombatModeSystemMessages.SetTargetZoneMessage(obj));
|
||||
}
|
||||
|
||||
public bool IsInCombatMode()
|
||||
{
|
||||
var entity = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
|
||||
if (entity == null)
|
||||
return false;
|
||||
|
||||
return IsInCombatMode(entity.Value);
|
||||
}
|
||||
|
||||
public override void SetInCombatMode(EntityUid entity, bool inCombatMode, CombatModeComponent? component = null)
|
||||
{
|
||||
base.SetInCombatMode(entity, inCombatMode, component);
|
||||
UpdateHud(entity);
|
||||
}
|
||||
|
||||
public override void SetActiveZone(EntityUid entity, TargetingZone zone, CombatModeComponent? component = null)
|
||||
{
|
||||
base.SetActiveZone(entity, zone, component);
|
||||
UpdateHud(entity);
|
||||
}
|
||||
|
||||
private void UpdateHud(EntityUid entity)
|
||||
{
|
||||
if (entity != _playerManager.LocalPlayer?.ControlledEntity)
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CombatModeComponent, ComponentHandleState>(OnHandleState);
|
||||
return;
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, CombatModeComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not CombatModeComponentState state)
|
||||
return;
|
||||
LocalPlayerCombatModeUpdated?.Invoke();
|
||||
}
|
||||
|
||||
component.IsInCombatMode = state.IsInCombatMode;
|
||||
component.ActiveZone = state.TargetingZone;
|
||||
UpdateHud(uid);
|
||||
private void OnShowCombatIndicatorsChanged(bool isShow)
|
||||
{
|
||||
if (isShow)
|
||||
{
|
||||
_overlayManager.AddOverlay(new CombatModeIndicatorsOverlay(
|
||||
_inputManager,
|
||||
EntityManager,
|
||||
_eye,
|
||||
this,
|
||||
EntityManager.System<HandsSystem>()));
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
else
|
||||
{
|
||||
CommandBinds.Unregister<CombatModeSystem>();
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
private void OnTargetingZoneChanged(TargetingZone obj)
|
||||
{
|
||||
EntityManager.RaisePredictiveEvent(new CombatModeSystemMessages.SetTargetZoneMessage(obj));
|
||||
}
|
||||
|
||||
public bool IsInCombatMode()
|
||||
{
|
||||
var entity = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
|
||||
if (entity == null)
|
||||
return false;
|
||||
|
||||
return IsInCombatMode(entity.Value);
|
||||
}
|
||||
|
||||
public override void SetInCombatMode(EntityUid entity, bool inCombatMode, CombatModeComponent? component = null)
|
||||
{
|
||||
base.SetInCombatMode(entity, inCombatMode, component);
|
||||
UpdateHud(entity);
|
||||
}
|
||||
|
||||
public override void SetActiveZone(EntityUid entity, TargetingZone zone, CombatModeComponent? component = null)
|
||||
{
|
||||
base.SetActiveZone(entity, zone, component);
|
||||
UpdateHud(entity);
|
||||
}
|
||||
|
||||
private void UpdateHud(EntityUid entity)
|
||||
{
|
||||
if (entity != _playerManager.LocalPlayer?.ControlledEntity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
LocalPlayerCombatModeUpdated?.Invoke();
|
||||
_overlayManager.RemoveOverlay<CombatModeIndicatorsOverlay>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user