SeaScene with randomized ghost positions
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Sun, 5 Jun 2011 19:56:20 +0000 (22:56 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Sun, 5 Jun 2011 19:56:20 +0000 (22:56 +0300)
mainwindow.cpp
mainwindow.h
rotationcontrol2.pro
seascene.cpp [new file with mode: 0644]
seascene.h [new file with mode: 0644]
timercontrolledtursas.cpp

index c508710..d292082 100644 (file)
@@ -13,13 +13,13 @@ MainWindow::MainWindow(QWidget *parent)
 {
     paused_ = false;
 
-    pScene_ = new QGraphicsScene ();
+    pScene_ = new SeaScene ();
     pView_  = new QGraphicsView ();
 
     QPixmap waves (":/pix/meri.png");
     pScene_->setBackgroundBrush(QBrush(waves));
 
-    pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/tursas.png"));
+    pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/laiva.png"));
     pScene_->addItem(pTursas_);
 
     pView_->setScene(pScene_);
@@ -37,8 +37,8 @@ MainWindow::MainWindow(QWidget *parent)
     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 * 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);
@@ -77,6 +77,8 @@ void MainWindow::initializeBoundaries()
     pTursas_->startMoving();
 
     qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width();
+
+    pScene_->setupMap(5,5,5);
 }
 
 void MainWindow::pause(bool paused)
index 58d2a4e..6a708e7 100644 (file)
@@ -4,6 +4,7 @@
 #include <QtGui/QMainWindow>
 #include <QGraphicsView>
 #include "orientationcontrolledgraphicspixmapobject.h"
+#include "seascene.h"
 
 class MainWindow : public QMainWindow
 {
@@ -19,7 +20,7 @@ public slots:
 
 private:
 
-QGraphicsScene * pScene_;
+SeaScene * pScene_;
 QGraphicsView * pView_;
 OrientationControlledGraphicsPixmapObject * pTursas_;
 bool paused_;
index 4dac436..68072d7 100644 (file)
@@ -13,11 +13,15 @@ TEMPLATE = app
 SOURCES += main.cpp\
         mainwindow.cpp \
     orientationcontrolledgraphicspixmapobject.cpp \
-    timercontrolledtursas.cpp
+    timercontrolledtursas.cpp \
+    seascene.cpp \
+    seaview.cpp
 
 HEADERS  += mainwindow.h \
     orientationcontrolledgraphicspixmapobject.h \
-    timercontrolledtursas.h
+    timercontrolledtursas.h \
+    seascene.h \
+    seaview.h
 
 CONFIG += mobility
 MOBILITY = sensors
@@ -40,3 +44,9 @@ unix:!symbian {
 
 RESOURCES += \
     orientationcontrol2pix.qrc
+
+maemo5 {
+    desktopfile.files = $${TARGET}.desktop
+    desktopfile.path = /usr/share/applications/hildon
+    INSTALLS += desktopfile
+}
diff --git a/seascene.cpp b/seascene.cpp
new file mode 100644 (file)
index 0000000..eaaef92
--- /dev/null
@@ -0,0 +1,59 @@
+#include "seascene.h"
+#include <QGraphicsPixmapItem>
+#include <QDebug>
+
+const QString ghostImageFilename_ = ":/pix/aave.png";
+const QString rockImageFilename_ =":/pix/kari.png";
+const QString octopusImageFilename_= ":/pix/tursas.png";
+
+
+SeaScene::SeaScene(QObject *parent) :
+    QGraphicsScene(parent)
+{
+
+
+}
+
+void SeaScene::setupMap(int ghosts, int rocks, int octopuses)
+{
+    //empty the list of free slots
+    freeTiles_.clear();
+
+    //fill the list of free slots
+
+    int numberOfXTiles  = width() / 40;
+    int numberOfYTiles = height() /40;
+
+    qDebug() << numberOfXTiles << " slots in x direction";
+    qDebug() << numberOfYTiles << " slots in y rirection";
+
+    for (int i = 0; i < numberOfXTiles; i++ )
+    {
+        for (int j = 0; j < numberOfYTiles; j++)
+        {
+            freeTiles_.append(QPointF(i*40,j*40));
+        }
+    }
+    spreadGhosts(ghosts);
+}
+
+void SeaScene::spreadGhosts(int ghosts)
+{
+    for (int i=0; i < ghosts; i++)
+    {
+        QPointF position = findRandomFreeSlot();
+
+        QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png"));
+        pGhost->setData(0,"ghost");
+        pGhost->setPos(position);
+    }
+}
+
+QPointF SeaScene::findRandomFreeSlot()
+{
+    int index = qrand()%freeTiles_.size();
+
+    qDebug()  << index << " index";
+    return freeTiles_.takeAt(index);
+
+}
diff --git a/seascene.h b/seascene.h
new file mode 100644 (file)
index 0000000..bfeb5cb
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef SEASCENE_H
+#define SEASCENE_H
+
+#include <QGraphicsScene>
+
+class SeaScene : public QGraphicsScene
+{
+    Q_OBJECT
+public:
+    explicit SeaScene(QObject *parent = 0);
+
+signals:
+
+public slots:
+
+    void setupMap(int ghosts, int rocks, int octopuses);
+
+    void spreadGhosts(int ghosts);
+
+protected:
+
+    QPointF findRandomFreeSlot();
+
+    const QString ghostImageFilename_;
+    const QString rockImageFilename_;
+    const QString octopusImageFilename_;
+    const QStringList shipImageFilenames_;
+
+    QList<QPointF> freeTiles_;
+
+
+
+
+};
+
+#endif // SEASCENE_H
index 520b94c..317bab9 100644 (file)
@@ -61,6 +61,7 @@ void TimerControlledTursas::move()
 
 
     //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;