-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
305 lines (282 loc) · 8.87 KB
/
game.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
303
304
305
#include "game.h"
game_direction game_actual_ball_direction(game_state* gs) {
if (gs->ball.bounce == GAME_STOP) {
return gs->ball.dir;
} else {
return gs->ball.bounce;
}
}
void game_move_ball(game_state* gs, float dt) {
if (gs->ball.going_up) {
gs->ball.y += dt*gs->config.ud_speed;
} else {
gs->ball.y -= dt*gs->config.ud_speed;
}
if (game_actual_ball_direction(gs) == GAME_LEFT) {
gs->ball.x -= dt*gs->config.lr_speed/gs->config.aspect;
} else if (game_actual_ball_direction(gs) == GAME_RIGHT) {
gs->ball.x += dt*gs->config.lr_speed/gs->config.aspect;
}
gs->time += dt;
if (gs->ball.bounce != GAME_STOP && gs->ball.bounce_remaining >= 0) {
gs->ball.bounce_remaining -= dt;
if (gs->ball.bounce_remaining <= 0) {
gs->ball.bounce = GAME_STOP;
} else {
}
}
}
void game_bounce_ball(game_state* gs, game_direction d) {
if (d == GAME_LEFT) {
gs->ball.bounce = GAME_RIGHT;
} else if (d == GAME_RIGHT) {
gs->ball.bounce = GAME_LEFT;
} else {
// This shouldn't happen.
printf("Uh oh, bounce without moving to side.\n");
exit(1);
}
gs->ball.bounce_remaining = gs->config.bounce_time;
}
// Return 1 if the tile was hit, 0 if pass has been granted
// This is also responsible for actions caused by hitting the tile
// ie. Tile removed, ball killed, key acquired, etc...
// Return 1 if bouncing off this tile, 0 if going through
int game_hit_tile(game_state* gs, int x, int y, int* react) {
game_tile* t;
if (x<0 || y<0 || x>=gs->config.x || y>=gs->config.y) {
printf("%d %d is not in level\n",x,y);
exit(1);
}
t = &gs->tiles[gs->config.x * y + x];
*react = 1;
switch (t->type) {
case EMPTY_TILE:
return 0;
break;
case COLOR_TILE:
if (t->color == gs->ball.color) {
t->type = EMPTY_TILE;
gs->color_tile_count--;
}
return 1;
break;
case DIAMOND_TILE:
if (gs->color_tile_count == 0) {
t->type = EMPTY_TILE;
gs->diamond_tile_count--;
}
return 1;
break;
case REVERSER_TILE:
t->type = EMPTY_TILE;
gs->reversed = !gs->reversed;
if (gs->ball.dir == GAME_LEFT) {
gs->ball.dir = GAME_RIGHT;
} else if (gs->ball.dir == GAME_RIGHT) {
gs->ball.dir = GAME_LEFT;
}
return 1;
break;
case KILL_TILE:
*react = 0; // To stop the new ball from bouncing
gs->lives--;
gs->reversed = 0;
gs->ball.color = gs->config.init_color;
gs->ball.x = gs->config.ix + 0.5;
gs->ball.y = gs->config.iy + 0.5;
gs->ball.going_up = gs->config.going_up_init;
gs->ball.bounce = GAME_STOP;
return 1;
break;
case KEY_TILE:
if (!gs->have_key && gs->ball.color == t->color) {
t->type = EMPTY_TILE;
gs->key_color = t->color;
gs->have_key = 1;
}
return 1;
break;
case LOCK_TILE:
if (gs->have_key && gs->ball.color == t->color && gs->key_color == t->color) {
t->type = EMPTY_TILE;
gs->have_key = 0;
}
return 1;
break;
case PAINT_TILE:
gs->ball.color = t->color;
return 1;
break;
case BRICK_TILE:
return 1;
break;
default:
printf("Unknown tile type.\n");
exit(1);
break;
}
return t->type;
}
void game_update(game_state* gs, float dt) { // Note that this algorithm will break if the ball is moving too fast
// This currently assumes the ball is actually a square for easier math
// Could probably be changed to an actual round ball with some ease
if (gs->ball.bounce != GAME_STOP && gs->ball.bounce_remaining < dt) {
// Account for ball changing direction in the middle of the update
dt -= gs->ball.bounce_remaining;
game_update(gs,gs->ball.bounce_remaining);
}
// What direction is the ball moving (left/right/stop)?
game_direction gd;
gd = game_actual_ball_direction(gs);
// Compute the time to reach the next x and y boundaries
float ttxw, ttyw;
int ix, iy,tx,ty;
tx = floor(gs->ball.x);
ty = floor(gs->ball.y);
if (gs->ball.going_up) {
float t = gs->ball.y + gs->config.ball_size;
ttyw = ceil(t) - t;
iy = ceil(t);
} else {
float t = gs->ball.y - gs->config.ball_size;
ttyw = t - floor(t);
iy = floor(t) - 1;
}
ttyw /= gs->config.ud_speed;
if (gd == GAME_LEFT) {
float t = gs->ball.x - gs->config.ball_size/gs->config.aspect;
ttxw = t - floor(t);
ix = floor(t) - 1;
} else if (gd == GAME_RIGHT) {
float t = gs->ball.x + gs->config.ball_size/gs->config.aspect;
ttxw = ceil(t) - t;
ix = ceil(t);
} else {
ttxw = 1000;
ix = tx;
}
ttxw /= gs->config.lr_speed/gs->config.aspect;
// Based on the distance to the boundaries of this tile, pick an action
if ((ttxw > dt && ttyw > dt)) { // || (ttxw <= 0 && ttyw > dt) || (ttyw <= 0 && ttxw > dt)) {
// Just move the ball and return, we're not leaving this tile
game_move_ball(gs,dt);
} else if (ttxw < ttyw && ttxw >= 0) {
// We're going to hit a side of this tile, do work then recurse
game_move_ball(gs,ttxw);
dt -= ttxw;
ttyw -= ttxw;
// Attempt hitting the tile we're closest to
int ty1,ty2,react=1;
ty1 = floor(gs->ball.y - gs->config.ball_size);
ty2 = floor(gs->ball.y + gs->config.ball_size);
game_direction td = gs->ball.dir;
if (ix < 0 || ix == gs->config.x || game_hit_tile(gs,ix,ty,&react) || game_hit_tile(gs,ix,ty1,&react) || game_hit_tile(gs,ix,ty2,&react)) {
// we hit a tile on the side, so bounce off
// (or a boundary wall side)
if (react) game_bounce_ball(gs,td);
}
if (dt <= ttyw) {
game_move_ball(gs,dt);
if (dt==ttyw) {
gs->ball.going_up = !gs->ball.going_up;
}
} else {
game_move_ball(gs,ttyw/2.0);
game_update(gs,ttyw/2.0);
}
} else if (ttyw >= 0) {
// We're going to hit the top or bottom of this tile, do work then recurse
game_move_ball(gs,ttyw);
dt -= ttyw;
ttxw -= ttyw;
int tx1,tx2,react=1;
tx1 = floor(gs->ball.x - gs->config.ball_size/gs->config.aspect);
tx2 = floor(gs->ball.x + gs->config.ball_size/gs->config.aspect);
game_direction td = gs->ball.dir;
if (iy < 0 || iy == gs->config.y || game_hit_tile(gs,tx,iy,&react) || game_hit_tile(gs,tx1,iy,&react) || game_hit_tile(gs,tx2,iy,&react)) {
// we hit a tile on the top or bottom or a boundary top/bottom
// reverse the direction of the ball
if (react) gs->ball.going_up = !gs->ball.going_up;
}
if (dt <= ttxw || ttxw < 0) {
game_move_ball(gs,dt);
if (dt == ttxw) {
game_bounce_ball(gs,td);
}
} else {
game_move_ball(gs,ttxw/2.0);
game_update(gs,ttxw/2);
}
} else {
game_move_ball(gs,dt);
}
}
void game_change_direction(game_state* gs, game_direction dir) {
if (gs->reversed && dir == GAME_LEFT) {
dir = GAME_RIGHT;
} else if (gs->reversed && dir == GAME_RIGHT) {
dir = GAME_LEFT;
}
gs->ball.dir = dir;
}
game_state* game_load(FILE* f, float lr, float ud, float aspect, float bounce, float bsize) {
game_state* gs;
gs = malloc(sizeof(game_state));
gs->config.colored_tiles = 0;
gs->config.diamond_tiles = 0;
gs->config.lr_speed = lr;
gs->config.ud_speed = ud;
gs->config.aspect = aspect;
gs->config.bounce_time = bounce;
gs->config.ball_size = bsize;
int i;
i = fscanf(f,"%50s %d %d %d %d %d %d",gs->config.name,&gs->config.x,&gs->config.y,&gs->config.ix,&gs->config.iy,&gs->config.init_color,&gs->config.going_up_init);
if (i != 7) {
free(gs);
return NULL;
}
gs->tiles = malloc(gs->config.x * gs->config.y * sizeof(game_tile));
gs->config.tiles = malloc(gs->config.x * gs->config.y * sizeof(game_tile));
int x,y;
for (y=0;y<gs->config.y;y++) {
for (x=0;x<gs->config.x;x++) {
int type;
i = fscanf(f,"%d %d",&type,&(gs->config.tiles[gs->config.x * y + x].color));
gs->config.tiles[gs->config.x * y + x].type = (game_tile_type) type;
if (i != 2) {
free(gs->config.tiles);
free(gs->tiles);
free(gs);
return NULL;
}
if (gs->config.tiles[gs->config.x * y + x].type == COLOR_TILE) {
gs->config.colored_tiles++;
}
if (gs->config.tiles[gs->config.x * y + x].type == DIAMOND_TILE) {
gs->config.diamond_tiles++;
}
}
}
return gs;
}
void game_init(game_state* gs, int lives) {
gs->time = 0.0;
gs->lives = lives;
gs->reversed = 0;
gs->have_key = 0;
gs->color_tile_count = gs->config.colored_tiles;
gs->diamond_tile_count = gs->config.diamond_tiles;
memcpy(gs->tiles,gs->config.tiles,gs->config.x * gs->config.y * sizeof(game_tile));
gs->ball.color = gs->config.init_color;
gs->ball.x = gs->config.ix + 0.5;
gs->ball.y = gs->config.iy + 0.5;
gs->ball.going_up = gs->config.going_up_init;
gs->ball.dir = GAME_STOP;
gs->ball.bounce = GAME_STOP;
}
void game_close(game_state* gs) {
free(gs->config.tiles);
free(gs->tiles);
free(gs);
}