diff options
Diffstat (limited to 'examples/statemachine/tankgame/tankitem.cpp')
-rw-r--r-- | examples/statemachine/tankgame/tankitem.cpp | 73 |
1 files changed, 56 insertions, 17 deletions
diff --git a/examples/statemachine/tankgame/tankitem.cpp b/examples/statemachine/tankgame/tankitem.cpp index c322d21..393d65f 100644 --- a/examples/statemachine/tankgame/tankitem.cpp +++ b/examples/statemachine/tankgame/tankitem.cpp @@ -1,3 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + #include "tankitem.h" #include <QPainter> @@ -35,15 +76,15 @@ public: m_reverse = m_distance < 0.0; } - bool apply(qreal timeDelta) + bool apply(qreal timeDelta) { qreal dist = timeDelta * item()->speed() * (m_reverse ? -1.0 : 1.0); - bool done = false; - if (qAbs(m_distance) < qAbs(dist)) { + bool done = false; + if (qAbs(m_distance) < qAbs(dist)) { done = true; dist = m_distance; - } + } m_distance -= dist; qreal a = item()->direction() * M_PI / 180.0; @@ -69,14 +110,14 @@ public: m_reverse = m_distance < 0.0; } - bool apply(qreal timeDelta) + bool apply(qreal timeDelta) { qreal dist = timeDelta * item()->angularSpeed() * (m_reverse ? -1.0 : 1.0); - bool done = false; + bool done = false; if (qAbs(m_distance) < qAbs(dist)) { done = true; dist = m_distance; - } + } m_distance -= dist; item()->setDirection(item()->direction() + dist); @@ -88,7 +129,7 @@ private: bool m_reverse; }; -TankItem::TankItem(QObject *parent) +TankItem::TankItem(QObject *parent) : GameItem(parent), m_currentAction(0), m_currentDirection(0.0), m_enabled(true) { connect(this, SIGNAL(cannonFired()), this, SIGNAL(actionCompleted())); @@ -106,7 +147,7 @@ void TankItem::idle(qreal elapsed) QGraphicsItem *item = 0; qreal distance = distanceToObstacle(&item); if (TankItem *tankItem = qgraphicsitem_cast<TankItem *>(item)) - emit tankSpotted(tankItem->direction(), distance); + emit tankSpotted(tankItem->direction(), distance); } } } @@ -119,7 +160,7 @@ void TankItem::hitByRocket() void TankItem::setAction(Action *newAction) { - if (m_currentAction != 0) + if (m_currentAction != 0) delete m_currentAction; m_currentAction = newAction; @@ -184,9 +225,9 @@ void TankItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge QRectF cannonBase = brect.adjusted(10.0, 6.0, -12.0, -6.0); painter->drawEllipse(cannonBase); - painter->drawRect(QRectF(QPointF(cannonBase.center().x(), cannonBase.center().y() - 2.0), + painter->drawRect(QRectF(QPointF(cannonBase.center().x(), cannonBase.center().y() - 2.0), QPointF(brect.right(), cannonBase.center().y() + 2.0))); - + // left track painter->setBrush(QBrush(Qt::black, Qt::VerPattern)); QRectF leftTrackRect = QRectF(brect.topLeft(), QPointF(brect.right() - 2.0, brect.top() + 4.0)); @@ -223,7 +264,7 @@ qreal TankItem::direction() const void TankItem::setDirection(qreal newDirection) { - int fullRotations = int(newDirection) / 360; + int fullRotations = int(newDirection) / 360; newDirection -= fullRotations * 360.0; qreal diff = newDirection - m_currentDirection; @@ -245,8 +286,8 @@ qreal TankItem::distanceToObstacle(QGraphicsItem **obstacle) const QPointF nextPosition = tryMove(requestedPosition, 0, &collidedItem); if (collidedItem != 0) { if (obstacle != 0) - *obstacle = collidedItem; - + *obstacle = collidedItem; + QPointF d = nextPosition - pos(); return sqrt(pow(d.x(), 2) + pow(d.y(), 2)); } else { @@ -259,5 +300,3 @@ qreal TankItem::distanceToObstacle() const return distanceToObstacle(0); } - - |