Level collections (draft).
[evilplumber] / src / game.cpp
index df29387..0aad055 100644 (file)
 #include "game.h"
 
 #include <QTableWidget>
-#include <QDebug>
+#include <QListWidget>
 #include <QLabel>
 #include <QFile>
 #include <QPushButton>
-#include <QFrame>
 #include <QApplication>
+#include <QDebug>
 
 
 QString pieceToIconId(const Piece* piece, bool flow1 = false, bool flow2 = false)
@@ -79,7 +79,6 @@ GameField::GameField(QTableWidget* ui)
 void GameField::initGame(int rows_, int cols_, int count, PrePlacedPiece* prePlaced)
 {
     fieldUi->clear();
-    // FIXME: Does the table widget call the destructors of its items...
 
     rows = rows_;
     cols = cols_;
@@ -202,8 +201,6 @@ void GameField::indicateFlow(int row, int col, Direction dir)
     QLabel* label = (QLabel*)fieldUi->indexWidget(mIndex);
 
     label->setPixmap(QPixmap(iconId));
-    // The pixmap won't show nicely if we're just sleeping...
-    QApplication::processEvents();
 }
 
 QHash<QPair<PieceType, int>, const Piece*> AvailablePieces::pieceCache;
@@ -532,33 +529,73 @@ void GameController::computeFlow()
         flowPreplaced += 1;
 }
 
-LevelSwitcher::LevelSwitcher(GameController* gameController, QLabel* levelLabel, 
-                             QFrame* startFrame, QLabel* startTitle, QLabel* startLabel, QPushButton* startButton,
-                             QLabel* scoreLabel,
-
-                             QStringList levels)
-    : gameController(gameController), levelLabel(levelLabel), 
-      startFrame(startFrame), startTitle(startTitle), startLabel(startLabel), startButton(startButton),
-      scoreLabel(scoreLabel),
-      levels(levels), level(0), totalScore(0)
+LevelSwitcher::LevelSwitcher(GameController* gameController,
+                             QWidget* levelWidget, QListWidget* levelList, 
+                             QPushButton* levelStartButton,
+                             QWidget* startWidget, QLabel* startTitle, 
+                             QLabel* startLabel, QPushButton* startButton,
+                             QLabel* levelLabel, QLabel* scoreLabel,
+                             QStringList levelCollections)
+    : gameController(gameController),
+      levelWidget(levelWidget), levelList(levelList), levelStartButton(levelStartButton),
+      startWidget(startWidget), startTitle(startTitle), startLabel(startLabel), startButton(startButton),
+      levelLabel(levelLabel), scoreLabel(scoreLabel),
+      levelCollections(levelCollections), level(0), totalScore(0)
 {
+    connect(levelStartButton, SIGNAL(clicked()), this, SLOT(onLevelCollectionChosen()));
+
     connect(startButton, SIGNAL(clicked()), this, SLOT(onStartClicked()));
     connect(gameController, SIGNAL(levelPassed(int)), this, SLOT(onLevelPassed(int)));
     connect(gameController, SIGNAL(levelFailed()), this, SLOT(onLevelFailed()));
     startTitle->setText("Starting a new game.");
     scoreLabel->setText("0");
+    chooseLevelCollection();
+}
+
+void LevelSwitcher::chooseLevelCollection()
+{
+    levelList->clear();
+    foreach (const QString& collection, levelCollections) {
+        QListWidgetItem *newItem = new QListWidgetItem();
+        newItem->setText(collection);
+        levelList->addItem(newItem); // transfers ownership
+    }
+    levelWidget->show();
+}
+
+void LevelSwitcher::onLevelCollectionChosen()
+{
+    levelWidget->hide();
+    QString collection = levelList->currentItem()->text();
+    QFile file(QString(LEVDIR) + "/" + collection + ".dat");
+    if (!file.exists())
+        qFatal("Error reading game file: doesn't exist");
+    file.open(QIODevice::ReadOnly);
+    QTextStream levelData(&file);
+    levels.clear();
+    
+    while (!levelData.atEnd())
+        levels << levelData.readLine();
+
+    level = 0;
+    totalScore = 0;
     initiateLevel();
 }
 
 void LevelSwitcher::onStartClicked()
 {
-    startFrame->hide();
+    startWidget->hide();
     levelLabel->setText(QString::number(level+1));
     gameController->startLevel(QString(LEVDIR) + "/" + levels[level] + ".dat");
 }
 
 void LevelSwitcher::initiateLevel()
 {
+    if (level >= levels.size()) {
+        qWarning() << "Level index too large";
+        return;
+    }
+
     QFile file(QString(LEVDIR) + "/" + levels[level] + ".leg");
     if (!file.exists())
         qFatal("Error reading game file: doesn't exist");
@@ -568,7 +605,7 @@ void LevelSwitcher::initiateLevel()
     QString introText = gameData.readLine();
     introText.replace("IMGDIR", IMGDIR);
     startLabel->setText(introText);
-    startFrame->show();
+    startWidget->show();
 }
 
 void LevelSwitcher::onLevelPassed(int score)
@@ -586,7 +623,7 @@ void LevelSwitcher::onLevelPassed(int score)
         startLabel->setText("Start a new game?");
         // TODO: go to the level set selection screen
         level = 0;
-        startFrame->show();
+        startWidget->show();
     }
 }
 
@@ -597,14 +634,13 @@ void LevelSwitcher::onLevelFailed()
 }
 
 // Todo next:
-// desktop stuff
-// icon for app manager
-// install all graphics
 // better graphics
 // save & load
 // level collections: introduction + basic
 // more levels
 // make fixed pipes look different than non-fixed ones
+// color theme
+// transparency
 // --------------
 // re-placing pieces
 // graphical hints on what to do next