Don't spawn all bullets in a magazine until necessary.
This commit is contained in:
@@ -17,29 +17,23 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
|
|||||||
public override string Name => "BallisticMagazine";
|
public override string Name => "BallisticMagazine";
|
||||||
|
|
||||||
// Stack of loaded bullets.
|
// Stack of loaded bullets.
|
||||||
[ViewVariables]
|
[ViewVariables] private readonly Stack<IEntity> _loadedBullets = new Stack<IEntity>();
|
||||||
private readonly Stack<IEntity> _loadedBullets = new Stack<IEntity>();
|
[ViewVariables] private string _fillType;
|
||||||
[ViewVariables]
|
|
||||||
private string _fillType;
|
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables] private Container _bulletContainer;
|
||||||
private Container _bulletContainer;
|
[ViewVariables] private AppearanceComponent _appearance;
|
||||||
[ViewVariables]
|
|
||||||
private AppearanceComponent _appearance;
|
|
||||||
|
|
||||||
private BallisticMagazineType _magazineType;
|
private BallisticMagazineType _magazineType;
|
||||||
private BallisticCaliber _caliber;
|
private BallisticCaliber _caliber;
|
||||||
private int _capacity;
|
private int _capacity;
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables] public BallisticMagazineType MagazineType => _magazineType;
|
||||||
public BallisticMagazineType MagazineType => _magazineType;
|
[ViewVariables] public BallisticCaliber Caliber => _caliber;
|
||||||
[ViewVariables]
|
[ViewVariables] public int Capacity => _capacity;
|
||||||
public BallisticCaliber Caliber => _caliber;
|
|
||||||
[ViewVariables]
|
|
||||||
public int Capacity => _capacity;
|
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables] public int CountLoaded => _loadedBullets.Count + _availableSpawnCount;
|
||||||
public int CountLoaded => _loadedBullets.Count;
|
|
||||||
|
[ViewVariables] private int _availableSpawnCount;
|
||||||
|
|
||||||
public event Action OnAmmoCountChanged;
|
public event Action OnAmmoCountChanged;
|
||||||
|
|
||||||
@@ -51,6 +45,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
|
|||||||
serializer.DataField(ref _caliber, "caliber", BallisticCaliber.Unspecified);
|
serializer.DataField(ref _caliber, "caliber", BallisticCaliber.Unspecified);
|
||||||
serializer.DataField(ref _fillType, "fill", null);
|
serializer.DataField(ref _fillType, "fill", null);
|
||||||
serializer.DataField(ref _capacity, "capacity", 20);
|
serializer.DataField(ref _capacity, "capacity", 20);
|
||||||
|
serializer.DataField(ref _availableSpawnCount, "availableSpawnCount", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
@@ -93,6 +88,11 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
|
|||||||
throw new ArgumentException("entity is of the wrong caliber.", nameof(bullet));
|
throw new ArgumentException("entity is of the wrong caliber.", nameof(bullet));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (CountLoaded >= Capacity)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Magazine is full.");
|
||||||
|
}
|
||||||
|
|
||||||
_bulletContainer.Insert(bullet);
|
_bulletContainer.Insert(bullet);
|
||||||
_loadedBullets.Push(bullet);
|
_loadedBullets.Push(bullet);
|
||||||
_updateAppearance();
|
_updateAppearance();
|
||||||
@@ -101,12 +101,23 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
|
|||||||
|
|
||||||
public IEntity TakeBullet()
|
public IEntity TakeBullet()
|
||||||
{
|
{
|
||||||
|
IEntity bullet;
|
||||||
if (_loadedBullets.Count == 0)
|
if (_loadedBullets.Count == 0)
|
||||||
|
{
|
||||||
|
if (_availableSpawnCount == 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var bullet = _loadedBullets.Pop();
|
_availableSpawnCount -= 1;
|
||||||
|
bullet = Owner.EntityManager.SpawnEntity(_fillType);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bullet = _loadedBullets.Pop();
|
||||||
|
_bulletContainer.Remove(bullet);
|
||||||
|
}
|
||||||
|
|
||||||
_updateAppearance();
|
_updateAppearance();
|
||||||
OnAmmoCountChanged?.Invoke();
|
OnAmmoCountChanged?.Invoke();
|
||||||
return bullet;
|
return bullet;
|
||||||
@@ -117,51 +128,53 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
|
|||||||
_appearance.SetData(BallisticMagazineVisuals.AmmoLeft, CountLoaded);
|
_appearance.SetData(BallisticMagazineVisuals.AmmoLeft, CountLoaded);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IMapInit.MapInit()
|
public void MapInit()
|
||||||
{
|
{
|
||||||
if (_fillType == null)
|
_availableSpawnCount = Capacity;
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load up bullets from fill.
|
|
||||||
for (var i = 0; i < Capacity; i++)
|
|
||||||
{
|
|
||||||
var bullet = Owner.EntityManager.SpawnEntity(_fillType);
|
|
||||||
AddBullet(bullet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum BallisticMagazineType
|
public enum BallisticMagazineType
|
||||||
{
|
{
|
||||||
Unspecified = 0,
|
Unspecified = 0,
|
||||||
|
|
||||||
// .32
|
// .32
|
||||||
A32,
|
A32,
|
||||||
|
|
||||||
// .357
|
// .357
|
||||||
A357,
|
A357,
|
||||||
|
|
||||||
// .44
|
// .44
|
||||||
A44,
|
A44,
|
||||||
|
|
||||||
// .45mm
|
// .45mm
|
||||||
A45mm,
|
A45mm,
|
||||||
|
|
||||||
// .50 cal
|
// .50 cal
|
||||||
A50,
|
A50,
|
||||||
|
|
||||||
// 5.56mm
|
// 5.56mm
|
||||||
A556mm,
|
A556mm,
|
||||||
|
|
||||||
// 6.5mm
|
// 6.5mm
|
||||||
A65mm,
|
A65mm,
|
||||||
|
|
||||||
// 7.62mm
|
// 7.62mm
|
||||||
A762mm,
|
A762mm,
|
||||||
Maxim,
|
Maxim,
|
||||||
|
|
||||||
// 9mm
|
// 9mm
|
||||||
A9mm,
|
A9mm,
|
||||||
A9mmSMG,
|
A9mmSMG,
|
||||||
A9mmTopMounted,
|
A9mmTopMounted,
|
||||||
|
|
||||||
// 10mm
|
// 10mm
|
||||||
A10mm,
|
A10mm,
|
||||||
A10mmSMG,
|
A10mmSMG,
|
||||||
|
|
||||||
// 20mm
|
// 20mm
|
||||||
A20mm,
|
A20mm,
|
||||||
|
|
||||||
// 24mm
|
// 24mm
|
||||||
A24mm,
|
A24mm,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user