gamecontroller.cpp Example File
accelgame/src/gamecontroller.cpp
#include "gamecontroller.hpp"
#include "board.hpp"
#include "player.hpp"
GameController::GameController(QObject *parent)
: QObject(parent)
, m_boardContainer(0)
, m_board(0)
, m_player(0)
{
m_sensor.start();
}
void GameController::setBoard(bb::cascades::Container *board)
{
m_boardContainer = board;
m_board = new Board(m_boardContainer, this);
m_board->reset();
m_player = new Player(m_board, this);
m_player->reset();
bool ok = connect(m_player, SIGNAL(moved()), SLOT(evaluateInput()), Qt::QueuedConnection);
Q_ASSERT(ok);
Q_UNUSED(ok);
evaluateInput();
}
void GameController::newGame()
{
m_board->reset();
m_player->reset();
m_sensor.start();
evaluateInput();
}
void GameController::evaluateInput()
{
if (!m_player)
return;
if (m_sensor.x() > 0.8)
m_player->goLeft();
else if (m_sensor.x() < -0.8)
m_player->goRight();
else if (m_sensor.y() > 0.8)
m_player->goDown();
else if (m_sensor.y() < -0.8)
m_player->goUp();
else
QTimer::singleShot(80, this, SLOT(evaluateInput()));
}