Seascene map setup places rocks, octopuses and the ship now
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 10:43:42 +0000 (13:43 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 10:43:42 +0000 (13:43 +0300)
Also removed placing them from MainWindow.

mainwindow.cpp
seascene.cpp
seascene.h

index d292082..e4ab78a 100644 (file)
@@ -19,9 +19,6 @@ MainWindow::MainWindow(QWidget *parent)
     QPixmap waves (":/pix/meri.png");
     pScene_->setBackgroundBrush(QBrush(waves));
 
-    pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/laiva.png"));
-    pScene_->addItem(pTursas_);
-
     pView_->setScene(pScene_);
     setCentralWidget(pView_);
 
@@ -31,29 +28,6 @@ MainWindow::MainWindow(QWidget *parent)
     QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
 
 
-    QAction * pPauseAction = new QAction(tr("Pause"),this);
-    pPauseAction->setCheckable(true);
-    addAction(pPauseAction);
-    connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
-    menuBar()->addAction(pPauseAction);
-
-//    QGraphicsPixmapItem * pGhost = pScene_->addPixmap(QPixmap(":/pix/aave.png"));
-//    pGhost->setData(0,"ghost");
-    QGraphicsPixmapItem * pRock =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
-        QGraphicsPixmapItem * pRock2 =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
-    pRock->moveBy(40,40);
-    pRock->setData(0,"rock");
-    pRock2->moveBy(80,80);
-    pRock2->setData(0,"rock");
-
-    TimerControlledTursas * pMustekala = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
-    pScene_->addItem(pMustekala);
-    pMustekala->moveBy(100,100);
-    pMustekala->startMoving();
-
-
-
-
 }
 
 MainWindow::~MainWindow()
@@ -73,8 +47,6 @@ void MainWindow::initializeBoundaries()
 
     pScene_->setSceneRect(rectangle);
     pView_->setSceneRect(rectangle);
-    pTursas_->setBoundaries(rectangle);
-    pTursas_->startMoving();
 
     qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width();
 
index 963317f..5c35e49 100644 (file)
@@ -1,6 +1,9 @@
 #include "seascene.h"
+#include "timercontrolledtursas.h"
+#include "orientationcontrolledgraphicspixmapobject.h"
 #include <QGraphicsPixmapItem>
 #include <QDebug>
+#include <QMessageBox>
 
 const QString ghostImageFilename_ = ":/pix/aave.png";
 const QString rockImageFilename_ =":/pix/kari.png";
@@ -34,9 +37,72 @@ void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
             freeTiles_.append(QPointF(i*40,j*40));
         }
     }
+
+
+    //spread the rocks
+
+    for (int i = 0; i < rocks; i++)
+    {
+        QPointF * pPosition = findRandomFreeSlot();
+
+        //If there was no room no point to continue
+        if (pPosition == NULL)
+            break;
+
+        QGraphicsPixmapItem * pRock = addPixmap(QPixmap(":/pix/kari.png"));
+        pRock->setData(0,"rock");
+        pRock->setPos(*pPosition);
+        delete pPosition;
+
+    }
+
+    //spread the ghosts
+
     spreadGhosts(ghosts);
+
+
+
+    //spread the octopuses
+
+
+    for (int i=0; i < octopuses; i++)
+    {
+        QPointF * pPosition = findRandomFreeSlot();
+
+        //If there was no room no point to continue
+        if (pPosition == NULL)
+            break;
+
+    TimerControlledTursas * pOctopus = new TimerControlledTursas (QPixmap(":/pix/tursas.png"),100);
+    pOctopus->setData(0,"octopus");
+    pOctopus->setPos(*pPosition);
+    addItem(pOctopus);
+    pOctopus->startMoving();
+    delete pPosition;
+
+    }
+
+
+    //place the ship
+
+    QPointF * pPosition = findRandomFreeSlot();
+    if (pPosition == NULL)
+    {
+        // Game cannot begin without a free position for ship, so give an error message and return
+
+        QMessageBox::critical(NULL,"Error! Too many objects on screen","No free space to place the ship. The game cannot start. Please choose another level.");
+        return;
+    }
+
+    OrientationControlledGraphicsPixmapObject * pShip = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/laiva.png"));
+    pShip->setData(0,"ship");
+    pShip->setPos(*pPosition);
+    addItem(pShip);
+    pShip->startMoving();
+    delete pPosition;
 }
 
+
 void SeaScene::spreadGhosts(int ghosts)
 {
     for (int i=0; i < ghosts; i++)
index 9abc977..bb97911 100644 (file)
@@ -13,6 +13,12 @@ signals:
 
 public slots:
 
+    /*! Places all needed items for a level to (re)start.
+    Relies on the scene size to be explicitly set to a a value that will persist troughout play.
+    @param ghosts the number of ghosts to be placed on the map
+    @param rocks the number of rocks to be placed on the map
+    @param octopuses number of octopuses to be placed on the map
+    */
     void setupMap(int ghosts, int rocks, int octopuses);
 
     void spreadGhosts(int ghosts);