ECS chemical ammo shooting (#5620)

This commit is contained in:
metalgearsloth
2021-12-01 12:40:00 +11:00
committed by GitHub
parent b68ca21480
commit c2f2d27b16
4 changed files with 90 additions and 63 deletions

View File

@@ -1,67 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Weapon.Ranged.Barrels.Components;
using Content.Shared.Chemistry.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Weapon.Ranged.Ammunition.Components
{
[RegisterComponent]
public class ChemicalAmmoComponent : Component
public sealed class ChemicalAmmoComponent : Component
{
public override string Name => "ChemicalAmmo";
public const string DefaultSolutionName = "ammo";
[DataField("solution")]
public string SolutionName { get; set; } = DefaultSolutionName;
[Obsolete("Component Messages are deprecated, use Entity Events instead.")]
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
#pragma warning disable 618
base.HandleMessage(message, component);
#pragma warning restore 618
switch (message)
{
case BarrelFiredMessage barrelFired:
TransferSolution(barrelFired);
break;
}
}
private void TransferSolution(BarrelFiredMessage barrelFired)
{
if (!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner.Uid, SolutionName, out var ammoSolution))
return;
var projectiles = barrelFired.FiredProjectiles;
var solutionContainerSystem = EntitySystem.Get<SolutionContainerSystem>();
var projectileSolutionContainers = new List<(EntityUid, Solution)>();
foreach (var projectile in projectiles)
{
if (EntitySystem.Get<SolutionContainerSystem>()
.TryGetSolution(projectile.Uid, SolutionName, out var projectileSolutionContainer))
{
projectileSolutionContainers.Add((projectile.Uid, projectileSolutionContainer));
}
}
if (!projectileSolutionContainers.Any())
return;
var solutionPerProjectile = ammoSolution.CurrentVolume * (1 / projectileSolutionContainers.Count);
foreach (var (projectileUid, projectileSolution) in projectileSolutionContainers)
{
var solutionToTransfer = solutionContainerSystem.SplitSolution(Owner.Uid, ammoSolution, solutionPerProjectile);
solutionContainerSystem.TryAddSolution(projectileUid, projectileSolution, solutionToTransfer);
}
solutionContainerSystem.RemoveAllSolution(Owner.Uid, ammoSolution);
}
}
}

View File

@@ -318,7 +318,7 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
sprayAngleChange = Linspace(-evenSpreadAngle / 2, evenSpreadAngle / 2, count);
}
var firedProjectiles = new List<IEntity>();
var firedProjectiles = new EntityUid[count];
for (var i = 0; i < count; i++)
{
IEntity projectile;
@@ -332,7 +332,8 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
projectile =
Owner.EntityManager.SpawnEntity(baseProjectile.Prototype?.ID, baseProjectile.Transform.Coordinates);
}
firedProjectiles.Add(projectile);
firedProjectiles[i] = projectile.Uid;
Angle projectileAngle;
@@ -364,9 +365,9 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
projectile.Transform.WorldRotation = projectileAngle + MathHelper.PiOver2;
}
#pragma warning disable 618
ammo.SendMessage(this, new BarrelFiredMessage(firedProjectiles));
#pragma warning restore 618
Owner.EntityManager.EventBus.RaiseLocalEvent(OwnerUid, new GunShotEvent(firedProjectiles));
Owner.EntityManager.EventBus.RaiseLocalEvent(ammo.Uid, new AmmoShotEvent(firedProjectiles));
}
/// <summary>
@@ -425,13 +426,37 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components
}
}
#pragma warning disable 618
public class BarrelFiredMessage : ComponentMessage
#pragma warning restore 618
/// <summary>
/// Raised on a gun when it fires projectiles.
/// </summary>
public sealed class GunShotEvent : EntityEventArgs
{
public readonly List<IEntity> FiredProjectiles;
/// <summary>
/// Uid of the entity that shot.
/// </summary>
public EntityUid Uid;
public BarrelFiredMessage(List<IEntity> firedProjectiles)
public readonly EntityUid[] FiredProjectiles;
public GunShotEvent(EntityUid[] firedProjectiles)
{
FiredProjectiles = firedProjectiles;
}
}
/// <summary>
/// Raised on ammo when it is fired.
/// </summary>
public sealed class AmmoShotEvent : EntityEventArgs
{
/// <summary>
/// Uid of the entity that shot.
/// </summary>
public EntityUid Uid;
public readonly EntityUid[] FiredProjectiles;
public AmmoShotEvent(EntityUid[] firedProjectiles)
{
FiredProjectiles = firedProjectiles;
}

View File

@@ -0,0 +1,52 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Weapon.Ranged.Ammunition.Components;
using Content.Server.Weapon.Ranged.Barrels.Components;
using Content.Shared.Chemistry.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Weapon.Ranged
{
public sealed class ChemicalAmmoSystem : EntitySystem
{
[Dependency] private SolutionContainerSystem _solutionSystem = default!;
public override void Initialize()
{
SubscribeLocalEvent<ChemicalAmmoComponent, AmmoShotEvent>(OnFire);
}
private void OnFire(EntityUid uid, ChemicalAmmoComponent component, AmmoShotEvent args)
{
if (!_solutionSystem.TryGetSolution(uid, component.SolutionName, out var ammoSolution))
return;
var projectiles = args.FiredProjectiles;
var projectileSolutionContainers = new List<(EntityUid, Solution)>();
foreach (var projectile in projectiles)
{
if (_solutionSystem
.TryGetSolution(projectile, component.SolutionName, out var projectileSolutionContainer))
{
projectileSolutionContainers.Add((uid, projectileSolutionContainer));
}
}
if (!projectileSolutionContainers.Any())
return;
var solutionPerProjectile = ammoSolution.CurrentVolume * (1 / projectileSolutionContainers.Count);
foreach (var (projectileUid, projectileSolution) in projectileSolutionContainers)
{
var solutionToTransfer = _solutionSystem.SplitSolution(uid, ammoSolution, solutionPerProjectile);
_solutionSystem.TryAddSolution(projectileUid, projectileSolution, solutionToTransfer);
}
_solutionSystem.RemoveAllSolution(uid, ammoSolution);
}
}
}

View File

@@ -106,3 +106,5 @@
solution: ammo
- type: SolutionInjectOnCollide
transferAmount: 15
- type: InjectableSolution
solution: ammo