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;
67 static const Piece ppieces[] = {
68 {PieceNone, 0, false, 0, 0, // 0
69 {DirNone, DirNone, DirNone, DirNone}},
70 {PieceStraight, 0, true, 0, 0, // 1
71 {DirUp, DirDown, DirNone, DirNone}},
72 {PieceStraight, 90, true, 0, 1, // 2
73 {DirLeft, DirRight, DirNone, DirNone}},
74 {PieceCorner, 270, true, 1, 0, // 3
75 {DirDown, DirRight, DirNone, DirNone}},
76 {PieceCorner, 0, true, 1, 1, // 4
77 {DirLeft, DirDown, DirNone, DirNone}},
78 {PieceCorner, 180, true, 2, 0, // 5
79 {DirUp, DirRight, DirNone, DirNone}},
80 {PieceCorner, 90, true, 2, 1, // 6
81 {DirLeft, DirUp, DirNone, DirNone}},
82 {PieceCross, 0, true, 3, 0, // 7
83 {DirLeft, DirRight, DirUp, DirDown}},
84 {PieceCorners, 0, true, 4, 0, // 8
85 {DirLeft, DirUp, DirRight, DirDown}},
86 {PieceCorners, 90, true, 4, 1, // 9
87 {DirUp, DirRight, DirLeft, DirDown}},
88 {PieceStart, 0, false, 0, 0, // 10
89 {DirLeft, DirNone, DirNone, DirNone}},
90 {PieceStart, 90, false, 0, 0, // 11
91 {DirUp, DirNone, DirNone, DirNone}},
92 {PieceStart, 180, false, 0, 0, // 12
93 {DirRight, DirNone, DirNone, DirNone}},
94 {PieceStart, 270, false, 0, 0, // 13
95 {DirDown, DirNone, DirNone, DirNone}},
96 {PieceEnd, 0, false, 0, 0, // 14
97 {DirLeft, DirDone, DirNone, DirNone}},
98 {PieceEnd, 90, false, 0, 0, // 15
99 {DirUp, DirDone, DirNone, DirNone}},
100 {PieceEnd, 180, false, 0, 0, // 16
101 {DirRight, DirDone, DirNone, DirNone}},
102 {PieceEnd, 270, false, 0, 0, // 17
103 {DirDown, DirDone, DirNone, DirNone}},
104 {PieceBlock, 0, false, 0, 0, // 18
105 {DirNone, DirNone, DirNone, DirNone}},
106 {PiecesEnd, 0, false, 0, 0, // 19
107 {DirNone, DirNone, DirNone, DirNone}}};
109 const int noPieces = 20;
111 typedef struct _PlacedPiece
115 bool flow[2]; // which directions the liquid has already flown
118 typedef struct _PrePlacedPiece
125 typedef struct _AvailablePiece
131 class GameField : public QObject
135 GameField(QTableWidget* ui);
136 void initGame(int rows_, int cols_, int count, PrePlacedPiece* prePlaced);
138 bool setPiece(int row, int col, const Piece* piece, bool fixed = false);
139 const Piece* pieceAt(int row, int col);
140 bool isPrePlaced(int row, int col);
141 void indicateFlow(int row, int col, Direction dir);
144 void cellClicked(int, int);
147 int toIndex(int row, int col);
149 QTableWidget* fieldUi;
155 class AvailablePieces : public QObject
159 AvailablePieces(QTableWidget* pieceTable);
160 void initGame(int count, AvailablePiece* pieces);
163 void validPieceSelected(const Piece* piece);
164 void invalidPieceSelected();
167 void onPieceUsed(const Piece* piece);
170 void onItemClicked(QTableWidgetItem* item);
173 void initPieceCache();
174 static int pieceToId(const Piece* piece);
175 static const Piece* idToPiece(int id);
177 static QHash<QPair<PieceType, int>, const Piece*> pieceCache;
178 QTableWidget* pieceUi;
179 QHash<const Piece*, int> pieceCounts;
182 class GameController : public QObject
186 GameController(AvailablePieces* pieceUi, GameField* fieldUi,
187 QLabel* timeLabel, QPushButton* doneButton);
188 void startLevel(QString fileName);
191 void pieceUsed(const Piece* piece);
192 void levelPassed(int score);
196 void onCellClicked(int row, int column);
197 void onValidPieceSelected(const Piece* piece);
198 void onInvalidPieceSelected();
204 AvailablePieces* pieceUi; // Not owned
205 GameField* fieldUi; // Not owned
206 QLabel* timeLabel; // Not owned
207 QPushButton* doneButton; // Not owned
209 // Data about the current situation in the game
210 const Piece* currentPiece;
216 // how many times does the liquid need to flow through the pre-placed pieces
228 class LevelSwitcher : public QObject
232 LevelSwitcher(GameController* gameController,
233 QWidget* levelWidget, QListWidget* levelList, QPushButton* levelStartButton,
234 QWidget* startWidget, QLabel* startTitle, QLabel* startLabel, QPushButton* startButton,
235 QLabel* levelLabel, QLabel* scoreLabel,
239 void onLevelCollectionChosen();
240 void onStartClicked();
241 void onLevelPassed(int score);
242 void onLevelFailed();
245 void initiateLevel();
246 void chooseLevelCollection();
248 GameController* gameController; // Not owned
249 QWidget* levelWidget; // Not owned
250 QListWidget* levelList; // Not owned
251 QPushButton* levelStartButton; // Not owned
252 QWidget* startWidget; // Not owned
253 QLabel* startTitle; // Not owned
254 QLabel* startLabel; // Not owned
255 QPushButton* startButton; // Not owned
256 QLabel* levelLabel; // Not owned
257 QLabel* scoreLabel; // Not owned
259 QStringList levelCollections;