trexrunner.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <furi_hal_random.h>
  4. #include <gui/gui.h>
  5. #include <gui/icon_i.h>
  6. #include <gui/elements.h>
  7. #include <input/input.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include "t_rex_runner_icons.h"
  11. #define DINO_START_X 10
  12. #define DINO_START_Y 34 // 64 - 22 - BACKGROUND_H / 2 - 2
  13. #define FPS 20
  14. #define DINO_RUNNING_MS_PER_FRAME 500
  15. #define GRAVITY 60
  16. #define JUMP_SPEED 30
  17. #define CACTUS_W 10
  18. #define CACTUS_H 10
  19. #define START_x_speed 35
  20. #define X_INCREASE 3
  21. #define BACKGROUND_W 128
  22. #define BACKGROUND_H 12
  23. typedef enum {
  24. EventTypeTick,
  25. EventTypeKey,
  26. } EventType;
  27. typedef struct {
  28. EventType type;
  29. InputEvent input;
  30. } PluginEvent;
  31. typedef struct {
  32. FuriTimer *timer;
  33. uint32_t last_tick;
  34. const Icon* dino_icon;
  35. int dino_frame_ms;
  36. FuriMutex* mutex;
  37. // Dino info
  38. float y_position;
  39. float y_speed;
  40. int y_acceleration;
  41. float x_speed;
  42. // Cactus info
  43. int cactus_position;
  44. int has_cactus;
  45. // Horizontal line
  46. int background_position;
  47. int lost;
  48. int score;
  49. } GameState;
  50. static void timer_callback(void *ctx) {
  51. GameState *game_state = ctx;
  52. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  53. if (game_state == NULL) {
  54. return;
  55. }
  56. uint32_t ticks_elapsed = furi_get_tick() - game_state->last_tick;
  57. game_state->last_tick = furi_get_tick();
  58. int delta_time_ms = ticks_elapsed * 1000 / furi_kernel_get_tick_frequency();
  59. // dino update
  60. game_state->dino_frame_ms += delta_time_ms;
  61. // TODO: switch by dino state
  62. if (game_state->dino_frame_ms >= DINO_RUNNING_MS_PER_FRAME) {
  63. if (game_state->dino_icon == &I_DinoRun0) {
  64. game_state->dino_icon = &I_DinoRun1;
  65. } else {
  66. game_state->dino_icon = &I_DinoRun0;
  67. }
  68. game_state->dino_frame_ms = 0;
  69. }
  70. // Compute dino dynamics
  71. game_state->y_acceleration = game_state->y_acceleration - GRAVITY * delta_time_ms / 1000;
  72. game_state->y_speed = game_state->y_speed + game_state->y_acceleration * delta_time_ms / 1000;
  73. game_state->y_position = game_state->y_position - game_state->y_speed * delta_time_ms / 1000;
  74. // Touch ground
  75. if (game_state->y_position >= DINO_START_Y) {
  76. game_state->y_acceleration = 0;
  77. game_state->y_speed = 0;
  78. game_state->y_position = DINO_START_Y;
  79. }
  80. // Update Cactus state
  81. if (game_state->has_cactus){
  82. game_state->cactus_position = game_state->cactus_position - (game_state->x_speed - 15) * delta_time_ms / 1000;
  83. if (game_state->cactus_position <= 0 ) {
  84. game_state->has_cactus = 0;
  85. game_state->score = game_state->score + 1;
  86. // Increase speed
  87. game_state->x_speed = game_state->x_speed + X_INCREASE;
  88. }
  89. }
  90. // Create cactus (random frame in 1.5s)
  91. else {
  92. uint8_t randomuint8[1];
  93. furi_hal_random_fill_buf(randomuint8,1);
  94. if (randomuint8[0] % 30 == 0) {
  95. game_state->has_cactus = 1;
  96. game_state->cactus_position = 120;
  97. }
  98. }
  99. // Move horizontal line
  100. if (game_state->background_position <= -BACKGROUND_W)
  101. game_state->background_position += BACKGROUND_W;
  102. game_state->background_position = game_state->background_position - game_state->x_speed * delta_time_ms / 1000;
  103. // Lose condition
  104. if (game_state->has_cactus && ((game_state->y_position + 22 >= (64 - CACTUS_H)) && ((DINO_START_X+20) >= game_state->cactus_position) && (DINO_START_X <= (game_state->cactus_position + CACTUS_W))))
  105. game_state->lost = 1;
  106. furi_mutex_release(game_state->mutex);
  107. }
  108. static void input_callback(InputEvent *input_event, FuriMessageQueue *event_queue) {
  109. furi_assert(event_queue);
  110. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  111. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  112. }
  113. static void render_callback(Canvas *const canvas, void *ctx) {
  114. const GameState *game_state = ctx;
  115. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  116. if (game_state == NULL) {
  117. return;
  118. }
  119. char score_string[12];
  120. if(!game_state->lost){
  121. // Show Ground
  122. canvas_draw_icon(canvas, game_state->background_position, 64 - BACKGROUND_H, &I_HorizonLine0);
  123. canvas_draw_icon(canvas, game_state->background_position + BACKGROUND_W, 64 - BACKGROUND_H, &I_HorizonLine0);
  124. // Show DINO
  125. canvas_draw_icon(canvas, DINO_START_X, game_state->y_position, game_state->dino_icon);
  126. // Show cactus
  127. if (game_state->has_cactus)
  128. //canvas_draw_triangle(canvas, game_state->cactus_position, 64 - BACKGROUND_H + CACTUS_W, CACTUS_W, CACTUS_H, CanvasDirectionBottomToTop);
  129. canvas_draw_icon(canvas, game_state->cactus_position, 64 - BACKGROUND_H/2 - CACTUS_H - 2, &I_Cactus);
  130. // Show score
  131. if(game_state->score == 0)
  132. canvas_set_font(canvas, FontSecondary);
  133. snprintf(score_string, 12, "Score: %d", game_state->score);
  134. canvas_draw_str_aligned(canvas, 85, 5, AlignLeft, AlignTop, score_string);
  135. } else {
  136. canvas_set_font(canvas, FontPrimary);
  137. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "You lost :c");
  138. }
  139. furi_mutex_release(game_state->mutex);
  140. }
  141. static void game_state_init(GameState *const game_state) {
  142. game_state->last_tick = furi_get_tick();
  143. game_state->dino_frame_ms = 0;
  144. game_state->dino_icon = &I_Dino;
  145. game_state->y_acceleration = game_state->y_speed = 0;
  146. game_state->y_position = DINO_START_Y;
  147. game_state->has_cactus = 0;
  148. game_state->background_position = 0;
  149. game_state->lost = 0;
  150. game_state->x_speed = START_x_speed;
  151. game_state->score = 0;
  152. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  153. }
  154. static void game_state_reinit(GameState* const game_state) {
  155. game_state->last_tick = furi_get_tick();
  156. game_state->y_acceleration = game_state->y_speed = 0;
  157. game_state->y_position = DINO_START_Y;
  158. game_state->has_cactus = 0;
  159. game_state->background_position = 0;
  160. game_state->lost = 0;
  161. game_state->x_speed = START_x_speed;
  162. game_state->score = 0;
  163. }
  164. int32_t trexrunner_app() {
  165. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  166. GameState *game_state = malloc(sizeof(GameState));
  167. game_state_init(game_state);
  168. if (!game_state->mutex) {
  169. FURI_LOG_E("T-rex runner", "cannot create mutex\r\n");
  170. free(game_state);
  171. return 255;
  172. }
  173. // BEGIN IMPLEMENTATION
  174. // Set system callbacks
  175. ViewPort *view_port = view_port_alloc();
  176. view_port_draw_callback_set(view_port, render_callback, game_state);
  177. view_port_input_callback_set(view_port, input_callback, event_queue);
  178. game_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, game_state);
  179. furi_timer_start(game_state->timer, (uint32_t) furi_kernel_get_tick_frequency() / FPS);
  180. // Open GUI and register view_port
  181. Gui *gui = furi_record_open("gui");
  182. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  183. PluginEvent event;
  184. for (bool processing = true; processing;) {
  185. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  186. if (event_status == FuriStatusOk) {
  187. // press events
  188. if (event.type == EventTypeKey) {
  189. if (event.input.type == InputTypeShort) {
  190. switch (event.input.key) {
  191. case InputKeyUp:
  192. break;
  193. case InputKeyDown:
  194. break;
  195. case InputKeyLeft:
  196. break;
  197. case InputKeyRight:
  198. break;
  199. case InputKeyOk:
  200. if(game_state->lost) {
  201. game_state_reinit(game_state);
  202. break;
  203. }
  204. if(game_state->y_position == DINO_START_Y)
  205. game_state->y_speed = JUMP_SPEED;
  206. break;
  207. case InputKeyMAX:
  208. break;
  209. case InputKeyBack:
  210. // Exit the app
  211. processing = false;
  212. break;
  213. }
  214. }
  215. }
  216. // } else {
  217. // // event timeout
  218. // ;
  219. }
  220. view_port_update(view_port);
  221. furi_mutex_release(game_state->mutex);
  222. }
  223. view_port_enabled_set(view_port, false);
  224. gui_remove_view_port(gui, view_port);
  225. furi_record_close("gui");
  226. view_port_free(view_port);
  227. furi_message_queue_free(event_queue);
  228. furi_mutex_free(game_state->mutex);
  229. furi_timer_free(game_state->timer);
  230. free(game_state);
  231. return 0;
  232. }