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;
}
}
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;
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;
{
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;
}