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:
Plykiya
2024-12-16 04:08:07 -08:00
committed by GitHub
parent 1266b05b02
commit a4d6f09a4f
14 changed files with 757 additions and 558 deletions

View File

@@ -0,0 +1,109 @@
using Content.Shared.Explosion.EntitySystems;
using Content.Shared.Whitelist;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.Explosion.Components;
/// <summary>
/// Use this component if the grenade splits into entities that make use of Timers
/// or if you just want it to throw entities out in the world
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(SharedScatteringGrenadeSystem))]
public sealed partial class ScatteringGrenadeComponent : Component
{
public Container Container = default!;
[DataField]
public EntityWhitelist? Whitelist;
/// <summary>
/// What we fill our prototype with if we want to pre-spawn with entities.
/// </summary>
[DataField]
public EntProtoId? FillPrototype;
/// <summary>
/// If we have a pre-fill how many more can we spawn.
/// </summary>
[AutoNetworkedField]
public int UnspawnedCount;
/// <summary>
/// Max amount of entities inside the container
/// </summary>
[DataField]
public int Capacity = 3;
/// <summary>
/// Decides if contained entities trigger after getting launched
/// </summary>
[DataField]
public bool TriggerContents = true;
#region Trigger time parameters for scattered entities
/// <summary>
/// Minimum delay in seconds before any entities start to be triggered.
/// </summary>
[DataField]
public float DelayBeforeTriggerContents = 1.0f;
/// <summary>
/// Maximum delay in seconds to add between individual entity triggers
/// </summary>
[DataField]
public float IntervalBetweenTriggersMax;
/// <summary>
/// Minimum delay in seconds to add between individual entity triggers
/// </summary>
[DataField]
public float IntervalBetweenTriggersMin;
#endregion
#region Throwing parameters for the scattered entities
/// <summary>
/// Should the angle the entities get thrown at be random
/// instead of uniformly distributed
/// </summary>
[DataField]
public bool RandomAngle;
/// <summary>
/// The speed at which the entities get thrown
/// </summary>
[DataField]
public float Velocity = 5;
/// <summary>
/// Static distance grenades will be thrown to if RandomDistance is false.
/// </summary>
[DataField]
public float Distance = 1f;
/// <summary>
/// Should the distance the entities get thrown be random
/// </summary>
[DataField]
public bool RandomDistance;
/// <summary>
/// Max distance grenades can randomly be thrown to.
/// </summary>
[DataField]
public float RandomThrowDistanceMax = 2.5f;
/// <summary>
/// Minimal distance grenades can randomly be thrown to.
/// </summary>
[DataField]
public float RandomThrowDistanceMin;
#endregion
/// <summary>
/// Whether the main grenade has been triggered or not
/// We need to store this because we are only allowed to spawn and trigger timed entities on the next available frame update
/// </summary>
public bool IsTriggered = false;
}

View File

@@ -0,0 +1,70 @@
using Content.Shared.Explosion.Components;
using Content.Shared.Interaction;
using Content.Shared.Whitelist;
using Robust.Shared.Containers;
namespace Content.Shared.Explosion.EntitySystems;
public abstract class SharedScatteringGrenadeSystem : EntitySystem
{
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ScatteringGrenadeComponent, ComponentInit>(OnScatteringInit);
SubscribeLocalEvent<ScatteringGrenadeComponent, ComponentStartup>(OnScatteringStartup);
SubscribeLocalEvent<ScatteringGrenadeComponent, InteractUsingEvent>(OnScatteringInteractUsing);
}
private void OnScatteringInit(Entity<ScatteringGrenadeComponent> 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
/// Update appearance based on initial fill amount
/// </summary>
private void OnScatteringStartup(Entity<ScatteringGrenadeComponent> entity, ref ComponentStartup args)
{
if (entity.Comp.FillPrototype == null)
return;
entity.Comp.UnspawnedCount = Math.Max(0, entity.Comp.Capacity - entity.Comp.Container.ContainedEntities.Count);
UpdateAppearance(entity);
Dirty(entity, entity.Comp);
}
/// <summary>
/// There are some scattergrenades you can fill up with more grenades (like clusterbangs)
/// This covers how you insert more into it
/// </summary>
private void OnScatteringInteractUsing(Entity<ScatteringGrenadeComponent> entity, ref InteractUsingEvent args)
{
if (entity.Comp.Whitelist == null)
return;
if (args.Handled || !_whitelistSystem.IsValid(entity.Comp.Whitelist, args.Used))
return;
_container.Insert(args.Used, entity.Comp.Container);
UpdateAppearance(entity);
args.Handled = true;
}
/// <summary>
/// Update appearance based off of total count of contents
/// </summary>
private void UpdateAppearance(Entity<ScatteringGrenadeComponent> entity)
{
if (!TryComp<AppearanceComponent>(entity, out var appearanceComponent))
return;
_appearance.SetData(entity, ClusterGrenadeVisuals.GrenadesCounter, entity.Comp.UnspawnedCount + entity.Comp.Container.ContainedEntities.Count, appearanceComponent);
}
}