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