Ejemplo n.º 1
0
QBodyDef* WorldModel::createBody(const QBodyDef *qbody)
{
    if (isSimulationRunning())
        return nullptr;

    QBodyDef* clone = qbody->clone();

    bodyList_.append(clone);
    emit bodyAdded(clone);

    return clone;
}
Ejemplo n.º 2
0
QBodyDef* WorldModel::createBody(const QFixtureDef *fixtureDef)
{
    if (isSimulationRunning())
        return nullptr;

    QBodyDef *body = new QBodyDef();
    if (fixtureDef) body->createFixture(fixtureDef);

    bodyList_.append(body);
    emit bodyAdded(body);

    return body;
}
Ejemplo n.º 3
0
void
LandscapeModel::onStopSimulationProcessor( const Command& _command )
{
	if ( !isSimulationRunning() )
		return;

	m_subscriber.unsubscribe();

	m_environment.removeTask( m_actionsProcessingTaskHandle );
	m_actionsProcessingTaskHandle.reset();

	m_environment.printMessage(
			Tools::Core::IMessenger::MessegeLevel::Success
			,	QString( Resources::SimulationStoppedMessage ).arg( Tools::Core::Time::currentTime() ).arg( m_ticksCounter ) );

} // LandscapeModel::onStopSimulationProcessor
Ejemplo n.º 4
0
void
LandscapeModel::onStartSimulationProcessor( const Command& _command )
{
	if ( isSimulationRunning() || !m_gameMode )
		return;

	locateStartPointObjects();

	try
	{
		m_environment.getLandscapeSerializer()->loadObjects( *this, *m_landscape, m_landscapeFilePath );
	}
	catch( ... )
	{
	}

	m_ticksCounter = 0;

	m_simulationStartTimeStamp = Tools::Core::Time::currentTime();

	m_environment.printMessage(
			Tools::Core::IMessenger::MessegeLevel::Info
		,	QString( Resources::SimulationHasBeenStartedMessage ).arg( m_simulationStartTimeStamp ) );

	m_actionsProcessingTaskHandle = m_environment.pushPeriodicalTask(
			Resources::ModelThreadName
		,	boost::bind( &LandscapeModel::gameMainLoop, this )
		,	Resources::TimeLimit );

	m_environment.riseEvent( Framework::Core::EventManager::Event( Events::SimulationStarted::Type ) );

	m_subscriber.subscribe(		Resources::ModelThreadName
							,	Framework::Core::EventManager::Resources::AnyEventName
							,	boost::bind( &LandscapeModel::onEvent, this, _1 ) );

} // LandscapeModel::onStartSimulationProcessor
Ejemplo n.º 5
0
void
LandscapeModel::gameMainLoop()
{
	const Tools::Core::Time::Milliseconds startTime = Tools::Core::Time::currentTime();

	{
		QMutexLocker locker( &m_mutex );

		if ( !m_gameMode->prepareToTick( m_ticksCounter + 1 ) )
		{
			m_simulationBlocked = true;
			return;
		}

		m_simulationBlocked = false;

		// Oops, simulation has been finished!
		if ( !isSimulationRunning() )
			return;

		++m_ticksCounter;

		m_environment.riseEvent(
			Framework::Core::EventManager::Event( Events::CurrentTickNumberChanged::Type )
				.pushMember( Events::CurrentTickNumberChanged::TickNumberAttribute, m_ticksCounter ) );

		if ( m_ticksCounter == 1 || ( m_ticksCounter % gs_aiThinkCallPeriod == 0 ) )
			processAIThinkCall();

		processAIPlayersGoals();

		if ( m_landscape )
		{
			ILandscape::ObjectsCollection objects;
			m_landscape->fetchObjects( objects );

			ILandscape::ObjectsCollectionIterator
					begin = objects.begin()
				,	end = objects.end();

			for ( ; begin != end; ++begin )
			{
				boost::intrusive_ptr< IActionsComponent > actionsComponent
					= ( *begin )->getComponent< IActionsComponent >( ComponentId::Actions );

				// Periodical Actions loop

				IActionsComponent::ActionsIterator periodocalActions
					= actionsComponent->getPeriodicalActionsIterator();

				while( periodocalActions->isValid() )
				{
					periodocalActions->current()->processAction();
					periodocalActions->next();
				}

				// Actions loop

				actionsComponent->processAction();
			}
		}

		// Process workers

		// TODO: workers can be removed during processing
		WorkersCollection workers = m_workers;

		WorkersCollectionIterator
				begin = workers.begin()
			,	end = workers.end();

		for ( ; begin != end; ++begin )
			begin->second->getComponent< IActionsComponent >( ComponentId::Actions )->processAction();

		// Fetch dying objects

		// Process notifications

		m_environment.getNotificationCenter()->processNotifiers();
	}

	if ( m_victoryChecker->check() )
	{
		pushCommand( Command( CommandId::StopSimulation ) );
		return;
	}

	const Tools::Core::Time::Milliseconds time = Tools::Core::Time::currentTime() - startTime;

	if ( time > Resources::TimeLimit )
	{
		/*m_environment.printMessage(
				Tools::Core::IMessenger::MessegeLevel::Warning
			,	QString( Resources::TimeLimitWarning ).arg( time ).arg( Resources::TimeLimit )  );*/
	}

} // LandscapeModel::gameMainLoop