Add events for GunComponent values, muzzle flashes and cartridge spread (#24077)

* Add a modifier event for GunComponent values

* Add docs

* Add VV readwrite to modified values

* Add more docs

* More docs

* Add Gun parameter to GunRefreshModifiersEvent

* Add another event for handling cartridge spread

* Fix pneumatic speed
This commit is contained in:
DrSmugleaf
2024-01-28 15:32:42 -08:00
committed by GitHub
parent 556545e324
commit 4e8b1fb0d3
16 changed files with 284 additions and 90 deletions

View File

@@ -95,18 +95,19 @@ public abstract partial class SharedGunSystem : EntitySystem
SubscribeLocalEvent<GunComponent, CycleModeEvent>(OnCycleMode);
SubscribeLocalEvent<GunComponent, HandSelectedEvent>(OnGunSelected);
SubscribeLocalEvent<GunComponent, EntityUnpausedEvent>(OnGunUnpaused);
#if DEBUG
SubscribeLocalEvent<GunComponent, MapInitEvent>(OnMapInit);
}
private void OnMapInit(EntityUid uid, GunComponent component, MapInitEvent args)
private void OnMapInit(Entity<GunComponent> gun, ref MapInitEvent args)
{
if (component.NextFire > Timing.CurTime)
Log.Warning($"Initializing a map that contains an entity that is on cooldown. Entity: {ToPrettyString(uid)}");
#if DEBUG
if (gun.Comp.NextFire > Timing.CurTime)
Log.Warning($"Initializing a map that contains an entity that is on cooldown. Entity: {ToPrettyString(gun)}");
DebugTools.Assert((component.AvailableModes & component.SelectedMode) != 0x0);
DebugTools.Assert((gun.Comp.AvailableModes & gun.Comp.SelectedMode) != 0x0);
#endif
RefreshModifiers((gun, gun));
}
private void OnGunMelee(EntityUid uid, GunComponent component, MeleeHitEvent args)
@@ -229,7 +230,7 @@ public abstract partial class SharedGunSystem : EntitySystem
private void AttemptShoot(EntityUid user, EntityUid gunUid, GunComponent gun)
{
if (gun.FireRate <= 0f ||
if (gun.FireRateModified <= 0f ||
!_actionBlockerSystem.CanAttack(user))
return;
@@ -259,7 +260,7 @@ public abstract partial class SharedGunSystem : EntitySystem
if (gun.NextFire > curTime)
return;
var fireRate = TimeSpan.FromSeconds(1f / gun.FireRate);
var fireRate = TimeSpan.FromSeconds(1f / gun.FireRateModified);
// First shot
// Previously we checked shotcounter but in some cases all the bullets got dumped at once
@@ -287,7 +288,7 @@ public abstract partial class SharedGunSystem : EntitySystem
shots = Math.Min(shots, 1 - gun.ShotCounter);
break;
case SelectiveFire.Burst:
shots = Math.Min(shots, 3 - gun.ShotCounter);
shots = Math.Min(shots, gun.ShotsPerBurstModified - gun.ShotCounter);
break;
case SelectiveFire.FullAuto:
break;
@@ -468,6 +469,11 @@ public abstract partial class SharedGunSystem : EntitySystem
protected void MuzzleFlash(EntityUid gun, AmmoComponent component, EntityUid? user = null)
{
var attemptEv = new GunMuzzleFlashAttemptEvent();
RaiseLocalEvent(gun, ref attemptEv);
if (attemptEv.Cancelled)
return;
var sprite = component.MuzzleFlash;
if (sprite == null)
@@ -487,6 +493,41 @@ public abstract partial class SharedGunSystem : EntitySystem
var impulseVector = shotDirection * impulseStrength;
Physics.ApplyLinearImpulse(user, -impulseVector, body: userPhysics);
}
public void RefreshModifiers(Entity<GunComponent?> gun)
{
if (!Resolve(gun, ref gun.Comp))
return;
var comp = gun.Comp;
var ev = new GunRefreshModifiersEvent(
(gun, comp),
comp.SoundGunshot,
comp.CameraRecoilScalar,
comp.AngleIncrease,
comp.AngleDecay,
comp.MaxAngle,
comp.MinAngle,
comp.ShotsPerBurst,
comp.FireRate,
comp.ProjectileSpeed
);
RaiseLocalEvent(gun, ref ev);
comp.SoundGunshotModified = ev.SoundGunshot;
comp.CameraRecoilScalarModified = ev.CameraRecoilScalar;
comp.AngleIncreaseModified = ev.AngleIncrease;
comp.AngleDecayModified = ev.AngleDecay;
comp.MaxAngleModified = ev.MaxAngle;
comp.MinAngleModified = ev.MinAngle;
comp.ShotsPerBurstModified = ev.ShotsPerBurst;
comp.FireRateModified = ev.FireRate;
comp.ProjectileSpeedModified = ev.ProjectileSpeed;
Dirty(gun);
}
protected abstract void CreateEffect(EntityUid uid, MuzzleFlashEvent message, EntityUid? user = null);
/// <summary>