Files
crystall-punk-14/Content.Server/GameObjects/Components/Explosion/ClusterFlashComponent.cs

178 lines
5.8 KiB
C#
Raw Normal View History

#nullable enable
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Server.GameObjects.Components.Explosion;
using Robust.Shared.GameObjects;
using System.Threading.Tasks;
using Robust.Shared.Serialization;
using System;
using System.Diagnostics.CodeAnalysis;
using Content.Server.GameObjects.Components.Trigger.TimerTrigger;
using Content.Server.Throw;
Physics (#3452) * Content side new physics structure * BroadPhase outline done * But we need to fix WorldAABB * Fix static pvs AABB * Fix import * Rando fixes * B is for balloon * Change human mob hitbox to circle * Decent movement * Start adding friction to player controller I think it's the best way to go about it to keep other objects somewhat consistent for physics. * This baby can fit so many physics bugs in it. * Slight mob mover optimisations. * Player mover kinda works okay. * Beginnings of testbed * More testbed * Circlestack bed * Namespaces * BB fixes * Pull WorldAABB * Joint pulling * Semi-decent movement I guess. * Pulling better * Bullet controller + old movement * im too dumb for this shit * Use kinematic mob controller again It's probably for the best TBH * Stashed shitcode * Remove SlipController * In which movement code is entirely refactored * Singularity fix * Fix ApplyLinearImpulse * MoveRelay fix * Fix door collisions * Disable subfloor collisions Saves on broadphase a fair bit * Re-implement ClimbController * Zumzum's pressure * Laggy item throwing * Minor atmos change * Some caching * Optimise controllers * Optimise CollideWith to hell and back * Re-do throwing and tile friction * Landing too * Optimise controllers * Move CCVars and other stuff swept is beautiful * Cleanup a bunch of controllers * Fix shooting and high pressure movement controller * Flashing improvements * Stuff and things * Combat collisions * Combat mode collisions * Pulling distance joint again * Cleanup physics interfaces * More like scuffedularity * Shit's fucked * Haha tests go green * Bigmoneycrab Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-01 03:11:29 +11:00
using Robust.Server.GameObjects;
using Content.Shared.GameObjects.Components.Explosion;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Explosives
{
[RegisterComponent]
public sealed class ClusterFlashComponent : Component, IInteractUsing, IUse
{
public override string Name => "ClusterFlash";
private Container _grenadesContainer = default!;
/// <summary>
/// What we fill our prototype with if we want to pre-spawn with grenades.
/// </summary>
[ViewVariables]
private string? _fillPrototype;
/// <summary>
/// If we have a pre-fill how many more can we spawn.
/// </summary>
private int _unspawnedCount;
/// <summary>
/// Maximum grenades in the container.
/// </summary>
[ViewVariables]
private int _maxGrenades;
/// <summary>
/// How long until our grenades are shot out and armed.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
private float _delay;
/// <summary>
/// Max distance grenades can be thrown.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
private float _throwDistance;
/// <summary>
/// This is the end.
/// </summary>
private bool _countDown;
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs args)
{
if (_grenadesContainer.ContainedEntities.Count >= _maxGrenades || !args.Using.HasComponent<FlashExplosiveComponent>())
return false;
_grenadesContainer.Insert(args.Using);
UpdateAppearance();
return true;
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _fillPrototype, "fillPrototype", null);
serializer.DataField(ref _maxGrenades, "maxGrenadesCount", 3);
serializer.DataField(ref _delay, "delay", 1.0f);
serializer.DataField(ref _throwDistance, "distance", 3.0f);
}
public override void Initialize()
{
base.Initialize();
_grenadesContainer = ContainerManagerComponent.Ensure<Container>("cluster-flash", Owner);
}
protected override void Startup()
{
base.Startup();
if (_fillPrototype != null)
{
_unspawnedCount = Math.Max(0, _maxGrenades - _grenadesContainer.ContainedEntities.Count);
UpdateAppearance();
}
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
{
if (_countDown || (_grenadesContainer.ContainedEntities.Count + _unspawnedCount) <= 0)
return false;
Owner.SpawnTimer((int) (_delay * 1000), () =>
{
if (Owner.Deleted)
return;
_countDown = true;
var random = IoCManager.Resolve<IRobustRandom>();
var delay = 20;
var grenadesInserted = _grenadesContainer.ContainedEntities.Count + _unspawnedCount;
var thrownCount = 0;
var segmentAngle = (int) (360 / grenadesInserted);
while (TryGetGrenade(out var grenade))
{
var angleMin = segmentAngle * thrownCount;
var angleMax = segmentAngle * (thrownCount + 1);
var angle = Angle.FromDegrees(random.Next(angleMin, angleMax));
var distance = (float)random.NextFloat() * _throwDistance;
var target = new EntityCoordinates(Owner.Uid, angle.ToVec().Normalized * distance);
grenade.Throw(0.5f, target, grenade.Transform.Coordinates);
grenade.SpawnTimer(delay, () =>
{
if (grenade.Deleted)
return;
if (grenade.TryGetComponent(out OnUseTimerTriggerComponent? useTimer))
{
useTimer.Trigger(eventArgs.User);
}
});
delay += random.Next(550, 900);
thrownCount++;
}
Owner.Delete();
});
return true;
}
private bool TryGetGrenade([NotNullWhen(true)] out IEntity? grenade)
{
grenade = null;
if (_unspawnedCount > 0)
{
_unspawnedCount--;
grenade = Owner.EntityManager.SpawnEntity(_fillPrototype, Owner.Transform.Coordinates);
return true;
}
if (_grenadesContainer.ContainedEntities.Count > 0)
{
grenade = _grenadesContainer.ContainedEntities[0];
// This shouldn't happen but you never know.
if (!_grenadesContainer.Remove(grenade))
return false;
return true;
}
return false;
}
private void UpdateAppearance()
{
if (!Owner.TryGetComponent(out AppearanceComponent? appearance)) return;
appearance.SetData(ClusterFlashVisuals.GrenadesCounter, _grenadesContainer.ContainedEntities.Count + _unspawnedCount);
}
}
}