Separated actual collision detection into subclass Ship
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 11:59:29 +0000 (14:59 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 11:59:29 +0000 (14:59 +0300)
Now uses the class Ship. The separation is to keep the base class
OrientationControlledGraphicsPixmapObject generic and reusable.

orientationcontrolledgraphicspixmapobject.cpp
orientationcontrolledgraphicspixmapobject.h
seascene.cpp
ship.cpp [new file with mode: 0644]
ship.h [new file with mode: 0644]

index cff72a6..af19e87 100644 (file)
@@ -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<QGraphicsItem*>  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<QVariant> values)
+bool OrientationControlledGraphicsPixmapObject::handleCollisions()
 {
-    obstacleKey_ = key;
-    obstacleValues_ = values;
+    qDebug() << "I should not be here!";
+    return true;
 }
 
-
-
index 22435c5..5e2b6f7 100644 (file)
@@ -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<QVariant> 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<QVariant> obstacleValues_;
+
 
 };
 
index 5c35e49..b330532 100644 (file)
@@ -1,6 +1,6 @@
 #include "seascene.h"
 #include "timercontrolledtursas.h"
-#include "orientationcontrolledgraphicspixmapobject.h"
+#include "ship.h"
 #include <QGraphicsPixmapItem>
 #include <QDebug>
 #include <QMessageBox>
@@ -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 (file)
index 0000000..6250522
--- /dev/null
+++ b/ship.cpp
@@ -0,0 +1,33 @@
+#include "ship.h"
+#include <QDebug>
+
+Ship::Ship(QPixmap pixmap, QGraphicsItem *parent) :
+    OrientationControlledGraphicsPixmapObject(pixmap,parent)
+{
+
+}
+
+bool Ship::handleCollisions()
+{
+    QList<QGraphicsItem*>  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 (file)
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