diff options
Diffstat (limited to 'examples/statemachine')
16 files changed, 65 insertions, 86 deletions
diff --git a/examples/statemachine/errorstate/errorstate.pro b/examples/statemachine/errorstate/errorstate.pro index c159f94..b93a691 100644 --- a/examples/statemachine/errorstate/errorstate.pro +++ b/examples/statemachine/errorstate/errorstate.pro @@ -8,6 +8,6 @@ DEPENDPATH += . INCLUDEPATH += C:/dev/kinetic/examples/statemachine/errorstate/. . # Input -HEADERS += mainwindow.h plugin.h tank.h tankitem.h rocketitem.h gameitem.h +HEADERS += mainwindow.h plugin.h tankitem.h rocketitem.h gameitem.h SOURCES += main.cpp mainwindow.cpp tankitem.cpp rocketitem.cpp gameitem.cpp CONFIG += console diff --git a/examples/statemachine/errorstate/gameitem.cpp b/examples/statemachine/errorstate/gameitem.cpp index 786cf51..d286df4 100644 --- a/examples/statemachine/errorstate/gameitem.cpp +++ b/examples/statemachine/errorstate/gameitem.cpp @@ -2,6 +2,10 @@ #include <QGraphicsScene> +GameItem::GameItem(QObject *parent) : QObject(parent) +{ +} + QPointF GameItem::tryMove(const QPointF &requestedPosition, QLineF *collidedLine, QGraphicsItem **collidedItem) const { diff --git a/examples/statemachine/errorstate/gameitem.h b/examples/statemachine/errorstate/gameitem.h index 9808706..43b8785 100644 --- a/examples/statemachine/errorstate/gameitem.h +++ b/examples/statemachine/errorstate/gameitem.h @@ -4,9 +4,15 @@ #include <QGraphicsItem> class QLineF; -class GameItem: public QGraphicsItem +class GameItem: public QObject, public QGraphicsItem { + Q_OBJECT public: + enum { Type = UserType + 1 }; + int type() const { return Type; } + + GameItem(QObject *parent = 0); + virtual void idle(qreal elapsed) = 0; protected: diff --git a/examples/statemachine/errorstate/mainwindow.cpp b/examples/statemachine/errorstate/mainwindow.cpp index daf7be0..4d9b88e 100644 --- a/examples/statemachine/errorstate/mainwindow.cpp +++ b/examples/statemachine/errorstate/mainwindow.cpp @@ -167,12 +167,7 @@ void MainWindow::runStep() qreal elapsedSecs = elapsed / 1000.0; QList<QGraphicsItem *> items = m_scene->items(); foreach (QGraphicsItem *item, items) { - // ### - GameItem *gameItem = qgraphicsitem_cast<TankItem *>(item); - if (gameItem == 0) - gameItem = qgraphicsitem_cast<RocketItem *>(item); - - if (gameItem != 0) + if (GameItem *gameItem = qgraphicsitem_cast<GameItem *>(item)) gameItem->idle(elapsedSecs); } } @@ -204,7 +199,7 @@ void MainWindow::addTank() if (plugin != 0) { TankItem *tankItem = m_spawns.takeLast(); m_scene->addItem(tankItem); - connect(tankItem, SIGNAL(fireCannon()), this, SLOT(addRocket())); + connect(tankItem, SIGNAL(cannonFired()), this, SLOT(addRocket())); if (m_spawns.isEmpty()) emit mapFull(); diff --git a/examples/statemachine/errorstate/plugin.h b/examples/statemachine/errorstate/plugin.h index 69b9574..2b48d43 100644 --- a/examples/statemachine/errorstate/plugin.h +++ b/examples/statemachine/errorstate/plugin.h @@ -4,13 +4,12 @@ #include <QtPlugin> class QState; -class Tank; class Plugin { public: virtual ~Plugin() {} - virtual QState *create(QState *parentState, Tank *tank) = 0; + virtual QState *create(QState *parentState, QObject *tank) = 0; }; Q_DECLARE_INTERFACE(Plugin, "TankPlugin") diff --git a/examples/statemachine/errorstate/rocketitem.cpp b/examples/statemachine/errorstate/rocketitem.cpp index 85d436b..c324980 100644 --- a/examples/statemachine/errorstate/rocketitem.cpp +++ b/examples/statemachine/errorstate/rocketitem.cpp @@ -10,8 +10,8 @@ #define M_PI 3.14159265358979323846 #endif -RocketItem::RocketItem() - : m_direction(0.0), m_distance(300.0) +RocketItem::RocketItem(QObject *parent) + : GameItem(parent), m_direction(0.0), m_distance(300.0) { } @@ -48,8 +48,11 @@ void RocketItem::idle(qreal elapsed) if (requestedPosition == nextPosition) { setPos(nextPosition); } else { - if (TankItem *tankItem = qgraphicsitem_cast<TankItem *>(collidedItem)) - tankItem->hitByRocket(); + if (GameItem *gameItem = qgraphicsitem_cast<GameItem *>(collidedItem)) { + TankItem *tankItem = qobject_cast<TankItem *>(gameItem); + if (tankItem != 0) + tankItem->hitByRocket(); + } scene()->removeItem(this); delete this; diff --git a/examples/statemachine/errorstate/rocketitem.h b/examples/statemachine/errorstate/rocketitem.h index b055b0a..189a1dd 100644 --- a/examples/statemachine/errorstate/rocketitem.h +++ b/examples/statemachine/errorstate/rocketitem.h @@ -5,12 +5,10 @@ class RocketItem: public GameItem { + Q_OBJECT public: - enum { Type = UserType + 2 }; + RocketItem(QObject *parent = 0); - RocketItem(); - - int type() const { return Type; } virtual void idle(qreal elapsed); qreal speed() const { return 100.0; } void setDirection(qreal direction) { m_direction = direction; } @@ -19,7 +17,6 @@ protected: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const; - private: qreal m_direction; qreal m_distance; diff --git a/examples/statemachine/errorstate/tank.h b/examples/statemachine/errorstate/tank.h deleted file mode 100644 index 49c5daf..0000000 --- a/examples/statemachine/errorstate/tank.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TANK_H -#define TANK_H - -#include <QObject> -#include <QLineF> - -class Tank: public QObject -{ - Q_OBJECT - Q_PROPERTY(qreal direction READ direction) - Q_PROPERTY(qreal distanceToObstacle READ distanceToObstacle) -public: - Tank(QObject *parent = 0) : QObject(parent) {} - - virtual qreal direction() const = 0; - virtual qreal distanceToObstacle() const = 0; - -signals: - void collision(const QLineF &collidedLine) const; - void actionCompleted(); - void tankSpotted(qreal otherTankDirection, qreal distance); - -public slots: - virtual void moveForwards(qreal length) = 0; - virtual void moveBackwards(qreal length) = 0; - virtual void turn(qreal degrees) = 0; - virtual void stop() = 0; - virtual void fireCannon() = 0; -}; - -#endif diff --git a/examples/statemachine/errorstate/tankitem.cpp b/examples/statemachine/errorstate/tankitem.cpp index 705623c..ae338fe 100644 --- a/examples/statemachine/errorstate/tankitem.cpp +++ b/examples/statemachine/errorstate/tankitem.cpp @@ -86,9 +86,9 @@ private: }; TankItem::TankItem(QObject *parent) - : Tank(parent), m_currentAction(0), m_currentDirection(0.0), m_enabled(true) + : GameItem(parent), m_currentAction(0), m_currentDirection(0.0), m_enabled(true) { - connect(this, SIGNAL(fireCannon()), this, SIGNAL(actionCompleted())); + connect(this, SIGNAL(cannonFired()), this, SIGNAL(actionCompleted())); } void TankItem::idle(qreal elapsed) @@ -121,6 +121,11 @@ void TankItem::setAction(Action *newAction) m_currentAction = newAction; } +void TankItem::fireCannon() +{ + emit cannonFired(); +} + void TankItem::moveForwards(qreal length) { setAction(new MoveAction(this, length)); diff --git a/examples/statemachine/errorstate/tankitem.h b/examples/statemachine/errorstate/tankitem.h index e598cfd..45016ec 100644 --- a/examples/statemachine/errorstate/tankitem.h +++ b/examples/statemachine/errorstate/tankitem.h @@ -1,31 +1,20 @@ #ifndef TANKITEM_H #define TANKITEM_H -#include "tank.h" #include "gameitem.h" #include <QColor> class Action; -class TankItem: public Tank, public GameItem +class TankItem: public GameItem { Q_OBJECT Q_PROPERTY(bool enabled READ enabled WRITE setEnabled) -public: - enum { Type = UserType + 1 }; - + Q_PROPERTY(qreal direction READ direction WRITE turn) + Q_PROPERTY(qreal distanceToObstacle READ distanceToObstacle) +public: TankItem(QObject *parent = 0); - - int type() const { return Type; } - - virtual void moveForwards(qreal length); - virtual void moveBackwards(qreal length); - virtual void turn(qreal newDirection); - virtual void stop(); - virtual qreal direction() const; - virtual qreal distanceToObstacle() const; - virtual qreal distanceToObstacle(QGraphicsItem **item) const; - + void setColor(const QColor &color) { m_color = color; } QColor color() const { return m_color; } @@ -42,8 +31,22 @@ public: void setEnabled(bool b) { m_enabled = b; } bool enabled() const { return m_enabled; } + qreal direction() const; + qreal distanceToObstacle() const; + qreal distanceToObstacle(QGraphicsItem **item) const; + signals: - virtual void fireCannon(); + void tankSpotted(qreal direction, qreal distance); + void collision(const QLineF &collidedLine); + void actionCompleted(); + void cannonFired(); + +public slots: + void moveForwards(qreal length); + void moveBackwards(qreal length); + void turn(qreal newDirection); + void stop(); + void fireCannon(); protected: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); diff --git a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp b/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp index 135c7b6..c196247 100644 --- a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp +++ b/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp @@ -1,13 +1,11 @@ #include "random_ai_plugin.h" -#include <errorstate/tank.h> - #include <QState> #include <QtPlugin> #include <time.h> -QState *RandomAiPlugin::create(QState *parentState, Tank *tank) +QState *RandomAiPlugin::create(QState *parentState, QObject *tank) { qsrand(uint(time(NULL))); diff --git a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h b/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h index 758b3e8..d3670bd 100644 --- a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h +++ b/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h @@ -56,7 +56,7 @@ class RandomAiPlugin: public QObject, public Plugin Q_OBJECT Q_INTERFACES(Plugin) public: - virtual QState *create(QState *parentState, Tank *tank); + virtual QState *create(QState *parentState, QObject *tank); }; #endif // RANDOM_AI_PLUGIN_H diff --git a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.cpp b/examples/statemachine/errorstateplugins/spin_ai/spin_ai.cpp index 8452523..de95f41 100644 --- a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.cpp +++ b/examples/statemachine/errorstateplugins/spin_ai/spin_ai.cpp @@ -2,7 +2,7 @@ #include <QtPlugin> -QState *SpinAi::create(QState *parentState, Tank *tank) +QState *SpinAi::create(QState *parentState, QObject *tank) { QState *topLevel = new QState(parentState); QState *spinState = new SpinState(tank, topLevel); diff --git a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h b/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h index 7336640..309ba14 100644 --- a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h +++ b/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h @@ -2,23 +2,23 @@ #define SPIN_AI_H #include <errorstate/plugin.h> -#include <errorstate/tank.h> #include <QObject> #include <QState> +#include <QVariant> class SpinState: public QState { Q_OBJECT public: - SpinState(Tank *tank, QState *parent) : QState(parent), m_tank(tank) + SpinState(QObject *tank, QState *parent) : QState(parent), m_tank(tank) { } public slots: void spin() { - m_tank->turn(90.0); + m_tank->setProperty("direction", 90.0); } protected: @@ -29,7 +29,7 @@ protected: } private: - Tank *m_tank; + QObject *m_tank; }; @@ -38,7 +38,7 @@ class SpinAi: public QObject, public Plugin Q_OBJECT Q_INTERFACES(Plugin) public: - virtual QState *create(QState *parentState, Tank *tank); + virtual QState *create(QState *parentState, QObject *tank); }; #endif diff --git a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.cpp b/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.cpp index dd73270..5499ba3 100644 --- a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.cpp +++ b/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.cpp @@ -2,7 +2,7 @@ #include <QtPlugin> -QState *SpinAiWithError::create(QState *parentState, Tank *tank) +QState *SpinAiWithError::create(QState *parentState, QObject *tank) { QState *topLevel = new QState(parentState); QState *spinState = new SpinState(tank, topLevel); diff --git a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h b/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h index 26def66..fa06d10 100644 --- a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h +++ b/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h @@ -2,23 +2,23 @@ #define SPIN_AI_WITH_ERROR_H #include <errorstate/plugin.h> -#include <errorstate/tank.h> #include <QObject> #include <QState> +#include <QVariant> class SpinState: public QState { Q_OBJECT public: - SpinState(Tank *tank, QState *parent) : QState(parent), m_tank(tank) + SpinState(QObject *tank, QState *parent) : QState(parent), m_tank(tank) { } public slots: void spin() { - m_tank->turn(90.0); + m_tank->setProperty("direction", 90.0); } protected: @@ -29,7 +29,7 @@ protected: } private: - Tank *m_tank; + QObject *m_tank; }; @@ -38,7 +38,7 @@ class SpinAiWithError: public QObject, public Plugin Q_OBJECT Q_INTERFACES(Plugin) public: - virtual QState *create(QState *parentState, Tank *tank); + virtual QState *create(QState *parentState, QObject *tank); }; #endif |