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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "body.h"
#include "collision.h"
#include "color.h"
#include "forces.h"
#include "list.h"
#include "polygon.h"
#include "scene.h"
#include "sdl_wrapper.h"
#include "state.h"
#include "vector.h"
#include "player.h"
#include "custom_utils.h"
#include "physics_constants.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#define MUS_PATH "omg.ogg"
Mix_Chunk *music = NULL;
// window constants
const vector_t WINDOW = (vector_t){.x = 1600, .y = 900};
const vector_t CENTER = (vector_t){.x = 800, .y = 450};
const vector_t MIN_POSITION = (vector_t){.x = 0, .y = 0};
// wall constants
const double WALL_THICKNESS = 10;
const double WALL_MASS = INFINITY;
const size_t WALL_LEFT_BODIES_INDEX = 2;
const size_t WALL_TOP_BODIES_INDEX = 3;
const size_t WALL_RIGHT_BODIES_INDEX = 4;
const double WALL_IMPULSE = 30000;
const color_t WALL_COLOR = (color_t){.r = 0.3, .g = 0.3, .b = 0.3, .a = 1};
// game constants
const size_t GAME_NUM_PLAYERS = 2;
const double PLAYER_COLLISION_IMPULSE = 100;
const size_t INFO_MAX_LEN = 20;
// state def
typedef struct state
{
scene_t *scene;
list_t *players;
bool game_started;
bool left_movement;
bool right_movement;
double time_since_dash;
double game_time;
} state_t;
// food constants
const double FOOD_MIN_SIZE_FACTOR = 0.5;
const double FOOD_MAX_SIZE_FACTOR = 1;
const size_t FOOD_COUNT_INITIAL = 13;
const double FOOD_SIDE_LENGTH = 10;
const time_t FOOD_SPAWN_TIME = 1;
const time_t FOOD_FLASH_FREQUENCY = 1;
const color_t FOOD_COLOR = (color_t){.r = 1, .g = 0.72, .b = 0.69, .a = 1};
const size_t PTS_IN_PELLET = 4;
const size_t FOOD_INFO_LENGTH = 5;
// Creates a circle
list_t *make_circle(size_t num_points, size_t length, vector_t center)
{
list_t *circle = list_init(num_points, free);
double x_pos, y_pos;
double increment_angle = 2 * M_PI / num_points;
double angle = 0;
for (uint32_t i = 0; i < num_points; i++)
{
x_pos = cos(angle) * length + center.x;
y_pos = sin(angle) * length + center.y;
vector_t *point = malloc(sizeof(vector_t));
point->x = x_pos;
point->y = y_pos;
list_add(circle, point);
angle += increment_angle;
}
return circle;
}
list_t *make_rectangle(double width, double height, vector_t center)
{
list_t *rectangle = list_init(4, free);
vector_t *v = malloc(sizeof(*v));
*v = (vector_t){-0.5 * width + center.x, -0.5 * height + center.y};
list_add(rectangle, v);
v = malloc(sizeof(*v));
*v = (vector_t){+0.5 * width + center.x, -0.5 * height + center.y};
list_add(rectangle, v);
v = malloc(sizeof(*v));
*v = (vector_t){+0.5 * width + center.x, +0.5 * height + center.y};
list_add(rectangle, v);
v = malloc(sizeof(*v));
*v = (vector_t){-0.5 * width + center.x, +0.5 * height + center.y};
list_add(rectangle, v);
return rectangle;
}
list_t *make_left_wall()
{
return make_rectangle(WALL_THICKNESS, WINDOW.y,
(vector_t){-0.5 * WALL_THICKNESS, CENTER.y});
}
list_t *make_bottom_wall()
{
return make_rectangle(WINDOW.x, WALL_THICKNESS,
(vector_t){CENTER.x, -0.5 * WALL_THICKNESS});
}
list_t *make_top_wall()
{
return make_rectangle(WINDOW.x, WALL_THICKNESS,
(vector_t){CENTER.x, WINDOW.y + 0.5 * WALL_THICKNESS});
}
list_t *make_right_wall()
{
return make_rectangle(WALL_THICKNESS, WINDOW.y,
(vector_t){WINDOW.x + 0.5 * WALL_THICKNESS, CENTER.y});
}
list_t *make_pellet(size_t length, vector_t position)
{
list_t *pts = list_init(PTS_IN_PELLET, free);
vector_t *lower_left = malloc(sizeof(vector_t));
lower_left->x = position.x - length / 2;
lower_left->y = position.y - length / 2;
list_add(pts, lower_left);
vector_t *upper_left = malloc(sizeof(vector_t));
upper_left->x = position.x - length / 2;
upper_left->y = position.y + length / 2;
list_add(pts, upper_left);
vector_t *upper_right = malloc(sizeof(vector_t));
upper_right->x = position.x + length / 2;
upper_right->y = position.y + length / 2;
list_add(pts, upper_right);
vector_t *lower_right = malloc(sizeof(vector_t));
lower_right->x = position.x + length / 2;
lower_right->y = position.y - length / 2;
list_add(pts, lower_right);
return pts;
}
// move to player.c
void spawn_dash_glow(state_t *state, player_t *player)
{
for (size_t i = 0; i < list_size(player->meta_bodies); i++)
{
body_t *meta_body = (body_t *)list_get(player->meta_bodies, i);
char *glow_info = malloc(sizeof(char) * (5));
glow_info = "glow\0";
body_t *meta_body_glow = body_init_with_info(body_get_shape(meta_body), 1, rand_color(1), glow_info, NULL);
body_set_centroid(meta_body_glow, body_get_centroid(meta_body));
scene_add_body(state->scene, meta_body_glow);
}
}
void wall_collision_handler(body_t *body1, body_t *body2, vector_t axis, void *aux)
{
list_t *shape1 = body_get_shape(body1);
list_t *shape2 = body_get_shape(body2);
if (find_collision(shape1, shape2).collided)
{
char *info = malloc(sizeof(char) * INFO_MAX_LEN);
strcpy(info, body_get_info(body2));
if (strcmp(info, "wall_top") == 0) {
vector_t velocity = (vector_t){.x = 0, .y = -WALL_IMPULSE};
body_add_impulse(body1, velocity);
} else if (strcmp(info, "wall_bottom") == 0) {
vector_t velocity = (vector_t){.x = 0, .y = WALL_IMPULSE};
body_add_impulse(body1, velocity);
} else if (strcmp(info, "wall_left") == 0) {
vector_t velocity = (vector_t){.x = WALL_IMPULSE, .y = 0};
body_add_impulse(body1, velocity);
} else if (strcmp(info, "wall_right") == 0) {
vector_t velocity = (vector_t){.x = -WALL_IMPULSE, .y = 0};
body_add_impulse(body1, velocity);
}
}
list_free(shape1);
list_free(shape2);
}
void player_collision_handler(body_t *body1, body_t *body2, vector_t axis, void *aux) {
state_t* state = (state_t*) aux;
if(list_size(state->players) > 0) {
// assume body1 is the slug head
// and body2 is a metabody from slug 2
size_t player_id1 = *((size_t *)body_get_info(body1));
printf("list size: %zu\n", list_size(state->players));
printf("%zu\n", player_id1);
size_t player_id2 = *((size_t *)body_get_info(body2));
printf("%zu\n", player_id2);
player_t *p1 = list_get(state->players, player_id1);
player_t *p2 = list_get(state->players, player_id2);
list_t *body1_pts = body_get_shape(body1);
list_t *body2_pts = body_get_shape(body2);
if(find_collision(body1_pts, body2_pts).collided && p1->cd_collide_player <= 0 && p2->cd_collide_player <= 0) {
vector_t head_velocity = body_get_velocity(body1);
vector_t body_velocity = body_get_velocity(body2);
vector_t added_vec1 = vec_multiply(-PLAYER_COLLISION_IMPULSE, vec_perpendicular(body_velocity));
vector_t added_vec2 = vec_multiply(PLAYER_COLLISION_IMPULSE,head_velocity);
body_add_impulse(body1, added_vec1);
body_add_impulse(body2, added_vec2);
player_refresh_cd_collide_player(p1);
player_refresh_cd_collide_player(p2);
}
list_free(body1_pts);
list_free(body2_pts);
}
}
void pellet_collision_handler(body_t *body1, body_t *body2, vector_t axis,
void *aux)
{
list_t *aux_casted = (list_t *)aux;
state_t *state = list_get(aux_casted, 0);
player_t *player = list_get(aux_casted, 1);
list_t *snake_pts = body_get_shape(body1);
list_t *food_pts = body_get_shape(body2);
if (find_collision(snake_pts, food_pts).collided)
{
// player_eat(body1, body2); // TODO: do this
body_t *added_body = player_add_body(player);
scene_add_body(state->scene, added_body);
create_drag(state->scene, DRAG_CONST, list_get(player->meta_bodies, list_size(player->meta_bodies) - 1));
create_spring(state->scene, SPRING_CONST, list_get(player->meta_bodies, list_size(player->meta_bodies) - 1), list_get(player->meta_bodies, list_size(player->meta_bodies) - 2));
for(size_t i = 0; i < GAME_NUM_PLAYERS; i++) {
if(list_get(state->players, i) != player) {
body_t *player_head = player_get_head(list_get(state->players, i));
create_collision(state->scene, player_head, added_body, player_collision_handler, state, NULL);
}
}
body_remove(body2);
}
list_free(snake_pts);
list_free(food_pts);
}
void game_init(state_t *state)
{
// music = Mix_LoadMUS(MUS_PATH);
// Mix_PlayMusic(music, -1);
// TODO: implement border
state->scene = scene_init();
state->game_started = false;
// spawn players
state->players = list_init(GAME_NUM_PLAYERS, player_free);
list_add(state->players, player_init(0, CENTER, 'q', 'e', 'w', 'r'));
list_add(state->players, player_init(1, CENTER, 'u', 'o', 'i', 'p'));
// list_add(state->players, player_init(2, CENTER, 'n', '<', 'm', '>'));
for (size_t i = 0; i < GAME_NUM_PLAYERS; i++)
{
player_t *player = list_get(state->players, i);
// player_add_glow(player, 10); // TODO: magic number
for (size_t j = 0; j < list_size(player->meta_bodies); j++)
{
body_t *meta_body = (body_t *)list_get(player->meta_bodies, j);
scene_add_body(state->scene, meta_body);
if (j == 0)
{
*player->ph_applied_force_magnitude = DRAG_CONST * vec_norm(body_get_velocity(player_get_head(player)));
create_applied_force(state->scene, player->ph_applied_force_magnitude, player_get_head(player));
create_drag(state->scene, DRAG_CONST, player_get_head(player));
}
else
{
create_drag(state->scene, DRAG_CONST, list_get(player->meta_bodies, j));
create_spring(state->scene, SPRING_CONST, meta_body, list_get(player->meta_bodies, j - 1));
}
}
}
// adds collisions between players:
for(size_t i = 0; i < GAME_NUM_PLAYERS; i++) {
for(size_t j = 0; j < GAME_NUM_PLAYERS; j++) {
if(i != j) {
body_t *player1_head = player_get_head(list_get(state->players, i));
player_t *player2 = (player_t *) list_get(state->players, j);
for(size_t k = 1; k < list_size(player2->meta_bodies); k++) {
body_t *player2_metabody = list_get(player2->meta_bodies, k);
create_collision(state->scene, player1_head, player2_metabody, player_collision_handler, state, NULL);
}
}
}
}
// spawn random food
list_t *pu_types = list_init(4, free);
list_add(pu_types, "base_speed");
list_add(pu_types, "bullet_speed");
list_add(pu_types, "rotate_rate");
list_add(pu_types, "dash_boost");
list_t *pu_colors = list_init(4, free);
color_t *color1 = malloc(sizeof(color_t));
color_t *color2 = malloc(sizeof(color_t));
color_t *color3 = malloc(sizeof(color_t));
color_t *color4 = malloc(sizeof(color_t));
*color1 = (color_t) {.r = 1, .b = 0.5, .g = 0.5, .a = 1};
*color2 = (color_t) {.r = 0.5, .b = 1, .g = 0.5, .a = 1};
*color3 = (color_t) {.r = 0.5, .b = 0.5, .g = 1, .a = 1};
*color4 = (color_t) {.r = 0.75, .b = 0.5, .g = 0.75, .a = 1};
list_add(pu_colors, color1);
list_add(pu_colors, color2);
list_add(pu_colors, color3);
list_add(pu_colors, color4);
for(size_t i = 0; i < FOOD_COUNT_INITIAL; i++) {
vector_t pellet_pos = (vector_t){.x = (double)rand_range(MIN_POSITION.x, WINDOW.x), .y = (double)rand_range(MIN_POSITION.y, WINDOW.y)};
int pellet_type = (int) (rand_range(0, 1) * 4);
body_t *food = body_init_with_info(make_pellet(FOOD_SIDE_LENGTH, pellet_pos), 1, *((color_t *) list_get(pu_colors, pellet_type)), (char *) list_get(pu_types, pellet_type), NULL);
scene_add_body(state->scene, food);
for (size_t k = 0; k < GAME_NUM_PLAYERS; k++) {
list_t *aux = list_init(2, NULL);
list_add(aux, state);
list_add(aux, list_get(state->players, k));
create_collision(state->scene, player_get_head(list_get(state->players, k)), food, pellet_collision_handler, aux, NULL);
}
}
// initialize walls
list_t *wall_left_pts = make_left_wall();
char *wall_left_info = malloc(sizeof(char) * INFO_MAX_LEN);
wall_left_info = "wall_left";
list_t *wall_top_pts = make_top_wall();
char *wall_top_info = malloc(sizeof(char) * INFO_MAX_LEN);
wall_top_info = "wall_top";
list_t *wall_right_pts = make_right_wall();
char *wall_right_info = malloc(sizeof(char) * INFO_MAX_LEN);
wall_right_info = "wall_right";
list_t *wall_bottom_pts = make_bottom_wall();
char *wall_bottom_info = malloc(sizeof(char) * INFO_MAX_LEN);
wall_bottom_info = "wall_bottom";
body_t *wall_left = body_init_with_info(wall_left_pts, WALL_MASS, WALL_COLOR, wall_left_info, NULL);
body_t *wall_top = body_init_with_info(wall_top_pts, WALL_MASS, WALL_COLOR, wall_top_info, NULL);
body_t *wall_right = body_init_with_info(wall_right_pts, WALL_MASS, WALL_COLOR, wall_right_info, NULL);
body_t *wall_bottom = body_init_with_info(wall_bottom_pts, WALL_MASS, WALL_COLOR, wall_bottom_info, NULL);
scene_add_body(state->scene, wall_left);
scene_add_body(state->scene, wall_top);
scene_add_body(state->scene, wall_right);
scene_add_body(state->scene, wall_bottom);
for (size_t k = 0; k < GAME_NUM_PLAYERS; k++)
{
create_collision(state->scene, player_get_head(list_get(state->players, k)), wall_top, wall_collision_handler, NULL, NULL);
create_collision(state->scene, player_get_head(list_get(state->players, k)), wall_bottom, wall_collision_handler, NULL, NULL);
create_collision(state->scene, player_get_head(list_get(state->players, k)), wall_left, wall_collision_handler, NULL, NULL);
create_collision(state->scene, player_get_head(list_get(state->players, k)), wall_right, wall_collision_handler, NULL, NULL);
}
}
// list_t *images = list_init(1, NULL);
// image_wrapper_t *windsor = image_init("windsor", "WINDSOR.JPG", 20, 20, CENTER);
// list_add(images, windsor);
// load_all_images(images, WINDOW);
void keyboard_handler(state_t *state, char key, key_event_type_t type, double held_time)
{
if (state->game_started)
{
player_t *p = NULL;
for (size_t player_id = 0; player_id < GAME_NUM_PLAYERS; player_id++)
{
player_t *curr_p = list_get(state->players, player_id);
if (player_moves_on_key(curr_p, key))
{
p = curr_p;
break;
}
}
// if player was not found
if (p == NULL)
return;
// if player was found
if (p->st_left_key == key)
{
if (type == 0 && !p->turn_left)
{
p->turn_left = true;
}
else if (type == 1)
{
p->turn_left = false;
}
}
else if (p->st_right_key == key)
{
if (type == 0 && !p->turn_right)
{
p->turn_right = true;
}
else if (type == 1) {
p->turn_right = false;
}
}
else if (type == 0 && p->st_boost_key == key && p->cd_dash == 0)
{
player_dash(p);
}
else if (type == 0 && p->st_shoot_key == key && p->cd_shoot == 0)
{
body_t *bullet = player_shoot(p);
scene_add_body(state->scene, bullet);
}
}
else
{
state->game_started = true;
}
}
// emscripten: init
state_t *emscripten_init()
{
// init sdl
sdl_init(MIN_POSITION, WINDOW);
// init state
state_t *state = malloc(sizeof(state_t));
game_init(state);
get_text_and_rect();
return state;
}
void handle_mouse(state_t *state)
{
// vector_t mouse_pos = get_mouse_pos();
// body_t *player_head = player_get_head(state->player1);
// vector_t head_vector = body_get_velocity(player_head);
// vector_t new_direction = vec_normalize(vec_subtract(mouse_pos, body_get_centroid(player_head)));
// double curr_magnitude = vec_norm(head_vector);
// vector_t updated_vector = vec_multiply(curr_magnitude, new_direction);
// body_set_velocity(player_head, updated_vector);
}
void emscripten_main(state_t *state)
{
// sdl: clear window
sdl_clear();
double dt = time_since_last_tick();
state->game_time += dt;
// handle mouse
// handle_mouse(state);
// handle keypresses
sdl_on_key(keyboard_handler);
if (state->game_started)
{
for (size_t i = 0; i < GAME_NUM_PLAYERS; i++)
{
player_t *p = list_get(state->players, i);
player_update_cd(p, dt);
player_update_pu(p, dt);
player_turn(p);
}
scene_tick_canon(state->scene, dt);
}
// // Renders Text
// //this opens a font style and sets a size
// TTF_Font* Sans = TTF_OpenFont("Sans.ttf", 24);
// // this is the color in rgb format,
// // maxing out all would give you the color white,
// // and it will be your text's color
// SDL_Color White = {255, 255, 255};
// // as TTF_RenderText_Solid could only be used on
// // SDL_Surface then you have to create the surface first
// SDL_Surface* surfaceMessage =
// TTF_RenderText_Solid(Sans, "Dobby > Windsor", White);
// // now you can convert it into a texture
// SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
// SDL_Rect Message_rect; //create a rect
// Message_rect.x = 0; //controls the rect's x coordinate
// Message_rect.y = 0; // controls the rect's y coordinte
// Message_rect.w = 100; // controls the width of the rect
// Message_rect.h = 100; // controls the height of the rect
// // (0,0) is on the top left of the window/screen,
// // think a rect as the text's box,
// // that way it would be very simple to understand
// // Now since it's a texture, you have to put RenderCopy
// // in your game loop area, the area where the whole code executes
// // you put the renderer's name first, the Message,
// // the crop size (you can ignore this if you don't want
// // to dabble with cropping), and the rect which is the size
// // and coordinate of your texture
// SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
// // Don't forget to free your surface and texture
// SDL_FreeSurface(surfaceMessage);
// SDL_DestroyTexture(Message);
// draws all the bodies in a scene
scene_draw(state->scene);
// sdl: show
sdl_show();
}
// emscripten: free resources
void emscripten_free(state_t *state)
{
scene_free(state->scene);
free(state);
}