Exemple #1
0
void Battle::NextTurn()
{
	std::queue<BattleEvent> events;

	std::sort(combatants.begin(), combatants.end(),
		[](Creature* a, Creature* b) { return a->GetAgility() > b->GetAgility(); });

	for (auto com : combatants)
	{
		if (com->GetID() == "Player")
		{
			Dialogue targetSelection = Dialogue("Who?", {});
			for (auto target : combatants)
				if (target->GetID() != "Player")
					targetSelection.AddChoice(target->GetName());

			int choice = battleOptions.Activate();

			switch (choice)
			{
			default:
			case 1:
			{
				int position = targetSelection.Activate();
				for (int i = 0; i < position; i++)
					if (combatants[i]->GetID() == "Player")
						position++;

				Creature* target = combatants[position - 1];
				events.push(BattleEvent(com, target, BattleEventType::ATTACK));
				break;
			}
			case 2:
			{
				events.push(BattleEvent(com, nullptr, BattleEventType::DEFEND));
				break;
			}
			}
		}
		else
		{
			Creature* player = *std::find_if(combatants.begin(), combatants.end(),
				[](Creature* a) { return a->GetID() == "Player"; });
			events.push(BattleEvent(com, player, BattleEventType::ATTACK));
		}
	}

	while (!events.empty())
	{
		BattleEvent event = events.front();
		switch (event.GetType())
		{
		case BattleEventType::ATTACK:
		{
			auto a = combatants.begin();
			auto b = combatants.end();

			if (std::find(a, b, event.GetSource()) == b || std::find(a, b, event.GetTarget()) == b)
				break;

			std::cout << event.GetSource()->GetName() << " attacks " << event.GetTarget()->GetName() << " for "	<< event.Run() << " damage!" << std::endl;
			if (event.GetTarget()->GetHP() <= 0)
				Kill(event.GetTarget());

			break;
		}
		case BattleEventType::DEFEND:
			std::cout << event.GetSource()->GetName() << " defends!\n";
			break;
		default:
			break;
		}

		events.pop();
	}
}
void BattleView::Process(void)
{

	Assert(m_eventQueue);
	if(!m_eventQueue)
		return;

	PointerList<BattleEvent>::PointerListNode *eventNode =
		m_eventQueue->GetHeadNode();

	while (eventNode)
    {
		BattleEvent *       event           = eventNode->GetObj();
		Assert(event);
		BattleViewActor *   actor           = event->GetActor();
		bool                addEvent        = true;
        bool                isAfterAttack   = IsAfterAttack(*event);
		PointerList<BattleEvent>::PointerListNode *
                            activeNode  = m_activeEvents->GetHeadNode();

		while (activeNode)
        {
            if (isAfterAttack &&
				(activeNode->GetObj()->GetType() == BATTLE_EVENT_TYPE_ATTACK)
               )
            {
				addEvent = false;
				break;
			}




			if((event->GetType() == BATTLE_EVENT_TYPE_PLACEMENT) &&
				(activeNode->GetObj()->GetType() != BATTLE_EVENT_TYPE_PLACEMENT)) {
				addEvent = false;
				break;
			}




			if(activeNode->GetObj()->GetActor() == actor) {
				addEvent = false;
				break;
			}

			activeNode = activeNode->GetNext();
		}

		if(addEvent) {

			PointerList<BattleEvent>::PointerListNode *addNode = eventNode;

			eventNode = eventNode->GetNext();

			m_eventQueue->Remove(addNode);

			m_activeEvents->AddTail(event);
		} else {

			eventNode = eventNode->GetNext();

		}
	}

	if(m_activeEvents->GetCount() > 0) {

		m_walker->SetList(m_activeEvents);


		while (m_walker->IsValid())
        {
			BattleEvent *   event = m_walker->GetObj();
			Assert(event);
			event->Process();

			if (event->IsFinished())
            {
				m_walker->Remove();
				delete event;

				if (!IsProcessing() && (!g_theCurrentBattle || !g_theCurrentBattle->IsDone()))
                {
					g_gevManager->GotUserInput();
                }
			}
            else
            {
				m_walker->Next();
            }
		}
	}
}