jetpack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include <stdlib.h>
  2. #include <jetpack_joyride_icons.h>
  3. #include <furi.h>
  4. #include <gui/gui.h>
  5. #include <gui/icon_animation.h>
  6. #include <input/input.h>
  7. #define TAG "Jetpack Joyride"
  8. #define GRAVITY_BOOST -0.3
  9. #define GRAVITY_TICK 0.15
  10. #define PARTIVLE_VELOCITY 2
  11. #define SCIENTIST_VELOCITY_MIN 2
  12. #define SCIENTIST_VELOCITY_MAX 2
  13. #define SCIENTISTS_MAX 6
  14. #define COINS_MAX 25
  15. #define PARTICLES_MAX 50
  16. typedef struct {
  17. int x;
  18. int y;
  19. } POINT;
  20. typedef struct {
  21. float gravity;
  22. POINT point;
  23. bool isBoosting;
  24. } BARRY;
  25. typedef struct {
  26. float gravity;
  27. POINT point;
  28. } COIN;
  29. typedef struct {
  30. POINT point;
  31. } PARTICLE;
  32. typedef enum {
  33. ScientistStateAlive,
  34. ScientistStateDead,
  35. } ScientistState;
  36. typedef struct {
  37. float gravity;
  38. POINT point;
  39. int velocity_x;
  40. ScientistState state;
  41. } SCIENTIST;
  42. typedef enum {
  43. GameStateLife,
  44. GameStateGameOver,
  45. } State;
  46. typedef struct {
  47. IconAnimation* barry;
  48. IconAnimation* scientist;
  49. Icon* coin;
  50. Icon* dead_scientist;
  51. } GameSprites;
  52. typedef struct {
  53. int points;
  54. int distance;
  55. BARRY barry;
  56. SCIENTIST scientists[SCIENTISTS_MAX];
  57. COIN coins[COINS_MAX];
  58. PARTICLE particles[PARTICLES_MAX];
  59. State state;
  60. GameSprites sprites;
  61. FuriMutex* mutex;
  62. } GameState;
  63. typedef enum {
  64. EventTypeTick,
  65. EventTypeKey,
  66. } EventType;
  67. typedef struct {
  68. EventType type;
  69. InputEvent input;
  70. } GameEvent;
  71. static void jetpack_game_random_coins(GameState* const game_state) {
  72. // Check for an available slot for a new coin
  73. for(int i = 0; i < COINS_MAX; ++i) {
  74. if(game_state->coins[i].point.x <= 0 && (rand() % 1000) < 1) {
  75. game_state->coins[i].point.x = 127;
  76. game_state->coins[i].point.y = rand() % 64;
  77. break;
  78. }
  79. }
  80. }
  81. static void jetpack_game_random_scientists(GameState* const game_state) {
  82. // Check for an available slot for a new scientist
  83. for(int i = 0; i < SCIENTISTS_MAX; ++i) {
  84. if(game_state->scientists[i].point.x <= 0 &&
  85. (rand() % 1000) < 10) { // Spawn rate is less frequent than coins
  86. game_state->scientists[i].state = ScientistStateAlive;
  87. game_state->scientists[i].point.x = 127;
  88. game_state->scientists[i].point.y = 49;
  89. game_state->scientists[i].velocity_x =
  90. (rand() % (SCIENTIST_VELOCITY_MAX - SCIENTIST_VELOCITY_MIN + 1)) +
  91. SCIENTIST_VELOCITY_MIN; // random velocity between SCIENTIST_VELOCITY_MIN and SCIENTIST_VELOCITY_MAX
  92. break;
  93. }
  94. }
  95. }
  96. static void jetpack_game_spawn_particles(GameState* const game_state) {
  97. for(int i = 0; i < PARTICLES_MAX; i++) {
  98. if(game_state->particles[i].point.y <= 0) {
  99. game_state->particles[i].point.x = game_state->barry.point.x + (rand() % 7) - 3;
  100. game_state->particles[i].point.y = game_state->barry.point.y;
  101. break;
  102. }
  103. }
  104. }
  105. static void jetpack_game_state_init(GameState* const game_state) {
  106. UNUSED(game_state);
  107. BARRY barry;
  108. barry.gravity = 0;
  109. barry.point.x = 64;
  110. barry.point.y = 32;
  111. barry.isBoosting = false;
  112. GameSprites sprites;
  113. sprites.barry = icon_animation_alloc(&A_barry);
  114. sprites.scientist = icon_animation_alloc(&A_scientist);
  115. icon_animation_start(sprites.scientist);
  116. icon_animation_start(sprites.barry);
  117. game_state->barry = barry;
  118. game_state->points = 0;
  119. game_state->distance = 0;
  120. game_state->sprites = sprites;
  121. game_state->state = GameStateLife;
  122. memset(game_state->scientists, 0, sizeof(game_state->scientists));
  123. memset(game_state->coins, 0, sizeof(game_state->coins));
  124. memset(game_state->particles, 0, sizeof(game_state->particles));
  125. }
  126. static void jetpack_game_state_free(GameState* const game_state) {
  127. icon_animation_free(game_state->sprites.barry);
  128. icon_animation_free(game_state->sprites.scientist);
  129. free(game_state);
  130. }
  131. static void jetpack_game_tick(GameState* const game_state) {
  132. // Do jetpack things
  133. game_state->barry.gravity += GRAVITY_TICK;
  134. game_state->barry.point.y += game_state->barry.gravity;
  135. // Increment distance
  136. game_state->distance++;
  137. // Move coins towards the player
  138. for(int i = 0; i < COINS_MAX; i++) {
  139. if(game_state->coins[i].point.x > 0) {
  140. if(!(game_state->barry.point.x >
  141. game_state->coins[i].point.x + 7 || // Barry is to the right of the coin
  142. game_state->barry.point.x + 11 <
  143. game_state->coins[i].point.x || // Barry is to the left of the coin
  144. game_state->barry.point.y >
  145. game_state->coins[i].point.y + 7 || // Barry is below the coin
  146. game_state->barry.point.y + 15 <
  147. game_state->coins[i].point.y)) { // Barry is above the coin
  148. game_state->coins[i].point.x = 0; // Remove the coin
  149. game_state->points++; // Increase the score
  150. }
  151. game_state->coins[i].point.x -= 1; // move left by 1 unit
  152. if(game_state->coins[i].point.x < -16) { // if the coin is out of screen
  153. game_state->coins[i].point.x =
  154. 0; // set coin x coordinate to 0 to mark it as "inactive"
  155. }
  156. }
  157. }
  158. // Move particles
  159. for(int i = 0; i < PARTICLES_MAX; i++) {
  160. if(game_state->particles[i].point.y > 0) {
  161. game_state->particles[i].point.y += PARTIVLE_VELOCITY;
  162. // Check collision with scientists
  163. for(int j = 0; j < SCIENTISTS_MAX; j++) {
  164. if(game_state->scientists[j].state == ScientistStateAlive &&
  165. game_state->scientists[j].point.x > 0) {
  166. // Added half the width and height of the scientist sprite to the scientist's x and y respectively
  167. float scientist_center_x = game_state->scientists[j].point.x + 5.5;
  168. float scientist_center_y = game_state->scientists[j].point.y + 7.5;
  169. if(!(game_state->particles[i].point.x >
  170. scientist_center_x +
  171. 5.5 || // particle is to the right of the scientist
  172. game_state->particles[i].point.x + 11 <
  173. scientist_center_x -
  174. 5.5 || // particle is to the left of the scientist
  175. game_state->particles[i].point.y >
  176. scientist_center_y + 7.5 || // particle is below the scientist
  177. game_state->particles[i].point.y + 15 <
  178. scientist_center_y - 7.5)) { // particle is above the scientist
  179. game_state->scientists[j].state = ScientistStateDead;
  180. game_state->points += 2; // Increase the score by 2
  181. }
  182. }
  183. }
  184. if(game_state->particles[i].point.x < 0 || game_state->particles[i].point.x > 128 ||
  185. game_state->particles[i].point.y < 0 || game_state->particles[i].point.y > 64) {
  186. game_state->particles[i].point.y = 0;
  187. }
  188. }
  189. }
  190. // Move scientists
  191. for(int i = 0; i < SCIENTISTS_MAX; i++) {
  192. if(game_state->scientists[i].point.x > 0) {
  193. game_state->scientists[i].point.x -=
  194. game_state->scientists[i].velocity_x; // move based on velocity_x
  195. if(game_state->scientists[i].point.x < -16) { // if the scientist is out of screen
  196. game_state->scientists[i].point.x =
  197. 0; // set scientist x coordinate to 0 to mark it as "inactive"
  198. }
  199. }
  200. }
  201. // Spawn scientists and coins...
  202. jetpack_game_random_coins(game_state);
  203. // Sprite height of Barry
  204. int sprite_height = 15;
  205. // Constrain barry's height within sprite_height and 64 - sprite_height
  206. if(game_state->barry.point.y > (64 - sprite_height)) {
  207. game_state->barry.point.y = 64 - sprite_height;
  208. game_state->barry.gravity = 0; // stop upward momentum
  209. } else if(game_state->barry.point.y < 0) {
  210. game_state->barry.point.y = 0;
  211. game_state->barry.gravity = 0; // stop downward momentum
  212. }
  213. // spawn scientists and coins...
  214. jetpack_game_random_coins(game_state);
  215. jetpack_game_random_scientists(game_state);
  216. if(game_state->barry.isBoosting) {
  217. game_state->barry.gravity += GRAVITY_BOOST;
  218. jetpack_game_spawn_particles(game_state);
  219. }
  220. }
  221. static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
  222. furi_assert(ctx);
  223. const GameState* game_state = ctx;
  224. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  225. if(game_state->state == GameStateLife) {
  226. // Draw scene
  227. // Draw coins
  228. for(int i = 0; i < COINS_MAX; ++i) {
  229. if(game_state->coins[i].point.x > 0) {
  230. canvas_draw_icon(
  231. canvas, game_state->coins[i].point.x, game_state->coins[i].point.y, &I_coin);
  232. }
  233. }
  234. // Draw scientists
  235. for(int i = 0; i < SCIENTISTS_MAX; ++i) {
  236. if(game_state->scientists[i].point.x > 0) {
  237. if(game_state->scientists[i].state == ScientistStateAlive) {
  238. canvas_draw_icon_animation(
  239. canvas,
  240. game_state->scientists[i].point.x,
  241. game_state->scientists[i].point.y,
  242. game_state->sprites.scientist);
  243. } else {
  244. canvas_draw_icon(
  245. canvas,
  246. game_state->scientists[i].point.x,
  247. game_state->scientists[i].point.y,
  248. &I_dead_scientist);
  249. }
  250. }
  251. }
  252. // Draw particles
  253. for(int i = 0; i < PARTICLES_MAX; i++) {
  254. if(game_state->particles[i].point.y > 0) {
  255. canvas_draw_line(
  256. canvas,
  257. game_state->particles[i].point.x,
  258. game_state->particles[i].point.y,
  259. game_state->particles[i].point.x,
  260. game_state->particles[i].point.y + 3);
  261. }
  262. }
  263. // Draw barry
  264. canvas_draw_icon_animation(
  265. canvas,
  266. game_state->barry.point.x,
  267. game_state->barry.point.y,
  268. game_state->sprites.barry);
  269. canvas_set_font(canvas, FontSecondary);
  270. char buffer[12];
  271. snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
  272. canvas_draw_str_aligned(canvas, 123, 12, AlignRight, AlignBottom, buffer);
  273. snprintf(buffer, sizeof(buffer), "Score: %u", game_state->points);
  274. canvas_draw_str_aligned(canvas, 5, 12, AlignLeft, AlignBottom, buffer);
  275. }
  276. if(game_state->state == GameStateGameOver) {
  277. // Show highscore
  278. }
  279. canvas_draw_frame(canvas, 0, 0, 128, 64);
  280. furi_mutex_release(game_state->mutex);
  281. }
  282. static void jetpack_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  283. furi_assert(event_queue);
  284. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  285. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  286. }
  287. static void jetpack_game_update_timer_callback(FuriMessageQueue* event_queue) {
  288. furi_assert(event_queue);
  289. GameEvent event = {.type = EventTypeTick};
  290. furi_message_queue_put(event_queue, &event, 0);
  291. }
  292. int32_t jetpack_game_app(void* p) {
  293. UNUSED(p);
  294. int32_t return_code = 0;
  295. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  296. GameState* game_state = malloc(sizeof(GameState));
  297. jetpack_game_state_init(game_state);
  298. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  299. if(!game_state->mutex) {
  300. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  301. return_code = 255;
  302. goto free_and_exit;
  303. }
  304. // Set system callbacks
  305. ViewPort* view_port = view_port_alloc();
  306. view_port_draw_callback_set(view_port, jetpack_game_render_callback, game_state);
  307. view_port_input_callback_set(view_port, jetpack_game_input_callback, event_queue);
  308. FuriTimer* timer =
  309. furi_timer_alloc(jetpack_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  310. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  311. // Open GUI and register view_port
  312. Gui* gui = furi_record_open(RECORD_GUI);
  313. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  314. GameEvent event;
  315. for(bool processing = true; processing;) {
  316. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  317. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  318. if(event_status == FuriStatusOk) {
  319. // press events
  320. if(event.type == EventTypeKey) {
  321. if(event.input.type == InputTypeRelease && event.input.key == InputKeyOk) {
  322. game_state->barry.isBoosting = false;
  323. }
  324. if(event.input.type == InputTypePress) {
  325. switch(event.input.key) {
  326. case InputKeyUp:
  327. break;
  328. case InputKeyDown:
  329. break;
  330. case InputKeyRight:
  331. break;
  332. case InputKeyLeft:
  333. break;
  334. case InputKeyOk:
  335. if(game_state->state == GameStateGameOver) {
  336. jetpack_game_state_init(game_state);
  337. }
  338. if(game_state->state == GameStateLife) {
  339. // Do something
  340. game_state->barry.isBoosting = true;
  341. }
  342. break;
  343. case InputKeyBack:
  344. processing = false;
  345. break;
  346. default:
  347. break;
  348. }
  349. }
  350. } else if(event.type == EventTypeTick) {
  351. jetpack_game_tick(game_state);
  352. }
  353. }
  354. view_port_update(view_port);
  355. furi_mutex_release(game_state->mutex);
  356. }
  357. furi_timer_free(timer);
  358. view_port_enabled_set(view_port, false);
  359. gui_remove_view_port(gui, view_port);
  360. furi_record_close(RECORD_GUI);
  361. view_port_free(view_port);
  362. furi_mutex_free(game_state->mutex);
  363. free_and_exit:
  364. jetpack_game_state_free(game_state);
  365. furi_message_queue_free(event_queue);
  366. return return_code;
  367. }