Commit ba34ed87 authored by Frederik Schwarzer's avatar Frederik Schwarzer

Format.

parent c6598414
......@@ -17,18 +17,18 @@
*/
#include "board.h"
#include "plane.h"
#include "building.h"
#include "bomb.h"
#include "building.h"
#include "plane.h"
#include "settings.h"
#include <KRandom>
#include <QTimer>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QStandardPaths>
#include <QTimer>
/** The value that the plane velocity increases by */
const qreal PLANE_INC_VELOCITY = 0.0005;
......@@ -49,8 +49,11 @@ const unsigned int PLANE_EXPLODE_TIME = 2000;
/** This time in milliseconds that the bomb exploding animation is played for */
const unsigned int BOMB_EXPLODE_TIME = 1000;
BomberBoard::BomberBoard(KGameRenderer *renderer, QGraphicsView *view, QObject *parent) :
QGraphicsScene(parent), m_renderer(renderer), m_bomb(nullptr), m_view(view)
BomberBoard::BomberBoard(KGameRenderer * renderer, QGraphicsView * view, QObject * parent)
: QGraphicsScene(parent)
, m_renderer(renderer)
, m_bomb(nullptr)
, m_view(view)
{
m_clock = new QTimer(this);
m_clock->setInterval(GAME_DELAY);
......@@ -77,7 +80,7 @@ void BomberBoard::resetPlane()
m_plane->resetPosition();
}
void BomberBoard::resize(QSize &size)
void BomberBoard::resize(QSize & size)
{
setBackgroundBrush(m_renderer->spritePixmap(QLatin1Literal("background"), size));
......@@ -86,7 +89,7 @@ void BomberBoard::resize(QSize &size)
m_tileSize = QSize(minTileSizeWidth, minTileSizeHeight);
foreach (Building *building, m_buildings) {
foreach (Building * building, m_buildings) {
building->resize(m_tileSize);
}
......@@ -166,7 +169,7 @@ void BomberBoard::tick()
m_bomb->advanceItem();
}
foreach (Bomb *bomb, m_explodingBombs) {
foreach (Bomb * bomb, m_explodingBombs) {
bomb->advanceItem();
}
......@@ -177,7 +180,7 @@ void BomberBoard::tick()
m_bomb->update();
}
foreach (Bomb *bomb, m_explodingBombs) {
foreach (Bomb * bomb, m_explodingBombs) {
bomb->update();
}
}
......@@ -194,9 +197,8 @@ void BomberBoard::dropBomb()
void BomberBoard::checkCollisions()
{
foreach (Building *building, m_buildings) {
if (m_plane->nextBoundingRect().intersects(building->boundingRect()) && m_plane->state()
== Explodable::State::Moving) {
foreach (Building * building, m_buildings) {
if (m_plane->nextBoundingRect().intersects(building->boundingRect()) && m_plane->state() == Explodable::State::Moving) {
// Plane crashed into the building
building->destoryTop();
--m_buildingBlocks;
......@@ -204,8 +206,7 @@ void BomberBoard::checkCollisions()
}
if (m_bomb != nullptr) {
if (m_bomb->nextBoundingRect().intersects(building->boundingRect()) && m_bomb->state()
== Explodable::State::Moving) {
if (m_bomb->nextBoundingRect().intersects(building->boundingRect()) && m_bomb->state() == Explodable::State::Moving) {
// Bomb hit a building
building->destoryTop();
--m_buildingBlocks;
......@@ -225,7 +226,7 @@ void BomberBoard::checkCollisions()
}
}
void BomberBoard::bombHit(Bomb *bomb, qreal moveBombToX, qreal moveBombToY)
void BomberBoard::bombHit(Bomb * bomb, qreal moveBombToX, qreal moveBombToY)
{
bomb->setPosition(moveBombToX, moveBombToY);
bomb->setState(Bomb::State::Exploding);
......@@ -236,7 +237,7 @@ void BomberBoard::bombHit(Bomb *bomb, qreal moveBombToX, qreal moveBombToY)
void BomberBoard::bombExploded()
{
Bomb *bomb = m_explodingBombs.dequeue();
Bomb * bomb = m_explodingBombs.dequeue();
bomb->hide();
delete bomb;
}
......@@ -273,14 +274,12 @@ void BomberBoard::clear()
resetPlane();
}
QPoint BomberBoard::mapPosition(const QPointF &pos) const
QPoint BomberBoard::mapPosition(const QPointF & pos) const
{
return QPoint(static_cast<unsigned int>(m_tileSize.width() * pos.x()),
static_cast<int>(m_tileSize.height() * pos.y()));
return QPoint(static_cast<unsigned int>(m_tileSize.width() * pos.x()), static_cast<int>(m_tileSize.height() * pos.y()));
}
QPointF BomberBoard::unmapPosition(const QPoint &pos) const
QPointF BomberBoard::unmapPosition(const QPoint & pos) const
{
return QPointF(1.0 * pos.x() / m_tileSize.width(), 1.0 * pos.y()
/ m_tileSize.height());
return QPointF(1.0 * pos.x() / m_tileSize.width(), 1.0 * pos.y() / m_tileSize.height());
}
......@@ -18,13 +18,13 @@
#ifndef BOARD_H
#define BOARD_H
#include <QGraphicsScene>
#include <KGameRenderer>
#include <QGraphicsScene>
#include <QSize>
#include <QDir>
#include <QList>
#include <QQueue>
#include <QDir>
#include <QSize>
class Plane;
class Building;
......@@ -34,19 +34,18 @@ class Bomb;
* This class used to represent the game board. This makes sure all the game objects
* get moved and redrawn every second. It also checks for any collisions
*/
class BomberBoard: public QGraphicsScene
class BomberBoard : public QGraphicsScene
{
Q_OBJECT
public:
/**
* The constructor used to create the board.
* \param renderer The renderer used to render game objects
* \param view The graphics view object which this board is bound to
* \param parent The widget which the board is inserted into
*/
explicit BomberBoard(KGameRenderer *renderer, QGraphicsView *view, QObject *parent = nullptr);
explicit BomberBoard(KGameRenderer * renderer, QGraphicsView * view, QObject * parent = nullptr);
~BomberBoard();
......@@ -54,7 +53,7 @@ public:
* This is called when the game board is resized
* \param size The new tile size used on the game board
*/
void resize(QSize &size);
void resize(QSize & size);
/**
* This will redraw the game board
......@@ -78,14 +77,14 @@ public:
* \param pos The cords relative to the tile
* \return The cords relative to the widget
*/
QPoint mapPosition(const QPointF &pos) const;
QPoint mapPosition(const QPointF & pos) const;
/**
* This will convert the widget location to tile locations
* \param pos The cords relative to the widget
* \return The cords relative to the tile
*/
QPointF unmapPosition(const QPoint &pos) const;
QPointF unmapPosition(const QPoint & pos) const;
/**
* Used to set the plane state to flying and move it to the start position
......@@ -146,7 +145,7 @@ private:
* \param moveBombToX The x position to move the explosion too
* \param moveBombToY The y position to move the explosion too
*/
void bombHit(Bomb *bomb, qreal moveBombToX, qreal moveBombToY);
void bombHit(Bomb * bomb, qreal moveBombToX, qreal moveBombToY);
/**
* This is used to remove all the current game objects, usually called before
......@@ -167,23 +166,23 @@ private:
/**
* This is the renderer used to render game objects
*/
KGameRenderer *m_renderer;
KGameRenderer * m_renderer;
/**
* This is the size of a tiling block
*/
QSize m_tileSize;
QTimer *m_clock;
QTimer * m_clock;
/**
* If their is a bomb currently dropping then it is pointed to by this
*/
Bomb *m_bomb;
Bomb * m_bomb;
/**
* This points to the plane object used in the level
*/
Plane *m_plane;
Plane * m_plane;
/**
* This contains all the buildings in the current level
......@@ -203,7 +202,7 @@ private:
/**
* This is the graphics view object which this board is bound.
*/
QGraphicsView *m_view;
QGraphicsView * m_view;
};
#endif
......@@ -27,10 +27,9 @@ const qreal DEFAULT_VELOCITY = 0.2;
const qreal Bomb::BOMB_RELATIVE_SIZE_H = 0.7;
const qreal Bomb::BOMB_RELATIVE_SIZE_W = 0.2;
Bomb::Bomb(KGameRenderer *renderer, BomberBoard *board, qreal xPos,
qreal yPos, const QSize &tileSize) :
Explodable(QLatin1Literal("bomb"), QLatin1Literal("bomb_explode"), BOMB_RELATIVE_SIZE_W,
BOMB_RELATIVE_SIZE_H, renderer, board)
Bomb::Bomb(KGameRenderer * renderer, BomberBoard * board, qreal xPos, qreal yPos, const QSize & tileSize)
: Explodable(QLatin1Literal("bomb"), QLatin1Literal("bomb_explode"), BOMB_RELATIVE_SIZE_W,
BOMB_RELATIVE_SIZE_H, renderer, board)
{
setVelocity(DEFAULT_VELOCITY);
setPosition(xPos, yPos);
......
......@@ -24,7 +24,7 @@
* This is the bomb game object class. It extends Explodable as it
* is a explodable object.
*/
class Bomb: public Explodable
class Bomb : public Explodable
{
public:
/** The width of the bomb relative to the tile */
......@@ -32,8 +32,8 @@ public:
/** The height of the bomb relative to the tile */
static const qreal BOMB_RELATIVE_SIZE_H;
Bomb(KGameRenderer *renderer, BomberBoard *board, qreal xPos, qreal yPos,
const QSize &tileSize);
Bomb(KGameRenderer * renderer, BomberBoard * board, qreal xPos, qreal yPos,
const QSize & tileSize);
~Bomb();
/**
......
......@@ -20,24 +20,24 @@
#include "bomberwidget.h"
#include "settings.h"
#include <QAction>
#include <KActionCollection>
#include <KLocalizedString>
#include <KMessageBox>
#include <krandom.h>
#include <KScoreDialog>
#include <KToggleAction>
#include <QStatusBar>
#include <QAction>
#include <QLabel>
#include <kstandardgameaction.h>
#include <KScoreDialog>
#include <QStatusBar>
#include <kgthemeselector.h>
#include <krandom.h>
#include <kstandardgameaction.h>
Bomber::Bomber()
{
m_provider.discoverThemes(
"appdata", QLatin1String("themes"), //theme data location
QLatin1String("kbomber") //default theme name
);
QLatin1String("kbomber")); //default theme name
m_selector = new KgThemeSelector(&m_provider);
m_statusBar = statusBar();
......@@ -89,7 +89,7 @@ void Bomber::initXMLUI()
actionCollection()->addAction(QLatin1String("toggle_sound"), m_soundAction);
connect(m_soundAction, &KToggleAction::triggered, m_gameWidget, &BomberGameWidget::setSoundsEnabled);
QAction *dropBombAction = actionCollection()->addAction(QLatin1String("drop_bomb"));
QAction * dropBombAction = actionCollection()->addAction(QLatin1String("drop_bomb"));
dropBombAction->setText(i18nc("The name of the action used for dropping bombs", "&Drop bomb"));
dropBombAction->setToolTip(i18nc("The tool tip text for the action used to drop bombs", "Drop bomb"));
dropBombAction->setWhatsThis(i18nc("Description of the action used to drop bombs",
......@@ -110,7 +110,7 @@ void Bomber::newGame()
// Check for running game
closeGame();
if (m_gameWidget->state() == BomberGameWidget::State::BeforeFirstGame
|| m_gameWidget->state() == BomberGameWidget::State::GameOver) {
|| m_gameWidget->state() == BomberGameWidget::State::GameOver) {
m_gameWidget->newGame();
}
}
......@@ -127,7 +127,7 @@ void Bomber::pauseGame()
void Bomber::closeGame()
{
if (m_gameWidget->state() == BomberGameWidget::State::BeforeFirstGame
|| m_gameWidget->state() == BomberGameWidget::State::GameOver) {
|| m_gameWidget->state() == BomberGameWidget::State::GameOver) {
return;
}
......@@ -136,7 +136,8 @@ void Bomber::closeGame()
m_gameWidget->setPaused(true);
}
int ret = KMessageBox::questionYesNo(this, i18nc("Message displayed when player tries to quit a game that is currently running",
"Do you really want to close the running game?"), QString(),
"Do you really want to close the running game?"),
QString(),
KStandardGuiItem::close(), KStandardGuiItem::cancel());
if (ret == KMessageBox::Yes) {
m_gameWidget->closeGame();
......@@ -174,41 +175,41 @@ void Bomber::highscore()
void Bomber::displayLevel(unsigned int level)
{
m_level->setText(i18nc(
"Used to display the current level of play to the user",
"Level: %1", level));
"Used to display the current level of play to the user",
"Level: %1", level));
}
void Bomber::displayScore(unsigned int score)
{
m_score->setText(i18nc(
"Used to inform the user of their current score", "Score: %1",
score));
"Used to inform the user of their current score", "Score: %1",
score));
}
void Bomber::displayLives(unsigned int lives)
{
m_lives->setText(i18nc(
"Used to tell the user how many lives they have left", "Lives: %1",
lives));
"Used to tell the user how many lives they have left", "Lives: %1",
lives));
}
void Bomber::gameStateChanged(BomberGameWidget::State state)
{
switch (state) {
case BomberGameWidget::State::Paused:
m_pauseAction->setChecked(true);
m_statusBar->clearMessage();
break;
case BomberGameWidget::State::Running:
m_pauseAction->setChecked(false);
m_statusBar->clearMessage();
break;
case BomberGameWidget::State::GameOver:
m_statusBar->showMessage(i18nc("Game over messaged displayed in the status bar", "Game over. Press '%1' for a new game",
m_newAction->shortcuts().first().toString(QKeySequence::NativeText)));
highscore();
break;
default:
break;
case BomberGameWidget::State::Paused:
m_pauseAction->setChecked(true);
m_statusBar->clearMessage();
break;
case BomberGameWidget::State::Running:
m_pauseAction->setChecked(false);
m_statusBar->clearMessage();
break;
case BomberGameWidget::State::GameOver:
m_statusBar->showMessage(i18nc("Game over messaged displayed in the status bar", "Game over. Press '%1' for a new game",
m_newAction->shortcuts().first().toString(QKeySequence::NativeText)));
highscore();
break;
default:
break;
}
}
......@@ -20,8 +20,8 @@
#include "bomberwidget.h"
#include <kxmlguiwindow.h>
#include <kgthemeprovider.h>
#include <kxmlguiwindow.h>
class QAction;
class QStatusBar;
......@@ -35,7 +35,7 @@ class QLabel;
*
* @short Main window class
*/
class Bomber: public KXmlGuiWindow
class Bomber : public KXmlGuiWindow
{
Q_OBJECT
public:
......@@ -61,13 +61,13 @@ private:
void setupActions();
KgThemeProvider m_provider;
KgThemeSelector *m_selector;
KgThemeSelector * m_selector;
BomberGameWidget *m_gameWidget;
QStatusBar *m_statusBar;
BomberGameWidget * m_gameWidget;
QStatusBar * m_statusBar;
KToggleAction *m_pauseAction, *m_backgroundShowAction, *m_soundAction;
QAction *m_newAction;
QAction * m_newAction;
QLabel *m_level, *m_score, *m_lives;
};
......
......@@ -19,9 +19,9 @@
#include "bomberwidget.h"
#include "settings.h"
#include <QTimer>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QGraphicsView>
#include <QTimer>
#include <KLocalizedString>
#include <sys/stat.h>
......@@ -36,11 +36,15 @@ static const unsigned int TICKS_PER_SECOND = 1000 / GAME_TIME_DELAY;
/** The z-value for overlays */
static const unsigned int OVERLAY_Z_VALUE = 1000;
BomberGameWidget::BomberGameWidget(KgThemeProvider *provider, QWidget *parent) :
QGraphicsView(parent), m_state(State::BeforeFirstGame), m_level(0), m_lives(0), m_time(0),
m_renderer(provider),
m_soundBomb((QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("bomber/sounds/bomb.ogg")))),
m_soundCrash((QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("bomber/sounds/crash.ogg"))))
BomberGameWidget::BomberGameWidget(KgThemeProvider * provider, QWidget * parent)
: QGraphicsView(parent)
, m_state(State::BeforeFirstGame)
, m_level(0)
, m_lives(0)
, m_time(0)
, m_renderer(provider)
, m_soundBomb((QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("bomber/sounds/bomb.ogg"))))
, m_soundCrash((QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("bomber/sounds/crash.ogg"))))
{
// Gameboard
m_board = new BomberBoard(&m_renderer, this, this);
......@@ -211,7 +215,7 @@ void BomberGameWidget::tick()
}
}
void BomberGameWidget::resizeEvent(QResizeEvent *ev)
void BomberGameWidget::resizeEvent(QResizeEvent * ev)
{
QSize boardSize = ev->size();
m_board->resize(boardSize);
......@@ -239,7 +243,7 @@ void BomberGameWidget::newLevel()
redraw();
}
void BomberGameWidget::mouseReleaseEvent(QMouseEvent *event)
void BomberGameWidget::mouseReleaseEvent(QMouseEvent * event)
{
if (event->button() & Qt::LeftButton) {
onDropBomb();
......@@ -262,28 +266,28 @@ void BomberGameWidget::redraw()
if (size().isEmpty()) {
return;
}
QGraphicsItem *item;
QGraphicsItem * item;
switch (m_state) {
case State::BeforeFirstGame:
foreach (item, m_board->items()) {
item->hide();
}
generateOverlay();
m_overlay->show();
break;
case State::Running:
foreach (item, m_board->items()) {
item->show();
}
m_overlay->hide();
break;
default:
foreach (item, m_board->items()) {
item->show();
}
generateOverlay();
m_overlay->show();
break;
case State::BeforeFirstGame:
foreach (item, m_board->items()) {
item->hide();
}
generateOverlay();
m_overlay->show();
break;
case State::Running:
foreach (item, m_board->items()) {
item->show();
}
m_overlay->hide();
break;
default:
foreach (item, m_board->items()) {
item->show();
}
generateOverlay();
m_overlay->show();
break;
}
m_board->redraw();
update();
......@@ -305,21 +309,21 @@ void BomberGameWidget::generateOverlay()
QString text;
switch (m_state) {
case State::BeforeFirstGame:
text = i18nc("Message show to the user when the game is loaded", "Welcome to Bomber.\nClick to start a game");
break;
case State::Paused:
text = i18nc("Message show to the user while the game is paused", "Paused");
break;
case State::BetweenLevels:
text = i18nc("Message telling user which level they just completed", "You have successfully cleared level %1\n", m_level - 1)
+ i18nc("Message telling user which level they are about to start", "On to level %1.", m_level);
break;
case State::GameOver:
text = i18nc("Used to tell the user that the game is over", "Game over.");
break;
default:
text.clear();
case State::BeforeFirstGame:
text = i18nc("Message show to the user when the game is loaded", "Welcome to Bomber.\nClick to start a game");
break;
case State::Paused:
text = i18nc("Message show to the user while the game is paused", "Paused");
break;
case State::BetweenLevels:
text = i18nc("Message telling user which level they just completed", "You have successfully cleared level %1\n", m_level - 1)
+ i18nc("Message telling user which level they are about to start", "On to level %1.", m_level);
break;
case State::GameOver:
text = i18nc("Used to tell the user that the game is over", "Game over.");
break;
default:
text.clear();
}
QFont font;
......
......@@ -32,7 +32,7 @@ class KgThemeProvider;
/**
* This is the main game widget class. It manages things like the lives, source counts and game states.
*/
class BomberGameWidget: public QGraphicsView
class BomberGameWidget : public QGraphicsView
{
Q_OBJECT
......@@ -40,10 +40,14 @@ public:
/**
* The different states that the game can be in
*/
enum class State
{ BeforeFirstGame, Running, BetweenLevels, Paused, Suspended, GameOver};
enum class State { BeforeFirstGame,
Running,
BetweenLevels,
Paused,
Suspended,
GameOver };
explicit BomberGameWidget(KgThemeProvider *provider, QWidget *parent = nullptr);
explicit BomberGameWidget(KgThemeProvider * provider, QWidget * parent = nullptr);
~BomberGameWidget();
/**
......@@ -162,8 +166,8 @@ private slots:
void onPlaneCrashed();
private:
virtual void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
virtual void resizeEvent(QResizeEvent * event) Q_DECL_OVERRIDE;
virtual void mouseReleaseEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
/**
* Create the overlay used to display info to the user. The info will
......@@ -183,16 +187,17 @@ private:
unsigned int m_score;
unsigned int m_lives;
unsigned int m_time;
/**
* Used to store the remaining score before a new life is given
*/
int m_scoreLeftBeforeNewLife;
KGameRenderer m_renderer;
QTimer *m_clock;
BomberBoard *m_board;
QTimer * m_clock;
BomberBoard * m_board;
QGraphicsPixmapItem *m_overlay;
QGraphicsPixmapItem * m_overlay;
KgSound m_soundBomb;
KgSound m_soundCrash;
......
......@@ -36,9 +36,12 @@ const qreal BUILDING_RELATIVE_HEIGHT = 1.0;
/** The vertical tile number of the bottom tile in the building */
const unsigned int Building::BUILD_BASE_LOCATION = 16;
Building::Building(KGameRenderer *renderer, BomberBoard *board