]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add reflection for crystals (#16426)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Mon, 15 May 2023 05:21:05 +0000 (15:21 +1000)
committerGitHub <noreply@github.com>
Mon, 15 May 2023 05:21:05 +0000 (15:21 +1000)
Content.Server/Projectiles/ProjectileSystem.cs
Content.Server/Weapons/Ranged/Systems/GunSystem.cs
Content.Shared/Projectiles/ProjectileCollideEvent.cs [new file with mode: 0644]
Content.Shared/Weapons/Ranged/Components/ReflectiveComponent.cs [new file with mode: 0644]
Content.Shared/Weapons/Ranged/Events/HitScanReflectAttempt.cs
Content.Shared/Weapons/Ranged/HitscanPrototype.cs
Content.Shared/Weapons/Reflect/ReflectComponent.cs
Content.Shared/Weapons/Reflect/SharedReflectSystem.cs
Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml
Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml
Resources/Prototypes/Entities/Structures/Decoration/crystals.yml

index af517a0231a9d73a3e89592e4b3032564756c68b..a8803a17235bdce6886bcad0126a175a73b619e0 100644 (file)
@@ -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)
+        var ev = new ProjectileCollideEvent(uid, false);
+        RaiseLocalEvent(args.OtherEntity, ref ev);
+
+        if (!ev.Cancelled)
         {
-            QueueDel(uid);
+            component.DamagedEntity = true;
+
+            if (component.DeleteOnCollide)
+            {
+                QueueDel(uid);
+            }
 
-            if (component.ImpactEffect != null && TryComp<TransformComponent>(component.Owner, out var xform))
+            if (component.ImpactEffect != null && TryComp<TransformComponent>(uid, out var xform))
             {
                 RaiseNetworkEvent(new ImpactEffectEvent(component.ImpactEffect, xform.Coordinates), Filter.Pvs(xform.Coordinates, entityMan: EntityManager));
             }
index c872a8d975d2ca71007a124a6af5ea6064db1b5d..9831b0643e05c2b62a8a5a92505e8a46587cab2c 100644 (file)
@@ -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 (file)
index 0000000..0f3c463
--- /dev/null
@@ -0,0 +1,7 @@
+namespace Content.Shared.Projectiles;
+
+/// <summary>
+/// Raised directed on what a projectile collides with. Can have its deletion cancelled.
+/// </summary>
+[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 (file)
index 0000000..65cc21c
--- /dev/null
@@ -0,0 +1,15 @@
+using Content.Shared.Weapons.Reflect;
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Weapons.Ranged.Components;
+
+/// <summary>
+/// Can this entity be reflected.
+/// Only applies if it is shot like a projectile and not if it is thrown.
+/// </summary>
+[RegisterComponent, NetworkedComponent]
+public sealed class ReflectiveComponent : Component
+{
+    [ViewVariables(VVAccess.ReadWrite), DataField("reflective")]
+    public ReflectType Reflective = ReflectType.NonEnergy;
+}
index 2bee1e41b48d81e06df25f48be949432d1ef8545..9857da87bc02b96cc4ffb92cbed1bd7d2cb937d6 100644 (file)
@@ -1,3 +1,5 @@
+using Content.Shared.Weapons.Reflect;
+
 namespace Content.Shared.Weapons.Ranged.Events;
 
 /// <summary>
@@ -5,4 +7,4 @@ namespace Content.Shared.Weapons.Ranged.Events;
 /// and changing <see cref="Direction"/> where shot will go next
 /// </summary>
 [ByRefEvent]
-public record struct HitScanReflectAttemptEvent(Vector2 Direction, bool Reflected);
+public record struct HitScanReflectAttemptEvent(ReflectType Reflective, Vector2 Direction, bool Reflected);
index fe50de724505cf7eceac81f763f9a436d84c9835..17f9ff4793987b9c0f7ac80cfad6844b4cf3e963 100644 (file)
@@ -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;
 
+    /// <summary>
+    /// What we count as for reflection.
+    /// </summary>
+    [DataField("reflective")] public ReflectType Reflective = ReflectType.Energy;
+
     /// <summary>
     /// Sound that plays upon the thing being hit.
     /// </summary>
index 2653cf198a7345c7bf469b27df74579ef72047ce..8e7b8975d9d1b128ac21a7213c351ee55863e880 100644 (file)
@@ -1,44 +1,43 @@
 using Robust.Shared.Audio;
 using Robust.Shared.GameStates;
-using Robust.Shared.Serialization;
 
 namespace Content.Shared.Weapons.Reflect;
 
 /// <summary>
 /// Entities with this component have a chance to reflect projectiles and hitscan shots
 /// </summary>
-[RegisterComponent, NetworkedComponent]
-public sealed class ReflectComponent : Component
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class ReflectComponent : Component
 {
     /// <summary>
     /// Can only reflect when enabled
     /// </summary>
-    [DataField("enabled"), ViewVariables(VVAccess.ReadWrite)]
+    [DataField("enabled"), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
     public bool Enabled = true;
 
+    /// <summary>
+    /// What we reflect.
+    /// </summary>
+    [ViewVariables(VVAccess.ReadWrite), DataField("reflects")]
+    public ReflectType Reflects = ReflectType.Energy | ReflectType.NonEnergy;
+
     /// <summary>
     /// Probability for a projectile to be reflected.
     /// </summary>
-    [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,
 }
index 7ab56ab8aac6829482b561de26da9a338b4aee2b..0940f9dfadad708e9da1d62165d291eba834205a 100644 (file)
@@ -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;
 /// </summary>
 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<HandsComponent, ProjectileReflectAttemptEvent>(OnHandReflectProjectile);
         SubscribeLocalEvent<HandsComponent, HitScanReflectAttemptEvent>(OnHandsReflectHitscan);
 
-        SubscribeLocalEvent<ReflectComponent, ComponentHandleState>(OnHandleState);
-        SubscribeLocalEvent<ReflectComponent, ComponentGetState>(OnGetState);
+        SubscribeLocalEvent<ReflectComponent, ProjectileCollideEvent>(OnReflectCollide);
+        SubscribeLocalEvent<ReflectComponent, HitScanReflectAttemptEvent>(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<ReflectComponent>(reflector, out var reflect) ||
+        if (!Resolve(reflector, ref reflect, false) ||
             !reflect.Enabled ||
+            !TryComp<ReflectiveComponent>(projectile, out var reflective) ||
+            (reflect.Reflects & reflective.Reflective) == 0x0 ||
             !_random.Prob(reflect.ReflectProb) ||
             !TryComp<PhysicsComponent>(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(hands.ActiveHandEntity.Value, args.Direction, out var dir))
+        {
+            args.Direction = dir.Value;
+            args.Reflected = true;
+        }
+    }
+
+    private void OnReflectHitscan(EntityUid uid, ReflectComponent component, ref HitScanReflectAttemptEvent args)
+    {
+        if (args.Reflected ||
+            (component.Reflects & args.Reflective) == 0x0)
+        {
             return;
+        }
 
-        if (TryReflectHitscan(uid, hands.ActiveHandEntity, args.Direction, out var dir))
+        if (TryReflectHitscan(uid, 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 bool TryReflectHitscan(EntityUid reflector, Vector2 direction,
+        [NotNullWhen(true)] out Vector2? newDirection)
     {
-        if (TryComp<ReflectComponent>(reflector, out var reflect) &&
-            reflect.Enabled &&
-            _random.Prob(reflect.ReflectProb))
+        if (!TryComp<ReflectComponent>(reflector, out var reflect) ||
+            !reflect.Enabled ||
+            !_random.Prob(reflect.ReflectProb))
+        {
+            newDirection = null;
+            return false;
+        }
+
+        if (_netManager.IsServer)
         {
-            _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;
+            _popup.PopupEntity(Loc.GetString("reflect-shot"), reflector);
+            _audio.PlayPvs(reflect.SoundOnReflect, reflector, AudioHelpers.WithVariation(0.05f, _random));
         }
 
-        newDirection = null;
-        return false;
+        var spread = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2);
+        newDirection = -spread.RotateVec(direction);
+        return true;
     }
 }
index b03a2c969228393c345a21c070d5d01ae5ed2523..f59153f3203185a4b9ad8fd1206d9562d304d787 100644 (file)
@@ -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
   parent: BaseBullet
   noSpawn: true
   components:
+  - type: Reflective
+    reflective:
+    - Energy
   - type: FlyBySound
     sound:
       collection: EnergyMiss
   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
   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
index 6e0dab316aaf8444d425d448352a419d4c7a506a..f2e30dc7ee67934af4da2f070a41a10e6dfd4b46 100644 (file)
@@ -53,8 +53,6 @@
     malus: 0
   - type: Reflect
     enabled: false
-    reflectProb: 0.25
-    spread: 45
 
 - type: entity
   name: pen
index 704de51c66ab7ee089ff72cf956962d78a096bb9..b8f2a03a05b307cbd8158f865bd64c2349f02cb4 100644 (file)
@@ -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