diff --git a/Content.Server/Projectiles/ProjectileSystem.cs b/Content.Server/Projectiles/ProjectileSystem.cs index af517a0231..a8803a1723 100644 --- a/Content.Server/Projectiles/ProjectileSystem.cs +++ b/Content.Server/Projectiles/ProjectileSystem.cs @@ -14,7 +14,6 @@ using Robust.Shared.Physics.Events; namespace Content.Server.Projectiles; -[UsedImplicitly] public sealed class ProjectileSystem : SharedProjectileSystem { [Dependency] private readonly IAdminLogManager _adminLogger = default!; @@ -47,7 +46,6 @@ public sealed class ProjectileSystem : SharedProjectileSystem var otherName = ToPrettyString(otherEntity); var direction = args.OurBody.LinearVelocity.Normalized; var modifiedDamage = _damageableSystem.TryChangeDamage(otherEntity, component.Damage, component.IgnoreResistances, origin: component.Shooter); - component.DamagedEntity = true; var deleted = Deleted(otherEntity); if (modifiedDamage is not null && EntityManager.EntityExists(component.Shooter)) @@ -68,11 +66,19 @@ public sealed class ProjectileSystem : SharedProjectileSystem _sharedCameraRecoil.KickCamera(otherEntity, direction); } - if (component.DeleteOnCollide) - { - QueueDel(uid); + var ev = new ProjectileCollideEvent(uid, false); + RaiseLocalEvent(args.OtherEntity, ref ev); - if (component.ImpactEffect != null && TryComp(component.Owner, out var xform)) + if (!ev.Cancelled) + { + component.DamagedEntity = true; + + if (component.DeleteOnCollide) + { + QueueDel(uid); + } + + if (component.ImpactEffect != null && TryComp(uid, out var xform)) { RaiseNetworkEvent(new ImpactEffectEvent(component.ImpactEffect, xform.Coordinates), Filter.Pvs(xform.Coordinates, entityMan: EntityManager)); } diff --git a/Content.Server/Weapons/Ranged/Systems/GunSystem.cs b/Content.Server/Weapons/Ranged/Systems/GunSystem.cs index c872a8d975..9831b0643e 100644 --- a/Content.Server/Weapons/Ranged/Systems/GunSystem.cs +++ b/Content.Server/Weapons/Ranged/Systems/GunSystem.cs @@ -16,6 +16,7 @@ using Content.Shared.Weapons.Melee; using Content.Shared.Weapons.Ranged; using Content.Shared.Weapons.Ranged.Components; using Content.Shared.Weapons.Ranged.Events; +using Content.Shared.Weapons.Reflect; using Robust.Server.GameObjects; using Robust.Shared.Audio; using Robust.Shared.Map; @@ -40,7 +41,6 @@ public sealed partial class GunSystem : SharedGunSystem [Dependency] private readonly SharedTransformSystem _transform = default!; [Dependency] private readonly BatterySystem _battery = default!; - public const float DamagePitchVariation = SharedMeleeWeaponSystem.DamagePitchVariation; public const float GunClumsyChance = 0.5f; @@ -185,30 +185,34 @@ public sealed partial class GunSystem : SharedGunSystem var fromEffect = fromCoordinates; // can't use map coords above because funny FireEffects var dir = mapDirection.Normalized; var lastUser = user; - for (var reflectAttempt = 0; reflectAttempt < 3; reflectAttempt++) + + if (hitscan.Reflective != ReflectType.None) { - var ray = new CollisionRay(from.Position, dir, hitscan.CollisionMask); - var rayCastResults = - Physics.IntersectRay(from.MapId, ray, hitscan.MaxLength, lastUser, false).ToList(); - if (!rayCastResults.Any()) - break; + for (var reflectAttempt = 0; reflectAttempt < 3; reflectAttempt++) + { + var ray = new CollisionRay(from.Position, dir, hitscan.CollisionMask); + var rayCastResults = + Physics.IntersectRay(from.MapId, ray, hitscan.MaxLength, lastUser, false).ToList(); + if (!rayCastResults.Any()) + break; - var result = rayCastResults[0]; - var hit = result.HitEntity; - lastHit = hit; + var result = rayCastResults[0]; + var hit = result.HitEntity; + lastHit = hit; - FireEffects(fromEffect, result.Distance, dir.Normalized.ToAngle(), hitscan, hit); + FireEffects(fromEffect, result.Distance, dir.Normalized.ToAngle(), hitscan, hit); - var ev = new HitScanReflectAttemptEvent(dir, false); - RaiseLocalEvent(hit, ref ev); + var ev = new HitScanReflectAttemptEvent(hitscan.Reflective, dir, false); + RaiseLocalEvent(hit, ref ev); - if (!ev.Reflected) - break; + if (!ev.Reflected) + break; - fromEffect = Transform(hit).Coordinates; - from = fromEffect.ToMap(EntityManager, _transform); - dir = ev.Direction; - lastUser = hit; + fromEffect = Transform(hit).Coordinates; + from = fromEffect.ToMap(EntityManager, _transform); + dir = ev.Direction; + lastUser = hit; + } } if (lastHit != null) @@ -395,7 +399,7 @@ public sealed partial class GunSystem : SharedGunSystem if (xformQuery.TryGetComponent(gridUid, out var gridXform)) { - var (_, gridRot, gridInvMatrix) = TransformSystem.GetWorldPositionRotationInvMatrix(gridUid.Value, xformQuery); + var (_, gridRot, gridInvMatrix) = TransformSystem.GetWorldPositionRotationInvMatrix(gridXform, xformQuery); fromCoordinates = new EntityCoordinates(gridUid.Value, gridInvMatrix.Transform(fromCoordinates.ToMapPos(EntityManager, TransformSystem))); diff --git a/Content.Shared/Projectiles/ProjectileCollideEvent.cs b/Content.Shared/Projectiles/ProjectileCollideEvent.cs new file mode 100644 index 0000000000..0f3c463a3e --- /dev/null +++ b/Content.Shared/Projectiles/ProjectileCollideEvent.cs @@ -0,0 +1,7 @@ +namespace Content.Shared.Projectiles; + +/// +/// Raised directed on what a projectile collides with. Can have its deletion cancelled. +/// +[ByRefEvent] +public record struct ProjectileCollideEvent(EntityUid OtherEntity, bool Cancelled); diff --git a/Content.Shared/Weapons/Ranged/Components/ReflectiveComponent.cs b/Content.Shared/Weapons/Ranged/Components/ReflectiveComponent.cs new file mode 100644 index 0000000000..65cc21c314 --- /dev/null +++ b/Content.Shared/Weapons/Ranged/Components/ReflectiveComponent.cs @@ -0,0 +1,15 @@ +using Content.Shared.Weapons.Reflect; +using Robust.Shared.GameStates; + +namespace Content.Shared.Weapons.Ranged.Components; + +/// +/// Can this entity be reflected. +/// Only applies if it is shot like a projectile and not if it is thrown. +/// +[RegisterComponent, NetworkedComponent] +public sealed class ReflectiveComponent : Component +{ + [ViewVariables(VVAccess.ReadWrite), DataField("reflective")] + public ReflectType Reflective = ReflectType.NonEnergy; +} diff --git a/Content.Shared/Weapons/Ranged/Events/HitScanReflectAttempt.cs b/Content.Shared/Weapons/Ranged/Events/HitScanReflectAttempt.cs index 2bee1e41b4..9857da87bc 100644 --- a/Content.Shared/Weapons/Ranged/Events/HitScanReflectAttempt.cs +++ b/Content.Shared/Weapons/Ranged/Events/HitScanReflectAttempt.cs @@ -1,3 +1,5 @@ +using Content.Shared.Weapons.Reflect; + namespace Content.Shared.Weapons.Ranged.Events; /// @@ -5,4 +7,4 @@ namespace Content.Shared.Weapons.Ranged.Events; /// and changing where shot will go next /// [ByRefEvent] -public record struct HitScanReflectAttemptEvent(Vector2 Direction, bool Reflected); +public record struct HitScanReflectAttemptEvent(ReflectType Reflective, Vector2 Direction, bool Reflected); diff --git a/Content.Shared/Weapons/Ranged/HitscanPrototype.cs b/Content.Shared/Weapons/Ranged/HitscanPrototype.cs index fe50de7245..17f9ff4793 100644 --- a/Content.Shared/Weapons/Ranged/HitscanPrototype.cs +++ b/Content.Shared/Weapons/Ranged/HitscanPrototype.cs @@ -1,5 +1,6 @@ using Content.Shared.Damage; using Content.Shared.Physics; +using Content.Shared.Weapons.Reflect; using Robust.Shared.Audio; using Robust.Shared.Prototypes; using Robust.Shared.Utility; @@ -31,6 +32,11 @@ public sealed class HitscanPrototype : IPrototype, IShootable [DataField("collisionMask")] public int CollisionMask = (int) CollisionGroup.Opaque; + /// + /// What we count as for reflection. + /// + [DataField("reflective")] public ReflectType Reflective = ReflectType.Energy; + /// /// Sound that plays upon the thing being hit. /// diff --git a/Content.Shared/Weapons/Reflect/ReflectComponent.cs b/Content.Shared/Weapons/Reflect/ReflectComponent.cs index 2653cf198a..8e7b8975d9 100644 --- a/Content.Shared/Weapons/Reflect/ReflectComponent.cs +++ b/Content.Shared/Weapons/Reflect/ReflectComponent.cs @@ -1,44 +1,43 @@ using Robust.Shared.Audio; using Robust.Shared.GameStates; -using Robust.Shared.Serialization; namespace Content.Shared.Weapons.Reflect; /// /// Entities with this component have a chance to reflect projectiles and hitscan shots /// -[RegisterComponent, NetworkedComponent] -public sealed class ReflectComponent : Component +[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] +public sealed partial class ReflectComponent : Component { /// /// Can only reflect when enabled /// - [DataField("enabled"), ViewVariables(VVAccess.ReadWrite)] + [DataField("enabled"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] public bool Enabled = true; + /// + /// What we reflect. + /// + [ViewVariables(VVAccess.ReadWrite), DataField("reflects")] + public ReflectType Reflects = ReflectType.Energy | ReflectType.NonEnergy; + /// /// Probability for a projectile to be reflected. /// - [DataField("reflectProb"), ViewVariables(VVAccess.ReadWrite)] - public float ReflectProb; + [DataField("reflectProb"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] + public float ReflectProb = 0.25f; - [DataField("spread"), ViewVariables(VVAccess.ReadWrite)] - public Angle Spread = Angle.FromDegrees(5); + [DataField("spread"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] + public Angle Spread = Angle.FromDegrees(45); [DataField("soundOnReflect")] public SoundSpecifier? SoundOnReflect = new SoundPathSpecifier("/Audio/Weapons/Guns/Hits/laser_sear_wall.ogg"); } -[Serializable, NetSerializable] -public sealed class ReflectComponentState : ComponentState +[Flags] +public enum ReflectType : byte { - public bool Enabled; - public float ReflectProb; - public Angle Spread; - public ReflectComponentState(bool enabled, float reflectProb, Angle spread) - { - Enabled = enabled; - ReflectProb = reflectProb; - Spread = spread; - } + None = 0, + NonEnergy = 1 << 0, + Energy = 1 << 1, } diff --git a/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs b/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs index 7ab56ab8aa..0940f9dfad 100644 --- a/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs +++ b/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs @@ -1,10 +1,12 @@ using System.Diagnostics.CodeAnalysis; using Content.Shared.Audio; using Content.Shared.Hands.Components; -using Robust.Shared.GameStates; using Content.Shared.Weapons.Ranged.Events; using Robust.Shared.Physics.Components; using Content.Shared.Popups; +using Content.Shared.Projectiles; +using Content.Shared.Weapons.Ranged.Components; +using Robust.Shared.Network; using Robust.Shared.Physics.Systems; using Robust.Shared.Random; @@ -15,6 +17,7 @@ namespace Content.Shared.Weapons.Reflect; /// public abstract class SharedReflectSystem : EntitySystem { + [Dependency] private readonly INetManager _netManager = default!; [Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly SharedPopupSystem _popup = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!; @@ -27,23 +30,19 @@ public abstract class SharedReflectSystem : EntitySystem SubscribeLocalEvent(OnHandReflectProjectile); SubscribeLocalEvent(OnHandsReflectHitscan); - SubscribeLocalEvent(OnHandleState); - SubscribeLocalEvent(OnGetState); + SubscribeLocalEvent(OnReflectCollide); + SubscribeLocalEvent(OnReflectHitscan); } - private static void OnHandleState(EntityUid uid, ReflectComponent component, ref ComponentHandleState args) + private void OnReflectCollide(EntityUid uid, ReflectComponent component, ref ProjectileCollideEvent args) { - if (args.Current is not ReflectComponentState state) + if (args.Cancelled) + { return; + } - component.Enabled = state.Enabled; - component.ReflectProb = state.ReflectProb; - component.Spread = state.Spread; - } - - private static void OnGetState(EntityUid uid, ReflectComponent component, ref ComponentGetState args) - { - args.State = new ReflectComponentState(component.Enabled, component.ReflectProb, component.Spread); + if (TryReflectProjectile(uid, args.OtherEntity, reflect: component)) + args.Cancelled = true; } private void OnHandReflectProjectile(EntityUid uid, HandsComponent hands, ref ProjectileReflectAttemptEvent args) @@ -51,14 +50,16 @@ public abstract class SharedReflectSystem : EntitySystem if (args.Cancelled) return; - if (TryReflectProjectile(uid, hands.ActiveHandEntity, args.ProjUid)) + if (hands.ActiveHandEntity != null && TryReflectProjectile(hands.ActiveHandEntity.Value, args.ProjUid)) args.Cancelled = true; } - private bool TryReflectProjectile(EntityUid user, EntityUid? reflector, EntityUid projectile) + private bool TryReflectProjectile(EntityUid reflector, EntityUid projectile, ProjectileComponent? projectileComp = null, ReflectComponent? reflect = null) { - if (!TryComp(reflector, out var reflect) || + if (!Resolve(reflector, ref reflect, false) || !reflect.Enabled || + !TryComp(projectile, out var reflective) || + (reflect.Reflects & reflective.Reflective) == 0x0 || !_random.Prob(reflect.ReflectProb) || !TryComp(projectile, out var physics)) { @@ -67,7 +68,7 @@ public abstract class SharedReflectSystem : EntitySystem var rotation = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2).Opposite(); var existingVelocity = _physics.GetMapLinearVelocity(projectile, component: physics); - var relativeVelocity = existingVelocity - _physics.GetMapLinearVelocity(user); + var relativeVelocity = existingVelocity - _physics.GetMapLinearVelocity(reflector); var newVelocity = rotation.RotateVec(relativeVelocity); // Have the velocity in world terms above so need to convert it back to local. @@ -79,37 +80,68 @@ public abstract class SharedReflectSystem : EntitySystem var newRot = rotation.RotateVec(locRot.ToVec()); _transform.SetLocalRotation(projectile, newRot.ToAngle()); - _popup.PopupEntity(Loc.GetString("reflect-shot"), user); - _audio.PlayPvs(reflect.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random)); + if (_netManager.IsServer) + { + _popup.PopupEntity(Loc.GetString("reflect-shot"), reflector); + _audio.PlayPvs(reflect.SoundOnReflect, reflector, AudioHelpers.WithVariation(0.05f, _random)); + } + + if (Resolve(projectile, ref projectileComp, false)) + { + projectileComp.Shooter = reflector; + projectileComp.Weapon = reflector; + Dirty(projectileComp); + } + return true; } private void OnHandsReflectHitscan(EntityUid uid, HandsComponent hands, ref HitScanReflectAttemptEvent args) { - if (args.Reflected) + if (args.Reflected || hands.ActiveHandEntity == null) return; - if (TryReflectHitscan(uid, hands.ActiveHandEntity, args.Direction, out var dir)) + if (TryReflectHitscan(hands.ActiveHandEntity.Value, args.Direction, out var dir)) { args.Direction = dir.Value; args.Reflected = true; } } - private bool TryReflectHitscan(EntityUid user, EntityUid? reflector, Vector2 direction, [NotNullWhen(true)] out Vector2? newDirection) + private void OnReflectHitscan(EntityUid uid, ReflectComponent component, ref HitScanReflectAttemptEvent args) { - if (TryComp(reflector, out var reflect) && - reflect.Enabled && - _random.Prob(reflect.ReflectProb)) + if (args.Reflected || + (component.Reflects & args.Reflective) == 0x0) { - _popup.PopupEntity(Loc.GetString("reflect-shot"), user, PopupType.Small); - _audio.PlayPvs(reflect.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random)); - var spread = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2); - newDirection = -spread.RotateVec(direction); - return true; + return; } - newDirection = null; - return false; + if (TryReflectHitscan(uid, args.Direction, out var dir)) + { + args.Direction = dir.Value; + args.Reflected = true; + } + } + + private bool TryReflectHitscan(EntityUid reflector, Vector2 direction, + [NotNullWhen(true)] out Vector2? newDirection) + { + if (!TryComp(reflector, out var reflect) || + !reflect.Enabled || + !_random.Prob(reflect.ReflectProb)) + { + newDirection = null; + return false; + } + + if (_netManager.IsServer) + { + _popup.PopupEntity(Loc.GetString("reflect-shot"), reflector); + _audio.PlayPvs(reflect.SoundOnReflect, reflector, AudioHelpers.WithVariation(0.05f, _random)); + } + + var spread = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2); + newDirection = -spread.RotateVec(direction); + return true; } } diff --git a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml index b03a2c9692..f59153f320 100644 --- a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml +++ b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml @@ -25,6 +25,7 @@ description: If you can see this you're probably dead! abstract: true components: + - type: Reflective - type: FlyBySound - type: Clickable - type: Sprite @@ -176,6 +177,9 @@ parent: BaseBullet noSpawn: true components: + - type: Reflective + reflective: + - Energy - type: FlyBySound sound: collection: EnergyMiss @@ -293,6 +297,9 @@ noSpawn: true description: Not too bad, but you still don't want to get hit by it. components: + - type: Reflective + reflective: + - NonEnergy - type: Sprite noRot: false sprite: Objects/Weapons/Guns/Projectiles/magic.rsi @@ -316,6 +323,9 @@ noSpawn: true description: Marks a target for additional damage. components: + - type: Reflective + reflective: + - NonEnergy - type: Sprite noRot: false sprite: Objects/Weapons/Guns/Projectiles/magic.rsi diff --git a/Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml b/Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml index 6e0dab316a..f2e30dc7ee 100644 --- a/Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml +++ b/Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml @@ -53,8 +53,6 @@ malus: 0 - type: Reflect enabled: false - reflectProb: 0.25 - spread: 45 - type: entity name: pen diff --git a/Resources/Prototypes/Entities/Structures/Decoration/crystals.yml b/Resources/Prototypes/Entities/Structures/Decoration/crystals.yml index 704de51c66..b8f2a03a05 100644 --- a/Resources/Prototypes/Entities/Structures/Decoration/crystals.yml +++ b/Resources/Prototypes/Entities/Structures/Decoration/crystals.yml @@ -7,7 +7,24 @@ - type: Sprite sprite: Structures/Decoration/crystal.rsi state: crystal_green - netsync: false + - type: Reflect + reflectProb: 0.5 + reflects: + - Energy + - type: Fixtures + fixtures: + fix1: + shape: + !type:PhysShapeCircle + radius: 0.45 + density: 60 + mask: + - MachineMask + layer: + - MidImpassable + - LowImpassable + - BulletImpassable + - Opaque - type: PointLight radius: 3 energy: 3