本文整理汇总了C++中BattleItem::setAmmoItem方法的典型用法代码示例。如果您正苦于以下问题:C++ BattleItem::setAmmoItem方法的具体用法?C++ BattleItem::setAmmoItem怎么用?C++ BattleItem::setAmmoItem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BattleItem
的用法示例。
在下文中一共展示了BattleItem::setAmmoItem方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: btnApplyTemplateClick
void InventoryState::btnApplyTemplateClick(Action *)
{
// don't accept clicks when moving items
// it's ok if the template is empty -- it will just result in clearing the
// unit's inventory
if (_inv->getSelectedItem() != 0)
{
return;
}
BattleUnit *unit = _battleGame->getSelectedUnit();
std::vector<BattleItem*> *unitInv = unit->getInventory();
Tile *groundTile = unit->getTile();
std::vector<BattleItem*> *groundInv = groundTile->getInventory();
RuleInventory *groundRuleInv = _game->getMod()->getInventory("STR_GROUND");
_clearInventory(_game, unitInv, groundTile);
// attempt to replicate inventory template by grabbing corresponding items
// from the ground. if any item is not found on the ground, display warning
// message, but continue attempting to fulfill the template as best we can
bool itemMissing = false;
std::vector<EquipmentLayoutItem*>::iterator templateIt;
for (templateIt = _curInventoryTemplate.begin(); templateIt != _curInventoryTemplate.end(); ++templateIt)
{
// search for template item in ground inventory
std::vector<BattleItem*>::iterator groundItem;
const bool needsAmmo = !_game->getMod()->getItem((*templateIt)->getItemType())->getCompatibleAmmo()->empty();
bool found = false;
bool rescan = true;
while (rescan)
{
rescan = false;
const std::string targetAmmo = (*templateIt)->getAmmoItem();
BattleItem *matchedWeapon = NULL;
BattleItem *matchedAmmo = NULL;
for (groundItem = groundInv->begin(); groundItem != groundInv->end(); ++groundItem)
{
// if we find the appropriate ammo, remember it for later for if we find
// the right weapon but with the wrong ammo
const std::string groundItemName = (*groundItem)->getRules()->getType();
if (needsAmmo && targetAmmo == groundItemName)
{
matchedAmmo = *groundItem;
}
if ((*templateIt)->getItemType() == groundItemName)
{
// if the loaded ammo doesn't match the template item's,
// remember the weapon for later and continue scanning
BattleItem *loadedAmmo = (*groundItem)->getAmmoItem();
if ((needsAmmo && loadedAmmo && targetAmmo != loadedAmmo->getRules()->getType())
|| (needsAmmo && !loadedAmmo))
{
// remember the last matched weapon for simplicity (but prefer empty weapons if any are found)
if (!matchedWeapon || matchedWeapon->getAmmoItem())
{
matchedWeapon = *groundItem;
}
continue;
}
// move matched item from ground to the appropriate inv slot
(*groundItem)->setOwner(unit);
(*groundItem)->setSlot(_game->getMod()->getInventory((*templateIt)->getSlot()));
(*groundItem)->setSlotX((*templateIt)->getSlotX());
(*groundItem)->setSlotY((*templateIt)->getSlotY());
(*groundItem)->setFuseTimer((*templateIt)->getFuseTimer());
unitInv->push_back(*groundItem);
groundInv->erase(groundItem);
found = true;
break;
}
}
// if we failed to find an exact match, but found unloaded ammo and
// the right weapon, unload the target weapon, load the right ammo, and use it
if (!found && matchedWeapon && (!needsAmmo || matchedAmmo))
{
// unload the existing ammo (if any) from the weapon
BattleItem *loadedAmmo = matchedWeapon->getAmmoItem();
if (loadedAmmo)
{
groundTile->addItem(loadedAmmo, groundRuleInv);
matchedWeapon->setAmmoItem(NULL);
}
// load the correct ammo into the weapon
if (matchedAmmo)
{
matchedWeapon->setAmmoItem(matchedAmmo);
groundTile->removeItem(matchedAmmo);
}
// rescan and pick up the newly-loaded/unloaded weapon
rescan = true;
}
}
//.........这里部分代码省略.........