-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_state.c
52 lines (43 loc) · 1.38 KB
/
game_state.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
#include <furi.h>
#include <stdlib.h>
#include "game_state.h"
#include "paper.h"
#include "map.h"
void game_state_init(GameState* const game_state) {
game_state->last_tick = furi_get_tick();
game_state->crash_flag = 0;
game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
Paper* paper = malloc(sizeof(Paper));
paper_init(paper);
uint16_t* map = malloc(sizeof(uint16_t) * MAP_HEIGHT);
init_map(map);
game_state->paper = paper;
game_state->map = map;
}
void game_state_reinit(GameState* const game_state) {
game_state->last_tick = furi_get_tick();
game_state->crash_flag = 0;
paper_init(game_state->paper);
}
void check_collision(GameState* const game_state) {
/*
to make collision detection easier,
convert the u_int16_t to an array of
u_int8_t's
*/
u_int8_t currentRow[sizeof(uint16_t) * 8];
u_int16_t mapCopy = game_state->map[(int)game_state->paper->y + 3];
for(unsigned int j = 0; j < sizeof(uint16_t) * 8; j++) {
if(mapCopy & 0x8000) {
currentRow[j] = 1;
} else {
currentRow[j] = 0;
}
mapCopy <<= 1;
}
// TODO: this collision code barely works, needs a refactor
if(currentRow[(unsigned int)(game_state->paper->x + 0.375)] ||
currentRow[(unsigned int)(game_state->paper->x + 0.625)]) {
game_state->crash_flag = 1;
}
}