game_state.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <furi.h>
  2. #include <stdlib.h>
  3. #include "game_state.h"
  4. #include "paper.h"
  5. #include "map.h"
  6. void game_state_init(GameState* const game_state) {
  7. game_state->last_tick = furi_get_tick();
  8. game_state->crash_flag = 0;
  9. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  10. Paper* paper = malloc(sizeof(Paper));
  11. paper_init(paper);
  12. uint16_t* map = malloc(sizeof(uint16_t) * MAP_HEIGHT);
  13. init_map(map);
  14. game_state->paper = paper;
  15. game_state->map = map;
  16. }
  17. void game_state_reinit(GameState* const game_state) {
  18. game_state->last_tick = furi_get_tick();
  19. game_state->crash_flag = 0;
  20. paper_init(game_state->paper);
  21. }
  22. void check_collision(GameState* const game_state) {
  23. /*
  24. to make collision detection easier,
  25. convert the uint16_t to an array of
  26. uint8_t's
  27. */
  28. uint8_t currentRow[sizeof(uint16_t) * 8];
  29. uint16_t mapCopy = game_state->map[(int)game_state->paper->y + 3];
  30. for(unsigned int j = 0; j < sizeof(uint16_t) * 8; j++) {
  31. if(mapCopy & 0x8000) {
  32. currentRow[j] = 1;
  33. } else {
  34. currentRow[j] = 0;
  35. }
  36. mapCopy <<= 1;
  37. }
  38. // TODO: this collision code barely works, needs a refactor
  39. if(currentRow[(unsigned int)(game_state->paper->x + 0.375)] ||
  40. currentRow[(unsigned int)(game_state->paper->x + 0.625)]) {
  41. game_state->crash_flag = 1;
  42. }
  43. }