Fix effects (#27533)

* Fix effects

- Fix muzzle flash rotations.
- Fix effects so they update every frame.
- Fix effects tanking client performance.

* Fix merge artifact
This commit is contained in:
metalgearsloth
2024-05-02 12:40:07 +10:00
committed by GitHub
parent 6596584722
commit 5053c8afdb
8 changed files with 91 additions and 51 deletions

View File

@@ -1,4 +1,5 @@
using System.Numerics;
using Content.Client.Animations;
using Content.Client.Items;
using Content.Client.Weapons.Ranged.Components;
using Content.Shared.Camera;
@@ -15,6 +16,7 @@ using Robust.Client.Player;
using Robust.Shared.Animations;
using Robust.Shared.Input;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using SharedGunSystem = Content.Shared.Weapons.Ranged.Systems.SharedGunSystem;
@@ -24,13 +26,14 @@ namespace Content.Client.Weapons.Ranged.Systems;
public sealed partial class GunSystem : SharedGunSystem
{
[Dependency] private readonly IComponentFactory _factory = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly AnimationPlayerSystem _animPlayer = default!;
[Dependency] private readonly InputSystem _inputSystem = default!;
[Dependency] private readonly SharedCameraRecoilSystem _recoil = default!;
[Dependency] private readonly IComponentFactory _factory = default!;
[Dependency] private readonly SharedMapSystem _maps = default!;
[ValidatePrototypeId<EntityPrototype>]
public const string HitscanProto = "HitscanEffect";
@@ -123,7 +126,7 @@ public sealed partial class GunSystem : SharedGunSystem
}
};
_animPlayer.Play(ent, null, anim, "hitscan-effect");
_animPlayer.Play(ent, anim, "hitscan-effect");
}
}
@@ -189,6 +192,7 @@ public sealed partial class GunSystem : SharedGunSystem
// to just delete the spawned entities. This is for programmer sanity despite the wasted perf.
// This also means any ammo specific stuff can be grabbed as necessary.
var direction = fromCoordinates.ToMapPos(EntityManager, TransformSystem) - toCoordinates.ToMapPos(EntityManager, TransformSystem);
var worldAngle = direction.ToAngle().Opposite();
foreach (var (ent, shootable) in ammo)
{
@@ -208,7 +212,7 @@ public sealed partial class GunSystem : SharedGunSystem
if (!cartridge.Spent)
{
SetCartridgeSpent(ent!.Value, cartridge, true);
MuzzleFlash(gunUid, cartridge, user);
MuzzleFlash(gunUid, cartridge, worldAngle, user);
Audio.PlayPredicted(gun.SoundGunshotModified, gunUid, user);
Recoil(user, direction, gun.CameraRecoilScalarModified);
// TODO: Can't predict entity deletions.
@@ -226,7 +230,7 @@ public sealed partial class GunSystem : SharedGunSystem
break;
case AmmoComponent newAmmo:
MuzzleFlash(gunUid, newAmmo, user);
MuzzleFlash(gunUid, newAmmo, worldAngle, user);
Audio.PlayPredicted(gun.SoundGunshotModified, gunUid, user);
Recoil(user, direction, gun.CameraRecoilScalarModified);
if (IsClientSide(ent!.Value))
@@ -258,33 +262,41 @@ public sealed partial class GunSystem : SharedGunSystem
PopupSystem.PopupEntity(message, uid.Value, user.Value);
}
protected override void CreateEffect(EntityUid uid, MuzzleFlashEvent message, EntityUid? user = null)
protected override void CreateEffect(EntityUid gunUid, MuzzleFlashEvent message, EntityUid? user = null)
{
if (!Timing.IsFirstTimePredicted)
return;
var gunXform = Transform(gunUid);
var gridUid = gunXform.GridUid;
EntityCoordinates coordinates;
if (message.MatchRotation)
coordinates = new EntityCoordinates(uid, Vector2.Zero);
else if (TryComp<TransformComponent>(uid, out var xform))
coordinates = xform.Coordinates;
if (TryComp(gridUid, out MapGridComponent? mapGrid))
{
coordinates = new EntityCoordinates(gridUid.Value, _maps.LocalToGrid(gridUid.Value, mapGrid, gunXform.Coordinates));
}
else if (gunXform.MapUid != null)
{
coordinates = new EntityCoordinates(gunXform.MapUid.Value, TransformSystem.GetWorldPosition(gunXform));
}
else
{
return;
if (!coordinates.IsValid(EntityManager))
return;
}
var ent = Spawn(message.Prototype, coordinates);
TransformSystem.SetWorldRotationNoLerp(ent, message.Angle);
var effectXform = Transform(ent);
TransformSystem.SetLocalPositionRotation(effectXform,
effectXform.LocalPosition + new Vector2(0f, -0.5f),
effectXform.LocalRotation - MathF.PI / 2);
if (user != null)
{
var track = EnsureComp<TrackUserComponent>(ent);
track.User = user;
track.Offset = Vector2.UnitX / 2f;
}
var lifetime = 0.4f;
if (TryComp<TimedDespawnComponent>(uid, out var despawn))
if (TryComp<TimedDespawnComponent>(gunUid, out var despawn))
{
lifetime = despawn.Lifetime;
}
@@ -309,18 +321,17 @@ public sealed partial class GunSystem : SharedGunSystem
};
_animPlayer.Play(ent, anim, "muzzle-flash");
if (!TryComp(uid, out PointLightComponent? light))
if (!TryComp(gunUid, out PointLightComponent? light))
{
light = (PointLightComponent) _factory.GetComponent(typeof(PointLightComponent));
light.Owner = uid;
light.NetSyncEnabled = false;
AddComp(uid, light);
AddComp(gunUid, light);
}
Lights.SetEnabled(uid, true, light);
Lights.SetRadius(uid, 2f, light);
Lights.SetColor(uid, Color.FromHex("#cc8e2b"), light);
Lights.SetEnergy(uid, 5f, light);
Lights.SetEnabled(gunUid, true, light);
Lights.SetRadius(gunUid, 2f, light);
Lights.SetColor(gunUid, Color.FromHex("#cc8e2b"), light);
Lights.SetEnergy(gunUid, 5f, light);
var animTwo = new Animation()
{
@@ -352,9 +363,9 @@ public sealed partial class GunSystem : SharedGunSystem
}
};
var uidPlayer = EnsureComp<AnimationPlayerComponent>(uid);
var uidPlayer = EnsureComp<AnimationPlayerComponent>(gunUid);
_animPlayer.Stop(uid, uidPlayer, "muzzle-flash-light");
_animPlayer.Play(uid, uidPlayer, animTwo,"muzzle-flash-light");
_animPlayer.Stop(gunUid, uidPlayer, "muzzle-flash-light");
_animPlayer.Play((gunUid, uidPlayer), animTwo,"muzzle-flash-light");
}
}