1
2 /**
3 *
4 * @file gamemode.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created level.c and menu.c
10 * - 3rd of February 2009: Renamed level.c to level.cpp and menu.c to menu.cpp
11 * - 9th March 2009: Created game.cpp from parts of menu.cpp and level.cpp
12 * - 18th July 2009: Created servergame.cpp from parts of game.cpp
13 * - 2nd August 2009: Created gamemode.cpp from parts of servergame.cpp
14 *
15 * @par Licence:
16 * Copyright (c) 2005-2017 Alister Thomson
17 *
18 * OpenJazz is distributed under the terms of
19 * the GNU General Public License, version 2.0
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 */
26
27
28 #include "game.h"
29 #include "gamemode.h"
30
31 #include "io/gfx/font.h"
32 #include "player/player.h"
33
34
35 /**
36 * Destroy game mode
37 */
~GameMode()38 GameMode::~GameMode () {
39 }
40
41
42 /**
43 * Outcome of player being hit
44 *
45 * @param source Player responsible for the hit
46 * @param victim Player victim of the hit
47 *
48 * @return Whether or not the hit should result in energy loss (true)
49 */
hit(Player * source,Player * victim)50 bool GameMode::hit (Player *source, Player *victim) {
51
52 (void)source;
53 (void)victim;
54
55 return true;
56
57 }
58
59
60 /**
61 * Outcome of player being killed
62 *
63 * @param game The current game
64 * @param source Player responsible for the kill
65 * @param victim Player victim of the kill
66 *
67 * @return Whether or not the player should be be killed (true)
68 */
kill(Game * game,Player * source,Player * victim)69 bool GameMode::kill (Game* game, Player *source, Player *victim) {
70
71 if (source && (victim == localPlayer)) game->score(source->getTeam());
72
73 return true;
74
75 }
76
77
78 /**
79 * Outcome of level being completed
80 *
81 * @param game The current game
82 * @param player Player that has completed level
83 * @param gridX X-coordinate (in tiles) of finishing position
84 * @param gridY Y-coordinate (in tiles) of finishing position
85 *
86 * @return Whether or not the level should end (true)
87 */
endOfLevel(Game * game,Player * player,int gridX,int gridY)88 bool GameMode::endOfLevel (Game* game, Player *player, int gridX, int gridY) {
89
90 (void)player;
91
92 game->setCheckpoint(gridX, gridY);
93
94 return true;
95
96 }
97
98
99 /**
100 * Outcome of time running out
101 */
outOfTime()102 void GameMode::outOfTime () {
103
104 return;
105
106 }
107
108
109 /**
110 * Get the game mode type
111 *
112 * @return Game mode type (M_SINGLE)
113 */
getMode()114 GameModeType SingleGameMode::getMode () {
115
116 return M_SINGLE;
117
118 }
119
120
121 /**
122 * Choose a team for a new player
123 *
124 * @return New player's team (0)
125 */
chooseTeam()126 unsigned char SingleGameMode::chooseTeam () {
127
128 return 0;
129
130 }
131
132
133 /**
134 * Draw the player's team's score (not in single-player mode)
135 *
136 * @param font Font to use to draw score
137 */
drawScore(Font * font)138 void SingleGameMode::drawScore (Font* font) {
139
140 (void)font;
141
142 return;
143
144 }
145
146
147 /**
148 * Choose a team for a new player
149 *
150 * @return New player's team (0)
151 */
chooseTeam()152 unsigned char CooperativeGameMode::chooseTeam () {
153
154 // All players are on the same team
155
156 return 0;
157
158 }
159
160
161 /**
162 * Draw the player's team's score (not in cooperative mode)
163 *
164 * @param font Font to use to draw score
165 */
drawScore(Font * font)166 void CooperativeGameMode::drawScore (Font* font) {
167
168 (void)font;
169
170 // Do nothing
171
172 return;
173
174 }
175
176
177 /**
178 * Choose a team for a new player
179 *
180 * @return New player's team (unique)
181 */
chooseTeam()182 unsigned char FreeForAllGameMode::chooseTeam () {
183
184 // Every player is on a separate team
185
186 int count;
187 unsigned char team;
188
189 team = 1;
190
191 // Find a team number higher than any other
192 for (count = nPlayers - 1; count >= 0; count--) {
193
194 if (players[count].getTeam() > team)
195 team = players[count].getTeam() + 1;
196
197 }
198
199 return team;
200
201 }
202
203
204 /**
205 * Draw the player's team's score
206 *
207 * @param font Font to use to draw score
208 */
drawScore(Font * font)209 void FreeForAllGameMode::drawScore (Font* font) {
210
211 font->showNumber(localPlayer->teamScore, 64, 4);
212
213 return;
214
215 }
216
217
218 /**
219 * Choose a team for a new player
220 *
221 * @return New player's team (0 or 1)
222 */
chooseTeam()223 unsigned char TeamGameMode::chooseTeam () {
224
225 // Players are split between two teams
226
227 int count, difference;
228
229 // Calculate team imbalance
230
231 difference = 0;
232
233 for (count = 0; count < nPlayers; count++) {
234
235 if (players[count].getTeam()) difference++;
236 else difference--;
237
238 }
239
240 // Assign to the team with the least players
241
242 if (difference >= 0) return 0;
243
244 return 1;
245
246 }
247
248
249 /**
250 * Draw the player's team's score
251 *
252 * @param font Font to use to draw score
253 */
drawScore(Font * font)254 void TeamGameMode::drawScore (Font* font) {
255
256 font->showNumber(localPlayer->teamScore, 64, 4);
257
258 return;
259
260 }
261
262
263 /**
264 * Get the game mode type
265 *
266 * @return Game mode type (M_COOP)
267 */
getMode()268 GameModeType CoopGameMode::getMode () {
269
270 return M_COOP;
271
272 }
273
274
275 /**
276 * Get the game mode type
277 *
278 * @return Game mode type (M_BATTLE)
279 */
getMode()280 GameModeType BattleGameMode::getMode () {
281
282 return M_BATTLE;
283
284 }
285
286
287 /**
288 * Get the game mode type
289 *
290 * @return Game mode type (M_TEAMBATTLE)
291 */
getMode()292 GameModeType TeamBattleGameMode::getMode () {
293
294 return M_TEAMBATTLE;
295
296 }
297
298
299 /**
300 * Get the game mode type
301 *
302 * @return Game mode type (M_RACE)
303 */
getMode()304 GameModeType RaceGameMode::getMode () {
305
306 return M_RACE;
307
308 }
309
310
311 /**
312 * Outcome of player being hit
313 *
314 * @param source Player responsible for the hit
315 * @param victim Player victim of the hit
316 *
317 * @return Whether or not the hit should result in energy loss (false)
318 */
hit(Player * source,Player * victim)319 bool RaceGameMode::hit (Player *source, Player *victim) {
320
321 (void)source;
322 (void)victim;
323
324 return false;
325
326 }
327
328
329 /**
330 * Outcome of level being completed
331 *
332 * @param game The current game
333 * @param player Player that has completed level
334 * @param gridX X-coordinate (in tiles) of finishing position
335 * @param gridY Y-coordinate (in tiles) of finishing position
336 *
337 * @return Whether or not the level should end (false)
338 */
endOfLevel(Game * game,Player * player,int gridX,int gridY)339 bool RaceGameMode::endOfLevel (Game* game, Player *player, int gridX, int gridY) {
340
341 (void)gridX;
342 (void)gridY;
343
344 if (player == localPlayer) game->score(localPlayer->getTeam());
345
346 game->resetPlayer(player);
347
348 return false;
349
350 }
351
352