Files
tbd-station-14/Content.Server/GameObjects/Components/Explosion/ClusterFlashComponent.cs
metalgearsloth 4d064abcd7 Physics (#3485)
* 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

* Fix dupe pulling

* Zumzum's based fix

* Don't run tile friction for non-predicted bodies

* Experimental pulling improvement

* Everything's a poly now

* Optimise AI region debugging a bit

Could still be better but should improve default performance a LOT

* Mover no updater

* Crazy kinematic body idea

* Good collisions

* KinematicController

* Fix aghost

* Throwing refactor

* Pushing cleanup

* Fix throwing and footstep sounds

* Frametime in ICollideBehavior

* Fix stuff

* Actually fix weightlessness

* Optimise collision behaviors a lot

* Make open lockers still collide with walls

* powwweeerrrrr

* Merge master proper

* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* Ch ch ch changesss

* SHIP IT

* Fix #if DEBUG

* Fix vaulting and item locker collision

* Fix throwing

* Editing yaml by hand what can go wrong

* on

* Last yaml fixes

* Okay now it's fixed

* Linter

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
Co-authored-by: Vera Aguilera Puerto <zddm@outlook.es>
2021-03-08 04:09:59 +11:00

168 lines
5.5 KiB
C#

#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.Trigger.TimerTrigger;
using Content.Server.GameObjects.Components.Items;
using Content.Shared.GameObjects.Components.Explosion;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Explosion
{
[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] [DataField("fillPrototype")]
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] [DataField("maxGrenadesCount")]
private int _maxGrenades = 3;
/// <summary>
/// How long until our grenades are shot out and armed.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)] [DataField("delay")]
private float _delay = 1;
/// <summary>
/// Max distance grenades can be thrown.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)] [DataField("distance")]
private float _throwDistance = 3;
/// <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 Initialize()
{
base.Initialize();
_grenadesContainer = ContainerHelpers.EnsureContainer<Container>(Owner, "cluster-flash");
}
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 = random.NextFloat() * _throwDistance;
delay += random.Next(550, 900);
thrownCount++;
// TODO: Suss out throw strength
grenade.TryThrow(angle.ToVec().Normalized * 50);
grenade.SpawnTimer(delay, () =>
{
if (grenade.Deleted)
return;
if (grenade.TryGetComponent(out OnUseTimerTriggerComponent? useTimer))
{
useTimer.Trigger(eventArgs.User);
}
});
}
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.MapPosition);
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);
}
}
}