A more sane approach to flow.
authorMarja Hassinen <marja2@Vahvero.(none)>
Mon, 11 Jan 2010 19:46:11 +0000 (21:46 +0200)
committerMarja Hassinen <marja2@Vahvero.(none)>
Mon, 11 Jan 2010 19:46:11 +0000 (21:46 +0200)
src/game.cpp

index 502771d..36e7821 100644 (file)
@@ -25,6 +25,7 @@
 #include <QFile>
 #include <QDir>
 #include <QDebug>
+#include <QPainter>
 
 const Piece* findPiece(PieceType type, int rotation)
 {
@@ -79,7 +80,7 @@ GameField::GameField(QTableWidget* ui)
     connect(fieldUi, SIGNAL(cellClicked(int, int)), this, SIGNAL(cellClicked(int, int)));
     fieldUi->setContentsMargins(0, 0, 0, 0);
 
-    flowTimer.setInterval(200);
+    flowTimer.setInterval(100);
     flowTimer.setSingleShot(false);
     connect(&flowTimer, SIGNAL(timeout()), this, SLOT(showFlow()));
     connect(this, SIGNAL(flowIndicated()), &flowTimer, SLOT(stop()));
@@ -207,15 +208,15 @@ void GameField::indicateFlow(int row, int col, Direction dirFrom, Direction dirT
 
 void GameField::showFlow()
 {
+    qDebug() << "show flow";
     static bool animateNow = false;
-    static int y = 0, yMin = 0, yMax = 0, yTarget = 0, yDelta = 0;
-    static int x = 0, xMin = 0, xMax = 0, xTarget = 0, xDelta = 0;
+    static QList<int> xs, ys;
+    static int i = 0;
     static QImage image;
+    //static QPainter painter;
     static QLabel* label = 0;
     static QRgb value = qRgb(40, 125, 169);
 
-    qDebug() << "show flow" << y << xMin << xMax << yTarget << yDelta;
-
     int index = toIndex(flowRow, flowCol);
 
     // Special cases: the liquid spills and doesn't flow
@@ -236,60 +237,93 @@ void GameField::showFlow()
 
         QString iconId = pieceToIconId(field[index].piece);
         image = QImage(iconId);
+        //painter.end();
+        //painter.begin(&image);
+        xs.clear();
+        ys.clear();
+        i = 0;
+
         if (flowFrom == DirUp || flowFrom == DirDown) {
-            xMin = 28;
-            xMax = 42;
-            if (flowFrom == DirUp) { y = 10; yDelta = 1; yTarget = 60;}
-            else { y = 60; yDelta = -1; yTarget = 10;} 
+            qDebug() << "Ind flow inserting";
+            // 50 = 15 straight + 20 turning + 15 straight
+            // Flow first straight... 10 -> 25
+            int x = 35;
+            int xDelta = 0;
+            int xTarget = x;
+            int y = (flowFrom == DirUp ? 10 : 60);
+            int yDelta = (flowFrom == DirUp ? 1 : -1);
+            int yTarget = (flowFrom == DirUp ? 25 : 45);
+
+            while (y != yTarget || x != xTarget) {
+                xs.push_back(x);
+                ys.push_back(y);
+                qDebug() << "straight 1" << x << y;
+                y += yDelta;
+            }
+
+            // Then turn if needed... 25 -> 45
+            yTarget = 35;
+            if (flowTo == DirRight) {
+                xDelta = 1;
+                xTarget = 45;
+            }
+            else if (flowTo == DirLeft) {
+                xDelta = -1;
+                xTarget = 25;
+            }
+            
+            while (y != yTarget || x != xTarget) {
+                xs.push_back(x);
+                ys.push_back(y);
+                qDebug() << "turning" << x << y;
+                qDebug() << xTarget << yTarget << xDelta << yDelta;
+                y += yDelta;
+                x += xDelta;
+            }
+
+            // Then go straight again
+            if (flowTo == DirRight || flowTo == DirLeft) { 
+                yTarget = y;
+                xTarget = (flowTo == DirLeft ? 10 : 60); 
+                yDelta = 0;
+            }
+            else {
+                yTarget = (flowFrom == DirUp ? 60 : 10);
+            }
+            
+            while (y != yTarget || x != xTarget) {
+                xs.push_back(x);
+                ys.push_back(y);
+                qDebug() << "straight 2" << x << y;
+                y += yDelta;
+                x += xDelta;
+            }
+
         }
         else if (flowFrom == DirLeft || flowFrom == DirRight) {
-            yMin = 28;
-            yMax = 42;
-            if (flowFrom == DirLeft) { x = 10; xDelta = 1; xTarget = 60;}
-            else { x = 60; xDelta = -1; xTarget = 10;} 
+            emit flowIndicated();
+            return;
         }
         else {
             // hack for starting
             emit flowIndicated();
             return;
         }
-        qDebug() << y << yTarget << yDelta << xMin << xMax;
-        qDebug() << x << xTarget << xDelta << yMin << yMax;
         animateNow = true;
     }
     else {
-        qDebug() << "animating" << y << xMin << xMax << yTarget << yDelta;
-        qDebug() << "flow dirs" << flowFrom << flowTo;
+        QPainter painter(&image);
         // The real animation
-        if (flowFrom == DirUp || flowFrom == DirDown) {
-            if (y != yTarget) {
-                // Draw one line
-                for (int x = xMin; x <= xMax; ++x) {
-                    image.setPixel(x, y, value);
-                }
-                y += yDelta;
-                qDebug() << "setting image";
-                label->setPixmap(QPixmap::fromImage(image));
-            }
-            else {
-                animateNow = false;
-                emit flowIndicated();
-            }
+        qDebug() << "show flow animating" << i << xs.size();
+        if (i < xs.size()) {
+            int size = 8;
+            painter.drawEllipse(xs[i] - size, ys[i] - size, 2*size, 2*size);
+            label->setPixmap(QPixmap::fromImage(image));
+            ++i;
         }
         else {
-            if (x != xTarget) {
-                // Draw one line
-                for (int y = yMin; y <= yMax; ++y) {
-                    image.setPixel(x, y, value);
-                }
-                x += xDelta;
-                qDebug() << "setting image";
-                label->setPixmap(QPixmap::fromImage(image));
-            }
-            else {
-                animateNow = false;
-                emit flowIndicated();
-            }
+            animateNow = false;
+            emit flowIndicated();
         }
     }
 }
@@ -587,10 +621,6 @@ void GameController::computeFlow()
     qDebug() << "compute flow" << flowRow << flowCol;
     printFlow(flowDirFrom);
     printFlow(flowDirTo);
-    // We know:
-    // Where the flow currently is
-    // and which direction the flow goes after that piece
-    fieldUi->indicateFlow(flowRow, flowCol, flowDirFrom, flowDirTo);
 
     if (flowDirFrom == DirFailed) {
         flowTimer.stop();
@@ -601,6 +631,7 @@ void GameController::computeFlow()
     if (flowDirFrom == DirPassed) {
         flowTimer.stop();
         emit levelPassed(flowScore);
+        return;
     }
 
     if (flowDirTo == DirNone) {
@@ -613,6 +644,11 @@ void GameController::computeFlow()
     }
     flowScore += 10;
 
+    // We know:
+    // Where the flow currently is
+    // and which direction the flow goes after that piece
+    fieldUi->indicateFlow(flowRow, flowCol, flowDirFrom, flowDirTo);
+
     if (flowDirTo == DirDone) {
         // Again, give the user some time...
         if (flowPreplaced < neededFlow) {