GUNS PELASE WORK.

This commit is contained in:
Pieter-Jan Briers
2020-06-24 20:00:31 +02:00
parent 9b6455519b
commit 74a63c9555
7 changed files with 54 additions and 48 deletions

View File

@@ -69,7 +69,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Ammunition
/// </summary> /// </summary>
public float Velocity => _velocity; public float Velocity => _velocity;
private float _velocity; private float _velocity;
private string _muzzleFlashSprite; private string _muzzleFlashSprite;
public string SoundCollectionEject => _soundCollectionEject; public string SoundCollectionEject => _soundCollectionEject;
@@ -104,7 +104,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Ammunition
} }
} }
public IEntity TakeBullet() public IEntity TakeBullet(GridCoordinates spawnAt)
{ {
if (_ammoIsProjectile) if (_ammoIsProjectile)
{ {
@@ -121,12 +121,12 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Ammunition
{ {
appearanceComponent.SetData(AmmoVisuals.Spent, true); appearanceComponent.SetData(AmmoVisuals.Spent, true);
} }
var entity = Owner.EntityManager.SpawnEntity(_projectileId, Owner.Transform.GridPosition); var entity = Owner.EntityManager.SpawnEntity(_projectileId, spawnAt);
DebugTools.AssertNotNull(entity); DebugTools.AssertNotNull(entity);
return entity; return entity;
} }
public void MuzzleFlash(GridCoordinates grid, Angle angle) public void MuzzleFlash(GridCoordinates grid, Angle angle)
{ {
if (_muzzleFlashSprite == null) if (_muzzleFlashSprite == null)
@@ -138,7 +138,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Ammunition
var deathTime = time + TimeSpan.FromMilliseconds(200); var deathTime = time + TimeSpan.FromMilliseconds(200);
// Offset the sprite so it actually looks like it's coming from the gun // Offset the sprite so it actually looks like it's coming from the gun
var offset = angle.ToVec().Normalized / 2; var offset = angle.ToVec().Normalized / 2;
var message = new EffectSystemMessage var message = new EffectSystemMessage
{ {
EffectSprite = _muzzleFlashSprite, EffectSprite = _muzzleFlashSprite,

View File

@@ -16,6 +16,7 @@ using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
@@ -161,14 +162,14 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
return _chamberContainer.ContainedEntity; return _chamberContainer.ContainedEntity;
} }
public override IEntity TakeProjectile() public override IEntity TakeProjectile(GridCoordinates spawnAt)
{ {
var chamberEntity = _chamberContainer.ContainedEntity; var chamberEntity = _chamberContainer.ContainedEntity;
if (_autoCycle) if (_autoCycle)
{ {
Cycle(); Cycle();
} }
return chamberEntity?.GetComponent<AmmoComponent>().TakeBullet(); return chamberEntity?.GetComponent<AmmoComponent>().TakeBullet(spawnAt);
} }
protected override bool WeaponCanFire() protected override bool WeaponCanFire()

View File

@@ -14,6 +14,7 @@ using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
@@ -37,7 +38,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
public override int Capacity => _capacity; public override int Capacity => _capacity;
private int _capacity; private int _capacity;
// Even a point having a chamber? I guess it makes some of the below code cleaner // Even a point having a chamber? I guess it makes some of the below code cleaner
private ContainerSlot _chamberContainer; private ContainerSlot _chamberContainer;
private Stack<IEntity> _spawnedAmmo; private Stack<IEntity> _spawnedAmmo;
@@ -47,11 +48,11 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
private string _fillPrototype; private string _fillPrototype;
private int _unspawnedCount; private int _unspawnedCount;
private bool _manualCycle; private bool _manualCycle;
private AppearanceComponent _appearanceComponent; private AppearanceComponent _appearanceComponent;
// Sounds // Sounds
private string _soundCycle; private string _soundCycle;
private string _soundInsert; private string _soundInsert;
@@ -66,10 +67,10 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
serializer.DataField(ref _soundCycle, "soundCycle", "/Audio/Guns/Cock/sf_rifle_cock.ogg"); serializer.DataField(ref _soundCycle, "soundCycle", "/Audio/Guns/Cock/sf_rifle_cock.ogg");
serializer.DataField(ref _soundInsert, "soundInsert", "/Audio/Guns/MagIn/bullet_insert.ogg"); serializer.DataField(ref _soundInsert, "soundInsert", "/Audio/Guns/MagIn/bullet_insert.ogg");
_spawnedAmmo = new Stack<IEntity>(_capacity - 1); _spawnedAmmo = new Stack<IEntity>(_capacity - 1);
} }
void IMapInit.MapInit() void IMapInit.MapInit()
{ {
if (_fillPrototype != null) if (_fillPrototype != null)
@@ -82,7 +83,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_ammoContainer = _ammoContainer =
ContainerManagerComponent.Ensure<Container>($"{Name}-ammo-container", Owner, out var existing); ContainerManagerComponent.Ensure<Container>($"{Name}-ammo-container", Owner, out var existing);
@@ -95,7 +96,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
} }
} }
_chamberContainer = _chamberContainer =
ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-chamber-container", Owner, out existing); ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-chamber-container", Owner, out existing);
if (existing) if (existing)
{ {
@@ -106,7 +107,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
{ {
_appearanceComponent = appearanceComponent; _appearanceComponent = appearanceComponent;
} }
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, true); _appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, true);
UpdateAppearance(); UpdateAppearance();
} }
@@ -122,14 +123,14 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
return _chamberContainer.ContainedEntity; return _chamberContainer.ContainedEntity;
} }
public override IEntity TakeProjectile() public override IEntity TakeProjectile(GridCoordinates spawnAt)
{ {
var chamberEntity = _chamberContainer.ContainedEntity; var chamberEntity = _chamberContainer.ContainedEntity;
if (!_manualCycle) if (!_manualCycle)
{ {
Cycle(); Cycle();
} }
return chamberEntity?.GetComponent<AmmoComponent>().TakeBullet(); return chamberEntity?.GetComponent<AmmoComponent>().TakeBullet(spawnAt);
} }
private void Cycle(bool manual = false) private void Cycle(bool manual = false)
@@ -141,7 +142,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
var ammoComponent = chamberedEntity.GetComponent<AmmoComponent>(); var ammoComponent = chamberedEntity.GetComponent<AmmoComponent>();
if (!ammoComponent.Caseless) if (!ammoComponent.Caseless)
{ {
EjectCasing(chamberedEntity); EjectCasing(chamberedEntity);
} }
} }
@@ -165,7 +166,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
EntitySystem.Get<AudioSystem>().PlayAtCoords(_soundCycle, Owner.Transform.GridPosition, AudioParams.Default.WithVolume(-2)); EntitySystem.Get<AudioSystem>().PlayAtCoords(_soundCycle, Owner.Transform.GridPosition, AudioParams.Default.WithVolume(-2));
} }
} }
// Dirty(); // Dirty();
UpdateAppearance(); UpdateAppearance();
} }
@@ -195,9 +196,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
} }
return true; return true;
} }
Owner.PopupMessage(eventArgs.User, Loc.GetString("No room")); Owner.PopupMessage(eventArgs.User, Loc.GetString("No room"));
return false; return false;
} }
@@ -212,4 +213,4 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
return TryInsertBullet(eventArgs); return TryInsertBullet(eventArgs);
} }
} }
} }

View File

@@ -15,6 +15,7 @@ using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random; using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
@@ -88,7 +89,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
{ {
_appearanceComponent = appearanceComponent; _appearanceComponent = appearanceComponent;
} }
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, true); _appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, true);
} }
@@ -169,16 +170,17 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
/// Takes a projectile out if possible /// Takes a projectile out if possible
/// IEnumerable just to make supporting shotguns saner /// IEnumerable just to make supporting shotguns saner
/// </summary> /// </summary>
/// <param name="spawnAt"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
public override IEntity TakeProjectile() public override IEntity TakeProjectile(GridCoordinates spawnAt)
{ {
var ammo = _ammoSlots[_currentSlot]; var ammo = _ammoSlots[_currentSlot];
IEntity bullet = null; IEntity bullet = null;
if (ammo != null) if (ammo != null)
{ {
var ammoComponent = ammo.GetComponent<AmmoComponent>(); var ammoComponent = ammo.GetComponent<AmmoComponent>();
bullet = ammoComponent.TakeBullet(); bullet = ammoComponent.TakeBullet(spawnAt);
if (ammoComponent.Caseless) if (ammoComponent.Caseless)
{ {
_ammoSlots[_currentSlot] = null; _ammoSlots[_currentSlot] = null;

View File

@@ -14,6 +14,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
using Logger = Robust.Shared.Log.Logger; using Logger = Robust.Shared.Log.Logger;
@@ -134,7 +135,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
return ammo; return ammo;
} }
public override IEntity TakeProjectile() public override IEntity TakeProjectile(GridCoordinates spawnAt)
{ {
var powerCellEntity = _powerCellContainer.ContainedEntity; var powerCellEntity = _powerCellContainer.ContainedEntity;

View File

@@ -15,6 +15,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -180,7 +181,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
return BoltOpen ? null : _chamberContainer.ContainedEntity; return BoltOpen ? null : _chamberContainer.ContainedEntity;
} }
public override IEntity TakeProjectile() public override IEntity TakeProjectile(GridCoordinates spawnAt)
{ {
if (BoltOpen) if (BoltOpen)
{ {
@@ -189,7 +190,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
var entity = _chamberContainer.ContainedEntity; var entity = _chamberContainer.ContainedEntity;
Cycle(); Cycle();
return entity?.GetComponent<AmmoComponent>().TakeBullet(); return entity?.GetComponent<AmmoComponent>().TakeBullet(spawnAt);
} }
private void Cycle(bool manual = false) private void Cycle(bool manual = false)

View File

@@ -44,19 +44,19 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
[Dependency] private IGameTiming _gameTiming; [Dependency] private IGameTiming _gameTiming;
[Dependency] private IRobustRandom _robustRandom; [Dependency] private IRobustRandom _robustRandom;
#pragma warning restore 649 #pragma warning restore 649
public override FireRateSelector FireRateSelector => _fireRateSelector; public override FireRateSelector FireRateSelector => _fireRateSelector;
private FireRateSelector _fireRateSelector; private FireRateSelector _fireRateSelector;
public override FireRateSelector AllRateSelectors => _fireRateSelector; public override FireRateSelector AllRateSelectors => _fireRateSelector;
private FireRateSelector _allRateSelectors; private FireRateSelector _allRateSelectors;
public override float FireRate => _fireRate; public override float FireRate => _fireRate;
private float _fireRate; private float _fireRate;
// _lastFire is when we actually fired (so if we hold the button then recoil doesn't build up if we're not firing) // _lastFire is when we actually fired (so if we hold the button then recoil doesn't build up if we're not firing)
private TimeSpan _lastFire; private TimeSpan _lastFire;
public abstract IEntity PeekAmmo(); public abstract IEntity PeekAmmo();
public abstract IEntity TakeProjectile(); public abstract IEntity TakeProjectile(GridCoordinates spawnAt);
// Recoil / spray control // Recoil / spray control
private Angle _minAngle; private Angle _minAngle;
@@ -98,7 +98,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
{ {
var minAngle = serializer.ReadDataField("minAngle", 0) / 2; var minAngle = serializer.ReadDataField("minAngle", 0) / 2;
_minAngle = Angle.FromDegrees(minAngle); _minAngle = Angle.FromDegrees(minAngle);
// Random doubles it as it's +/- so uhh we'll just half it here for readability // Random doubles it as it's +/- so uhh we'll just half it here for readability
var maxAngle = serializer.ReadDataField("maxAngle", 45) / 2; var maxAngle = serializer.ReadDataField("maxAngle", 45) / 2;
_maxAngle = Angle.FromDegrees(maxAngle); _maxAngle = Angle.FromDegrees(maxAngle);
var angleIncrease = serializer.ReadDataField("angleIncrease", (40 / _fireRate)); var angleIncrease = serializer.ReadDataField("angleIncrease", (40 / _fireRate));
@@ -106,7 +106,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
var angleDecay = serializer.ReadDataField("angleDecay", (float) 20); var angleDecay = serializer.ReadDataField("angleDecay", (float) 20);
_angleDecay = angleDecay * (float) Math.PI / 180; _angleDecay = angleDecay * (float) Math.PI / 180;
serializer.DataField(ref _spreadRatio, "ammoSpreadRatio", 1.0f); serializer.DataField(ref _spreadRatio, "ammoSpreadRatio", 1.0f);
// FireRate options // FireRate options
var allFireRates = serializer.ReadDataField("allSelectors", new List<FireRateSelector>()); var allFireRates = serializer.ReadDataField("allSelectors", new List<FireRateSelector>());
foreach (var fireRate in allFireRates) foreach (var fireRate in allFireRates)
@@ -193,7 +193,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
} }
var ammo = PeekAmmo(); var ammo = PeekAmmo();
var projectile = TakeProjectile(); var projectile = TakeProjectile(shooter.Transform.GridPosition);
if (projectile == null) if (projectile == null)
{ {
soundSystem.PlayAtCoords(_soundEmpty, Owner.Transform.GridPosition); soundSystem.PlayAtCoords(_soundEmpty, Owner.Transform.GridPosition);
@@ -236,7 +236,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
// Invalid types // Invalid types
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
soundSystem.PlayAtCoords(_soundGunshot, Owner.Transform.GridPosition); soundSystem.PlayAtCoords(_soundGunshot, Owner.Transform.GridPosition);
_lastFire = _gameTiming.CurTime; _lastFire = _gameTiming.CurTime;
@@ -253,9 +253,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
/// <param name="prototypeManager"></param> /// <param name="prototypeManager"></param>
/// <param name="ejectDirections"></param> /// <param name="ejectDirections"></param>
public static void EjectCasing( public static void EjectCasing(
IEntity entity, IEntity entity,
bool playSound = true, bool playSound = true,
IRobustRandom robustRandom = null, IRobustRandom robustRandom = null,
IPrototypeManager prototypeManager = null, IPrototypeManager prototypeManager = null,
Direction[] ejectDirections = null) Direction[] ejectDirections = null)
{ {
@@ -268,7 +268,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
{ {
ejectDirections = new[] {Direction.East, Direction.North, Direction.South, Direction.West}; ejectDirections = new[] {Direction.East, Direction.North, Direction.South, Direction.West};
} }
const float ejectOffset = 0.2f; const float ejectOffset = 0.2f;
var ammo = entity.GetComponent<AmmoComponent>(); var ammo = entity.GetComponent<AmmoComponent>();
var offsetPos = (robustRandom.NextFloat() * ejectOffset, robustRandom.NextFloat() * ejectOffset); var offsetPos = (robustRandom.NextFloat() * ejectOffset, robustRandom.NextFloat() * ejectOffset);
@@ -330,7 +330,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {
IEntity projectile; IEntity projectile;
if (i == 0) if (i == 0)
{ {
projectile = baseProjectile; projectile = baseProjectile;
@@ -355,21 +355,21 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
var physicsComponent = projectile.GetComponent<PhysicsComponent>(); var physicsComponent = projectile.GetComponent<PhysicsComponent>();
physicsComponent.Status = BodyStatus.InAir; physicsComponent.Status = BodyStatus.InAir;
projectile.Transform.GridPosition = Owner.Transform.GridPosition; projectile.Transform.GridPosition = Owner.Transform.GridPosition;
var projectileComponent = projectile.GetComponent<ProjectileComponent>(); var projectileComponent = projectile.GetComponent<ProjectileComponent>();
projectileComponent.IgnoreEntity(shooter); projectileComponent.IgnoreEntity(shooter);
projectile.GetComponent<PhysicsComponent>().LinearVelocity = projectileAngle.ToVec() * velocity; projectile.GetComponent<PhysicsComponent>().LinearVelocity = projectileAngle.ToVec() * velocity;
projectile.Transform.LocalRotation = projectileAngle.Theta; projectile.Transform.LocalRotation = projectileAngle.Theta;
} }
} }
/// <summary> /// <summary>
/// Returns a list of numbers that form a set of equal intervals between the start and end value. Used to calculate shotgun spread angles. /// Returns a list of numbers that form a set of equal intervals between the start and end value. Used to calculate shotgun spread angles.
/// </summary> /// </summary>
private List<Angle> Linspace(double start, double end, int intervals) private List<Angle> Linspace(double start, double end, int intervals)
{ {
DebugTools.Assert(intervals > 1); DebugTools.Assert(intervals > 1);
var linspace = new List<Angle>(intervals); var linspace = new List<Angle>(intervals);
for (var i = 0; i <= intervals - 1; i++) for (var i = 0; i <= intervals - 1; i++)
@@ -398,11 +398,11 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
{ {
return; return;
} }
damageable.TakeDamage( damageable.TakeDamage(
hitscan.DamageType, hitscan.DamageType,
(int)Math.Round(hitscan.Damage, MidpointRounding.AwayFromZero), (int)Math.Round(hitscan.Damage, MidpointRounding.AwayFromZero),
Owner, Owner,
shooter); shooter);
//I used Math.Round over Convert.toInt32, as toInt32 always rounds to //I used Math.Round over Convert.toInt32, as toInt32 always rounds to
//even numbers if halfway between two numbers, rather than rounding to nearest //even numbers if halfway between two numbers, rather than rounding to nearest