Minor ReflectionSystem refactor (#37039)

* ReflectComponentLogicFix

Added bool InRightPlace and updated relevant system

* Using SlotFlags

* edits

* refactor

* add missing relay

---------

Co-authored-by: BIGZi0348 <svalker0348@gmail.com>
This commit is contained in:
slarticodefast
2025-04-30 16:10:54 +02:00
committed by GitHub
parent a8ff999b08
commit b068b5bb89
9 changed files with 143 additions and 147 deletions

View File

@@ -2,6 +2,8 @@ using Content.Shared.Atmos;
using Content.Shared.Camera;
using Content.Shared.Hands.Components;
using Content.Shared.Movement.Systems;
using Content.Shared.Projectiles;
using Content.Shared.Weapons.Ranged.Events;
namespace Content.Shared.Hands.EntitySystems;
@@ -15,6 +17,8 @@ public abstract partial class SharedHandsSystem
// By-ref events.
SubscribeLocalEvent<HandsComponent, ExtinguishEvent>(RefRelayEvent);
SubscribeLocalEvent<HandsComponent, ProjectileReflectAttemptEvent>(RefRelayEvent);
SubscribeLocalEvent<HandsComponent, HitScanReflectAttemptEvent>(RefRelayEvent);
}
private void RelayEvent<T>(Entity<HandsComponent> entity, ref T args) where T : EntityEventArgs

View File

@@ -17,6 +17,7 @@ using Content.Shared.Movement.Events;
using Content.Shared.Movement.Systems;
using Content.Shared.NameModifier.EntitySystems;
using Content.Shared.Overlays;
using Content.Shared.Projectiles;
using Content.Shared.Radio;
using Content.Shared.Slippery;
using Content.Shared.Strip.Components;
@@ -56,6 +57,8 @@ public partial class InventorySystem
SubscribeLocalEvent<InventoryComponent, GetSlowedOverSlipperyModifierEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, ModifySlowOnDamageSpeedEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, ExtinguishEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, ProjectileReflectAttemptEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, HitScanReflectAttemptEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, GetContrabandDetailsEvent>(RefRelayInventoryEvent);
// Eye/vision events

View File

@@ -4,6 +4,7 @@ using Content.Shared.Damage;
using Content.Shared.DoAfter;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Inventory;
using Content.Shared.Mobs.Components;
using Content.Shared.Throwing;
using Robust.Shared.Audio.Systems;
@@ -238,7 +239,10 @@ public sealed class ImpactEffectEvent : EntityEventArgs
/// Raised when an entity is just about to be hit with a projectile but can reflect it
/// </summary>
[ByRefEvent]
public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled);
public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled) : IInventoryRelayEvent
{
SlotFlags IInventoryRelayEvent.TargetSlots => SlotFlags.WITHOUT_POCKET;
}
/// <summary>
/// Raised when a projectile hits an entity

View File

@@ -1,4 +1,5 @@
using System.Numerics;
using Content.Shared.Inventory;
using Content.Shared.Weapons.Reflect;
namespace Content.Shared.Weapons.Ranged.Events;
@@ -8,4 +9,7 @@ namespace Content.Shared.Weapons.Ranged.Events;
/// and changing <see cref="Direction"/> where shot will go next
/// </summary>
[ByRefEvent]
public record struct HitScanReflectAttemptEvent(EntityUid? Shooter, EntityUid SourceItem, ReflectType Reflective, Vector2 Direction, bool Reflected);
public record struct HitScanReflectAttemptEvent(EntityUid? Shooter, EntityUid SourceItem, ReflectType Reflective, Vector2 Direction, bool Reflected) : IInventoryRelayEvent
{
SlotFlags IInventoryRelayEvent.TargetSlots => SlotFlags.WITHOUT_POCKET;
}

View File

@@ -1,5 +1,7 @@
using Content.Shared.Inventory;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Weapons.Reflect;
@@ -13,23 +15,48 @@ public sealed partial class ReflectComponent : Component
/// <summary>
/// What we reflect.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField]
[DataField]
public ReflectType Reflects = ReflectType.Energy | ReflectType.NonEnergy;
/// <summary>
/// Select in which inventory slots it will reflect.
/// By default, it will reflect in any inventory position, except pockets.
/// </summary>
[DataField]
public SlotFlags SlotFlags = SlotFlags.WITHOUT_POCKET;
/// <summary>
/// Is it allowed to reflect while being in hands.
/// </summary>
[DataField, AutoNetworkedField]
public bool ReflectingInHands = true;
/// <summary>
/// Can only reflect when placed correctly.
/// </summary>
[DataField, AutoNetworkedField]
public bool InRightPlace;
/// <summary>
/// Probability for a projectile to be reflected.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
[DataField, AutoNetworkedField]
public float ReflectProb = 0.25f;
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
/// <summary>
/// Probability for a projectile to be reflected.
/// </summary>
[DataField, AutoNetworkedField]
public Angle Spread = Angle.FromDegrees(45);
/// <summary>
/// The sound to play when reflecting.
/// </summary>
[DataField]
public SoundSpecifier? SoundOnReflect = new SoundPathSpecifier("/Audio/Weapons/Guns/Hits/laser_sear_wall.ogg", AudioParams.Default.WithVariation(0.05f));
}
[Flags]
[Flags, Serializable, NetSerializable]
public enum ReflectType : byte
{
None = 0,

View File

@@ -1,25 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using Content.Shared.Administration.Logs;
using Content.Shared.Alert;
using Content.Shared.Audio;
using Content.Shared.Database;
using Content.Shared.Hands;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Item.ItemToggle;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.Popups;
using Content.Shared.Projectiles;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Network;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Shared.Weapons.Reflect;
@@ -28,7 +23,6 @@ namespace Content.Shared.Weapons.Reflect;
/// </summary>
public sealed class ReflectSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
@@ -37,74 +31,82 @@ public sealed class ReflectSystem : EntitySystem
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;
public override void Initialize()
{
base.Initialize();
Subs.SubscribeWithRelay<ReflectComponent, ProjectileReflectAttemptEvent>(OnReflectUserCollide, baseEvent: false);
Subs.SubscribeWithRelay<ReflectComponent, HitScanReflectAttemptEvent>(OnReflectUserHitscan, baseEvent: false);
SubscribeLocalEvent<ReflectComponent, ProjectileReflectAttemptEvent>(OnReflectCollide);
SubscribeLocalEvent<ReflectComponent, HitScanReflectAttemptEvent>(OnReflectHitscan);
SubscribeLocalEvent<ReflectComponent, GotEquippedEvent>(OnReflectEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedEvent>(OnReflectUnequipped);
SubscribeLocalEvent<ReflectComponent, GotEquippedHandEvent>(OnReflectHandEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedHandEvent>(OnReflectHandUnequipped);
SubscribeLocalEvent<ReflectComponent, ItemToggledEvent>(OnToggleReflect);
SubscribeLocalEvent<ReflectUserComponent, ProjectileReflectAttemptEvent>(OnReflectUserCollide);
SubscribeLocalEvent<ReflectUserComponent, HitScanReflectAttemptEvent>(OnReflectUserHitscan);
}
private void OnReflectUserHitscan(EntityUid uid, ReflectUserComponent component, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected)
return;
foreach (var ent in _inventorySystem.GetHandOrInventoryEntities(uid, SlotFlags.All & ~SlotFlags.POCKET))
{
if (!TryReflectHitscan(uid, ent, args.Shooter, args.SourceItem, args.Direction, args.Reflective, out var dir))
continue;
args.Direction = dir.Value;
args.Reflected = true;
break;
}
}
private void OnReflectUserCollide(EntityUid uid, ReflectUserComponent component, ref ProjectileReflectAttemptEvent args)
{
foreach (var ent in _inventorySystem.GetHandOrInventoryEntities(uid, SlotFlags.All & ~SlotFlags.POCKET))
{
if (!TryReflectProjectile(uid, ent, args.ProjUid))
continue;
args.Cancelled = true;
break;
}
}
private void OnReflectCollide(EntityUid uid, ReflectComponent component, ref ProjectileReflectAttemptEvent args)
private void OnReflectUserCollide(Entity<ReflectComponent> ent, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
if (TryReflectProjectile(uid, uid, args.ProjUid, reflect: component))
if (!ent.Comp.InRightPlace)
return; // only reflect when equipped correctly
if (TryReflectProjectile(ent, ent.Owner, args.ProjUid))
args.Cancelled = true;
}
private bool TryReflectProjectile(EntityUid user, EntityUid reflector, EntityUid projectile, ProjectileComponent? projectileComp = null, ReflectComponent? reflect = null)
private void OnReflectUserHitscan(Entity<ReflectComponent> ent, ref HitScanReflectAttemptEvent args)
{
if (!Resolve(reflector, ref reflect, false) ||
!TryComp<ReflectiveComponent>(projectile, out var reflective) ||
(reflect.Reflects & reflective.Reflective) == 0x0 ||
!_toggle.IsActivated(reflector) ||
!_random.Prob(reflect.ReflectProb) ||
if (args.Reflected)
return;
if (!ent.Comp.InRightPlace)
return; // only reflect when equipped correctly
if (TryReflectHitscan(ent, ent.Owner, args.Shooter, args.SourceItem, args.Direction, args.Reflective, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
}
private void OnReflectCollide(Entity<ReflectComponent> ent, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
if (TryReflectProjectile(ent, ent.Owner, args.ProjUid))
args.Cancelled = true;
}
private void OnReflectHitscan(Entity<ReflectComponent> ent, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected)
return;
if (TryReflectHitscan(ent, ent.Owner, args.Shooter, args.SourceItem, args.Direction, args.Reflective, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
}
private bool TryReflectProjectile(Entity<ReflectComponent> reflector, EntityUid user, Entity<ProjectileComponent?> projectile)
{
if (!TryComp<ReflectiveComponent>(projectile, out var reflective) ||
(reflector.Comp.Reflects & reflective.Reflective) == 0x0 ||
!_toggle.IsActivated(reflector.Owner) ||
!_random.Prob(reflector.Comp.ReflectProb) ||
!TryComp<PhysicsComponent>(projectile, out var physics))
{
return false;
}
var rotation = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2).Opposite();
var rotation = _random.NextAngle(-reflector.Comp.Spread / 2, reflector.Comp.Spread / 2).Opposite();
var existingVelocity = _physics.GetMapLinearVelocity(projectile, component: physics);
var relativeVelocity = existingVelocity - _physics.GetMapLinearVelocity(user);
var newVelocity = rotation.RotateVec(relativeVelocity);
@@ -118,15 +120,15 @@ public sealed class ReflectSystem : EntitySystem
var newRot = rotation.RotateVec(locRot.ToVec());
_transform.SetLocalRotation(projectile, newRot.ToAngle());
PlayAudioAndPopup(reflect, user);
PlayAudioAndPopup(reflector.Comp, user);
if (Resolve(projectile, ref projectileComp, false))
if (Resolve(projectile, ref projectile.Comp, false))
{
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectileComp.Weapon)} shot by {projectileComp.Shooter}");
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectile.Comp.Weapon)} shot by {projectile.Comp.Shooter}");
projectileComp.Shooter = user;
projectileComp.Weapon = user;
Dirty(projectile, projectileComp);
projectile.Comp.Shooter = user;
projectile.Comp.Weapon = user;
Dirty(projectile, projectile.Comp);
}
else
{
@@ -135,19 +137,34 @@ public sealed class ReflectSystem : EntitySystem
return true;
}
private void OnReflectHitscan(EntityUid uid, ReflectComponent component, ref HitScanReflectAttemptEvent args)
private bool TryReflectHitscan(
Entity<ReflectComponent> reflector,
EntityUid user,
EntityUid? shooter,
EntityUid shotSource,
Vector2 direction,
ReflectType hitscanReflectType,
[NotNullWhen(true)] out Vector2? newDirection)
{
if (args.Reflected)
if ((reflector.Comp.Reflects & hitscanReflectType) == 0x0 ||
!_toggle.IsActivated(reflector.Owner) ||
!_random.Prob(reflector.Comp.ReflectProb))
{
return;
newDirection = null;
return false;
}
if (TryReflectHitscan(uid, uid, args.Shooter, args.SourceItem, args.Direction, args.Reflective, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
PlayAudioAndPopup(reflector.Comp, user);
var spread = _random.NextAngle(-reflector.Comp.Spread / 2, reflector.Comp.Spread / 2);
newDirection = -spread.RotateVec(direction);
if (shooter != null)
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)} shot by {ToPrettyString(shooter.Value)}");
else
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)}");
return true;
}
private void PlayAudioAndPopup(ReflectComponent reflect, EntityUid user)
@@ -160,83 +177,27 @@ public sealed class ReflectSystem : EntitySystem
}
}
private bool TryReflectHitscan(
EntityUid user,
EntityUid reflector,
EntityUid? shooter,
EntityUid shotSource,
Vector2 direction,
ReflectType hitscanReflectType,
[NotNullWhen(true)] out Vector2? newDirection)
private void OnReflectEquipped(Entity<ReflectComponent> ent, ref GotEquippedEvent args)
{
if (!TryComp<ReflectComponent>(reflector, out var reflect) ||
(reflect.Reflects & hitscanReflectType) == 0x0 ||
!_toggle.IsActivated(reflector) ||
!_random.Prob(reflect.ReflectProb))
{
newDirection = null;
return false;
}
PlayAudioAndPopup(reflect, user);
var spread = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2);
newDirection = -spread.RotateVec(direction);
if (shooter != null)
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)} shot by {ToPrettyString(shooter.Value)}");
else
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)}");
return true;
ent.Comp.InRightPlace = (ent.Comp.SlotFlags & args.SlotFlags) == args.SlotFlags;
Dirty(ent);
}
private void OnReflectEquipped(EntityUid uid, ReflectComponent component, GotEquippedEvent args)
private void OnReflectUnequipped(Entity<ReflectComponent> ent, ref GotUnequippedEvent args)
{
if (_gameTiming.ApplyingState)
return;
EnsureComp<ReflectUserComponent>(args.Equipee);
ent.Comp.InRightPlace = false;
Dirty(ent);
}
private void OnReflectUnequipped(EntityUid uid, ReflectComponent comp, GotUnequippedEvent args)
private void OnReflectHandEquipped(Entity<ReflectComponent> ent, ref GotEquippedHandEvent args)
{
RefreshReflectUser(args.Equipee);
ent.Comp.InRightPlace = ent.Comp.ReflectingInHands;
Dirty(ent);
}
private void OnReflectHandEquipped(EntityUid uid, ReflectComponent component, GotEquippedHandEvent args)
private void OnReflectHandUnequipped(Entity<ReflectComponent> ent, ref GotUnequippedHandEvent args)
{
if (_gameTiming.ApplyingState)
return;
EnsureComp<ReflectUserComponent>(args.User);
}
private void OnReflectHandUnequipped(EntityUid uid, ReflectComponent component, GotUnequippedHandEvent args)
{
RefreshReflectUser(args.User);
}
private void OnToggleReflect(EntityUid uid, ReflectComponent comp, ref ItemToggledEvent args)
{
if (args.User is {} user)
RefreshReflectUser(user);
}
/// <summary>
/// Refreshes whether someone has reflection potential so we can raise directed events on them.
/// </summary>
private void RefreshReflectUser(EntityUid user)
{
foreach (var ent in _inventorySystem.GetHandOrInventoryEntities(user, SlotFlags.All & ~SlotFlags.POCKET))
{
if (!HasComp<ReflectComponent>(ent) || !_toggle.IsActivated(ent))
continue;
EnsureComp<ReflectUserComponent>(user);
return;
}
RemCompDeferred<ReflectUserComponent>(user);
ent.Comp.InRightPlace = false;
Dirty(ent);
}
}

View File

@@ -1,10 +0,0 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Weapons.Reflect;
/// <summary>
/// Added to an entity if it equips a reflection item in a hand slot or into its clothing.
/// Reflection events will then be relayed.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class ReflectUserComponent : Component;

View File

@@ -83,6 +83,7 @@
reflectProb: 1
reflects:
- Energy
reflectingInHands: false
#Detective's vest
- type: entity

View File

@@ -169,6 +169,8 @@
- Back
- Belt
- type: Reflect
slotFlags:
- NONE
#Greatswords
- type: entity