Add reflection for crystals (#16426)
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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(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<ReflectComponent>(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<ReflectComponent>(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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user