Cleanup QtWidgets animation examples
Cleanup the QtWidgets animation examples: - use nullptr - use normalized includes, remove unused includes - fix style - fix crash of sub-attaq when the game ended (error during range-based for loop porting) - don't use keyword 'final' for a variable name Change-Id: Id23be8ff8b1b310da005d13c052fe547f6a0d63a Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
parent
cb3e1e551f
commit
13426aff24
@ -55,11 +55,10 @@ Window::Window(QWidget *parent)
|
||||
m_iconSize(64, 64)
|
||||
{
|
||||
m_ui.setupUi(this);
|
||||
QButtonGroup *buttonGroup = findChild<QButtonGroup *>(); // ### workaround for uic in 4.4
|
||||
m_ui.easingCurvePicker->setIconSize(m_iconSize);
|
||||
m_ui.easingCurvePicker->setMinimumHeight(m_iconSize.height() + 50);
|
||||
buttonGroup->setId(m_ui.lineRadio, 0);
|
||||
buttonGroup->setId(m_ui.circleRadio, 1);
|
||||
m_ui.buttonGroup->setId(m_ui.lineRadio, 0);
|
||||
m_ui.buttonGroup->setId(m_ui.circleRadio, 1);
|
||||
|
||||
QEasingCurve dummy;
|
||||
m_ui.periodSpinBox->setValue(dummy.period());
|
||||
@ -68,7 +67,7 @@ Window::Window(QWidget *parent)
|
||||
|
||||
connect(m_ui.easingCurvePicker, &QListWidget::currentRowChanged,
|
||||
this, &Window::curveChanged);
|
||||
connect(buttonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
|
||||
connect(m_ui.buttonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
|
||||
this, &Window::pathChanged);
|
||||
connect(m_ui.periodSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
|
||||
this, &Window::periodChanged);
|
||||
|
@ -50,16 +50,13 @@
|
||||
|
||||
#include "animation.h"
|
||||
|
||||
#include <QPointF>
|
||||
#include <QVector>
|
||||
#include <QIODevice>
|
||||
#include <QDataStream>
|
||||
|
||||
class Frame
|
||||
{
|
||||
public:
|
||||
Frame() {
|
||||
}
|
||||
Frame() = default;
|
||||
|
||||
int nodeCount() const
|
||||
{
|
||||
@ -85,9 +82,8 @@ private:
|
||||
QVector<QPointF> m_nodePositions;
|
||||
};
|
||||
|
||||
Animation::Animation()
|
||||
Animation::Animation() : m_currentFrame(0)
|
||||
{
|
||||
m_currentFrame = 0;
|
||||
m_frames.append(new Frame);
|
||||
}
|
||||
|
||||
@ -103,6 +99,8 @@ void Animation::setTotalFrames(int totalFrames)
|
||||
|
||||
while (totalFrames < m_frames.size())
|
||||
delete m_frames.takeLast();
|
||||
|
||||
setCurrentFrame(m_currentFrame);
|
||||
}
|
||||
|
||||
int Animation::totalFrames() const
|
||||
@ -112,7 +110,7 @@ int Animation::totalFrames() const
|
||||
|
||||
void Animation::setCurrentFrame(int currentFrame)
|
||||
{
|
||||
m_currentFrame = qMax(qMin(currentFrame, totalFrames()-1), 0);
|
||||
m_currentFrame = qBound(0, currentFrame, totalFrames() - 1);
|
||||
}
|
||||
|
||||
int Animation::currentFrame() const
|
||||
@ -177,18 +175,16 @@ void Animation::load(QIODevice *device)
|
||||
int frameCount;
|
||||
stream >> frameCount;
|
||||
|
||||
for (int i=0; i<frameCount; ++i) {
|
||||
|
||||
for (int i = 0; i < frameCount; ++i) {
|
||||
int nodeCount;
|
||||
stream >> nodeCount;
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setNodeCount(nodeCount);
|
||||
|
||||
for (int j=0; j<nodeCount; ++j) {
|
||||
for (int j = 0; j < nodeCount; ++j) {
|
||||
QPointF pos;
|
||||
stream >> pos;
|
||||
|
||||
frame->setNodePos(j, pos);
|
||||
}
|
||||
|
||||
|
@ -52,8 +52,8 @@
|
||||
#define ANIMATION_H
|
||||
|
||||
#include <QPointF>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
class Frame;
|
||||
QT_BEGIN_NAMESPACE
|
||||
@ -85,7 +85,7 @@ public:
|
||||
|
||||
private:
|
||||
QString m_name;
|
||||
QList<Frame *> m_frames;
|
||||
QVector<Frame *> m_frames;
|
||||
int m_currentFrame;
|
||||
};
|
||||
|
||||
|
@ -51,13 +51,8 @@
|
||||
#include "graphicsview.h"
|
||||
#include "stickman.h"
|
||||
|
||||
#include <QtGui/QKeyEvent>
|
||||
#include <QtWidgets/QGraphicsScene>
|
||||
#include <QtWidgets/QGraphicsView>
|
||||
|
||||
GraphicsView::GraphicsView(QWidget *parent)
|
||||
: QGraphicsView(parent), m_editor(nullptr)
|
||||
{}
|
||||
#include <QKeyEvent>
|
||||
#include <QGraphicsScene>
|
||||
|
||||
void GraphicsView::keyPressEvent(QKeyEvent *e)
|
||||
{
|
||||
@ -66,7 +61,8 @@ void GraphicsView::keyPressEvent(QKeyEvent *e)
|
||||
emit keyPressed(Qt::Key(e->key()));
|
||||
}
|
||||
|
||||
void GraphicsView::resizeEvent(QResizeEvent *)
|
||||
void GraphicsView::resizeEvent(QResizeEvent *e)
|
||||
{
|
||||
fitInView(scene()->sceneRect());
|
||||
QGraphicsView::resizeEvent(e);
|
||||
}
|
||||
|
@ -51,24 +51,20 @@
|
||||
#ifndef GRAPHICSVIEW_H
|
||||
#define GRAPHICSVIEW_H
|
||||
|
||||
#include <QtWidgets/QGraphicsView>
|
||||
#include <QGraphicsView>
|
||||
|
||||
class MainWindow;
|
||||
class GraphicsView: public QGraphicsView
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GraphicsView(QWidget *parent = nullptr);
|
||||
using QGraphicsView::QGraphicsView;
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
void keyPressEvent(QKeyEvent *) override;
|
||||
void resizeEvent(QResizeEvent *e) override;
|
||||
void keyPressEvent(QKeyEvent *e) override;
|
||||
|
||||
signals:
|
||||
void keyPressed(int key);
|
||||
|
||||
private:
|
||||
MainWindow *m_editor;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -54,8 +54,15 @@
|
||||
#include "animation.h"
|
||||
#include "graphicsview.h"
|
||||
|
||||
#include <QtCore>
|
||||
#include <QtWidgets>
|
||||
#include <QEventTransition>
|
||||
#include <QFile>
|
||||
#include <QParallelAnimationGroup>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QRandomGenerator>
|
||||
#include <QSignalTransition>
|
||||
#include <QState>
|
||||
#include <QStateMachine>
|
||||
#include <QTimer>
|
||||
|
||||
class KeyPressTransition: public QSignalTransition
|
||||
{
|
||||
@ -107,7 +114,7 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
|
||||
// Create animation group to be used for all transitions
|
||||
m_animationGroup = new QParallelAnimationGroup();
|
||||
const int stickManNodeCount = m_stickMan->nodeCount();
|
||||
for (int i=0; i<stickManNodeCount; ++i) {
|
||||
for (int i = 0; i < stickManNodeCount; ++i) {
|
||||
QPropertyAnimation *pa = new QPropertyAnimation(m_stickMan->node(i), "pos");
|
||||
m_animationGroup->addAnimation(pa);
|
||||
}
|
||||
@ -175,7 +182,7 @@ void LifeCycle::addActivity(const QString &fileName, Qt::Key key, QObject *sende
|
||||
QState *state = makeState(m_alive, fileName);
|
||||
m_alive->addTransition(new KeyPressTransition(m_keyReceiver, key, state));
|
||||
|
||||
if (sender || signal)
|
||||
if (sender && signal)
|
||||
m_alive->addTransition(sender, signal, state);
|
||||
}
|
||||
|
||||
@ -192,13 +199,13 @@ QState *LifeCycle::makeState(QState *parentState, const QString &animationFileNa
|
||||
|
||||
const int frameCount = animation.totalFrames();
|
||||
QState *previousState = nullptr;
|
||||
for (int i=0; i<frameCount; ++i) {
|
||||
for (int i = 0; i < frameCount; ++i) {
|
||||
animation.setCurrentFrame(i);
|
||||
|
||||
//! [1]
|
||||
QState *frameState = new QState(topLevel);
|
||||
const int nodeCount = animation.nodeCount();
|
||||
for (int j=0; j<nodeCount; ++j)
|
||||
for (int j = 0; j < nodeCount; ++j)
|
||||
frameState->assignProperty(m_stickMan->node(j), "pos", animation.nodePos(j));
|
||||
//! [1]
|
||||
|
||||
|
@ -53,16 +53,16 @@
|
||||
|
||||
#include <Qt>
|
||||
|
||||
class StickMan;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QStateMachine;
|
||||
class QAnimationGroup;
|
||||
class QState;
|
||||
class QAbstractState;
|
||||
class QAbstractTransition;
|
||||
class QAnimationGroup;
|
||||
class QObject;
|
||||
class QState;
|
||||
class QStateMachine;
|
||||
QT_END_NAMESPACE
|
||||
class GraphicsView;
|
||||
class StickMan;
|
||||
class LifeCycle
|
||||
{
|
||||
public:
|
||||
@ -70,7 +70,8 @@ public:
|
||||
~LifeCycle();
|
||||
|
||||
void setDeathAnimation(const QString &fileName);
|
||||
void addActivity(const QString &fileName, Qt::Key key, QObject *sender = NULL, const char *signal = NULL);
|
||||
void addActivity(const QString &fileName, Qt::Key key,
|
||||
QObject *sender = nullptr, const char *signal = nullptr);
|
||||
|
||||
void start();
|
||||
|
||||
|
@ -51,13 +51,13 @@
|
||||
#ifndef NODE_H
|
||||
#define NODE_H
|
||||
|
||||
#include <QGraphicsItem>
|
||||
#include <QGraphicsObject>
|
||||
|
||||
class Node: public QGraphicsObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Node(const QPointF &pos, QGraphicsItem *parent = 0);
|
||||
explicit Node(const QPointF &pos, QGraphicsItem *parent = nullptr);
|
||||
~Node();
|
||||
|
||||
QRectF boundingRect() const override;
|
||||
|
@ -51,12 +51,7 @@
|
||||
#include "rectbutton.h"
|
||||
#include <QPainter>
|
||||
|
||||
RectButton::RectButton(QString buttonText) : m_ButtonText(buttonText)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
RectButton::~RectButton()
|
||||
RectButton::RectButton(const QString &buttonText) : m_ButtonText(buttonText)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -57,19 +57,19 @@ class RectButton : public QGraphicsObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
RectButton(QString buttonText);
|
||||
~RectButton();
|
||||
RectButton(const QString &buttonText);
|
||||
|
||||
QRectF boundingRect() const override;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||
|
||||
protected:
|
||||
QString m_ButtonText;
|
||||
|
||||
void mousePressEvent (QGraphicsSceneMouseEvent *event) override;
|
||||
|
||||
signals:
|
||||
void clicked();
|
||||
|
||||
private:
|
||||
QString m_ButtonText;
|
||||
};
|
||||
|
||||
#endif // RECTBUTTON_H
|
||||
|
@ -52,10 +52,9 @@
|
||||
#include "node.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QTimer>
|
||||
#include <qmath.h>
|
||||
#include <QtMath>
|
||||
|
||||
static const qreal Coords[NodeCount * 2] = {
|
||||
static constexpr qreal Coords[NodeCount * 2] = {
|
||||
0.0, -150.0, // head, #0
|
||||
|
||||
0.0, -100.0, // body pentagon, top->bottom, left->right, #1 - 5
|
||||
@ -81,7 +80,7 @@ static const qreal Coords[NodeCount * 2] = {
|
||||
|
||||
};
|
||||
|
||||
static const int Bones[BoneCount * 2] = {
|
||||
static constexpr int Bones[BoneCount * 2] = {
|
||||
0, 1, // neck
|
||||
|
||||
1, 2, // body
|
||||
@ -117,19 +116,13 @@ static const int Bones[BoneCount * 2] = {
|
||||
|
||||
StickMan::StickMan()
|
||||
{
|
||||
m_sticks = true;
|
||||
m_isDead = false;
|
||||
m_pixmap = QPixmap("images/head.png");
|
||||
m_penColor = Qt::white;
|
||||
m_fillColor = Qt::black;
|
||||
|
||||
// Set up start position of limbs
|
||||
for (int i=0; i<NodeCount; ++i) {
|
||||
for (int i = 0; i < NodeCount; ++i) {
|
||||
m_nodes[i] = new Node(QPointF(Coords[i * 2], Coords[i * 2 + 1]), this);
|
||||
connect(m_nodes[i], &Node::positionChanged, this, &StickMan::childPositionChanged);
|
||||
}
|
||||
|
||||
for (int i=0; i<BoneCount; ++i) {
|
||||
for (int i = 0; i < BoneCount; ++i) {
|
||||
int n1 = Bones[i * 2];
|
||||
int n2 = Bones[i * 2 + 1];
|
||||
|
||||
@ -137,16 +130,12 @@ StickMan::StickMan()
|
||||
Node *node2 = m_nodes[n2];
|
||||
|
||||
QPointF dist = node1->pos() - node2->pos();
|
||||
m_perfectBoneLengths[i] = sqrt(pow(dist.x(),2) + pow(dist.y(),2));
|
||||
m_perfectBoneLengths[i] = sqrt(pow(dist.x(), 2) + pow(dist.y(), 2));
|
||||
}
|
||||
|
||||
startTimer(10);
|
||||
}
|
||||
|
||||
StickMan::~StickMan()
|
||||
{
|
||||
}
|
||||
|
||||
void StickMan::childPositionChanged()
|
||||
{
|
||||
prepareGeometryChange();
|
||||
@ -155,7 +144,7 @@ void StickMan::childPositionChanged()
|
||||
void StickMan::setDrawSticks(bool on)
|
||||
{
|
||||
m_sticks = on;
|
||||
for (int i=0;i<nodeCount();++i) {
|
||||
for (int i = 0; i < nodeCount(); ++i) {
|
||||
Node *node = m_nodes[i];
|
||||
node->setVisible(on);
|
||||
}
|
||||
@ -188,7 +177,7 @@ void StickMan::stabilize()
|
||||
{
|
||||
static const qreal threshold = 0.001;
|
||||
|
||||
for (int i=0; i<BoneCount; ++i) {
|
||||
for (int i = 0; i < BoneCount; ++i) {
|
||||
int n1 = Bones[i * 2];
|
||||
int n2 = Bones[i * 2 + 1];
|
||||
|
||||
@ -236,7 +225,7 @@ void StickMan::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
|
||||
stabilize();
|
||||
if (m_sticks) {
|
||||
painter->setPen(Qt::white);
|
||||
for (int i=0; i<BoneCount; ++i) {
|
||||
for (int i = 0; i < BoneCount; ++i) {
|
||||
int n1 = Bones[i * 2];
|
||||
int n2 = Bones[i * 2 + 1];
|
||||
|
||||
|
@ -57,8 +57,6 @@ static const int NodeCount = 16;
|
||||
static const int BoneCount = 24;
|
||||
|
||||
class Node;
|
||||
QT_BEGIN_NAMESPACE
|
||||
QT_END_NAMESPACE
|
||||
class StickMan: public QGraphicsObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -67,7 +65,6 @@ class StickMan: public QGraphicsObject
|
||||
Q_PROPERTY(bool isDead WRITE setIsDead READ isDead)
|
||||
public:
|
||||
StickMan();
|
||||
~StickMan();
|
||||
|
||||
QRectF boundingRect() const override;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||
@ -101,13 +98,11 @@ private:
|
||||
Node *m_nodes[NodeCount];
|
||||
qreal m_perfectBoneLengths[BoneCount];
|
||||
|
||||
uint m_sticks : 1;
|
||||
uint m_isDead : 1;
|
||||
uint m_reserved : 30;
|
||||
bool m_sticks = true;
|
||||
bool m_isDead = false;
|
||||
|
||||
QPixmap m_pixmap;
|
||||
QColor m_penColor;
|
||||
QColor m_fillColor;
|
||||
QColor m_penColor = Qt::white;
|
||||
QColor m_fillColor = Qt::black;
|
||||
};
|
||||
|
||||
#endif // STICKMAN_H
|
||||
|
@ -51,22 +51,13 @@
|
||||
//Own
|
||||
#include "animationmanager.h"
|
||||
|
||||
//Qt
|
||||
#include <QtCore/QAbstractAnimation>
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
// the universe's only animation manager
|
||||
AnimationManager *AnimationManager::instance = nullptr;
|
||||
|
||||
AnimationManager::AnimationManager()
|
||||
{
|
||||
}
|
||||
#include <QAbstractAnimation>
|
||||
|
||||
AnimationManager *AnimationManager::self()
|
||||
{
|
||||
if (!instance)
|
||||
instance = new AnimationManager;
|
||||
return instance;
|
||||
// the universe's only animation manager
|
||||
static AnimationManager s_instance;
|
||||
return &s_instance;
|
||||
}
|
||||
|
||||
void AnimationManager::registerAnimation(QAbstractAnimation *anim)
|
||||
|
@ -51,7 +51,7 @@
|
||||
#ifndef ANIMATIONMANAGER_H
|
||||
#define ANIMATIONMANAGER_H
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QObject>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QAbstractAnimation;
|
||||
@ -59,9 +59,10 @@ QT_END_NAMESPACE
|
||||
|
||||
class AnimationManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_OBJECT
|
||||
AnimationManager() = default;
|
||||
~AnimationManager() = default;
|
||||
public:
|
||||
AnimationManager();
|
||||
void registerAnimation(QAbstractAnimation *anim);
|
||||
void unregisterAnimation(QAbstractAnimation *anim);
|
||||
void unregisterAllAnimations();
|
||||
@ -75,8 +76,7 @@ private slots:
|
||||
void unregisterAnimation_helper(QObject *obj);
|
||||
|
||||
private:
|
||||
static AnimationManager *instance;
|
||||
QList<QAbstractAnimation *> animations;
|
||||
QVector<QAbstractAnimation *> animations;
|
||||
};
|
||||
|
||||
#endif // ANIMATIONMANAGER_H
|
||||
|
@ -52,18 +52,17 @@
|
||||
#include "boat.h"
|
||||
#include "boat_p.h"
|
||||
#include "bomb.h"
|
||||
#include "pixmapitem.h"
|
||||
#include "graphicsscene.h"
|
||||
#include "animationmanager.h"
|
||||
#include "qanimationstate.h"
|
||||
|
||||
//Qt
|
||||
#include <QtCore/QPropertyAnimation>
|
||||
#include <QtCore/QStateMachine>
|
||||
#include <QtCore/QHistoryState>
|
||||
#include <QtCore/QFinalState>
|
||||
#include <QtCore/QState>
|
||||
#include <QtCore/QSequentialAnimationGroup>
|
||||
#include <QFinalState>
|
||||
#include <QHistoryState>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QSequentialAnimationGroup>
|
||||
#include <QState>
|
||||
#include <QStateMachine>
|
||||
|
||||
static QAbstractAnimation *setupDestroyAnimation(Boat *boat)
|
||||
{
|
||||
@ -181,7 +180,7 @@ Boat::Boat()
|
||||
launchStateLeft->addTransition(historyState);
|
||||
launchStateRight->addTransition(historyState);
|
||||
|
||||
QFinalState *final = new QFinalState(machine);
|
||||
QFinalState *finalState = new QFinalState(machine);
|
||||
|
||||
//This state play the destroyed animation
|
||||
QAnimationState *destroyedState = new QAnimationState(machine);
|
||||
@ -191,10 +190,10 @@ Boat::Boat()
|
||||
moving->addTransition(this, &Boat::boatDestroyed, destroyedState);
|
||||
|
||||
//Transition to final state when the destroyed animation is finished
|
||||
destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, final);
|
||||
destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, finalState);
|
||||
|
||||
//The machine has finished to be executed, then the boat is dead
|
||||
connect(machine,&QState::finished, this, &Boat::boatExecutionFinished);
|
||||
connect(machine, &QState::finished, this, &Boat::boatExecutionFinished);
|
||||
|
||||
}
|
||||
|
||||
|
@ -48,12 +48,11 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __BOAT__H__
|
||||
#define __BOAT__H__
|
||||
#ifndef BOAT_H
|
||||
#define BOAT_H
|
||||
|
||||
#include "pixmapitem.h"
|
||||
|
||||
class Bomb;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QVariantAnimation;
|
||||
class QAbstractAnimation;
|
||||
@ -101,4 +100,4 @@ private:
|
||||
QStateMachine *machine;
|
||||
};
|
||||
|
||||
#endif //__BOAT__H__
|
||||
#endif // BOAT_H
|
||||
|
@ -67,7 +67,9 @@
|
||||
#include "graphicsscene.h"
|
||||
|
||||
// Qt
|
||||
#include <QtWidgets/QKeyEventTransition>
|
||||
#include <QGraphicsRotation>
|
||||
#include <QKeyEventTransition>
|
||||
#include <QState>
|
||||
|
||||
static const int MAX_BOMB = 5;
|
||||
|
||||
@ -88,7 +90,7 @@ protected:
|
||||
return (boat->currentSpeed() == 1);
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
};
|
||||
|
||||
//These transtion test if we have to move the boat (i.e current speed was 0 or another value)
|
||||
@ -118,7 +120,7 @@ protected:
|
||||
boat->updateBoatMovement();
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
int key;
|
||||
};
|
||||
|
||||
@ -139,7 +141,7 @@ protected:
|
||||
return (boat->bombsLaunched() < MAX_BOMB);
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
};
|
||||
|
||||
//This state is describing when the boat is moving right
|
||||
@ -157,7 +159,7 @@ protected:
|
||||
boat->updateBoatMovement();
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
};
|
||||
|
||||
//This state is describing when the boat is moving left
|
||||
@ -175,7 +177,7 @@ protected:
|
||||
boat->updateBoatMovement();
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
};
|
||||
|
||||
//This state is describing when the boat is in a stand by position
|
||||
@ -194,7 +196,7 @@ protected:
|
||||
boat->updateBoatMovement();
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
};
|
||||
|
||||
//This state is describing the launch of the torpedo on the right
|
||||
@ -216,7 +218,7 @@ protected:
|
||||
boat->setBombsLaunched(boat->bombsLaunched() + 1);
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
};
|
||||
|
||||
//This state is describing the launch of the torpedo on the left
|
||||
@ -238,7 +240,7 @@ protected:
|
||||
boat->setBombsLaunched(boat->bombsLaunched() + 1);
|
||||
}
|
||||
private:
|
||||
Boat * boat;
|
||||
Boat *boat;
|
||||
};
|
||||
|
||||
#endif // BOAT_P_H
|
||||
|
@ -51,15 +51,14 @@
|
||||
//Own
|
||||
#include "bomb.h"
|
||||
#include "submarine.h"
|
||||
#include "pixmapitem.h"
|
||||
#include "animationmanager.h"
|
||||
#include "qanimationstate.h"
|
||||
|
||||
//Qt
|
||||
#include <QtCore/QSequentialAnimationGroup>
|
||||
#include <QtCore/QPropertyAnimation>
|
||||
#include <QtCore/QStateMachine>
|
||||
#include <QtCore/QFinalState>
|
||||
#include <QFinalState>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QSequentialAnimationGroup>
|
||||
#include <QStateMachine>
|
||||
|
||||
Bomb::Bomb() : PixmapItem(QString("bomb"), GraphicsScene::Big)
|
||||
{
|
||||
@ -83,7 +82,7 @@ void Bomb::launch(Bomb::Direction direction)
|
||||
anim->setEndValue(QPointF(x() + delta*2,scene()->height()));
|
||||
anim->setDuration(y()/2*60);
|
||||
launchAnimation->addAnimation(anim);
|
||||
connect(anim,&QVariantAnimation::valueChanged,this,&Bomb::onAnimationLaunchValueChanged);
|
||||
connect(anim, &QVariantAnimation::valueChanged, this, &Bomb::onAnimationLaunchValueChanged);
|
||||
connect(this, &Bomb::bombExploded, launchAnimation, &QAbstractAnimation::stop);
|
||||
//We setup the state machine of the bomb
|
||||
QStateMachine *machine = new QStateMachine(this);
|
||||
@ -93,18 +92,18 @@ void Bomb::launch(Bomb::Direction direction)
|
||||
launched->setAnimation(launchAnimation);
|
||||
|
||||
//End
|
||||
QFinalState *final = new QFinalState(machine);
|
||||
QFinalState *finalState = new QFinalState(machine);
|
||||
|
||||
machine->setInitialState(launched);
|
||||
|
||||
//### Add a nice animation when the bomb is destroyed
|
||||
launched->addTransition(this, &Bomb::bombExploded,final);
|
||||
launched->addTransition(this, &Bomb::bombExploded, finalState);
|
||||
|
||||
//If the animation is finished, then we move to the final state
|
||||
launched->addTransition(launched, &QAnimationState::animationFinished, final);
|
||||
launched->addTransition(launched, &QAnimationState::animationFinished, finalState);
|
||||
|
||||
//The machine has finished to be executed, then the boat is dead
|
||||
connect(machine,&QState::finished,this, &Bomb::bombExecutionFinished);
|
||||
connect(machine,&QState::finished, this, &Bomb::bombExecutionFinished);
|
||||
|
||||
machine->start();
|
||||
|
||||
|
@ -48,8 +48,8 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __BOMB__H__
|
||||
#define __BOMB__H__
|
||||
#ifndef BOMB_H
|
||||
#define BOMB_H
|
||||
|
||||
#include "pixmapitem.h"
|
||||
|
||||
@ -73,4 +73,4 @@ private slots:
|
||||
void onAnimationLaunchValueChanged(const QVariant &);
|
||||
};
|
||||
|
||||
#endif //__BOMB__H__
|
||||
#endif // BOMB_H
|
||||
|
@ -55,38 +55,33 @@
|
||||
#include "submarine.h"
|
||||
#include "torpedo.h"
|
||||
#include "bomb.h"
|
||||
#include "pixmapitem.h"
|
||||
#include "animationmanager.h"
|
||||
#include "qanimationstate.h"
|
||||
#include "progressitem.h"
|
||||
#include "textinformationitem.h"
|
||||
|
||||
//Qt
|
||||
#include <QtCore/QPropertyAnimation>
|
||||
#include <QtCore/QSequentialAnimationGroup>
|
||||
#include <QtCore/QParallelAnimationGroup>
|
||||
#include <QtCore/QStateMachine>
|
||||
#include <QtCore/QFinalState>
|
||||
#include <QtCore/QPauseAnimation>
|
||||
#include <QtWidgets/QAction>
|
||||
#include <QtCore/QDir>
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QMessageBox>
|
||||
#include <QtWidgets/QGraphicsView>
|
||||
#include <QtWidgets/QGraphicsSceneMouseEvent>
|
||||
#include <QtCore/QXmlStreamReader>
|
||||
#include <QAction>
|
||||
#include <QApplication>
|
||||
#include <QFile>
|
||||
#include <QFinalState>
|
||||
#include <QParallelAnimationGroup>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QSequentialAnimationGroup>
|
||||
#include <QStateMachine>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode)
|
||||
: QGraphicsScene(x , y, width, height), mode(mode), boat(new Boat)
|
||||
GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode, QObject *parent)
|
||||
: QGraphicsScene(x, y, width, height, parent), mode(mode), boat(new Boat)
|
||||
{
|
||||
PixmapItem *backgroundItem = new PixmapItem(QString("background"),mode);
|
||||
PixmapItem *backgroundItem = new PixmapItem(QStringLiteral("background"), mode);
|
||||
backgroundItem->setZValue(1);
|
||||
backgroundItem->setPos(0,0);
|
||||
addItem(backgroundItem);
|
||||
|
||||
PixmapItem *surfaceItem = new PixmapItem(QString("surface"),mode);
|
||||
PixmapItem *surfaceItem = new PixmapItem(QStringLiteral("surface"), mode);
|
||||
surfaceItem->setZValue(3);
|
||||
surfaceItem->setPos(0,sealLevel() - surfaceItem->boundingRect().height()/2);
|
||||
surfaceItem->setPos(0, sealLevel() - surfaceItem->boundingRect().height() / 2);
|
||||
addItem(surfaceItem);
|
||||
|
||||
//The item that display score and level
|
||||
@ -137,8 +132,8 @@ qreal GraphicsScene::sealLevel() const
|
||||
|
||||
void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction)
|
||||
{
|
||||
static const int nLetters = 10;
|
||||
static struct {
|
||||
static constexpr int nLetters = 10;
|
||||
static constexpr struct {
|
||||
char const *pix;
|
||||
qreal initX, initY;
|
||||
qreal destX, destY;
|
||||
@ -154,8 +149,8 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction)
|
||||
{"q", 200, 2000, 510, 250 },
|
||||
{"excl", 0, 2000, 570, 220 } };
|
||||
|
||||
QSequentialAnimationGroup * lettersGroupMoving = new QSequentialAnimationGroup(this);
|
||||
QParallelAnimationGroup * lettersGroupFading = new QParallelAnimationGroup(this);
|
||||
QSequentialAnimationGroup *lettersGroupMoving = new QSequentialAnimationGroup(this);
|
||||
QParallelAnimationGroup *lettersGroupFading = new QParallelAnimationGroup(this);
|
||||
|
||||
for (int i = 0; i < nLetters; ++i) {
|
||||
PixmapItem *logo = new PixmapItem(QLatin1String(":/logo-") + logoData[i].pix, this);
|
||||
@ -180,7 +175,7 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction)
|
||||
PlayState *gameState = new PlayState(this, machine);
|
||||
|
||||
//Final state
|
||||
QFinalState *final = new QFinalState(machine);
|
||||
QFinalState *finalState = new QFinalState(machine);
|
||||
|
||||
//Animation when the player enter in the game
|
||||
QAnimationState *lettersMovingState = new QAnimationState(machine);
|
||||
@ -198,8 +193,8 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction)
|
||||
gameState->addTransition(newAction, &QAction::triggered, gameState);
|
||||
|
||||
//Wanna quit, then connect to CTRL+Q
|
||||
gameState->addTransition(quitAction, &QAction::triggered, final);
|
||||
lettersMovingState->addTransition(quitAction, &QAction::triggered, final);
|
||||
gameState->addTransition(quitAction, &QAction::triggered, finalState);
|
||||
lettersMovingState->addTransition(quitAction, &QAction::triggered, finalState);
|
||||
|
||||
//Welcome screen is the initial state
|
||||
machine->setInitialState(lettersMovingState);
|
||||
@ -213,21 +208,24 @@ void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction)
|
||||
void GraphicsScene::addItem(Bomb *bomb)
|
||||
{
|
||||
bombs.insert(bomb);
|
||||
connect(bomb,&Bomb::bombExecutionFinished,this, &GraphicsScene::onBombExecutionFinished);
|
||||
connect(bomb, &Bomb::bombExecutionFinished,
|
||||
this, &GraphicsScene::onBombExecutionFinished);
|
||||
QGraphicsScene::addItem(bomb);
|
||||
}
|
||||
|
||||
void GraphicsScene::addItem(Torpedo *torpedo)
|
||||
{
|
||||
torpedos.insert(torpedo);
|
||||
connect(torpedo,&Torpedo::torpedoExecutionFinished,this, &GraphicsScene::onTorpedoExecutionFinished);
|
||||
connect(torpedo, &Torpedo::torpedoExecutionFinished,
|
||||
this, &GraphicsScene::onTorpedoExecutionFinished);
|
||||
QGraphicsScene::addItem(torpedo);
|
||||
}
|
||||
|
||||
void GraphicsScene::addItem(SubMarine *submarine)
|
||||
{
|
||||
submarines.insert(submarine);
|
||||
connect(submarine,&SubMarine::subMarineExecutionFinished,this, &GraphicsScene::onSubMarineExecutionFinished);
|
||||
connect(submarine, &SubMarine::subMarineExecutionFinished,
|
||||
this, &GraphicsScene::onSubMarineExecutionFinished);
|
||||
QGraphicsScene::addItem(submarine);
|
||||
}
|
||||
|
||||
@ -239,15 +237,18 @@ void GraphicsScene::addItem(QGraphicsItem *item)
|
||||
void GraphicsScene::onBombExecutionFinished()
|
||||
{
|
||||
Bomb *bomb = qobject_cast<Bomb *>(sender());
|
||||
if (!bomb)
|
||||
return;
|
||||
bombs.remove(bomb);
|
||||
bomb->deleteLater();
|
||||
if (boat)
|
||||
boat->setBombsLaunched(boat->bombsLaunched() - 1);
|
||||
boat->setBombsLaunched(boat->bombsLaunched() - 1);
|
||||
}
|
||||
|
||||
void GraphicsScene::onTorpedoExecutionFinished()
|
||||
{
|
||||
Torpedo *torpedo = qobject_cast<Torpedo *>(sender());
|
||||
if (!torpedo)
|
||||
return;
|
||||
torpedos.remove(torpedo);
|
||||
torpedo->deleteLater();
|
||||
}
|
||||
@ -255,6 +256,8 @@ void GraphicsScene::onTorpedoExecutionFinished()
|
||||
void GraphicsScene::onSubMarineExecutionFinished()
|
||||
{
|
||||
SubMarine *submarine = qobject_cast<SubMarine *>(sender());
|
||||
if (!submarine)
|
||||
return;
|
||||
submarines.remove(submarine);
|
||||
if (submarines.count() == 0)
|
||||
emit allSubMarineDestroyed(submarine->points());
|
||||
@ -266,16 +269,22 @@ void GraphicsScene::onSubMarineExecutionFinished()
|
||||
void GraphicsScene::clearScene()
|
||||
{
|
||||
for (SubMarine *sub : qAsConst(submarines)) {
|
||||
// make sure to not go into onSubMarineExecutionFinished
|
||||
sub->disconnect(this);
|
||||
sub->destroy();
|
||||
sub->deleteLater();
|
||||
}
|
||||
|
||||
for (Torpedo *torpedo : qAsConst(torpedos)) {
|
||||
// make sure to not go into onTorpedoExecutionFinished
|
||||
torpedo->disconnect(this);
|
||||
torpedo->destroy();
|
||||
torpedo->deleteLater();
|
||||
}
|
||||
|
||||
for (Bomb *bomb : qAsConst(bombs)) {
|
||||
// make sure to not go into onBombExecutionFinished
|
||||
bomb->disconnect(this);
|
||||
bomb->destroy();
|
||||
bomb->deleteLater();
|
||||
}
|
||||
|
@ -48,13 +48,12 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __GRAPHICSSCENE__H__
|
||||
#define __GRAPHICSSCENE__H__
|
||||
#ifndef GRAPHICSSCENE_H
|
||||
#define GRAPHICSSCENE_H
|
||||
|
||||
//Qt
|
||||
#include <QtWidgets/QGraphicsScene>
|
||||
#include <QtCore/QSet>
|
||||
#include <QtCore/QState>
|
||||
#include <QGraphicsScene>
|
||||
#include <QSet>
|
||||
|
||||
|
||||
class Boat;
|
||||
@ -78,18 +77,18 @@ public:
|
||||
};
|
||||
|
||||
struct SubmarineDescription {
|
||||
int type;
|
||||
int points;
|
||||
int type = 0;
|
||||
int points = 0;
|
||||
QString name;
|
||||
};
|
||||
|
||||
struct LevelDescription {
|
||||
int id;
|
||||
int id = 0;
|
||||
QString name;
|
||||
QList<QPair<int,int> > submarines;
|
||||
QVector<QPair<int, int>> submarines;
|
||||
};
|
||||
|
||||
GraphicsScene(int x, int y, int width, int height, Mode mode = Big);
|
||||
GraphicsScene(int x, int y, int width, int height, Mode mode, QObject *parent = nullptr);
|
||||
qreal sealLevel() const;
|
||||
void setupScene(QAction *newAction, QAction *quitAction);
|
||||
void addItem(Bomb *bomb);
|
||||
@ -127,5 +126,5 @@ private:
|
||||
friend class UpdateScoreTransition;
|
||||
};
|
||||
|
||||
#endif //__GRAPHICSSCENE__H__
|
||||
#endif // GRAPHICSSCENE_H
|
||||
|
||||
|
@ -56,11 +56,10 @@
|
||||
#include <QGraphicsView>
|
||||
#include <QApplication>
|
||||
#include <QMenu>
|
||||
#include <QMenuBar>
|
||||
#include <QLayout>
|
||||
|
||||
#ifndef QT_NO_OPENGL
|
||||
# include <QtOpenGL/QtOpenGL>
|
||||
# include <QtOpenGL>
|
||||
#endif
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
@ -74,10 +73,10 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
quitAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q));
|
||||
|
||||
if (QApplication::arguments().contains("-fullscreen")) {
|
||||
scene = new GraphicsScene(0, 0, 750, 400, GraphicsScene::Small);
|
||||
scene = new GraphicsScene(0, 0, 750, 400, GraphicsScene::Small, this);
|
||||
setWindowState(Qt::WindowFullScreen);
|
||||
} else {
|
||||
scene = new GraphicsScene(0, 0, 880, 630);
|
||||
scene = new GraphicsScene(0, 0, 880, 630, GraphicsScene::Big, this);
|
||||
layout()->setSizeConstraint(QLayout::SetFixedSize);
|
||||
}
|
||||
|
||||
|
@ -48,11 +48,11 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __MAINWINDOW__H__
|
||||
#define __MAINWINDOW__H__
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
//Qt
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include <QMainWindow>
|
||||
class GraphicsScene;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QGraphicsView;
|
||||
@ -69,4 +69,4 @@ private:
|
||||
QGraphicsView *view;
|
||||
};
|
||||
|
||||
#endif //__MAINWINDOW__H__
|
||||
#endif // MAINWINDOW_H
|
||||
|
@ -54,7 +54,7 @@
|
||||
//Qt
|
||||
#include <QPainter>
|
||||
|
||||
PixmapItem::PixmapItem(const QString &fileName,GraphicsScene::Mode mode, QGraphicsItem * parent)
|
||||
PixmapItem::PixmapItem(const QString &fileName, GraphicsScene::Mode mode, QGraphicsItem *parent)
|
||||
: QGraphicsObject(parent)
|
||||
{
|
||||
if (mode == GraphicsScene::Big)
|
||||
@ -63,7 +63,8 @@ PixmapItem::PixmapItem(const QString &fileName,GraphicsScene::Mode mode, QGraphi
|
||||
pix = QPixmap(QStringLiteral(":/small/") + fileName);
|
||||
}
|
||||
|
||||
PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) : QGraphicsObject(), pix(fileName)
|
||||
PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene)
|
||||
: QGraphicsObject(), pix(fileName)
|
||||
{
|
||||
scene->addItem(this);
|
||||
}
|
||||
|
@ -48,14 +48,14 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __PIXMAPITEM__H__
|
||||
#define __PIXMAPITEM__H__
|
||||
#ifndef PIXMAPITEM_H
|
||||
#define PIXMAPITEM_H
|
||||
|
||||
//Own
|
||||
#include "graphicsscene.h"
|
||||
|
||||
//Qt
|
||||
#include <QtWidgets/QGraphicsObject>
|
||||
#include <QGraphicsObject>
|
||||
|
||||
class PixmapItem : public QGraphicsObject
|
||||
{
|
||||
@ -69,4 +69,4 @@ private:
|
||||
QPixmap pix;
|
||||
};
|
||||
|
||||
#endif //__PIXMAPITEM__H__
|
||||
#endif // PIXMAPITEM_H
|
||||
|
@ -49,10 +49,11 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "progressitem.h"
|
||||
#include "pixmapitem.h"
|
||||
|
||||
ProgressItem::ProgressItem (QGraphicsItem * parent)
|
||||
: QGraphicsTextItem(parent), currentLevel(1), currentScore(0)
|
||||
#include <QFont>
|
||||
|
||||
ProgressItem::ProgressItem(QGraphicsItem *parent)
|
||||
: QGraphicsTextItem(parent)
|
||||
{
|
||||
setFont(QFont("Comic Sans MS"));
|
||||
setPos(parentItem()->boundingRect().topRight() - QPointF(180, -5));
|
||||
|
@ -52,19 +52,19 @@
|
||||
#define PROGRESSITEM_H
|
||||
|
||||
//Qt
|
||||
#include <QtWidgets/QGraphicsTextItem>
|
||||
#include <QGraphicsTextItem>
|
||||
|
||||
class ProgressItem : public QGraphicsTextItem
|
||||
{
|
||||
public:
|
||||
ProgressItem(QGraphicsItem * parent = 0);
|
||||
ProgressItem(QGraphicsItem *parent = nullptr);
|
||||
void setLevel(int level);
|
||||
void setScore(int score);
|
||||
|
||||
private:
|
||||
void updateProgress();
|
||||
int currentLevel;
|
||||
int currentScore;
|
||||
int currentLevel = 1;
|
||||
int currentScore = 0;
|
||||
};
|
||||
|
||||
#endif // PROGRESSITEM_H
|
||||
|
@ -50,7 +50,7 @@
|
||||
|
||||
#include "qanimationstate.h"
|
||||
|
||||
#include <QtCore/qstate.h>
|
||||
#include <QAbstractAnimation>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
@ -106,7 +106,7 @@ void QAnimationState::setAnimation(QAbstractAnimation *animation)
|
||||
return;
|
||||
|
||||
//Disconnect from the previous animation if exist
|
||||
if(m_animation)
|
||||
if (m_animation)
|
||||
disconnect(m_animation, &QAbstractAnimation::finished, this, &QAnimationState::animationFinished);
|
||||
|
||||
m_animation = animation;
|
||||
|
@ -51,13 +51,7 @@
|
||||
#ifndef QANIMATIONSTATE_H
|
||||
#define QANIMATIONSTATE_H
|
||||
|
||||
#ifndef QT_STATEMACHINE_SOLUTION
|
||||
# include <QtCore/qstate.h>
|
||||
# include <QtCore/qabstractanimation.h>
|
||||
#else
|
||||
# include "qstate.h"
|
||||
# include "qabstractanimation.h"
|
||||
#endif
|
||||
#include <QState>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
@ -67,7 +61,7 @@ class QAnimationState : public QState
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QAnimationState(QState *parent = 0);
|
||||
QAnimationState(QState *parent = nullptr);
|
||||
~QAnimationState();
|
||||
|
||||
void setAnimation(QAbstractAnimation *animation);
|
||||
|
@ -59,12 +59,12 @@
|
||||
#include "textinformationitem.h"
|
||||
|
||||
//Qt
|
||||
#include <QtWidgets/QMessageBox>
|
||||
#include <QtWidgets/QGraphicsView>
|
||||
#include <QtCore/QStateMachine>
|
||||
#include <QtWidgets/QKeyEventTransition>
|
||||
#include <QtCore/QFinalState>
|
||||
#include <QtCore/QRandomGenerator>
|
||||
#include <QFinalState>
|
||||
#include <QGraphicsView>
|
||||
#include <QKeyEventTransition>
|
||||
#include <QMessageBox>
|
||||
#include <QRandomGenerator>
|
||||
#include <QStateMachine>
|
||||
|
||||
PlayState::PlayState(GraphicsScene *scene, QState *parent)
|
||||
: QState(parent), scene(scene), machine(nullptr),
|
||||
@ -146,7 +146,7 @@ void PlayState::onEntry(QEvent *)
|
||||
machine->setInitialState(levelState);
|
||||
|
||||
//Final state
|
||||
QFinalState *final = new QFinalState(machine);
|
||||
QFinalState *finalState = new QFinalState(machine);
|
||||
|
||||
//This transition is triggered when the player press space after completing a level
|
||||
CustomSpaceTransition *spaceTransition = new CustomSpaceTransition(scene->views().at(0), this, QEvent::KeyPress, Qt::Key_Space);
|
||||
@ -154,7 +154,7 @@ void PlayState::onEntry(QEvent *)
|
||||
winState->addTransition(spaceTransition);
|
||||
|
||||
//We lost we should reach the final state
|
||||
lostState->addTransition(lostState, &QState::finished, final);
|
||||
lostState->addTransition(lostState, &QState::finished, finalState);
|
||||
|
||||
machine->start();
|
||||
}
|
||||
@ -181,11 +181,9 @@ void LevelState::initializeLevel()
|
||||
scene->progressItem->setScore(game->score);
|
||||
scene->progressItem->setLevel(game->currentLevel + 1);
|
||||
|
||||
GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(game->currentLevel);
|
||||
const GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(game->currentLevel);
|
||||
for (const QPair<int,int> &subContent : currentLevelDescription.submarines) {
|
||||
|
||||
for (int i = 0; i < currentLevelDescription.submarines.size(); ++i ) {
|
||||
|
||||
QPair<int,int> subContent = currentLevelDescription.submarines.at(i);
|
||||
GraphicsScene::SubmarineDescription submarineDesc = scene->submarinesData.at(subContent.first);
|
||||
|
||||
for (int j = 0; j < subContent.second; ++j ) {
|
||||
@ -202,9 +200,10 @@ void LevelState::initializeLevel()
|
||||
}
|
||||
|
||||
/** Pause State */
|
||||
PauseState::PauseState(GraphicsScene *scene, QState *parent) : QState(parent),scene(scene)
|
||||
PauseState::PauseState(GraphicsScene *scene, QState *parent) : QState(parent), scene(scene)
|
||||
{
|
||||
}
|
||||
|
||||
void PauseState::onEntry(QEvent *)
|
||||
{
|
||||
AnimationManager::self()->pauseAll();
|
||||
@ -324,8 +323,7 @@ bool WinTransition::eventTest(QEvent *event)
|
||||
|
||||
/** Space transition */
|
||||
CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key)
|
||||
: QKeyEventTransition(widget, type, key),
|
||||
game(game)
|
||||
: QKeyEventTransition(widget, type, key), game(game)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -52,15 +52,11 @@
|
||||
#define STATES_H
|
||||
|
||||
//Qt
|
||||
#include <QtCore/QState>
|
||||
#include <QtCore/QSignalTransition>
|
||||
#include <QtCore/QPropertyAnimation>
|
||||
#include <QtWidgets/QKeyEventTransition>
|
||||
#include <QtCore/QSet>
|
||||
#include <QKeyEventTransition>
|
||||
#include <QSignalTransition>
|
||||
#include <QState>
|
||||
|
||||
class GraphicsScene;
|
||||
class Boat;
|
||||
class SubMarine;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QStateMachine;
|
||||
QT_END_NAMESPACE
|
||||
@ -68,7 +64,7 @@ QT_END_NAMESPACE
|
||||
class PlayState : public QState
|
||||
{
|
||||
public:
|
||||
explicit PlayState(GraphicsScene *scene, QState *parent = 0);
|
||||
explicit PlayState(GraphicsScene *scene, QState *parent = nullptr);
|
||||
~PlayState();
|
||||
|
||||
protected:
|
||||
@ -92,7 +88,7 @@ private :
|
||||
class LevelState : public QState
|
||||
{
|
||||
public:
|
||||
LevelState(GraphicsScene *scene, PlayState *game, QState *parent = 0);
|
||||
LevelState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr);
|
||||
protected:
|
||||
void onEntry(QEvent *) override;
|
||||
private :
|
||||
@ -104,7 +100,7 @@ private :
|
||||
class PauseState : public QState
|
||||
{
|
||||
public:
|
||||
explicit PauseState(GraphicsScene *scene, QState *parent = 0);
|
||||
explicit PauseState(GraphicsScene *scene, QState *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void onEntry(QEvent *) override;
|
||||
@ -116,7 +112,7 @@ private :
|
||||
class LostState : public QState
|
||||
{
|
||||
public:
|
||||
LostState(GraphicsScene *scene, PlayState *game, QState *parent = 0);
|
||||
LostState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void onEntry(QEvent *) override;
|
||||
@ -129,7 +125,7 @@ private :
|
||||
class WinState : public QState
|
||||
{
|
||||
public:
|
||||
WinState(GraphicsScene *scene, PlayState *game, QState *parent = 0);
|
||||
WinState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void onEntry(QEvent *) override;
|
||||
@ -154,7 +150,7 @@ public:
|
||||
protected:
|
||||
bool eventTest(QEvent *event) override;
|
||||
private:
|
||||
PlayState * game;
|
||||
PlayState *game;
|
||||
GraphicsScene *scene;
|
||||
};
|
||||
|
||||
@ -166,7 +162,7 @@ public:
|
||||
protected:
|
||||
bool eventTest(QEvent *event) override;
|
||||
private:
|
||||
PlayState * game;
|
||||
PlayState *game;
|
||||
GraphicsScene *scene;
|
||||
};
|
||||
|
||||
|
@ -52,15 +52,14 @@
|
||||
#include "submarine.h"
|
||||
#include "submarine_p.h"
|
||||
#include "torpedo.h"
|
||||
#include "pixmapitem.h"
|
||||
#include "graphicsscene.h"
|
||||
#include "animationmanager.h"
|
||||
#include "qanimationstate.h"
|
||||
|
||||
#include <QtCore/QPropertyAnimation>
|
||||
#include <QtCore/QStateMachine>
|
||||
#include <QtCore/QFinalState>
|
||||
#include <QtCore/QSequentialAnimationGroup>
|
||||
#include <QFinalState>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QStateMachine>
|
||||
#include <QSequentialAnimationGroup>
|
||||
|
||||
static QAbstractAnimation *setupDestroyAnimation(SubMarine *sub)
|
||||
{
|
||||
@ -86,9 +85,8 @@ SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QSt
|
||||
|
||||
graphicsRotation = new QGraphicsRotation(this);
|
||||
graphicsRotation->setAxis(Qt::YAxis);
|
||||
graphicsRotation->setOrigin(QVector3D(size().width()/2, size().height()/2, 0));
|
||||
QList<QGraphicsTransform *> r;
|
||||
r.append(graphicsRotation);
|
||||
graphicsRotation->setOrigin(QVector3D(size().width() / 2, size().height() / 2, 0));
|
||||
QList<QGraphicsTransform *> r({graphicsRotation});
|
||||
setTransformations(r);
|
||||
|
||||
//We setup the state machine of the submarine
|
||||
@ -112,7 +110,7 @@ SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QSt
|
||||
machine->setInitialState(moving);
|
||||
|
||||
//End
|
||||
QFinalState *final = new QFinalState(machine);
|
||||
QFinalState *finalState = new QFinalState(machine);
|
||||
|
||||
//If the moving animation is finished we move to the return state
|
||||
movement->addTransition(movement, &QAnimationState::animationFinished, rotation);
|
||||
@ -128,7 +126,7 @@ SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QSt
|
||||
moving->addTransition(this, &SubMarine::subMarineDestroyed, destroyedState);
|
||||
|
||||
//Transition to final state when the destroyed animation is finished
|
||||
destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, final);
|
||||
destroyedState->addTransition(destroyedState, &QAnimationState::animationFinished, finalState);
|
||||
|
||||
//The machine has finished to be executed, then the submarine is dead
|
||||
connect(machine,&QState::finished,this, &SubMarine::subMarineExecutionFinished);
|
||||
@ -145,9 +143,8 @@ void SubMarine::setCurrentDirection(SubMarine::Movement direction)
|
||||
{
|
||||
if (this->direction == direction)
|
||||
return;
|
||||
if (direction == SubMarine::Right && this->direction == SubMarine::None) {
|
||||
if (direction == SubMarine::Right && this->direction == SubMarine::None)
|
||||
graphicsRotation->setAngle(180);
|
||||
}
|
||||
this->direction = direction;
|
||||
}
|
||||
|
||||
@ -158,9 +155,8 @@ enum SubMarine::Movement SubMarine::currentDirection() const
|
||||
|
||||
void SubMarine::setCurrentSpeed(int speed)
|
||||
{
|
||||
if (speed < 0 || speed > 3) {
|
||||
if (speed < 0 || speed > 3)
|
||||
qWarning("SubMarine::setCurrentSpeed : The speed is invalid");
|
||||
}
|
||||
this->speed = speed;
|
||||
emit subMarineStateChanged();
|
||||
}
|
||||
@ -172,7 +168,7 @@ int SubMarine::currentSpeed() const
|
||||
|
||||
void SubMarine::launchTorpedo(int speed)
|
||||
{
|
||||
Torpedo * torp = new Torpedo();
|
||||
Torpedo *torp = new Torpedo;
|
||||
GraphicsScene *scene = static_cast<GraphicsScene *>(this->scene());
|
||||
scene->addItem(torp);
|
||||
torp->setPos(pos());
|
||||
|
@ -48,15 +48,12 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __SUBMARINE__H__
|
||||
#define __SUBMARINE__H__
|
||||
|
||||
//Qt
|
||||
#include <QtWidgets/QGraphicsTransform>
|
||||
#ifndef SUBMARINE_H
|
||||
#define SUBMARINE_H
|
||||
|
||||
#include "pixmapitem.h"
|
||||
|
||||
class Torpedo;
|
||||
#include <QGraphicsRotation>
|
||||
|
||||
class SubMarine : public PixmapItem
|
||||
{
|
||||
@ -99,4 +96,4 @@ private:
|
||||
QGraphicsRotation *graphicsRotation;
|
||||
};
|
||||
|
||||
#endif //__SUBMARINE__H__
|
||||
#endif // SUBMARINE_H
|
||||
|
@ -68,16 +68,15 @@
|
||||
#include "qanimationstate.h"
|
||||
|
||||
//Qt
|
||||
#include <QtCore/QPropertyAnimation>
|
||||
#include <QtCore/QRandomGenerator>
|
||||
#include <QtWidgets/QGraphicsScene>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QRandomGenerator>
|
||||
|
||||
//This state is describing when the boat is moving right
|
||||
class MovementState : public QAnimationState
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MovementState(SubMarine *submarine, QState *parent = 0) : QAnimationState(parent)
|
||||
explicit MovementState(SubMarine *submarine, QState *parent = nullptr) : QAnimationState(parent)
|
||||
{
|
||||
movementAnimation = new QPropertyAnimation(submarine, "pos");
|
||||
connect(movementAnimation, &QPropertyAnimation::valueChanged,
|
||||
@ -117,7 +116,7 @@ private:
|
||||
class ReturnState : public QAnimationState
|
||||
{
|
||||
public:
|
||||
explicit ReturnState(SubMarine *submarine, QState *parent = 0) : QAnimationState(parent)
|
||||
explicit ReturnState(SubMarine *submarine, QState *parent = nullptr) : QAnimationState(parent)
|
||||
{
|
||||
returnAnimation = new QPropertyAnimation(submarine->rotation(), "angle");
|
||||
returnAnimation->setDuration(500);
|
||||
|
@ -50,14 +50,15 @@
|
||||
#include "textinformationitem.h"
|
||||
#include "pixmapitem.h"
|
||||
|
||||
TextInformationItem::TextInformationItem (QGraphicsItem * parent)
|
||||
TextInformationItem::TextInformationItem (QGraphicsItem *parent)
|
||||
: QGraphicsTextItem(parent)
|
||||
{
|
||||
setFont(QFont("Comic Sans MS", 15));
|
||||
}
|
||||
#include <QDebug>
|
||||
void TextInformationItem::setMessage(const QString& text)
|
||||
|
||||
void TextInformationItem::setMessage(const QString &text)
|
||||
{
|
||||
setHtml(text);
|
||||
setPos(parentItem()->boundingRect().center().x() - boundingRect().size().width()/2 , parentItem()->boundingRect().center().y());
|
||||
setPos(parentItem()->boundingRect().center().x() - boundingRect().size().width() / 2,
|
||||
parentItem()->boundingRect().center().y());
|
||||
}
|
||||
|
@ -52,13 +52,13 @@
|
||||
#define TEXTINFORMATIONITEM_H
|
||||
|
||||
//Qt
|
||||
#include <QtWidgets/QGraphicsTextItem>
|
||||
#include <QGraphicsTextItem>
|
||||
|
||||
class TextInformationItem : public QGraphicsTextItem
|
||||
{
|
||||
public:
|
||||
TextInformationItem(QGraphicsItem * parent = 0);
|
||||
void setMessage(const QString& text);
|
||||
TextInformationItem(QGraphicsItem *parent = nullptr);
|
||||
void setMessage(const QString &text);
|
||||
};
|
||||
|
||||
#endif // TEXTINFORMATIONITEM_H
|
||||
|
@ -50,15 +50,14 @@
|
||||
|
||||
//Own
|
||||
#include "torpedo.h"
|
||||
#include "pixmapitem.h"
|
||||
#include "boat.h"
|
||||
#include "graphicsscene.h"
|
||||
#include "animationmanager.h"
|
||||
#include "qanimationstate.h"
|
||||
|
||||
#include <QtCore/QPropertyAnimation>
|
||||
#include <QtCore/QStateMachine>
|
||||
#include <QtCore/QFinalState>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QStateMachine>
|
||||
#include <QFinalState>
|
||||
|
||||
Torpedo::Torpedo() : PixmapItem(QString::fromLatin1("torpedo"),GraphicsScene::Big),
|
||||
currentSpeed(0)
|
||||
@ -70,11 +69,11 @@ void Torpedo::launch()
|
||||
{
|
||||
QPropertyAnimation *launchAnimation = new QPropertyAnimation(this, "pos");
|
||||
AnimationManager::self()->registerAnimation(launchAnimation);
|
||||
launchAnimation->setEndValue(QPointF(x(),qobject_cast<GraphicsScene *>(scene())->sealLevel() - 15));
|
||||
launchAnimation->setEndValue(QPointF(x(), qobject_cast<GraphicsScene *>(scene())->sealLevel() - 15));
|
||||
launchAnimation->setEasingCurve(QEasingCurve::InQuad);
|
||||
launchAnimation->setDuration(y()/currentSpeed*10);
|
||||
connect(launchAnimation,&QVariantAnimation::valueChanged,this,&Torpedo::onAnimationLaunchValueChanged);
|
||||
connect(this,&Torpedo::torpedoExploded, launchAnimation, &QAbstractAnimation::stop);
|
||||
launchAnimation->setDuration(y() / currentSpeed * 10);
|
||||
connect(launchAnimation, &QVariantAnimation::valueChanged, this, &Torpedo::onAnimationLaunchValueChanged);
|
||||
connect(this, &Torpedo::torpedoExploded, launchAnimation, &QAbstractAnimation::stop);
|
||||
|
||||
//We setup the state machine of the torpedo
|
||||
QStateMachine *machine = new QStateMachine(this);
|
||||
@ -84,18 +83,18 @@ void Torpedo::launch()
|
||||
launched->setAnimation(launchAnimation);
|
||||
|
||||
//End
|
||||
QFinalState *final = new QFinalState(machine);
|
||||
QFinalState *finalState = new QFinalState(machine);
|
||||
|
||||
machine->setInitialState(launched);
|
||||
|
||||
//### Add a nice animation when the torpedo is destroyed
|
||||
launched->addTransition(this, &Torpedo::torpedoExploded,final);
|
||||
launched->addTransition(this, &Torpedo::torpedoExploded, finalState);
|
||||
|
||||
//If the animation is finished, then we move to the final state
|
||||
launched->addTransition(launched, &QAnimationState::animationFinished, final);
|
||||
launched->addTransition(launched, &QAnimationState::animationFinished, finalState);
|
||||
|
||||
//The machine has finished to be executed, then the boat is dead
|
||||
connect(machine,&QState::finished,this, &Torpedo::torpedoExecutionFinished);
|
||||
connect(machine, &QState::finished, this, &Torpedo::torpedoExecutionFinished);
|
||||
|
||||
machine->start();
|
||||
}
|
||||
|
@ -48,8 +48,8 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __TORPEDO__H__
|
||||
#define __TORPEDO__H__
|
||||
#ifndef TORPEDO_H
|
||||
#define TORPEDO_H
|
||||
|
||||
#include "pixmapitem.h"
|
||||
|
||||
@ -73,4 +73,4 @@ private:
|
||||
int currentSpeed;
|
||||
};
|
||||
|
||||
#endif //__TORPEDO__H__
|
||||
#endif // TORPEDO_H
|
||||
|
Loading…
Reference in New Issue
Block a user