Reflect changes (#15071)

This commit is contained in:
metalgearsloth
2023-04-19 20:02:30 +10:00
committed by GitHub
parent 31851e5468
commit 7102cc7159
3 changed files with 49 additions and 43 deletions

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

View 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();
var relVel = _physics.GetMapLinearVelocity(projectile) - _physics.GetMapLinearVelocity(user);
var newVel = rotation.RotateVec(relVel);
_physics.SetLinearVelocity(projectile, newVel);
var locRot = Transform(projectile).LocalRotation;
var newRot = rotation.RotateVec(locRot.ToVec());
_transform.SetLocalRotation(projectile, newRot.ToAngle());
_popup.PopupEntity(Loc.GetString("reflect-shot"), user, PopupType.Small);
_audio.PlayPvs(reflect.OnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
return true;
return false;
}
return false;
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);
// Have the velocity in world terms above so need to convert it back to local.
var difference = newVelocity - existingVelocity;
_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;
}

View File

@@ -53,8 +53,7 @@
malus: 0
- type: Reflect
enabled: false
energeticChance: 0.5
kineticChance: 0.25
reflectProb: 0.25
spread: 45
- type: entity