Re-organize all projects (#4166)
This commit is contained in:
166
Content.Server/Explosion/Components/ClusterFlashComponent.cs
Normal file
166
Content.Server/Explosion/Components/ClusterFlashComponent.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Throwing;
|
||||
using Content.Shared.Explosion;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
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.Explosion.Components
|
||||
{
|
||||
[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 = 50;
|
||||
|
||||
/// <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 * _throwDistance);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Content.Server.Throwing;
|
||||
using Content.Shared.Acts;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ExplosionLaunchedComponent : Component, IExAct
|
||||
{
|
||||
public override string Name => "ExplosionLaunched";
|
||||
|
||||
void IExAct.OnExplosion(ExplosionEventArgs eventArgs)
|
||||
{
|
||||
if (Owner.Deleted)
|
||||
return;
|
||||
|
||||
var sourceLocation = eventArgs.Source;
|
||||
var targetLocation = eventArgs.Target.Transform.Coordinates;
|
||||
var direction = (targetLocation.ToMapPos(Owner.EntityManager) - sourceLocation.ToMapPos(Owner.EntityManager)).Normalized;
|
||||
|
||||
var throwForce = eventArgs.Severity switch
|
||||
{
|
||||
ExplosionSeverity.Heavy => 30,
|
||||
ExplosionSeverity.Light => 20,
|
||||
_ => 0,
|
||||
};
|
||||
Owner.TryThrow(direction * throwForce);
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Content.Server/Explosion/Components/ExplosiveComponent.cs
Normal file
48
Content.Server/Explosion/Components/ExplosiveComponent.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Content.Shared.Acts;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ExplosiveComponent : Component, ITimerTrigger, IDestroyAct
|
||||
{
|
||||
public override string Name => "Explosive";
|
||||
|
||||
[DataField("devastationRange")]
|
||||
public int DevastationRange;
|
||||
[DataField("heavyImpactRange")]
|
||||
public int HeavyImpactRange;
|
||||
[DataField("lightImpactRange")]
|
||||
public int LightImpactRange;
|
||||
[DataField("flashRange")]
|
||||
public int FlashRange;
|
||||
|
||||
public bool Exploding { get; private set; } = false;
|
||||
|
||||
public bool Explosion()
|
||||
{
|
||||
if (Exploding)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Exploding = true;
|
||||
Owner.SpawnExplosion(DevastationRange, HeavyImpactRange, LightImpactRange, FlashRange);
|
||||
Owner.QueueDelete();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool ITimerTrigger.Trigger(TimerTriggerEventArgs eventArgs)
|
||||
{
|
||||
return Explosion();
|
||||
}
|
||||
|
||||
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
|
||||
{
|
||||
Explosion();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Physics.Collision;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class ExplosiveProjectileComponent : Component, IStartCollide
|
||||
{
|
||||
public override string Name => "ExplosiveProjectile";
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Owner.EnsureComponent<ExplosiveComponent>();
|
||||
}
|
||||
|
||||
void IStartCollide.CollideWith(Fixture ourFixture, Fixture otherFixture, in Manifold manifold)
|
||||
{
|
||||
if (Owner.TryGetComponent(out ExplosiveComponent? explosive))
|
||||
{
|
||||
explosive.Explosion();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Content.Server.Flash.Components;
|
||||
using Content.Server.Storage.Components;
|
||||
using Content.Shared.Acts;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// When triggered will flash in an area around the object and destroy itself
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class FlashExplosiveComponent : Component, ITimerTrigger, IDestroyAct
|
||||
{
|
||||
public override string Name => "FlashExplosive";
|
||||
|
||||
[DataField("range")]
|
||||
private float _range = 7.0f;
|
||||
[DataField("duration")]
|
||||
private float _duration = 8.0f;
|
||||
[DataField("sound")]
|
||||
private string _sound = "/Audio/Effects/flash_bang.ogg";
|
||||
[DataField("deleteOnFlash")]
|
||||
private bool _deleteOnFlash = true;
|
||||
|
||||
public bool Explode()
|
||||
{
|
||||
// If we're in a locker or whatever then can't flash anything
|
||||
Owner.TryGetContainer(out var container);
|
||||
if (container == null || !container.Owner.HasComponent<EntityStorageComponent>())
|
||||
{
|
||||
FlashableComponent.FlashAreaHelper(Owner, _range, _duration);
|
||||
}
|
||||
|
||||
if (_sound != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _sound, Owner.Transform.Coordinates);
|
||||
}
|
||||
|
||||
if (_deleteOnFlash && !Owner.Deleted)
|
||||
{
|
||||
Owner.Delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ITimerTrigger.Trigger(TimerTriggerEventArgs eventArgs)
|
||||
{
|
||||
return Explode();
|
||||
}
|
||||
|
||||
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
|
||||
{
|
||||
Explode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Trigger;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Explosion.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class OnUseTimerTriggerComponent : Component, IUse
|
||||
{
|
||||
public override string Name => "OnUseTimerTrigger";
|
||||
|
||||
[DataField("delay")]
|
||||
private float _delay = 0f;
|
||||
|
||||
public void Trigger(IEntity user)
|
||||
{
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
appearance.SetData(TriggerVisuals.VisualState, TriggerVisualState.Primed);
|
||||
|
||||
EntitySystem.Get<TriggerSystem>().HandleTimerTrigger(TimeSpan.FromSeconds(_delay), user, Owner);
|
||||
}
|
||||
|
||||
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
Trigger(eventArgs.User);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
328
Content.Server/Explosion/ExplosionHelper.cs
Normal file
328
Content.Server/Explosion/ExplosionHelper.cs
Normal file
@@ -0,0 +1,328 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.Camera;
|
||||
using Content.Server.Explosion.Components;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Explosion
|
||||
{
|
||||
public static class ExplosionHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Distance used for camera shake when distance from explosion is (0.0, 0.0).
|
||||
/// Avoids getting NaN values down the line from doing math on (0.0, 0.0).
|
||||
/// </summary>
|
||||
private static readonly Vector2 EpicenterDistance = (0.1f, 0.1f);
|
||||
|
||||
/// <summary>
|
||||
/// Chance of a tile breaking if the severity is Light and Heavy
|
||||
/// </summary>
|
||||
private static readonly float LightBreakChance = 0.3f;
|
||||
private static readonly float HeavyBreakChance = 0.8f;
|
||||
|
||||
private static bool IgnoreExplosivePassable(IEntity e) => e.HasTag("ExplosivePassable");
|
||||
|
||||
private static ExplosionSeverity CalculateSeverity(float distance, float devastationRange, float heaveyRange)
|
||||
{
|
||||
if (distance < devastationRange)
|
||||
{
|
||||
return ExplosionSeverity.Destruction;
|
||||
}
|
||||
else if (distance < heaveyRange)
|
||||
{
|
||||
return ExplosionSeverity.Heavy;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ExplosionSeverity.Light;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Damage entities inside the range. The damage depends on a discrete
|
||||
/// damage bracket [light, heavy, devastation] and the distance from the epicenter
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A dictionary of coordinates relative to the parents of every grid of entities that survived the explosion,
|
||||
/// have an airtight component and are currently blocking air. Like a wall.
|
||||
/// </returns>
|
||||
private static void DamageEntitiesInRange(EntityCoordinates epicenter, Box2 boundingBox,
|
||||
float devastationRange,
|
||||
float heaveyRange,
|
||||
float maxRange,
|
||||
MapId mapId)
|
||||
{
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
|
||||
var exAct = EntitySystem.Get<ActSystem>();
|
||||
|
||||
var entitiesInRange = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(mapId, boundingBox, 0).ToList();
|
||||
|
||||
var impassableEntities = new List<Tuple<IEntity, float>>();
|
||||
var nonImpassableEntities = new List<Tuple<IEntity, float>>();
|
||||
// TODO: Given this seems to rely on physics it should just query directly like everything else.
|
||||
|
||||
// The entities are paired with their distance to the epicenter
|
||||
// and splitted into two lists based on if they are Impassable or not
|
||||
foreach (var entity in entitiesInRange)
|
||||
{
|
||||
if (entity.Deleted || !entity.Transform.IsMapTransform)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entity.Transform.Coordinates.TryDistance(entityManager, epicenter, out var distance) || distance > maxRange)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entity.TryGetComponent(out PhysicsComponent? body) || body.Fixtures.Count < 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((body.CollisionLayer & (int) CollisionGroup.Impassable) != 0)
|
||||
{
|
||||
impassableEntities.Add(Tuple.Create(entity, distance));
|
||||
}
|
||||
else
|
||||
{
|
||||
nonImpassableEntities.Add(Tuple.Create(entity, distance));
|
||||
}
|
||||
}
|
||||
|
||||
// The Impassable entities are sorted in descending order
|
||||
// Entities closer to the epicenter are first
|
||||
impassableEntities.Sort((x, y) => x.Item2.CompareTo(y.Item2));
|
||||
|
||||
// Impassable entities are handled first. If they are damaged enough, they are destroyed and they may
|
||||
// be able to spawn a new entity. I.e Wall -> Girder.
|
||||
// Girder has a tag ExplosivePassable, and the predicate make it so the entities with this tag are ignored
|
||||
var epicenterMapPos = epicenter.ToMap(entityManager);
|
||||
foreach (var (entity, distance) in impassableEntities)
|
||||
{
|
||||
if (!entity.InRangeUnobstructed(epicenterMapPos, maxRange, ignoreInsideBlocker: true, predicate: IgnoreExplosivePassable))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
exAct.HandleExplosion(epicenter, entity, CalculateSeverity(distance, devastationRange, heaveyRange));
|
||||
}
|
||||
|
||||
// Impassable entities were handled first so NonImpassable entities have a bigger chance to get hit. As now
|
||||
// there are probably more ExplosivePassable entities around
|
||||
foreach (var (entity, distance) in nonImpassableEntities)
|
||||
{
|
||||
if (!entity.InRangeUnobstructed(epicenterMapPos, maxRange, ignoreInsideBlocker: true, predicate: IgnoreExplosivePassable))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
exAct.HandleExplosion(epicenter, entity, CalculateSeverity(distance, devastationRange, heaveyRange));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Damage tiles inside the range. The type of tile can change depending on a discrete
|
||||
/// damage bracket [light, heavy, devastation], the distance from the epicenter and
|
||||
/// a probabilty bracket [<see cref="LightBreakChance"/>, <see cref="HeavyBreakChance"/>, 1.0].
|
||||
/// </summary>
|
||||
///
|
||||
private static void DamageTilesInRange(EntityCoordinates epicenter,
|
||||
GridId gridId,
|
||||
Box2 boundingBox,
|
||||
float devastationRange,
|
||||
float heaveyRange,
|
||||
float maxRange)
|
||||
{
|
||||
var mapManager = IoCManager.Resolve<IMapManager>();
|
||||
if (!mapManager.TryGetGrid(gridId, out var mapGrid))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
if (!entityManager.TryGetEntity(mapGrid.GridEntityId, out var grid))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var robustRandom = IoCManager.Resolve<IRobustRandom>();
|
||||
var tileDefinitionManager = IoCManager.Resolve<ITileDefinitionManager>();
|
||||
|
||||
var tilesInGridAndCircle = mapGrid.GetTilesIntersecting(boundingBox);
|
||||
|
||||
var epicenterMapPos = epicenter.ToMap(entityManager);
|
||||
foreach (var tile in tilesInGridAndCircle)
|
||||
{
|
||||
var tileLoc = mapGrid.GridTileToLocal(tile.GridIndices);
|
||||
if (!tileLoc.TryDistance(entityManager, epicenter, out var distance) || distance > maxRange)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tile.IsBlockedTurf(false))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!tileLoc.ToMap(entityManager).InRangeUnobstructed(epicenterMapPos, maxRange, ignoreInsideBlocker: false, predicate: IgnoreExplosivePassable))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var tileDef = (ContentTileDefinition) tileDefinitionManager[tile.Tile.TypeId];
|
||||
var baseTurfs = tileDef.BaseTurfs;
|
||||
if (baseTurfs.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var zeroTile = new Robust.Shared.Map.Tile(tileDefinitionManager[baseTurfs[0]].TileId);
|
||||
var previousTile = new Robust.Shared.Map.Tile(tileDefinitionManager[baseTurfs[^1]].TileId);
|
||||
|
||||
var severity = CalculateSeverity(distance, devastationRange, heaveyRange);
|
||||
|
||||
switch (severity)
|
||||
{
|
||||
case ExplosionSeverity.Light:
|
||||
if (!previousTile.IsEmpty && robustRandom.Prob(LightBreakChance))
|
||||
{
|
||||
mapGrid.SetTile(tileLoc, previousTile);
|
||||
}
|
||||
break;
|
||||
case ExplosionSeverity.Heavy:
|
||||
if (!previousTile.IsEmpty && robustRandom.Prob(HeavyBreakChance))
|
||||
{
|
||||
mapGrid.SetTile(tileLoc, previousTile);
|
||||
}
|
||||
break;
|
||||
case ExplosionSeverity.Destruction:
|
||||
mapGrid.SetTile(tileLoc, zeroTile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void CameraShakeInRange(EntityCoordinates epicenter, float maxRange)
|
||||
{
|
||||
var playerManager = IoCManager.Resolve<IPlayerManager>();
|
||||
var players = playerManager.GetPlayersInRange(epicenter, (int) Math.Ceiling(maxRange));
|
||||
foreach (var player in players)
|
||||
{
|
||||
if (player.AttachedEntity == null || !player.AttachedEntity.TryGetComponent(out CameraRecoilComponent? recoil))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
|
||||
var playerPos = player.AttachedEntity.Transform.WorldPosition;
|
||||
var delta = epicenter.ToMapPos(entityManager) - playerPos;
|
||||
|
||||
//Change if zero. Will result in a NaN later breaking camera shake if not changed
|
||||
if (delta.EqualsApprox((0.0f, 0.0f)))
|
||||
delta = EpicenterDistance;
|
||||
|
||||
var distance = delta.LengthSquared;
|
||||
var effect = 10 * (1 / (1 + distance));
|
||||
if (effect > 0.01f)
|
||||
{
|
||||
var kick = -delta.Normalized * effect;
|
||||
recoil.Kick(kick);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void FlashInRange(EntityCoordinates epicenter, float flashrange)
|
||||
{
|
||||
if (flashrange > 0)
|
||||
{
|
||||
var entitySystemManager = IoCManager.Resolve<IEntitySystemManager>();
|
||||
var time = IoCManager.Resolve<IGameTiming>().CurTime;
|
||||
var message = new EffectSystemMessage
|
||||
{
|
||||
EffectSprite = "Effects/explosion.rsi",
|
||||
RsiState = "explosionfast",
|
||||
Born = time,
|
||||
DeathTime = time + TimeSpan.FromSeconds(5),
|
||||
Size = new Vector2(flashrange / 2, flashrange / 2),
|
||||
Coordinates = epicenter,
|
||||
Rotation = 0f,
|
||||
ColorDelta = new Vector4(0, 0, 0, -1500f),
|
||||
Color = Vector4.Multiply(new Vector4(255, 255, 255, 750), 0.5f),
|
||||
Shaded = false
|
||||
};
|
||||
entitySystemManager.GetEntitySystem<EffectSystem>().CreateParticle(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SpawnExplosion(this IEntity entity, int devastationRange = 0, int heavyImpactRange = 0,
|
||||
int lightImpactRange = 0, int flashRange = 0)
|
||||
{
|
||||
// If you want to directly set off the explosive
|
||||
if (!entity.Deleted && entity.TryGetComponent(out ExplosiveComponent? explosive) && !explosive.Exploding)
|
||||
{
|
||||
explosive.Explosion();
|
||||
}
|
||||
else
|
||||
{
|
||||
while (entity.TryGetContainer(out var cont))
|
||||
{
|
||||
entity = cont.Owner;
|
||||
}
|
||||
|
||||
var epicenter = entity.Transform.Coordinates;
|
||||
|
||||
SpawnExplosion(epicenter, devastationRange, heavyImpactRange, lightImpactRange, flashRange);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SpawnExplosion(EntityCoordinates epicenter, int devastationRange = 0,
|
||||
int heavyImpactRange = 0, int lightImpactRange = 0, int flashRange = 0)
|
||||
{
|
||||
var mapId = epicenter.GetMapId(IoCManager.Resolve<IEntityManager>());
|
||||
if (mapId == MapId.Nullspace)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var maxRange = MathHelper.Max(devastationRange, heavyImpactRange, lightImpactRange, 0);
|
||||
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
var mapManager = IoCManager.Resolve<IMapManager>();
|
||||
|
||||
var epicenterMapPos = epicenter.ToMapPos(entityManager);
|
||||
var boundingBox = new Box2(epicenterMapPos - new Vector2(maxRange, maxRange),
|
||||
epicenterMapPos + new Vector2(maxRange, maxRange));
|
||||
|
||||
SoundSystem.Play(Filter.Broadcast(), "/Audio/Effects/explosion.ogg", epicenter);
|
||||
DamageEntitiesInRange(epicenter, boundingBox, devastationRange, heavyImpactRange, maxRange, mapId);
|
||||
|
||||
var mapGridsNear = mapManager.FindGridsIntersecting(mapId, boundingBox);
|
||||
|
||||
foreach (var gridId in mapGridsNear)
|
||||
{
|
||||
DamageTilesInRange(epicenter, gridId.Index, boundingBox, devastationRange, heavyImpactRange, maxRange);
|
||||
}
|
||||
|
||||
CameraShakeInRange(epicenter, maxRange);
|
||||
FlashInRange(epicenter, flashRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
54
Content.Server/Explosion/TriggerSystem.cs
Normal file
54
Content.Server/Explosion/TriggerSystem.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Explosion
|
||||
{
|
||||
/// <summary>
|
||||
/// This interface gives components behavior when being "triggered" by timer or other conditions
|
||||
/// </summary>
|
||||
public interface ITimerTrigger
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when one object is triggering some event
|
||||
/// </summary>
|
||||
bool Trigger(TimerTriggerEventArgs eventArgs);
|
||||
}
|
||||
|
||||
public class TimerTriggerEventArgs : EventArgs
|
||||
{
|
||||
public TimerTriggerEventArgs(IEntity user, IEntity source)
|
||||
{
|
||||
User = user;
|
||||
Source = source;
|
||||
}
|
||||
|
||||
public IEntity User { get; set; }
|
||||
public IEntity Source { get; set; }
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public sealed class TriggerSystem : EntitySystem
|
||||
{
|
||||
public void HandleTimerTrigger(TimeSpan delay, IEntity user, IEntity trigger)
|
||||
{
|
||||
|
||||
Timer.Spawn(delay, () =>
|
||||
{
|
||||
var timerTriggerEventArgs = new TimerTriggerEventArgs(user, trigger);
|
||||
var timerTriggers = trigger.GetAllComponents<ITimerTrigger>().ToList();
|
||||
|
||||
foreach (var timerTrigger in timerTriggers)
|
||||
{
|
||||
if (timerTrigger.Trigger(timerTriggerEventArgs))
|
||||
{
|
||||
// If an IOnTimerTrigger returns a status completion we finish our trigger
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user