From 12868c14365387679c441440b751b0e922a2857c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Heli=20Hyv=C3=A4ttinen?= Date: Mon, 6 Jun 2011 14:59:29 +0300 Subject: [PATCH] Separated actual collision detection into subclass Ship Now uses the class Ship. The separation is to keep the base class OrientationControlledGraphicsPixmapObject generic and reusable. --- orientationcontrolledgraphicspixmapobject.cpp | 28 +++++++-------------- orientationcontrolledgraphicspixmapobject.h | 15 +++++------ seascene.cpp | 4 +-- ship.cpp | 33 +++++++++++++++++++++++++ ship.h | 24 ++++++++++++++++++ 5 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 ship.cpp create mode 100644 ship.h diff --git a/orientationcontrolledgraphicspixmapobject.cpp b/orientationcontrolledgraphicspixmapobject.cpp index cff72a6..af19e87 100644 --- a/orientationcontrolledgraphicspixmapobject.cpp +++ b/orientationcontrolledgraphicspixmapobject.cpp @@ -61,31 +61,21 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor() setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width())); setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height())); - QList collidesList = collidingItems(); - if (!collidesList.isEmpty()) + + //handle collisions and move back to the original position if false returned + + if (handleCollisions() == false) { - qDebug() << collidesList.at(0)->data(0); - if (collidesList.at(0)->data(0) == "rock") - { - setX(oldx); - setY(oldy); - } + setX(oldx); + setY(oldy); } } -void OrientationControlledGraphicsPixmapObject::setBoundaries(QRectF boundaryrect) -{ - boundaryrect_ = boundaryrect; - -} - -void OrientationControlledGraphicsPixmapObject::setObstacles(int key, QList values) +bool OrientationControlledGraphicsPixmapObject::handleCollisions() { - obstacleKey_ = key; - obstacleValues_ = values; + qDebug() << "I should not be here!"; + return true; } - - diff --git a/orientationcontrolledgraphicspixmapobject.h b/orientationcontrolledgraphicspixmapobject.h index 22435c5..5e2b6f7 100644 --- a/orientationcontrolledgraphicspixmapobject.h +++ b/orientationcontrolledgraphicspixmapobject.h @@ -21,23 +21,20 @@ public slots: void startMoving(); void stopMoving(); void readRotationSensor(); - void setBoundaries(QRectF boundaryrect); - /*! sets what QGraphicsItems to treat as obstacles not to move on top of - @param key The key to be used with QGraphicsItem::data() to find the correct value to compare - @param values The values received from QGraphicsItem::data() to treat as obstacles - */ - void setObstacles(int key, QList values); +protected: + /*! Returns true if the new position is to be maintained and false if a revert back to the old position is needed. + This stub always just returns true. Actual collision handling is left for subclasses to implement (by reimplementing this function). +*/ + virtual bool handleCollisions(); private: QRotationSensor rotationSensor_; - QRectF boundaryrect_; - int obstacleKey_; - QList obstacleValues_; + }; diff --git a/seascene.cpp b/seascene.cpp index 5c35e49..b330532 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -1,6 +1,6 @@ #include "seascene.h" #include "timercontrolledtursas.h" -#include "orientationcontrolledgraphicspixmapobject.h" +#include "ship.h" #include #include #include @@ -94,7 +94,7 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) return; } - OrientationControlledGraphicsPixmapObject * pShip = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/laiva.png")); + Ship * pShip = new Ship (QPixmap(":/pix/laiva.png")); pShip->setData(0,"ship"); pShip->setPos(*pPosition); addItem(pShip); diff --git a/ship.cpp b/ship.cpp new file mode 100644 index 0000000..6250522 --- /dev/null +++ b/ship.cpp @@ -0,0 +1,33 @@ +#include "ship.h" +#include + +Ship::Ship(QPixmap pixmap, QGraphicsItem *parent) : + OrientationControlledGraphicsPixmapObject(pixmap,parent) +{ + +} + +bool Ship::handleCollisions() +{ + QList collidesList = collidingItems(); + + if (collidesList.isEmpty()) + return true; + + else + { + QString type = collidesList.at(0)->data(0).toString(); + qDebug() << type; + if (type == "rock" || type == "octopus") + { +// dropGhosts(); + return false; + } + + else if (type == "ghost") + { + return true; + } + + } +} diff --git a/ship.h b/ship.h new file mode 100644 index 0000000..38138a2 --- /dev/null +++ b/ship.h @@ -0,0 +1,24 @@ +#ifndef SHIP_H +#define SHIP_H + +#include "orientationcontrolledgraphicspixmapobject.h" + +class Ship : public OrientationControlledGraphicsPixmapObject +{ + Q_OBJECT +public: + explicit Ship(QPixmap pixmap = 0, QGraphicsItem *parent = 0); + +signals: + +public slots: + +protected: + +protected: + bool handleCollisions(); + + +}; + +#endif // SHIP_H -- 1.7.9.5