Код: Выделить всё
GameInstance
- AbilityHealthInstance
- PlayerAvatarInstance
- MonsterCardInstance - multi inherit
- CardInstance
- MonsterCardInstance - multi inherit
- SpellCardInstance
- TokenInstance
У платы GAM Список std :: ulious_ptr
[*] Discard - Список std :: unique_ptr
[*] Market - список std :: ulious_ptr std :: ulious_ptr
[*] Поле - массив фиксированного размера std :: ulious_ptr
[*] Active - список std :: unique_ptr
/> Итак, в начале игры я делаю палубы std :: ulious_ptr < /code> Но строить каждую карту в качестве соответствующего типа (
Код: Выделить всё
Monster
Однако возникают проблемы, когда я пытаюсь использовать GameInstance объекты полиморфически или пытаться вывести их тип. Field Если это монстр или область Active , если это заклинание . Но компилятор не позволит мне сыграть из std :: unique_ptr на std :: ulious_ptr , как и если бы я использовал необработанные указатели и Dynamic_cast . Итак, я не могу определить тип, и я не могу переместить экземпляр в коллекцию, к которой он должен перейти, чтобы представить место на игровой доске.
Код: Выделить всё
bool BuyCard(size_t idx)
{
//find a free spot in the field and attempt to move the card there if it's a monster
auto newPos = std::find_if(Field.begin(), Field.end(), [&mktref, &index, iTargets](std::unique_ptr& fldref ) {
if (fldref == nullptr)
{
//unique_pointer_cast is some analoge for dynamic_cast in this instance
fldref = unique_pointer_cast(std::move(mktref));
return fldref == nullptr; //assume unique_pointer_cast would return null if cast impossible
}
index++;
return false;
});
if (newpos != Field.end())
{
return true; //buy monster worked
}
else
{
//wasn't a monster, put the card to the back of the active cards as we can assume it was a spell
Active.emplace_back(unique_pointer_cast(std::move(mktref));
return Active.back() != nullptr; //lets hope the buy spell worked
}
}
Пытаться перейти в std :: unique_ptr .
bool DamageCard(unique_ptr target, size_t damage)
{
target->TakeDamage(damage)
if (target->isDead())
{ //move to discard}
}
//won't allow the following
DamageCard(PlayerAvatar, 5);
DamageCard(Field[3], 5);
< /code>
Что я ошибаюсь с использованием умных указателей здесь? Я предполагаю, что мне либо упускаю трюк, либо недоразумю их вариант использования.
Подробнее здесь: https://stackoverflow.com/questions/796 ... ing-issues