]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Reflect changes (#15071)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Wed, 19 Apr 2023 10:02:30 +0000 (20:02 +1000)
committerGitHub <noreply@github.com>
Wed, 19 Apr 2023 10:02:30 +0000 (20:02 +1000)
Content.Shared/Weapons/Reflect/ReflectComponent.cs
Content.Shared/Weapons/Reflect/SharedReflectSystem.cs
Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml

index d68bc42a0b4561104336fe1c4f191d770b2c0a7f..2653cf198a7345c7bf469b27df74579ef72047ce 100644 (file)
@@ -17,33 +17,28 @@ public sealed class ReflectComponent : Component
     public bool Enabled = true;
 
     /// <summary>
-    /// Reflect chance for hitscan weapons (lasers) and projectiles with heat damage (disabler)
+    /// Probability for a projectile to be reflected.
     /// </summary>
-    [DataField("energeticChance"), ViewVariables(VVAccess.ReadWrite)]
-    public float EnergeticChance;
-
-    [DataField("kineticChance"), ViewVariables(VVAccess.ReadWrite)]
-    public float KineticChance;
+    [DataField("reflectProb"), ViewVariables(VVAccess.ReadWrite)]
+    public float ReflectProb;
 
     [DataField("spread"), ViewVariables(VVAccess.ReadWrite)]
     public Angle Spread = Angle.FromDegrees(5);
 
-    [DataField("onReflect")]
-    public SoundSpecifier? OnReflect = new SoundPathSpecifier("/Audio/Weapons/Guns/Hits/laser_sear_wall.ogg");
+    [DataField("soundOnReflect")]
+    public SoundSpecifier? SoundOnReflect = new SoundPathSpecifier("/Audio/Weapons/Guns/Hits/laser_sear_wall.ogg");
 }
 
 [Serializable, NetSerializable]
 public sealed class ReflectComponentState : ComponentState
 {
     public bool Enabled;
-    public float EnergeticChance;
-    public float KineticChance;
+    public float ReflectProb;
     public Angle Spread;
-    public ReflectComponentState(bool enabled, float energeticChance, float kineticChance, Angle spread)
+    public ReflectComponentState(bool enabled, float reflectProb, Angle spread)
     {
         Enabled = enabled;
-        EnergeticChance = energeticChance;
-        KineticChance = kineticChance;
+        ReflectProb = reflectProb;
         Spread = spread;
     }
 }
index c5801477a30e0b1a45e3b9d41f02331fdf8a6859..baca795bacc28b1934b088a885d7534a5f3e89ac 100644 (file)
@@ -1,6 +1,10 @@
 using System.Diagnostics.CodeAnalysis;
 using Content.Shared.Audio;
 using Content.Shared.Hands.Components;
+using Robust.Shared.GameStates;
+using Content.Shared.Weapons.Ranged.Events;
+using System.Diagnostics.CodeAnalysis;
+using Robust.Shared.Physics.Components;
 using Content.Shared.Popups;
 using Content.Shared.Projectiles;
 using Content.Shared.Weapons.Ranged.Events;
@@ -33,55 +37,62 @@ public abstract class SharedReflectSystem : EntitySystem
 
     private static void OnHandleState(EntityUid uid, ReflectComponent component, ref ComponentHandleState args)
     {
-        if (args.Current is not ReflectComponentState state) return;
+        if (args.Current is not ReflectComponentState state)
+            return;
+
         component.Enabled = state.Enabled;
-        component.EnergeticChance = state.EnergeticChance;
-        component.KineticChance = state.KineticChance;
+        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.EnergeticChance, component.KineticChance, component.Spread);
+        args.State = new ReflectComponentState(component.Enabled, component.ReflectProb, component.Spread);
     }
 
     private void OnHandReflectProjectile(EntityUid uid, HandsComponent hands, ref ProjectileReflectAttemptEvent args)
     {
         if (args.Cancelled)
             return;
-        if (TryReflectProjectile(uid, hands.ActiveHandEntity, args.ProjUid, args.Component))
+
+        if (TryReflectProjectile(uid, hands.ActiveHandEntity, args.ProjUid))
             args.Cancelled = true;
     }
-    
-    private bool TryReflectProjectile(EntityUid user, EntityUid? reflector, EntityUid projectile, ProjectileComponent component)
+
+    private bool TryReflectProjectile(EntityUid user, EntityUid? reflector, EntityUid projectile)
     {
-        var isEnergyProjectile = component.Damage.DamageDict.ContainsKey("Heat");
-        var isKineticProjectile = !isEnergyProjectile;
-        if (TryComp<ReflectComponent>(reflector, out var reflect) &&
-            reflect.Enabled && 
-            (isEnergyProjectile && _random.Prob(reflect.EnergeticChance) || isKineticProjectile && _random.Prob(reflect.KineticChance)))
+        if (!TryComp<ReflectComponent>(reflector, out var reflect) ||
+            !reflect.Enabled ||
+            !_random.Prob(reflect.ReflectProb) ||
+            !TryComp<PhysicsComponent>(projectile, out var physics))
         {
-            var rotation = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2).Opposite();
+            return false;
+        }
 
-            var relVel = _physics.GetMapLinearVelocity(projectile) - _physics.GetMapLinearVelocity(user);
-            var newVel = rotation.RotateVec(relVel);
-            _physics.SetLinearVelocity(projectile, newVel);
+        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 newVelocity = rotation.RotateVec(relativeVelocity);
 
-            var locRot = Transform(projectile).LocalRotation;
-            var newRot = rotation.RotateVec(locRot.ToVec());
-            _transform.SetLocalRotation(projectile, newRot.ToAngle());
+        // Have the velocity in world terms above so need to convert it back to local.
+        var difference = newVelocity - existingVelocity;
 
-            _popup.PopupEntity(Loc.GetString("reflect-shot"), user, PopupType.Small);
-            _audio.PlayPvs(reflect.OnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
-            return true;
-        }
-        return false;
+        _physics.SetLinearVelocity(projectile, physics.LinearVelocity + difference, body: physics);
+
+        var locRot = Transform(projectile).LocalRotation;
+        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));
+        return true;
     }
 
     private void OnHandsReflectHitscan(EntityUid uid, HandsComponent hands, ref HitScanReflectAttemptEvent args)
     {
         if (args.Reflected)
             return;
+
         if (TryReflectHitscan(uid, hands.ActiveHandEntity, args.Direction, out var dir))
         {
             args.Direction = dir.Value;
@@ -93,14 +104,15 @@ public abstract class SharedReflectSystem : EntitySystem
     {
         if (TryComp<ReflectComponent>(reflector, out var reflect) &&
             reflect.Enabled &&
-            _random.Prob(reflect.EnergeticChance))
+            _random.Prob(reflect.ReflectProb))
         {
             _popup.PopupEntity(Loc.GetString("reflect-shot"), user, PopupType.Small);
-            _audio.PlayPvs(reflect.OnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
+            _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;
         }
+
         newDirection = null;
         return false;
     }
index e092f1f7059c4d879ccaa3a75a2cffe71b94dfb1..6e0dab316aaf8444d425d448352a419d4c7a506a 100644 (file)
@@ -53,8 +53,7 @@
     malus: 0
   - type: Reflect
     enabled: false
-    energeticChance: 0.5
-    kineticChance: 0.25
+    reflectProb: 0.25
     spread: 45
 
 - type: entity