What is wrong with my code? (Weapon Reload)
I'm doing a real life reload system (if let's say you have 2 mags, if you shoot 10 bullets and reload, you'll have 9 bullets in your mag, one in the chamber and since your next mag is full you'll get you full mag bullets + 1, and if you reload again you will come back to your mag with 9 bullets) the thing is I don't want to read the mags that have 0 bullets, that way I will only reload if there's a mag with bullets inside. Could you guys help me out? Each mag is an object with a bullet property and a bool called isEmpty, then I have another object which is an array of objects with an index property, that way I can increment the index if I reload and if that was the last mag in the array I just change the index to 0.
Here is my mag object:
public class Magazine
{
#region Variables
private int _bullets;
private bool _isEmpty = true;
#endregion
#region Properties
public int bullets
{
get { return _bullets; }
set { _bullets = value; }
}
public bool isEmpty
{
get { return _isEmpty; }
set { _isEmpty = value; }
}
#endregion
public Magazine(int bullets)
{
_bullets = bullets;
}
}
here is my mag array (another class):
public class Magazines : IEnumerable
{
#region Variables
private Magazine[] _mags;
private int _magNum;
private int bullets;
private Magazine mag;
private int _magIndex;
#endregion
#region Properties
public int magIndex
{
get { return _magIndex; }
set { _magIndex = value; }
}
public int magNum
{
get { return _magNum; }
private set { _magNum = value; }
}
public Magazine[] mags
{
get { return _mags; }
set { _mags = value; }
}
public Magazine this[int i]
{
get { return _mags[i]; }
set { _mags[i] = value; }
}
public int maxIndex
{
get { return mags.Length - 1; }
private set { maxIndex = value; }
}
#endregion
#region Interfaces
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
public Magazines(int magNum, int bullets)
{
_magNum = magNum;
this.bullets = bullets;
mags = fillMags(mags);
_magIndex = 0;
}
public Magazine[] fillMags(Magazine[] mags)
{
mags = new Magazine[_magNum];
for (int i = 0; i < mags.Length; i++)
{
mag = new Magazine(bullets);
mags[i] = mag;
mags[i].isEmpty = false;
//_magIndex = i;
}
return mags;
}
}
and here is my Reload method, I know that the whole action thing is not necessary but I just wanted to test how it works:
void Reload()
{
Action<Magazines> reload = (mags) => {
Magazines m = mags;
int useMag = 0;
for (int i = 0; i < m.maxIndex; i++) if (m[i].isEmpty) useMag++;
if (useMag != 0)
{
if (currentMag == 0)
{
--magQty;
m[m.magIndex].isEmpty = true;
if (m.magIndex == m.maxIndex)
{
//magQty--;
//m[m.magIndex].isFull = false;
m[m.magIndex].bullets = currentMag; m.magIndex = 0;
if (!m[m.magIndex].isEmpty)
{
currentMag = m[m.magIndex].bullets;
Sounds.AK47reload(AK47, AK47reload, 0.3f);
}
}
else
{
//magQty--;
//m[m.magIndex].isFull = false;
m[m.magIndex].bullets = currentMag; m.magIndex++;
if (!m[m.magIndex].isEmpty)
{
currentMag = m[m.magIndex].bullets;
Sounds.AK47reload(AK47, AK47reload, 0.3f);
}
}
}
else if (currentMag != 0 && currentMag != bulletsPerMag + 1)
{
if (m.magIndex == m.maxIndex)
{
m[m.magIndex].bullets = currentMag - 1;
/*if (m[m.magIndex].bullets == 0)
{
m[m.magIndex].isFull = false;
magQty--;
}*/
m.magIndex = 0;
if (!m[m.magIndex].isEmpty) {
currentMag = m[m.magIndex].bullets + 1;
Sounds.AK47reload(AK47, AK47reload, 0.3f);
}
}
else
{
m[m.magIndex].bullets = currentMag - 1;
/*if (m[m.magIndex].bullets == 0)
{
m[m.magIndex].isFull = false;
magQty--;
}*/
m.magIndex++;
if (!m[m.magIndex].isEmpty)
{
currentMag = m[m.magIndex].bullets + 1;
Sounds.AK47reload(AK47, AK47reload, 0.3f);
}
}
}
}
}; reload(mags);
}
Your answer
Follow this Question
Related Questions
Ammo changing between guns 0 Answers
Reloading with R only goes up by 1. But reloading when the gun is empty works fine. 0 Answers
Reload animation makes gun too high 0 Answers
Reload Function Being Called Multiple Times 0 Answers
UFPS NoReload/Classic weapons 0 Answers