1 /* Evil Plumber is a small puzzle game.
2 Copyright (C) 2010 Marja Hassinen
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <QStringList>
27 class QTableWidgetItem;
68 static const Piece ppieces[] = {
69 {PieceNone, 0, false, 0, 0, // 0
70 {DirNone, DirNone, DirNone, DirNone}},
71 {PieceStraight, 0, true, 0, 0, // 1
72 {DirUp, DirDown, DirNone, DirNone}},
73 {PieceStraight, 90, true, 0, 1, // 2
74 {DirLeft, DirRight, DirNone, DirNone}},
75 {PieceCorner, 270, true, 1, 0, // 3
76 {DirDown, DirRight, DirNone, DirNone}},
77 {PieceCorner, 0, true, 1, 1, // 4
78 {DirLeft, DirDown, DirNone, DirNone}},
79 {PieceCorner, 180, true, 2, 0, // 5
80 {DirUp, DirRight, DirNone, DirNone}},
81 {PieceCorner, 90, true, 2, 1, // 6
82 {DirLeft, DirUp, DirNone, DirNone}},
83 {PieceCross, 0, true, 3, 0, // 7
84 {DirLeft, DirRight, DirUp, DirDown}},
85 {PieceCorners, 0, true, 4, 0, // 8
86 {DirLeft, DirDown, DirRight, DirUp}},
87 {PieceCorners, 90, true, 4, 1, // 9
88 {DirRight, DirDown, DirLeft, DirUp}},
89 {PieceStart, 0, false, 0, 0, // 10
90 {DirLeft, DirNone, DirNone, DirNone}},
91 {PieceStart, 90, false, 0, 0, // 11
92 {DirUp, DirNone, DirNone, DirNone}},
93 {PieceStart, 180, false, 0, 0, // 12
94 {DirRight, DirNone, DirNone, DirNone}},
95 {PieceStart, 270, false, 0, 0, // 13
96 {DirDown, DirNone, DirNone, DirNone}},
97 {PieceEnd, 0, false, 0, 0, // 14
98 {DirLeft, DirDone, DirNone, DirNone}},
99 {PieceEnd, 90, false, 0, 0, // 15
100 {DirUp, DirDone, DirNone, DirNone}},
101 {PieceEnd, 180, false, 0, 0, // 16
102 {DirRight, DirDone, DirNone, DirNone}},
103 {PieceEnd, 270, false, 0, 0, // 17
104 {DirDown, DirDone, DirNone, DirNone}},
105 {PieceBlock, 0, false, 0, 0, // 18
106 {DirNone, DirNone, DirNone, DirNone}},
107 {PiecesEnd, 0, false, 0, 0, // 19
108 {DirNone, DirNone, DirNone, DirNone}}};
110 const int noPieces = 20;
112 typedef struct _PlacedPiece
116 bool flow[2]; // which directions the liquid has already flown
119 typedef struct _PrePlacedPiece
126 typedef struct _AvailablePiece
132 class GameField : public QObject
136 GameField(QTableWidget* ui);
137 void initGame(int rows_, int cols_, int count, PrePlacedPiece* prePlaced);
139 bool setPiece(int row, int col, const Piece* piece, bool fixed = false);
140 const Piece* pieceAt(int row, int col);
141 bool isPrePlaced(int row, int col);
142 void indicateFlow(int row, int col, Direction dir);
145 void cellClicked(int, int);
148 int toIndex(int row, int col);
150 QTableWidget* fieldUi;
156 class AvailablePieces : public QObject
160 AvailablePieces(QTableWidget* pieceTable);
161 void initGame(int count, AvailablePiece* pieces);
164 void validPieceSelected(const Piece* piece);
165 void invalidPieceSelected();
168 void onPieceUsed(const Piece* piece);
171 void onItemClicked(QTableWidgetItem* item);
174 static int pieceToId(const Piece* piece);
175 static const Piece* idToPiece(int id);
177 QTableWidget* pieceUi;
178 QHash<const Piece*, int> pieceCounts;
181 class GameController : public QObject
185 GameController(AvailablePieces* pieceUi, GameField* fieldUi,
186 QLabel* timeLabel, QPushButton* doneButton);
187 void startLevel(QString fileName);
190 void pieceUsed(const Piece* piece);
191 void levelPassed(int score);
195 void onCellClicked(int row, int column);
196 void onValidPieceSelected(const Piece* piece);
197 void onInvalidPieceSelected();
203 AvailablePieces* pieceUi; // Not owned
204 GameField* fieldUi; // Not owned
205 QLabel* timeLabel; // Not owned
206 QPushButton* doneButton; // Not owned
208 // Data about the current situation in the game
209 const Piece* currentPiece;
215 // how many times does the liquid need to flow through the pre-placed pieces
227 class LevelSwitcher : public QObject
231 LevelSwitcher(GameController* gameController,
232 QWidget* levelWidget, QListWidget* levelList, QPushButton* levelStartButton,
233 QWidget* startWidget, QLabel* startTitle, QLabel* startLabel, QPushButton* startButton,
234 QLabel* levelLabel, QLabel* scoreLabel,
238 void onLevelCollectionChosen();
239 void onStartClicked();
240 void onLevelPassed(int score);
241 void onLevelFailed();
242 void chooseLevelCollection();
245 void initiateLevel();
246 void readSavedGames();
247 void writeSavedGames();
249 GameController* gameController; // Not owned
250 QWidget* levelWidget; // Not owned
251 QListWidget* levelList; // Not owned
252 QPushButton* levelStartButton; // Not owned
253 QWidget* startWidget; // Not owned
254 QLabel* startTitle; // Not owned
255 QLabel* startLabel; // Not owned
256 QPushButton* startButton; // Not owned
257 QLabel* levelLabel; // Not owned
258 QLabel* scoreLabel; // Not owned
260 QStringList levelCollections;
265 QHash<QString, int> savedGames; // level collection -> next level ix