From 6f572ab2c31518920272aa5ee40a19e1ef7997fe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Heli=20Hyv=C3=A4ttinen?= Date: Mon, 20 Jun 2011 20:35:06 +0300 Subject: [PATCH] Class restructure for octopus Class TimerControlledTursas replaced by class Octopus that derives from the class TimerControlledGraphicsPixmapObject (where most of the contents of TimerControlledTursas now are). Also updated UML model. Some work towards dropping the ghosts when the ship is hit by an octopus. --- Ghost Ship UML.xmi | 133 +++++++++++++++++++++------ ghostsoverboard.pro | 8 +- octopus.cpp | 26 ++++++ octopus.h | 24 +++++ seascene.cpp | 10 +- ship.cpp | 6 ++ ship.h | 2 + timercontrolledgraphicspixmapobject.cpp | 152 +++++++++++++++++++++++++++++++ timercontrolledgraphicspixmapobject.h | 47 ++++++++++ 9 files changed, 376 insertions(+), 32 deletions(-) create mode 100644 octopus.cpp create mode 100644 octopus.h create mode 100644 timercontrolledgraphicspixmapobject.cpp create mode 100644 timercontrolledgraphicspixmapobject.h diff --git a/Ghost Ship UML.xmi b/Ghost Ship UML.xmi index 495f257..7a73607 100644 --- a/Ghost Ship UML.xmi +++ b/Ghost Ship UML.xmi @@ -1,5 +1,5 @@ - + umbrello uml modeller http://uml.sf.net @@ -78,7 +78,7 @@ - + @@ -93,6 +93,11 @@ + + + + + @@ -194,6 +199,18 @@ + + + + + + + + + + + + @@ -212,6 +229,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -221,14 +271,15 @@ - - + + - + + @@ -252,22 +303,16 @@ - + - + - - - - - - @@ -286,9 +331,9 @@ - - - + + + @@ -314,7 +359,7 @@ - + @@ -322,10 +367,32 @@ - + + + + + + + + + + + + + + + + + + + + + + + @@ -347,7 +414,7 @@ - + @@ -356,16 +423,10 @@ - - + + + - - - - - - - @@ -373,18 +434,34 @@ + + + + + + + + + + + + + + + + diff --git a/ghostsoverboard.pro b/ghostsoverboard.pro index 70fe129..f847c88 100644 --- a/ghostsoverboard.pro +++ b/ghostsoverboard.pro @@ -17,7 +17,9 @@ SOURCES += main.cpp\ seascene.cpp \ seaview.cpp \ ship.cpp \ - screenlitkeeper.cpp + screenlitkeeper.cpp \ + timercontrolledgraphicspixmapobject.cpp \ + octopus.cpp HEADERS += mainwindow.h \ orientationcontrolledgraphicspixmapobject.h \ @@ -25,7 +27,9 @@ HEADERS += mainwindow.h \ seascene.h \ seaview.h \ ship.h \ - screenlitkeeper.h + screenlitkeeper.h \ + timercontrolledgraphicspixmapobject.h \ + octopus.h CONFIG += mobility MOBILITY = sensors diff --git a/octopus.cpp b/octopus.cpp new file mode 100644 index 0000000..2efc346 --- /dev/null +++ b/octopus.cpp @@ -0,0 +1,26 @@ +#include "octopus.h" + +Octopus::Octopus(QPixmap pixmap, int speed, QGraphicsItem *parent) : + TimerControlledGraphicsPixmapObject(pixmap,speed,parent) +{ +} + +bool Octopus::handleCollisions() +{ + + QList collidesList = collidingItems(); + if (collidesList.isEmpty()) + return true; //retain new position + + else + { + //change direction if hit anything + changeDirection(); + //the game allows only one object to saty at given spot, so just check the firs one + if (collidesList.at(0)->data(0) == "ship") + { + emit droppingGhosts(); + } + return false; //go back to old position + } +} diff --git a/octopus.h b/octopus.h new file mode 100644 index 0000000..2e5fcb6 --- /dev/null +++ b/octopus.h @@ -0,0 +1,24 @@ +#ifndef OCTOPUS_H +#define OCTOPUS_H + +#include "timercontrolledgraphicspixmapobject.h" + +class Octopus : public TimerControlledGraphicsPixmapObject +{ + Q_OBJECT +public: + explicit Octopus(QPixmap pixmap, int speed = 10, QGraphicsItem *parent = 0); + +signals: + + void droppingGhosts(); + +public slots: + +protected: + + virtual bool handleCollisions(); + +}; + +#endif // OCTOPUS_H diff --git a/seascene.cpp b/seascene.cpp index 0e4ff6c..2986f2d 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -1,5 +1,5 @@ #include "seascene.h" -#include "timercontrolledtursas.h" +#include "octopus.h" #include "ship.h" #include #include @@ -88,6 +88,7 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) //spread the octopuses + QList octopuses; for (int i=0; i < octopuses; i++) { @@ -98,7 +99,7 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) break; QPixmap octopusPixmap (":/pix/tursas.png"); - TimerControlledTursas * pOctopus = new TimerControlledTursas (octopusPixmap,100); + Octopus * pOctopus = new Octopus(octopusPixmap,100); pOctopus->setData(0,"octopus"); pOctopus->setPos(*pPosition); addItem(pOctopus); @@ -106,6 +107,7 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) movingItems_.append(pOctopus); connect(this,SIGNAL(pauseOn()),pOctopus,SLOT(stopMoving())); connect(this,SIGNAL(pauseOff()),pOctopus,SLOT(startMoving())); + octopuses.append(pOctopus); delete pPosition; } @@ -145,6 +147,10 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses) movingItems_.append(pShip); connect(this,SIGNAL(pauseOn()),pShip,SLOT(stopMoving())); connect(this,SIGNAL(pauseOff()),pShip,SLOT(startMoving())); + foreach (Octopus* pOctopus, octopuses) + { + connect(pOctopus,SIGNAL(droppingGhosts()),pShip,SLOT(dropAllGhosts())); + } delete pPosition; } diff --git a/ship.cpp b/ship.cpp index 5c353d8..1e80194 100644 --- a/ship.cpp +++ b/ship.cpp @@ -53,3 +53,9 @@ void Ship::updateShipImage() int index = qBound(0,ghostsAboard_,shipImages_.length()-1); setPixmap(shipImages_.at(index)); } + +void Ship::dropAllGhosts() +{ +//TODO +} + diff --git a/ship.h b/ship.h index 47c4cff..cbc3cc8 100644 --- a/ship.h +++ b/ship.h @@ -22,6 +22,8 @@ signals: public slots: + void dropAllGhosts(); + protected: protected: diff --git a/timercontrolledgraphicspixmapobject.cpp b/timercontrolledgraphicspixmapobject.cpp new file mode 100644 index 0000000..e6b838c --- /dev/null +++ b/timercontrolledgraphicspixmapobject.cpp @@ -0,0 +1,152 @@ +#include "timercontrolledgraphicspixmapobject.h" +#include +#include + + +TimerControlledGraphicsPixmapObject::TimerControlledGraphicsPixmapObject(QPixmap pixmap, int speed, QGraphicsItem* parent) : + QObject(), QGraphicsPixmapItem(pixmap, parent) +{ + setSpeed(speed); + direction_ = S; + connect(&timer_,SIGNAL(timeout()),this,SLOT(move())); +} + +void TimerControlledGraphicsPixmapObject::startMoving() +{ + timer_.start(); +} + +void TimerControlledGraphicsPixmapObject::stopMoving() +{ + timer_.stop(); +} + +void TimerControlledGraphicsPixmapObject::setSpeed(int speed) +{ + timer_.setInterval(1000/speed); //converts from pixels in second to milliseconds per pixel +} + +void TimerControlledGraphicsPixmapObject::move() +{ + + int oldx = x(); + int oldy = y(); + + int newx = oldx; + int newy = oldy; + + + //calculate the new position + + if (direction_ == E || direction_ == SE || direction_ == NE) + { + newx++; + } + + if (direction_ == W || direction_ == SW || direction_ == NW) + { + newx--; + } + + if (direction_ == S || direction_ == SE || direction_ == SW) + { + newy++; + } + + if (direction_ == N || direction_ == NE || direction_ == NW) + { + newy--; + } + + + + //Bound the item into the scene and change direction if hitting a boundary + //Only works if the old position is inside the boundaries + + if (!scene()) //no movement if this item does not belong to a scene + return; + + QRect sceneRectangle = scene()->sceneRect().toRect(); + + if (newx < sceneRectangle.left() || newx > sceneRectangle.right()-40) + { + changeDirection(); + return; + } + + + if (newy < sceneRectangle.top() || newy > sceneRectangle.bottom()-40) + { + changeDirection(); + return; //the old x and y values remain intact + } + + + //Set the new position + + setX(newx); + setY(newy); + + + //check for collisions and handle them (up to subclassess to implement) + //return to the old position if requested + + if (handleCollisions() == false) + { + setX(oldx); + setY(oldy); + } + + +} + +void TimerControlledGraphicsPixmapObject::changeDirection() +{ + qDebug () << "Supposed to change direction"; + + int direction = (qrand()%8); + qDebug() << direction; + + switch (direction) + { + case 0: + direction_ = S; + break; + + case 1: + direction_ = SW; + break; + + case 2: + direction_ = W; + break; + + case 3: + direction_ = NW; + break; + + case 4: + direction_ = N; + break; + + case 5: + direction_ = NE; + break; + + case 6: + direction_ = E; + break; + + case 7: + direction_ = SE; + break; + + + } + +} + +bool TimerControlledGraphicsPixmapObject::handleCollisions() +{ + return true; +} diff --git a/timercontrolledgraphicspixmapobject.h b/timercontrolledgraphicspixmapobject.h new file mode 100644 index 0000000..da179f7 --- /dev/null +++ b/timercontrolledgraphicspixmapobject.h @@ -0,0 +1,47 @@ +#ifndef TIMERCONTROLLEDGRAPHICSPIXMAPOBJECT_H +#define TIMERCONTROLLEDGRAPHICSPIXMAPOBJECT_H + +#include +#include +#include + +class TimerControlledGraphisPixmapObject : public QObject, public QGraphicsPixmapItem +{ + Q_OBJECT +public: + explicit TimerControlledGraphicsPixmapObject(QPixmap pixmap = QPixmap(), int speed = 1, QGraphicsItem *parent = 0); + + +signals: + +public slots: + + void startMoving(); + void stopMoving(); + + /*! Intended to be used internally by connecting to a timer + */ + void move(); + + /*! Sets the movement speed of the item + @param speed given in pixels per second + */ + void setSpeed(int speed); + + + protected: + + void changeDirection(); + + virtual bool handleCollisions(); + + QTimer timer_; + + enum direction {N, NE, E, SE, S, SW, W, NW}; + + direction direction_; + + +}; + +#endif // TIMERCONTROLLEDGRAPHICSPIXMAPOBJECT_H -- 1.7.9.5