-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameplay.c
302 lines (240 loc) · 6.23 KB
/
gameplay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <jo/jo.h>
#include "main.h"
#include "gameplay.h"
#include "objects/grid.h"
#include "objects/player.h"
#include "assets.h"
#include "pause.h"
#include "objects/explosion.h"
#include "audio.h"
extern PLAYER g_Players[MAX_PLAYERS];
typedef struct _TEAM_SCORE
{
int team_id;
int points;
int time;
} TEAM_SCORE, *PTEAM_SCORE;
bool g_RoundOver = false;
int g_RoundOverTimer = 0;
#define ROUND_OVER_TIME_FRAMES (30*4)
static bool isRoundOver(void);
static void drawStats(void);
static void drawScore(void);
static void drawStats(void)
{
int numSquares = 0;
int numMines = 0;
int yPos = -192;
int xPos = 0-90;
unsigned char digit1 = 0;
unsigned char digit2 = 0;
unsigned char digit3 = 0;
int letterSpacing = 30;
numSquares = countSquaresRemaining();
convertNumberToDigits(numSquares, &digit1, &digit2, &digit3);
jo_sprite_change_sprite_scale(2);
jo_sprite_draw3D(g_Assets.scores[0][digit1], xPos + (letterSpacing*0), yPos, SCORE_Z);
jo_sprite_draw3D(g_Assets.scores[0][digit2], xPos + (letterSpacing*1), yPos, SCORE_Z);
jo_sprite_draw3D(g_Assets.scores[0][digit3], xPos + (letterSpacing*2), yPos, SCORE_Z);
xPos = 30- 4;
numMines = countMinesRemaining();
convertNumberToDigits(numMines, &digit1, &digit2, &digit3);
jo_sprite_draw3D(g_Assets.scores[1][digit1], xPos + (letterSpacing*0), yPos, SCORE_Z);
jo_sprite_draw3D(g_Assets.scores[1][digit2], xPos + (letterSpacing*1), yPos, SCORE_Z);
jo_sprite_draw3D(g_Assets.scores[1][digit3], xPos + (letterSpacing*2), yPos, SCORE_Z);
jo_sprite_change_sprite_scale(1);
}
// sort players by score for coop mode
static int sortByTeamScore(const void * teamScore1, const void * teamScore2)
{
PTEAM_SCORE a = (PTEAM_SCORE)teamScore1;
PTEAM_SCORE b = (PTEAM_SCORE)teamScore2;
// sort by score
if(b->points > a->points)
{
return 1;
}
else if(b->points < a->points)
{
return -1;
}
// tie-breaker: lower team ID wins
if(b->team_id < a->team_id)
{
return 1;
}
else
{
return -1;
}
return 0;
}
static void drawScore(void)
{
int yPos = -200;
int xPos = -308;
unsigned char digit1 = 0;
unsigned char digit2 = 0;
unsigned char digit3 = 0;
int letterSpacing = 15;
TEAM_SCORE scores[MAX_TEAMS] = {0};
for(unsigned int i = 0; i < COUNTOF(scores); i++)
{
scores[i].team_id = i;
}
for(unsigned int j = 0; j < COUNTOF(g_Players); j++)
{
PPLAYER player = &g_Players[j];
if(player->objectState != OBJECT_STATE_ACTIVE)
{
continue;
}
scores[player->teamID].points += player->score.points;
}
qsort(scores, COUNTOF(scores), sizeof(TEAM_SCORE), sortByTeamScore);
/*
digit1 = (team_points / 100);
digit2 = ((team_points - ((team_points/100) * 100))/10);
digit3 = (team_points % 10);
*/
for(int i = 0; i < 12; i++)
{
if(i < 6)
{
yPos = -200;
xPos = -308 + (i * (32 + (letterSpacing*3)));
}
else
{
yPos = -200+24;
xPos = -308 + ((i-6) * (32 + (letterSpacing*3)));
}
if(i >= 3 && i <= 5)
{
xPos += 196;
}
else if(i >= 9)
{
xPos += 196;
}
unsigned int color = g_Assets.randomizedColors[scores[i].team_id];
unsigned int score = scores[i].points;
convertNumberToDigits(score, &digit1, &digit2, &digit3);
jo_sprite_draw3D(g_Assets.scores[color][digit1], xPos + (letterSpacing*0), yPos, SCORE_Z);
jo_sprite_draw3D(g_Assets.scores[color][digit2], xPos + (letterSpacing*1), yPos, SCORE_Z);
jo_sprite_draw3D(g_Assets.scores[color][digit3], xPos + (letterSpacing*2), yPos, SCORE_Z);
}
}
int countActiveTeams()
{
int teamCount[12] = {0}; // number of players on each team
int activeTeams = 0;
for(unsigned int i = 0; i < COUNTOF(g_Players); i++)
{
PPLAYER player = &g_Players[i];
if(player->objectState == OBJECT_STATE_INACTIVE)
{
continue;
}
if(player->subState == PLAYER_STATE_DEAD)
{
continue;
}
teamCount[player->teamID] = teamCount[player->teamID] + 1;
}
for(unsigned int j = 0; j < COUNTOF(teamCount); j++)
{
if(teamCount[j] > 0)
{
activeTeams++;
}
}
return activeTeams;
}
static bool isRoundOver(void)
{
int numTeams = 0;
int numSquares = 0;
// TODO: check for no time left in time attack
numTeams = countActiveTeams();
if(numTeams == 0)
{
// no more teams left, players lost
g_RoundOver = true;
g_RoundOverTimer = ROUND_OVER_TIME_FRAMES;
playCDTrack(DEATH_TRACK);
return true;
}
numSquares = countSquaresRemaining();
if(numSquares == 0)
{
// no more squares left to open, players win
g_RoundOver = true;
g_RoundOverTimer = ROUND_OVER_TIME_FRAMES;
playCDTrack(VICTORY_TRACK);
return true;
}
return false;
}
void gameplay_init(void)
{
initGrid(g_Game.gameDifficulty);
resetPlayerScores();
spawnPlayers();
initExplosions();
g_Game.isPaused = false;
g_RoundOver = false;
}
void gameplay_input(void)
{
if(g_Game.gameState != GAME_STATE_GAMEPLAY)
{
return;
}
if(g_Game.isPaused == true)
{
return;
}
getPlayersInput();
}
void gameplay_update(void)
{
if(g_Game.gameState != GAME_STATE_GAMEPLAY)
{
return;
}
// don't do anything if the game is paused
if(g_Game.isPaused == true)
{
return;
}
updatePlayers();
updateExplosions();
if(g_RoundOver == true)
{
g_RoundOverTimer--;
// check if it's time to go to the pause screen
if(g_RoundOverTimer <= 0)
{
g_Game.isPaused = true;
}
return;
}
g_RoundOver = isRoundOver();
}
void gameplay_draw(void)
{
if(g_Game.gameState != GAME_STATE_GAMEPLAY)
{
return;
}
if(g_Game.isPaused == true)
{
return;
}
drawGrid();
drawPlayers();
drawScore();
drawStats();
drawExplosions();
}