From c2f2d27b167b92b7749e88c85bd8079578c9e107 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 1 Dec 2021 12:40:00 +1100 Subject: [PATCH] ECS chemical ammo shooting (#5620) --- .../Components/ChemicalAmmoComponent.cs | 54 +------------------ .../Components/ServerRangedBarrelComponent.cs | 45 ++++++++++++---- .../Weapon/Ranged/ChemicalAmmoSystem.cs | 52 ++++++++++++++++++ .../Guns/Ammunition/Projectiles/shotgun.yml | 2 + 4 files changed, 90 insertions(+), 63 deletions(-) create mode 100644 Content.Server/Weapon/Ranged/ChemicalAmmoSystem.cs diff --git a/Content.Server/Weapon/Ranged/Ammunition/Components/ChemicalAmmoComponent.cs b/Content.Server/Weapon/Ranged/Ammunition/Components/ChemicalAmmoComponent.cs index 2a211f1ec9..bbb8eae1d0 100644 --- a/Content.Server/Weapon/Ranged/Ammunition/Components/ChemicalAmmoComponent.cs +++ b/Content.Server/Weapon/Ranged/Ammunition/Components/ChemicalAmmoComponent.cs @@ -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().TryGetSolution(Owner.Uid, SolutionName, out var ammoSolution)) - return; - - var projectiles = barrelFired.FiredProjectiles; - var solutionContainerSystem = EntitySystem.Get(); - - var projectileSolutionContainers = new List<(EntityUid, Solution)>(); - foreach (var projectile in projectiles) - { - if (EntitySystem.Get() - .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); - } } } diff --git a/Content.Server/Weapon/Ranged/Barrels/Components/ServerRangedBarrelComponent.cs b/Content.Server/Weapon/Ranged/Barrels/Components/ServerRangedBarrelComponent.cs index 32009cb377..2ed5f793da 100644 --- a/Content.Server/Weapon/Ranged/Barrels/Components/ServerRangedBarrelComponent.cs +++ b/Content.Server/Weapon/Ranged/Barrels/Components/ServerRangedBarrelComponent.cs @@ -318,7 +318,7 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components sprayAngleChange = Linspace(-evenSpreadAngle / 2, evenSpreadAngle / 2, count); } - var firedProjectiles = new List(); + 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)); } /// @@ -425,13 +426,37 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components } } -#pragma warning disable 618 - public class BarrelFiredMessage : ComponentMessage -#pragma warning restore 618 + /// + /// Raised on a gun when it fires projectiles. + /// + public sealed class GunShotEvent : EntityEventArgs { - public readonly List FiredProjectiles; + /// + /// Uid of the entity that shot. + /// + public EntityUid Uid; - public BarrelFiredMessage(List firedProjectiles) + public readonly EntityUid[] FiredProjectiles; + + public GunShotEvent(EntityUid[] firedProjectiles) + { + FiredProjectiles = firedProjectiles; + } + } + + /// + /// Raised on ammo when it is fired. + /// + public sealed class AmmoShotEvent : EntityEventArgs + { + /// + /// Uid of the entity that shot. + /// + public EntityUid Uid; + + public readonly EntityUid[] FiredProjectiles; + + public AmmoShotEvent(EntityUid[] firedProjectiles) { FiredProjectiles = firedProjectiles; } diff --git a/Content.Server/Weapon/Ranged/ChemicalAmmoSystem.cs b/Content.Server/Weapon/Ranged/ChemicalAmmoSystem.cs new file mode 100644 index 0000000000..ce054a8a5c --- /dev/null +++ b/Content.Server/Weapon/Ranged/ChemicalAmmoSystem.cs @@ -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(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); + } + } +} diff --git a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Projectiles/shotgun.yml b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Projectiles/shotgun.yml index 3202cf86cf..2c4d297654 100644 --- a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Projectiles/shotgun.yml +++ b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Projectiles/shotgun.yml @@ -106,3 +106,5 @@ solution: ammo - type: SolutionInjectOnCollide transferAmount: 15 + - type: InjectableSolution + solution: ammo