更新:我应该更清楚。我决定按升序对列表进行排序,然后获得第一个具有比当前选择武器更大价值的武器。
我的NextWeapon()函数此时没有做任何事情(它只是显示我尝试过的东西),但在过去我只是迭代到列表中的下一个项目,但是一个项目可能不在那里。
枚举F3DFXType是我的角色可以拾取的武器,但是如果它们实际上不在库存中则没有意义。因此,我尝试创建一个int类型的列表,并循环使用它。当角色拿起一个新武器时,它会作为一个int添加到列表中,然后当我切换到那个int时,我会在F3DFXType枚举中检查相同的int。
例如,在拾取器中我会检查碰撞事件,然后使用: weaponList.Add(5);认为它会将整数5添加到我的库存中。 F3DFXType中的第5项是“Seeker”。当我试图遍历我的库存时,它实际上不会添加第五个F3DFXType,它只是在F3DFXType中添加NEXT项。 (例如:我已经有了Vulcan,这只会添加SoloGun,这是枚举中的第二项)
我正在尝试遍历列表中的项目。
我遇到的问题是,如果我迭代到列表中的下一个项目,那个项目可能实际上并不存在。那么我该如何前进到列表中存在的下一个项目呢?
我不一定要使用下面的代码寻找答案,我只想提供一些背景信息,以便您可以看到我当前的方法。
// Possible weapon types
public enum F3DFXType
{
Vulcan = 1,
SoloGun,
Sniper,
ShotGun,
Seeker,
RailGun,
PlasmaGun,
PlasmaBeam,
PlasmaBeamHeavy,
LightningGun,
FlameRed,
LaserImpulse
}
// List of weapons currently avaialable
public List<int> weaponList;
public int currentIndex = 1;
void NextWeapon()
{
// Keep within bounds of list
if (currentIndex < weaponList.Count)
{
currentIndex++;
// Check if a higher value exists
var higherVal = weaponList.Any(item => item < currentIndex);
// create a new list to store current weapons in inventory
var newWeapList = new List<int>();
foreach (var weap in weaponList)
{
newWeapList.Add(weap);
}
weaponList = newWeapList;
// If a higher value exists....
if (higherVal)
{
// currentIndex = SOMEHIGHERVALUE
}
}
}
void PrevWeapon()
{
if (currentIndex > 1)
{
currentIndex--;
}
}
// Fire turret weapon
public void Fire()
{
switch (currentIndex)
{
case 1:
// Fire vulcan at specified rate until canceled
timerID = F3DTime.time.AddTimer(0.05f, Vulcan);
Vulcan();
break;
case 2:
timerID = F3DTime.time.AddTimer(0.2f, SoloGun);
SoloGun();
break;
case 3:
timerID = F3DTime.time.AddTimer(0.3f, Sniper);
Sniper();
break;
case 4:
ShotGun();
break;
case 5:
timerID = F3DTime.time.AddTimer(0.2f, Seeker);
Seeker();
break
default:
break;
}
}
答案 0 :(得分:3)
如果我正确地理解了这个问题,我认为你可以通过按升序对列表进行排序,然后获得比当前选择武器更大价值的第一种武器来做到这一点。
例如:
void Next()
{
var nextWeapon = weaponList
.OrderBy(w => w) // Sort the weapon list
.FirstOrDefault(w => w > currentIndex); // Get the next highest weapon
// If nextWeapon is 0, there was no higher weapon found
currentIndex = nextWeapon > 0 ? nextWeapon : currentIndex;
}
编辑:
您也可以将其反转以获取以前的武器:
void PrevWeapon()
{
if (currentIndex > 1)
{
var previousWeapon = weaponList
.OrderByDescending(w => w) // Sort the weapon list
.FirstOrDefault(w => w < currentIndex); // Get the next lowest weapon
// If previousWeapon is 0, there is no next lowest weapon
currentIndex = previousWeapon > 0 ? previousWeapon : currentIndex;
}
}
答案 1 :(得分:0)
我找到了解决方案。不知道为什么这花了这么长时间才弄明白。无论如何,我现在都在工作。
我从库存中的2支枪开始。我现在只能在这两者之间切换。 (我的数组中的项目0和4)。
当我与拾取器发生碰撞时,我可以在此库存中添加另一个项目(阵列中的第11项)。
现在,当我点击Previous或Advance时,它知道保持在数组范围内,如果找不到更高的武器,则默认返回到我们当前使用的武器。
// Weapon types
public enum F3DFXType
{
Vulcan = 0,
SoloGun = 1,
Sniper = 2,
ShotGun = 3,
Seeker = 4,
RailGun = 5,
PlasmaGun = 6,
PlasmaBeam = 7,
PlasmaBeamHeavy = 8,
LightningGun = 9,
FlameRed = 10,
LaserImpulse = 11,
MAX_WEAPONS = 12
}
public bool[] aWeaponsHave = new bool[(int)F3DFXType.MAX_WEAPONS];
int iWeaponsCurrent;
public Constructor()
{
.....
// Start at first weapon (VULCAN)
iWeaponsCurrent = 0;
aWeaponsHave[0] = true;
// DEBUG: Add weapon to inventory
aWeaponsHave[1] = true;
.....
}
private void GoNextWeapon()
{
// If there isn't a higher value found, then default back to the original one
var originalVal = iWeaponsCurrent;
do
{
iWeaponsCurrent++;
// Went to the end of array & didn't find a new weapon. Set it to the original one.
if (iWeaponsCurrent == 12)
{
iWeaponsCurrent = originalVal;
return;
}
} while (aWeaponsHave[iWeaponsCurrent] == false);
}
void GoPrevWeapon()
{
do
{
iWeaponsCurrent--;
// Prevent from spilling over
if (iWeaponsCurrent < 0)
{
// Went to end of array. Set weapon to lowest value and get out of here
iWeaponsCurrent = 0;
return;
}
} while (!aWeaponsHave[iWeaponsCurrent]);
}
// Fire turret weapon
public void Fire()
{
switch (iWeaponsCurrent)
{
//case F3DFXType.Vulcan:
case 0:
// Fire vulcan at specified rate until canceled
timerID = F3DTime.time.AddTimer(0.05f, Vulcan);
// Invoke manually before the timer ticked to avoid initial delay
Vulcan();
break;
//case F3DFXType.SoloGun:
case 1:
timerID = F3DTime.time.AddTimer(0.2f, SoloGun);
Debug.Log("Solo");
SoloGun();
break;
//case F3DFXType.Sniper:
case 2:
timerID = F3DTime.time.AddTimer(0.3f, Sniper);
Debug.Log("Sniper");
Sniper();
break;
//case F3DFXType.ShotGun:
case 3:
timerID = F3DTime.time.AddTimer(0.3f, ShotGun);
ShotGun();
break;
//case F3DFXType.Seeker:
case 4:
timerID = F3DTime.time.AddTimer(0.2f, Seeker);
Seeker();
break;
//case F3DFXType.RailGun:
case 5:
timerID = F3DTime.time.AddTimer(0.2f, RailGun);
Debug.Log("railgun");
RailGun();
break;
//case F3DFXType.PlasmaGun:
case 6:
timerID = F3DTime.time.AddTimer(0.2f, PlasmaGun);
PlasmaGun();
break;
//case F3DFXType.PlasmaBeam:
case 7:
// Beams has no timer requirement
PlasmaBeam();
break;
//case F3DFXType.PlasmaBeamHeavy:
case 8:
// Beams has no timer requirement
PlasmaBeamHeavy();
break;
//case F3DFXType.LightningGun:
case 9:
// Beams has no timer requirement
LightningGun();
break;
//case F3DFXType.FlameRed:
case 10:
// Flames has no timer requirement
FlameRed();
break;
//case F3DFXType.LaserImpulse:
case 11:
timerID = F3DTime.time.AddTimer(0.15f, LaserImpulse);
LaserImpulse();
break;
default:
break;
}
}