Doc: Add FOV to rogue example.
authorGeir Vattekar <geir.vattekar@nokia.com>
Wed, 15 Aug 2012 13:33:13 +0000 (15:33 +0200)
committerQt by Nokia <qt-info@nokia.com>
Sat, 18 Aug 2012 19:08:33 +0000 (21:08 +0200)
Change-Id: Ib0f4caad9bc828a94616466f303d879fc967970d
Reviewed-by: Casper van Donderen <casper.vandonderen@nokia.com>
examples/statemachine/rogue/window.cpp

index 5fcd259..0c57b2c 100644 (file)
@@ -107,10 +107,59 @@ void Window::paintEvent(QPaintEvent * /* event */)
                 continue;
             }
 
+            painter.setPen(Qt::white);
+
+            double x1 = static_cast<double>(pX);
+            double y1 = static_cast<double>(pY);
+            double x2 = static_cast<double>(x);
+            double y2 = static_cast<double>(y);
+
+            if (x2<x1) {
+                x2+=0.5;
+            } else if (x2>x1) {
+                 x2-=0.5;
+            }
+
+            if (y2<y1) {
+                 y2+=0.5;
+            } else if (y2>y1) {
+                 y2-=0.5;
+            }
+
+            double dx = x2 - x1;
+            double dy = y2 - y1;
+
+            double length = qSqrt(dx*dx+dy*dy);
+
+            dx /= length;
+            dy /= length;
+
+            double xi = x1;
+            double yi = y1;
+
+            while (length > 0) {
+                int cx = static_cast<int>(xi+0.5);
+                int cy = static_cast<int>(yi+0.5);
+
+                if (x2 == cx && y2 == cy)
+                    break;
+
+                if (!(x1==cx && y1==cy)
+                    && (map[cx][cy] == '#' || (length-10) > 0)) {
+                    painter.setPen(QColor(60,60,60));
+                    break;
+                }
+
+                xi += dx;
+                yi += dy;
+                --length;
+            }
+
             painter.drawText(QPoint(xPos, yPos), map[x][y]);
             xPos += fontWidth;
         }
     }
+    painter.setPen(Qt::white);
     painter.drawText(QPoint(pX * fontWidth, (pY + 2) * fontHeight), QChar('@'));
 }