Magazines now correctly get saved to the map.

This commit is contained in:
Pieter-Jan Briers
2019-03-24 23:37:28 +01:00
parent d9ff72c907
commit 6d0c2ed362
2 changed files with 43 additions and 6 deletions

View File

@@ -30,6 +30,8 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
public int CountLoaded => _loadedBullets.Count; public int CountLoaded => _loadedBullets.Count;
public event Action OnAmmoCountChanged;
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
{ {
base.ExposeData(serializer); base.ExposeData(serializer);
@@ -45,11 +47,24 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
base.Initialize(); base.Initialize();
_appearance = Owner.GetComponent<AppearanceComponent>(); _appearance = Owner.GetComponent<AppearanceComponent>();
}
public override void Startup()
{
base.Startup();
_bulletContainer = _bulletContainer =
ContainerManagerComponent.Ensure<Container>("magazine_bullet_container", Owner, out var existed); ContainerManagerComponent.Ensure<Container>("magazine_bullet_container", Owner, out var existed);
if (!existed && _fillType != null) if (existed)
{
foreach (var entity in _bulletContainer.ContainedEntities)
{
_loadedBullets.Push(entity);
}
_updateAppearance();
}
else if (_fillType != null)
{ {
// Load up bullets from fill. // Load up bullets from fill.
for (var i = 0; i < Capacity; i++) for (var i = 0; i < Capacity; i++)
@@ -59,6 +74,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
} }
} }
OnAmmoCountChanged?.Invoke();
_appearance.SetData(BallisticMagazineVisuals.AmmoCapacity, Capacity); _appearance.SetData(BallisticMagazineVisuals.AmmoCapacity, Capacity);
} }
@@ -77,6 +93,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
_bulletContainer.Insert(bullet); _bulletContainer.Insert(bullet);
_loadedBullets.Push(bullet); _loadedBullets.Push(bullet);
_updateAppearance(); _updateAppearance();
OnAmmoCountChanged?.Invoke();
} }
public IEntity TakeBullet() public IEntity TakeBullet()
@@ -88,6 +105,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
var bullet = _loadedBullets.Pop(); var bullet = _loadedBullets.Pop();
_updateAppearance(); _updateAppearance();
OnAmmoCountChanged?.Invoke();
return bullet; return bullet;
} }

View File

@@ -34,7 +34,8 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
private bool _autoEjectMagazine; private bool _autoEjectMagazine;
private AppearanceComponent _appearance; private AppearanceComponent _appearance;
private static readonly Direction[] _randomBulletDirs = { private static readonly Direction[] _randomBulletDirs =
{
Direction.North, Direction.North,
Direction.East, Direction.East,
Direction.South, Direction.South,
@@ -60,22 +61,32 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
base.Initialize(); base.Initialize();
_appearance = Owner.GetComponent<AppearanceComponent>(); _appearance = Owner.GetComponent<AppearanceComponent>();
_bulletDropRandom = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
}
public override void Startup()
{
base.Startup();
_magazineSlot = _magazineSlot =
ContainerManagerComponent.Ensure<ContainerSlot>("ballistic_gun_magazine", Owner, ContainerManagerComponent.Ensure<ContainerSlot>("ballistic_gun_magazine", Owner,
out var alreadyExisted); out var alreadyExisted);
_bulletDropRandom = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
if (!alreadyExisted && _defaultMagazine != null) if (!alreadyExisted && _defaultMagazine != null)
{ {
var magazine = Owner.EntityManager.SpawnEntity(_defaultMagazine); var magazine = Owner.EntityManager.SpawnEntity(_defaultMagazine);
InsertMagazine(magazine, false); InsertMagazine(magazine, false);
} }
else if (Magazine != null)
{
// Already got magazine from loading a container.
Magazine.GetComponent<BallisticMagazineComponent>().OnAmmoCountChanged += _magazineAmmoCountChanged;
}
_updateAppearance(); _updateAppearance();
} }
public bool InsertMagazine(IEntity magazine, bool playSound=true) public bool InsertMagazine(IEntity magazine, bool playSound = true)
{ {
if (!magazine.TryGetComponent(out BallisticMagazineComponent component)) if (!magazine.TryGetComponent(out BallisticMagazineComponent component))
{ {
@@ -103,6 +114,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
audioSystem.Play(_magInSound, Owner); audioSystem.Play(_magInSound, Owner);
} }
component.OnAmmoCountChanged += _magazineAmmoCountChanged;
if (GetChambered(0) == null) if (GetChambered(0) == null)
{ {
// No bullet in chamber, load one from magazine. // No bullet in chamber, load one from magazine.
@@ -117,7 +129,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
return true; return true;
} }
public bool EjectMagazine(bool playSound=true) public bool EjectMagazine(bool playSound = true)
{ {
var entity = Magazine; var entity = Magazine;
if (entity == null) if (entity == null)
@@ -133,7 +145,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
var audioSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>(); var audioSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>();
audioSystem.Play(_magOutSound, Owner); audioSystem.Play(_magOutSound, Owner);
} }
_updateAppearance(); _updateAppearance();
entity.GetComponent<BallisticMagazineComponent>().OnAmmoCountChanged -= _magazineAmmoCountChanged;
return true; return true;
} }
@@ -212,6 +226,11 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
return InsertMagazine(attackwith); return InsertMagazine(attackwith);
} }
private void _magazineAmmoCountChanged()
{
_updateAppearance();
}
private void _updateAppearance() private void _updateAppearance()
{ {
if (Magazine != null) if (Magazine != null)