Files
OldThink/Content.Server/Explosion/EntitySystems/ClusterGrenadeSystem.cs

178 lines
6.7 KiB
C#
Raw Normal View History

2022-02-02 18:04:38 +11:00
using Content.Server.Explosion.Components;
using Content.Shared.Flash.Components;
2022-02-02 18:04:38 +11:00
using Content.Shared.Interaction;
2022-03-24 02:33:01 +13:00
using Content.Shared.Throwing;
2022-02-02 18:04:38 +11:00
using Robust.Shared.Containers;
using Robust.Shared.Random;
using Content.Server.Weapons.Ranged.Systems;
using System.Numerics;
Move grenade components to shared (#22691) * Moves FlashComponent.cs, FlashOnTriggerComponent.cs, and SmokeOnTriggerComponent.cs to Shared * Moves ExplodeOnTriggerComponent.cs, OnUseTimerTriggerComponent.cs, ActiveTimerTriggerComponent.cs, and SmokeOnTriggerComponent.cs to Shared * Delete .run/Content Server+Client.run.xml HOW DID THIS GET IN HERE ITS NOT AHHHH * Update Content.Client/Explosion/SmokeOnTriggerSystem.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Shared/Explosion/Components/ActiveTimerTriggerComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Shared/Explosion/Components/OnUseTimerTriggerComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Shared/Explosion/Components/OnUseTimerTriggerComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Shared/Explosion/EntitySystems/SharedTriggerSystem.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Shared/Explosion/EntitySystems/SharedSmokeOnTriggerSystem.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update ExplodeOnTriggerComponent.cs * Revert "Delete .run/Content Server+Client.run.xml" This reverts commit 29ee05f57de60eab5c92158d8eba5e3acba483c2. * Fix? * cannot figure out how to get this to go back please forgive * Fixes a network issue * leftovers * Fixes --------- Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2024-02-01 07:29:01 -06:00
using Content.Shared.Explosion.Components;
using Robust.Server.Containers;
using Robust.Server.GameObjects;
2022-02-02 18:04:38 +11:00
namespace Content.Server.Explosion.EntitySystems;
public sealed class ClusterGrenadeSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
2022-03-24 02:33:01 +13:00
[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!;
2022-02-02 18:04:38 +11:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ClusterGrenadeComponent, ComponentInit>(OnClugInit);
SubscribeLocalEvent<ClusterGrenadeComponent, ComponentStartup>(OnClugStartup);
SubscribeLocalEvent<ClusterGrenadeComponent, InteractUsingEvent>(OnClugUsing);
SubscribeLocalEvent<ClusterGrenadeComponent, TriggerEvent>(OnClugTrigger);
2022-02-02 18:04:38 +11:00
}
private void OnClugInit(EntityUid uid, ClusterGrenadeComponent component, ComponentInit args)
{
component.GrenadesContainer = _container.EnsureContainer<Container>(uid, "cluster-payload");
2022-02-02 18:04:38 +11:00
}
private void OnClugStartup(Entity<ClusterGrenadeComponent> clug, ref ComponentStartup args)
2022-02-02 18:04:38 +11:00
{
var component = clug.Comp;
2022-02-02 18:04:38 +11:00
if (component.FillPrototype != null)
{
component.UnspawnedCount = Math.Max(0, component.MaxGrenades - component.GrenadesContainer.ContainedEntities.Count);
UpdateAppearance(clug);
2022-02-02 18:04:38 +11:00
}
}
private void OnClugUsing(Entity<ClusterGrenadeComponent> clug, ref InteractUsingEvent args)
2022-02-02 18:04:38 +11:00
{
if (args.Handled)
return;
var component = clug.Comp;
2022-02-02 18:04:38 +11:00
// TODO: Should use whitelist.
if (component.GrenadesContainer.ContainedEntities.Count >= component.MaxGrenades ||
!HasComp<FlashOnTriggerComponent>(args.Used))
return;
_containerSystem.Insert(args.Used, component.GrenadesContainer);
UpdateAppearance(clug);
2022-02-02 18:04:38 +11:00
args.Handled = true;
}
private void OnClugTrigger(Entity<ClusterGrenadeComponent> clug, ref TriggerEvent args)
2022-02-02 18:04:38 +11:00
{
var component = clug.Comp;
component.CountDown = true;
args.Handled = true;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<ClusterGrenadeComponent>();
2022-02-02 18:04:38 +11:00
while (query.MoveNext(out var uid, out var clug))
2022-02-02 18:04:38 +11:00
{
if (clug.CountDown && clug.UnspawnedCount > 0)
2022-02-02 18:04:38 +11:00
{
var grenadesInserted = clug.GrenadesContainer.ContainedEntities.Count + clug.UnspawnedCount;
var thrownCount = 0;
var segmentAngle = 360 / grenadesInserted;
var grenadeDelay = 0f;
2022-02-02 18:04:38 +11:00
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);
}
}
}
2022-02-02 18:04:38 +11:00
private void ShootProjectile(EntityUid grenade, Angle angle, ClusterGrenadeComponent clug, EntityUid clugUid)
{
var direction = angle.ToVec().Normalized();
2022-02-02 18:04:38 +11:00
if (clug.RandomSpread)
direction = _random.NextVector2().Normalized();
2022-02-02 18:04:38 +11:00
_gun.ShootProjectile(grenade, direction, Vector2.One.Normalized(), clugUid);
2022-02-02 18:04:38 +11:00
}
2022-02-02 18:04:38 +11:00
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);
2022-02-02 18:04:38 +11:00
}
private bool TryGetGrenade(EntityUid clugUid, ClusterGrenadeComponent component, out EntityUid grenade)
2022-02-02 18:04:38 +11:00
{
grenade = default;
if (component.UnspawnedCount > 0)
{
component.UnspawnedCount--;
grenade = Spawn(component.FillPrototype, _transformSystem.GetMapCoordinates(clugUid));
2022-02-02 18:04:38 +11:00
return true;
}
if (component.GrenadesContainer.ContainedEntities.Count > 0)
{
grenade = component.GrenadesContainer.ContainedEntities[0];
// This shouldn't happen but you never know.
2023-12-27 21:30:03 -08:00
if (!_containerSystem.Remove(grenade, component.GrenadesContainer))
2022-02-02 18:04:38 +11:00
return false;
return true;
}
return false;
}
private void UpdateAppearance(Entity<ClusterGrenadeComponent> clug)
2022-02-02 18:04:38 +11:00
{
var component = clug.Comp;
if (!TryComp<AppearanceComponent>(clug, out var appearance))
return;
2022-02-02 18:04:38 +11:00
_appearance.SetData(clug, ClusterGrenadeVisuals.GrenadesCounter, component.GrenadesContainer.ContainedEntities.Count + component.UnspawnedCount, appearance);
2022-02-02 18:04:38 +11:00
}
}