diff options
author | Eskil Abrahamsen Blomfeldt <eblomfel@trolltech.com> | 2009-05-12 14:40:44 (GMT) |
---|---|---|
committer | Eskil Abrahamsen Blomfeldt <eblomfel@trolltech.com> | 2009-05-12 14:42:26 (GMT) |
commit | d26a56e2d94bea2b5a1135b8336bbeefebf3ba1c (patch) | |
tree | c4a6497357266aa6f8bca69aef985a0f4ac5779d /examples/statemachine/tankgame/rocketitem.cpp | |
parent | cb516fd65149991a2105c545192a52f26a6ab67d (diff) | |
download | Qt-d26a56e2d94bea2b5a1135b8336bbeefebf3ba1c.zip Qt-d26a56e2d94bea2b5a1135b8336bbeefebf3ba1c.tar.gz Qt-d26a56e2d94bea2b5a1135b8336bbeefebf3ba1c.tar.bz2 |
Change name of "errorstate" example to "tankgame"
The error state is not a big enough part of the example to justify naming it
after it.
Diffstat (limited to 'examples/statemachine/tankgame/rocketitem.cpp')
-rw-r--r-- | examples/statemachine/tankgame/rocketitem.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/statemachine/tankgame/rocketitem.cpp b/examples/statemachine/tankgame/rocketitem.cpp new file mode 100644 index 0000000..c324980 --- /dev/null +++ b/examples/statemachine/tankgame/rocketitem.cpp @@ -0,0 +1,60 @@ +#include "rocketitem.h" +#include "tankitem.h" + +#include <QPainter> +#include <QGraphicsScene> + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +RocketItem::RocketItem(QObject *parent) + : GameItem(parent), m_direction(0.0), m_distance(300.0) +{ +} + +QRectF RocketItem::boundingRect() const +{ + return QRectF(-1.0, -1.0, 2.0, 2.0); +} + +void RocketItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + painter->setBrush(Qt::black); + painter->drawEllipse(boundingRect()); +} + +void RocketItem::idle(qreal elapsed) +{ + qreal dist = elapsed * speed(); + + m_distance -= dist; + if (m_distance < 0.0) { + scene()->removeItem(this); + delete this; + return; + } + + qreal a = m_direction * M_PI / 180.0; + + qreal yd = dist * sin(a); + qreal xd = dist * sin(M_PI / 2.0 - a); + + QPointF requestedPosition = pos() + QPointF(xd, yd); + QGraphicsItem *collidedItem = 0; + QPointF nextPosition = tryMove(requestedPosition, 0, &collidedItem); + if (requestedPosition == nextPosition) { + setPos(nextPosition); + } else { + if (GameItem *gameItem = qgraphicsitem_cast<GameItem *>(collidedItem)) { + TankItem *tankItem = qobject_cast<TankItem *>(gameItem); + if (tankItem != 0) + tankItem->hitByRocket(); + } + + scene()->removeItem(this); + delete this; + } +} |