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;
}
-
-
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_;
+
};
#include "seascene.h"
#include "timercontrolledtursas.h"
-#include "orientationcontrolledgraphicspixmapobject.h"
+#include "ship.h"
#include <QGraphicsPixmapItem>
#include <QDebug>
#include <QMessageBox>
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);
--- /dev/null
+#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;
+ }
+
+ }
+}
--- /dev/null
+#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