From c770c6b37ea8c8125bf96066145003c385c910e7 Mon Sep 17 00:00:00 2001 From: py01 <60152240+collinlunn@users.noreply.github.com> Date: Tue, 15 Dec 2020 07:38:41 -0600 Subject: [PATCH] ChemicalAmmoComponent (#2739) * ChemicalAmmoComponent * Moves BarrelFiredMessage to FireProjectiles Co-authored-by: py01 --- Content.Client/IgnoredComponents.cs | 3 +- .../Ammunition/ChemicalAmmoComponent.cs | 66 +++++++++++++++++++ .../Barrels/ServerRangedBarrelComponent.cs | 17 ++++- .../Guns/Ammunition/Shotgun/cartridges.yml | 5 ++ .../Guns/Ammunition/Shotgun/projectiles.yml | 9 +-- 5 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 Content.Server/GameObjects/Components/Weapon/Ranged/Ammunition/ChemicalAmmoComponent.cs diff --git a/Content.Client/IgnoredComponents.cs b/Content.Client/IgnoredComponents.cs index f61fac1c5f..cbdc8e2017 100644 --- a/Content.Client/IgnoredComponents.cs +++ b/Content.Client/IgnoredComponents.cs @@ -223,7 +223,8 @@ "Machine", "MachinePart", "MachineFrame", - "MachineBoard" + "MachineBoard", + "ChemicalAmmo" }; } } diff --git a/Content.Server/GameObjects/Components/Weapon/Ranged/Ammunition/ChemicalAmmoComponent.cs b/Content.Server/GameObjects/Components/Weapon/Ranged/Ammunition/ChemicalAmmoComponent.cs new file mode 100644 index 0000000000..1d7fb042b3 --- /dev/null +++ b/Content.Server/GameObjects/Components/Weapon/Ranged/Ammunition/ChemicalAmmoComponent.cs @@ -0,0 +1,66 @@ +using Content.Server.GameObjects.Components.Chemistry; +using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels; +using Content.Shared.Chemistry; +using Robust.Shared.GameObjects; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Serialization; +using System.Collections.Generic; +using System.Linq; + +namespace Content.Server.GameObjects.Components.Weapon.Ranged.Ammunition +{ + [RegisterComponent] + public class ChemicalAmmoComponent : Component + { + public override string Name => "ChemicalAmmo"; + + private float _fractionTransfered; + + public override void ExposeData(ObjectSerializer serializer) + { + base.ExposeData(serializer); + serializer.DataField(ref _fractionTransfered, "fractionTransfered", 1); + } + + public override void HandleMessage(ComponentMessage message, IComponent component) + { + base.HandleMessage(message, component); + switch (message) + { + case BarrelFiredMessage barrelFired: + TransferSolution(barrelFired); + break; + } + } + + private void TransferSolution(BarrelFiredMessage barrelFired) + { + if (!Owner.TryGetComponent(out var ammoSolutionContainer)) + return; + + var projectiles = barrelFired.FiredProjectiles; + + var projectileSolutionContainers = new List(); + foreach (var projectile in projectiles) + { + if (projectile.TryGetComponent(out var projectileSolutionContainer)) + { + projectileSolutionContainers.Add(projectileSolutionContainer); + } + } + + if (!projectileSolutionContainers.Any()) + return; + + var solutionPerProjectile = ammoSolutionContainer.CurrentVolume * (1 / projectileSolutionContainers.Count); + + foreach (var projectileSolutionContainer in projectileSolutionContainers) + { + var solutionToTransfer = ammoSolutionContainer.SplitSolution(solutionPerProjectile); + projectileSolutionContainer.TryAddSolution(solutionToTransfer); + } + + ammoSolutionContainer.RemoveAllSolution(); + } + } +} diff --git a/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs b/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs index b76bbb801f..a00464c424 100644 --- a/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs +++ b/Content.Server/GameObjects/Components/Weapon/Ranged/Barrels/ServerRangedBarrelComponent.cs @@ -252,7 +252,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels { var ammoComponent = ammo.GetComponent(); - FireProjectiles(shooter, projectile, ammoComponent.ProjectilesFired, ammoComponent.EvenSpreadAngle, angle, ammoComponent.Velocity); + FireProjectiles(shooter, projectile, ammoComponent.ProjectilesFired, ammoComponent.EvenSpreadAngle, angle, ammoComponent.Velocity, ammo); if (CanMuzzleFlash) { @@ -351,7 +351,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels /// /// Handles firing one or many projectiles /// - private void FireProjectiles(IEntity shooter, IEntity baseProjectile, int count, float evenSpreadAngle, Angle angle, float velocity) + private void FireProjectiles(IEntity shooter, IEntity baseProjectile, int count, float evenSpreadAngle, Angle angle, float velocity, IEntity ammo) { List sprayAngleChange = null; if (count > 1) @@ -360,6 +360,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels sprayAngleChange = Linspace(-evenSpreadAngle / 2, evenSpreadAngle / 2, count); } + var firedProjectiles = new List(); for (var i = 0; i < count; i++) { IEntity projectile; @@ -373,6 +374,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels projectile = Owner.EntityManager.SpawnEntity(baseProjectile.Prototype.ID, baseProjectile.Transform.Coordinates); } + firedProjectiles.Add(projectile); Angle projectileAngle; @@ -398,6 +400,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels projectile.Transform.LocalRotation = projectileAngle.Theta; } + ammo.SendMessage(this, new BarrelFiredMessage(firedProjectiles)); } /// @@ -458,4 +461,14 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels message.AddText(fireRateMessage); } } + + public class BarrelFiredMessage : ComponentMessage + { + public readonly List FiredProjectiles; + + public BarrelFiredMessage(List firedProjectiles) + { + FiredProjectiles = firedProjectiles; + } + } } diff --git a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/cartridges.yml b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/cartridges.yml index 78262f338e..c6a37c4017 100644 --- a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/cartridges.yml +++ b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/cartridges.yml @@ -99,3 +99,8 @@ projectile: PelletShotgunTranquilizer projectilesFired: 1 ammoSpread: 0 + - type: ChemicalAmmo + - type: SolutionContainer + maxVol: 15 + caps: AddTo, RemoveFrom + - type: Pourable \ No newline at end of file diff --git a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/projectiles.yml b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/projectiles.yml index d636b4ae77..a60c07a38b 100644 --- a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/projectiles.yml +++ b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Shotgun/projectiles.yml @@ -90,10 +90,7 @@ damages: Blunt: 1 - type: SolutionContainer - maxVol: 10 - contents: - reagents: - - ReagentId: chem.H2O - Quantity: 10 - caps: RemoveFrom + maxVol: 15 + caps: AddTo, RemoveFrom - type: ChemicalInjectionProjectile + transferAmount: 15