Cluster grenade refactor and contra markings (#31108)
* Cluster grenade refactor * oopsies on the name * Solve client-side errors * reviews addressed * filling scattering grenades is now predicted * reviews addressed
This commit is contained in:
@@ -1,177 +0,0 @@
|
||||
using Content.Server.Explosion.Components;
|
||||
using Content.Shared.Flash.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Throwing;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Random;
|
||||
using Content.Server.Weapons.Ranged.Systems;
|
||||
using System.Numerics;
|
||||
using Content.Shared.Explosion.Components;
|
||||
using Robust.Server.Containers;
|
||||
using Robust.Server.GameObjects;
|
||||
|
||||
namespace Content.Server.Explosion.EntitySystems;
|
||||
|
||||
public sealed class ClusterGrenadeSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly SharedContainerSystem _container = default!;
|
||||
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] private readonly GunSystem _gun = default!;
|
||||
[Dependency] private readonly TransformSystem _transformSystem = default!;
|
||||
[Dependency] private readonly ContainerSystem _containerSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ClusterGrenadeComponent, ComponentInit>(OnClugInit);
|
||||
SubscribeLocalEvent<ClusterGrenadeComponent, ComponentStartup>(OnClugStartup);
|
||||
SubscribeLocalEvent<ClusterGrenadeComponent, InteractUsingEvent>(OnClugUsing);
|
||||
SubscribeLocalEvent<ClusterGrenadeComponent, TriggerEvent>(OnClugTrigger);
|
||||
}
|
||||
|
||||
private void OnClugInit(EntityUid uid, ClusterGrenadeComponent component, ComponentInit args)
|
||||
{
|
||||
component.GrenadesContainer = _container.EnsureContainer<Container>(uid, "cluster-payload");
|
||||
}
|
||||
|
||||
private void OnClugStartup(Entity<ClusterGrenadeComponent> clug, ref ComponentStartup args)
|
||||
{
|
||||
var component = clug.Comp;
|
||||
if (component.FillPrototype != null)
|
||||
{
|
||||
component.UnspawnedCount = Math.Max(0, component.MaxGrenades - component.GrenadesContainer.ContainedEntities.Count);
|
||||
UpdateAppearance(clug);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnClugUsing(Entity<ClusterGrenadeComponent> clug, ref InteractUsingEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
var component = clug.Comp;
|
||||
|
||||
// TODO: Should use whitelist.
|
||||
if (component.GrenadesContainer.ContainedEntities.Count >= component.MaxGrenades ||
|
||||
!HasComp<FlashOnTriggerComponent>(args.Used))
|
||||
return;
|
||||
|
||||
_containerSystem.Insert(args.Used, component.GrenadesContainer);
|
||||
UpdateAppearance(clug);
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnClugTrigger(Entity<ClusterGrenadeComponent> clug, ref TriggerEvent args)
|
||||
{
|
||||
var component = clug.Comp;
|
||||
component.CountDown = true;
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
var query = EntityQueryEnumerator<ClusterGrenadeComponent>();
|
||||
|
||||
while (query.MoveNext(out var uid, out var clug))
|
||||
{
|
||||
if (clug.CountDown && clug.UnspawnedCount > 0)
|
||||
{
|
||||
var grenadesInserted = clug.GrenadesContainer.ContainedEntities.Count + clug.UnspawnedCount;
|
||||
var thrownCount = 0;
|
||||
var segmentAngle = 360 / grenadesInserted;
|
||||
var grenadeDelay = 0f;
|
||||
|
||||
while (TryGetGrenade(uid, clug, out var grenade))
|
||||
{
|
||||
// var distance = random.NextFloat() * _throwDistance;
|
||||
var angleMin = segmentAngle * thrownCount;
|
||||
var angleMax = segmentAngle * (thrownCount + 1);
|
||||
var angle = Angle.FromDegrees(_random.Next(angleMin, angleMax));
|
||||
if (clug.RandomAngle)
|
||||
angle = _random.NextAngle();
|
||||
thrownCount++;
|
||||
|
||||
switch (clug.GrenadeType)
|
||||
{
|
||||
case GrenadeType.Shoot:
|
||||
ShootProjectile(grenade, angle, clug, uid);
|
||||
break;
|
||||
case GrenadeType.Throw:
|
||||
ThrowGrenade(grenade, angle, clug);
|
||||
break;
|
||||
}
|
||||
|
||||
// give an active timer trigger to the contained grenades when they get launched
|
||||
if (clug.TriggerGrenades)
|
||||
{
|
||||
grenadeDelay += _random.NextFloat(clug.GrenadeTriggerIntervalMin, clug.GrenadeTriggerIntervalMax);
|
||||
var grenadeTimer = EnsureComp<ActiveTimerTriggerComponent>(grenade);
|
||||
grenadeTimer.TimeRemaining = (clug.BaseTriggerDelay + grenadeDelay);
|
||||
var ev = new ActiveTimerTriggerEvent(grenade, uid);
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
}
|
||||
}
|
||||
// delete the empty shell of the clusterbomb
|
||||
Del(uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ShootProjectile(EntityUid grenade, Angle angle, ClusterGrenadeComponent clug, EntityUid clugUid)
|
||||
{
|
||||
var direction = angle.ToVec().Normalized();
|
||||
|
||||
if (clug.RandomSpread)
|
||||
direction = _random.NextVector2().Normalized();
|
||||
|
||||
_gun.ShootProjectile(grenade, direction, Vector2.One.Normalized(), clugUid);
|
||||
|
||||
}
|
||||
|
||||
private void ThrowGrenade(EntityUid grenade, Angle angle, ClusterGrenadeComponent clug)
|
||||
{
|
||||
var direction = angle.ToVec().Normalized() * clug.Distance;
|
||||
|
||||
if (clug.RandomSpread)
|
||||
direction = angle.ToVec().Normalized() * _random.NextFloat(clug.MinSpreadDistance, clug.MaxSpreadDistance);
|
||||
|
||||
_throwingSystem.TryThrow(grenade, direction, clug.Velocity);
|
||||
}
|
||||
|
||||
private bool TryGetGrenade(EntityUid clugUid, ClusterGrenadeComponent component, out EntityUid grenade)
|
||||
{
|
||||
grenade = default;
|
||||
|
||||
if (component.UnspawnedCount > 0)
|
||||
{
|
||||
component.UnspawnedCount--;
|
||||
grenade = Spawn(component.FillPrototype, _transformSystem.GetMapCoordinates(clugUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (component.GrenadesContainer.ContainedEntities.Count > 0)
|
||||
{
|
||||
grenade = component.GrenadesContainer.ContainedEntities[0];
|
||||
|
||||
// This shouldn't happen but you never know.
|
||||
if (!_containerSystem.Remove(grenade, component.GrenadesContainer))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void UpdateAppearance(Entity<ClusterGrenadeComponent> clug)
|
||||
{
|
||||
var component = clug.Comp;
|
||||
if (!TryComp<AppearanceComponent>(clug, out var appearance))
|
||||
return;
|
||||
|
||||
_appearance.SetData(clug, ClusterGrenadeVisuals.GrenadesCounter, component.GrenadesContainer.ContainedEntities.Count + component.UnspawnedCount, appearance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using Content.Server.Explosion.Components;
|
||||
using Content.Server.Weapons.Ranged.Systems;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Explosion.EntitySystems;
|
||||
|
||||
public sealed class ProjectileGrenadeSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly GunSystem _gun = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly SharedContainerSystem _container = default!;
|
||||
[Dependency] private readonly TransformSystem _transformSystem = default!;
|
||||
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<ProjectileGrenadeComponent, ComponentInit>(OnFragInit);
|
||||
SubscribeLocalEvent<ProjectileGrenadeComponent, ComponentStartup>(OnFragStartup);
|
||||
SubscribeLocalEvent<ProjectileGrenadeComponent, TriggerEvent>(OnFragTrigger);
|
||||
}
|
||||
|
||||
private void OnFragInit(Entity<ProjectileGrenadeComponent> entity, ref ComponentInit args)
|
||||
{
|
||||
entity.Comp.Container = _container.EnsureContainer<Container>(entity.Owner, "cluster-payload");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setting the unspawned count based on capacity so we know how many new entities to spawn
|
||||
/// </summary>
|
||||
private void OnFragStartup(Entity<ProjectileGrenadeComponent> entity, ref ComponentStartup args)
|
||||
{
|
||||
if (entity.Comp.FillPrototype == null)
|
||||
return;
|
||||
|
||||
entity.Comp.UnspawnedCount = Math.Max(0, entity.Comp.Capacity - entity.Comp.Container.ContainedEntities.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Can be triggered either by damage or the use in hand timer
|
||||
/// </summary>
|
||||
private void OnFragTrigger(Entity<ProjectileGrenadeComponent> entity, ref TriggerEvent args)
|
||||
{
|
||||
FragmentIntoProjectiles(entity.Owner, entity.Comp);
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawns projectiles at the coordinates of the grenade upon triggering
|
||||
/// Can customize the angle and velocity the projectiles come out at
|
||||
/// </summary>
|
||||
private void FragmentIntoProjectiles(EntityUid uid, ProjectileGrenadeComponent component)
|
||||
{
|
||||
var grenadeCoord = _transformSystem.GetMapCoordinates(uid);
|
||||
var shootCount = 0;
|
||||
var totalCount = component.Container.ContainedEntities.Count + component.UnspawnedCount;
|
||||
var segmentAngle = 360 / totalCount;
|
||||
|
||||
while (TrySpawnContents(grenadeCoord, component, out var contentUid))
|
||||
{
|
||||
Angle angle;
|
||||
if (component.RandomAngle)
|
||||
angle = _random.NextAngle();
|
||||
else
|
||||
{
|
||||
var angleMin = segmentAngle * shootCount;
|
||||
var angleMax = segmentAngle * (shootCount + 1);
|
||||
angle = Angle.FromDegrees(_random.Next(angleMin, angleMax));
|
||||
shootCount++;
|
||||
}
|
||||
|
||||
// velocity is randomized to make the projectiles look
|
||||
// slightly uneven, doesn't really change much, but it looks better
|
||||
var direction = angle.ToVec().Normalized();
|
||||
var velocity = _random.NextVector2(component.MinVelocity, component.MaxVelocity);
|
||||
_gun.ShootProjectile(contentUid, direction, velocity, uid, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawns one instance of the fill prototype or contained entity at the coordinate indicated
|
||||
/// </summary>
|
||||
private bool TrySpawnContents(MapCoordinates spawnCoordinates, ProjectileGrenadeComponent component, out EntityUid contentUid)
|
||||
{
|
||||
contentUid = default;
|
||||
|
||||
if (component.UnspawnedCount > 0)
|
||||
{
|
||||
component.UnspawnedCount--;
|
||||
contentUid = Spawn(component.FillPrototype, spawnCoordinates);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (component.Container.ContainedEntities.Count > 0)
|
||||
{
|
||||
contentUid = component.Container.ContainedEntities[0];
|
||||
|
||||
if (!_container.Remove(contentUid, component.Container))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using Content.Shared.Explosion.Components;
|
||||
using Content.Shared.Throwing;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Random;
|
||||
using System.Numerics;
|
||||
using Content.Shared.Explosion.EntitySystems;
|
||||
|
||||
namespace Content.Server.Explosion.EntitySystems;
|
||||
|
||||
public sealed class ScatteringGrenadeSystem : SharedScatteringGrenadeSystem
|
||||
{
|
||||
[Dependency] private readonly SharedContainerSystem _container = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
|
||||
[Dependency] private readonly TransformSystem _transformSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<ScatteringGrenadeComponent, TriggerEvent>(OnScatteringTrigger);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Can be triggered either by damage or the use in hand timer, either way
|
||||
/// will store the event happening in IsTriggered for the next frame update rather than
|
||||
/// handling it here to prevent crashing the game
|
||||
/// </summary>
|
||||
private void OnScatteringTrigger(Entity<ScatteringGrenadeComponent> entity, ref TriggerEvent args)
|
||||
{
|
||||
entity.Comp.IsTriggered = true;
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every frame update we look for scattering grenades that were triggered (by damage or timer)
|
||||
/// Then we spawn the contents, throw them, optionally trigger them, then delete the original scatter grenade entity
|
||||
/// </summary>
|
||||
public override void Update(float frametime)
|
||||
{
|
||||
base.Update(frametime);
|
||||
var query = EntityQueryEnumerator<ScatteringGrenadeComponent>();
|
||||
|
||||
while (query.MoveNext(out var uid, out var component))
|
||||
{
|
||||
var totalCount = component.Container.ContainedEntities.Count + component.UnspawnedCount;
|
||||
|
||||
// if triggered while empty, (if it's blown up while empty) it'll just delete itself
|
||||
if (component.IsTriggered && totalCount > 0)
|
||||
{
|
||||
var grenadeCoord = _transformSystem.GetMapCoordinates(uid);
|
||||
var thrownCount = 0;
|
||||
var segmentAngle = 360 / totalCount;
|
||||
var additionalIntervalDelay = 0f;
|
||||
|
||||
while (TrySpawnContents(grenadeCoord, component, out var contentUid))
|
||||
{
|
||||
Angle angle;
|
||||
if (component.RandomAngle)
|
||||
angle = _random.NextAngle();
|
||||
else
|
||||
{
|
||||
var angleMin = segmentAngle * thrownCount;
|
||||
var angleMax = segmentAngle * (thrownCount + 1);
|
||||
angle = Angle.FromDegrees(_random.Next(angleMin, angleMax));
|
||||
thrownCount++;
|
||||
}
|
||||
|
||||
Vector2 direction = angle.ToVec().Normalized();
|
||||
if (component.RandomDistance)
|
||||
direction *= _random.NextFloat(component.RandomThrowDistanceMin, component.RandomThrowDistanceMax);
|
||||
else
|
||||
direction *= component.Distance;
|
||||
|
||||
_throwingSystem.TryThrow(contentUid, direction, component.Velocity);
|
||||
|
||||
if (component.TriggerContents)
|
||||
{
|
||||
additionalIntervalDelay += _random.NextFloat(component.IntervalBetweenTriggersMin, component.IntervalBetweenTriggersMax);
|
||||
var contentTimer = EnsureComp<ActiveTimerTriggerComponent>(contentUid);
|
||||
contentTimer.TimeRemaining = component.DelayBeforeTriggerContents + additionalIntervalDelay;
|
||||
var ev = new ActiveTimerTriggerEvent(contentUid, uid);
|
||||
RaiseLocalEvent(contentUid, ref ev);
|
||||
}
|
||||
}
|
||||
|
||||
// Normally we'd use DeleteOnTrigger but because we need to wait for the frame update
|
||||
// we have to delete it here instead
|
||||
Del(uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawns one instance of the fill prototype or contained entity at the coordinate indicated
|
||||
/// </summary>
|
||||
private bool TrySpawnContents(MapCoordinates spawnCoordinates, ScatteringGrenadeComponent component, out EntityUid contentUid)
|
||||
{
|
||||
contentUid = default;
|
||||
|
||||
if (component.UnspawnedCount > 0)
|
||||
{
|
||||
component.UnspawnedCount--;
|
||||
contentUid = Spawn(component.FillPrototype, spawnCoordinates);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (component.Container.ContainedEntities.Count > 0)
|
||||
{
|
||||
contentUid = component.Container.ContainedEntities[0];
|
||||
|
||||
if (!_container.Remove(contentUid, component.Container))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user