laser_tag_app.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "laser_tag_app.h"
  2. #include "laser_tag_view.h"
  3. #include "infrared_controller.h"
  4. #include "game_state.h"
  5. #include <furi.h>
  6. #include <gui/gui.h>
  7. #include <input/input.h>
  8. #include <notification/notification.h>
  9. #define TAG "LaserTagApp"
  10. struct LaserTagApp {
  11. Gui* gui;
  12. ViewPort* view_port;
  13. LaserTagView* view;
  14. FuriMessageQueue* event_queue;
  15. FuriTimer* timer;
  16. NotificationApp* notifications;
  17. InfraredController* ir_controller;
  18. GameState* game_state;
  19. LaserTagState state;
  20. bool need_redraw;
  21. };
  22. const NotificationSequence sequence_vibro_1 = {&message_vibro_on, &message_vibro_off, NULL};
  23. static void laser_tag_app_timer_callback(void* context) {
  24. furi_assert(context);
  25. LaserTagApp* app = context;
  26. FURI_LOG_D(TAG, "Timer callback triggered");
  27. if(app->state == LaserTagStateSplashScreen) {
  28. if(game_state_get_time(app->game_state) >= 2) {
  29. FURI_LOG_I(TAG, "Splash screen time over, switching to TeamSelect");
  30. app->state = LaserTagStateTeamSelect;
  31. game_state_reset(app->game_state);
  32. FURI_LOG_D(TAG, "Game state reset after splash screen");
  33. } else {
  34. FURI_LOG_D(TAG, "Updating splash screen time");
  35. game_state_update_time(app->game_state, 1);
  36. }
  37. } else if(app->state == LaserTagStateGame) {
  38. FURI_LOG_D(TAG, "Updating game time by 1 second");
  39. game_state_update_time(app->game_state, 1);
  40. }
  41. if(app->view) {
  42. FURI_LOG_D(TAG, "Updating view with the latest game state");
  43. laser_tag_view_update(app->view, app->game_state);
  44. app->need_redraw = true;
  45. }
  46. }
  47. static void laser_tag_app_input_callback(InputEvent* input_event, void* context) {
  48. furi_assert(context);
  49. LaserTagApp* app = context;
  50. FURI_LOG_D(TAG, "Input event received: type=%d, key=%d", input_event->type, input_event->key);
  51. furi_message_queue_put(app->event_queue, input_event, 0);
  52. FURI_LOG_D(TAG, "Input event queued successfully");
  53. }
  54. static void laser_tag_app_draw_callback(Canvas* canvas, void* context) {
  55. furi_assert(context);
  56. LaserTagApp* app = context;
  57. FURI_LOG_D(TAG, "Entering draw callback");
  58. if(app->state == LaserTagStateSplashScreen) {
  59. FURI_LOG_D(TAG, "Drawing splash screen");
  60. canvas_clear(canvas);
  61. canvas_set_font(canvas, FontPrimary);
  62. canvas_draw_str(canvas, 32, 32, "Laser Tag!");
  63. } else if(app->state == LaserTagStateTeamSelect) {
  64. FURI_LOG_D(TAG, "Drawing team selection screen");
  65. canvas_clear(canvas);
  66. canvas_set_font(canvas, FontPrimary);
  67. canvas_draw_str(canvas, 32, 32, "Select Team:");
  68. canvas_set_font(canvas, FontSecondary);
  69. canvas_draw_str(canvas, 32, 48, "LEFT: Red RIGHT: Blue");
  70. } else if(app->view) {
  71. FURI_LOG_D(TAG, "Drawing game view");
  72. laser_tag_view_draw(laser_tag_view_get_view(app->view), canvas);
  73. }
  74. FURI_LOG_D(TAG, "Exiting draw callback");
  75. }
  76. LaserTagApp* laser_tag_app_alloc() {
  77. FURI_LOG_D(TAG, "Allocating Laser Tag App");
  78. LaserTagApp* app = malloc(sizeof(LaserTagApp));
  79. if(!app) {
  80. FURI_LOG_E(TAG, "Failed to allocate LaserTagApp");
  81. return NULL;
  82. }
  83. FURI_LOG_I(TAG, "LaserTagApp allocated successfully");
  84. memset(app, 0, sizeof(LaserTagApp));
  85. app->gui = furi_record_open(RECORD_GUI);
  86. app->view_port = view_port_alloc();
  87. app->view = laser_tag_view_alloc();
  88. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  89. app->game_state = game_state_alloc();
  90. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  91. if(!app->gui || !app->view_port || !app->view || !app->notifications || !app->game_state ||
  92. !app->event_queue) {
  93. FURI_LOG_E(TAG, "Failed to allocate resources for LaserTagApp");
  94. laser_tag_app_free(app);
  95. return NULL;
  96. }
  97. app->state = LaserTagStateSplashScreen;
  98. app->need_redraw = true;
  99. FURI_LOG_I(TAG, "Initial state set to SplashScreen");
  100. view_port_draw_callback_set(app->view_port, laser_tag_app_draw_callback, app);
  101. view_port_input_callback_set(app->view_port, laser_tag_app_input_callback, app);
  102. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  103. FURI_LOG_D(TAG, "ViewPort callbacks set and added to GUI");
  104. app->timer = furi_timer_alloc(laser_tag_app_timer_callback, FuriTimerTypePeriodic, app);
  105. if(!app->timer) {
  106. FURI_LOG_E(TAG, "Failed to allocate timer");
  107. laser_tag_app_free(app);
  108. return NULL;
  109. }
  110. FURI_LOG_I(TAG, "Timer allocated");
  111. furi_timer_start(app->timer, furi_kernel_get_tick_frequency());
  112. FURI_LOG_D(TAG, "Timer started");
  113. return app;
  114. }
  115. void laser_tag_app_free(LaserTagApp* app) {
  116. FURI_LOG_D(TAG, "Freeing Laser Tag App");
  117. furi_assert(app);
  118. furi_timer_free(app->timer);
  119. view_port_enabled_set(app->view_port, false);
  120. gui_remove_view_port(app->gui, app->view_port);
  121. view_port_free(app->view_port);
  122. laser_tag_view_free(app->view);
  123. furi_message_queue_free(app->event_queue);
  124. if(app->ir_controller) {
  125. infrared_controller_free(app->ir_controller);
  126. }
  127. free(app->game_state);
  128. furi_record_close(RECORD_GUI);
  129. furi_record_close(RECORD_NOTIFICATION);
  130. free(app);
  131. FURI_LOG_I(TAG, "Laser Tag App freed successfully");
  132. }
  133. void laser_tag_app_fire(LaserTagApp* app) {
  134. furi_assert(app);
  135. FURI_LOG_D(TAG, "Firing laser");
  136. if(!app->ir_controller) {
  137. FURI_LOG_E(TAG, "IR controller is NULL in laser_tag_app_fire");
  138. return;
  139. }
  140. infrared_controller_send(app->ir_controller);
  141. FURI_LOG_D(TAG, "Laser fired, decreasing ammo by 1");
  142. game_state_decrease_ammo(app->game_state, 1);
  143. notification_message(app->notifications, &sequence_blink_blue_100);
  144. FURI_LOG_I(TAG, "Notifying user with blink blue");
  145. app->need_redraw = true;
  146. }
  147. void laser_tag_app_handle_hit(LaserTagApp* app) {
  148. furi_assert(app);
  149. FURI_LOG_D(TAG, "Handling hit, decreasing health by 10");
  150. game_state_decrease_health(app->game_state, 10);
  151. notification_message(app->notifications, &sequence_vibro_1);
  152. FURI_LOG_I(TAG, "Notifying user with vibration");
  153. app->need_redraw = true;
  154. }
  155. static bool laser_tag_app_enter_game_state(LaserTagApp* app) {
  156. furi_assert(app);
  157. FURI_LOG_I(TAG, "Entering game state");
  158. app->state = LaserTagStateGame;
  159. game_state_reset(app->game_state);
  160. FURI_LOG_D(TAG, "Game state reset");
  161. laser_tag_view_update(app->view, app->game_state);
  162. FURI_LOG_D(TAG, "View updated with new game state");
  163. app->ir_controller = infrared_controller_alloc();
  164. if(!app->ir_controller) {
  165. FURI_LOG_E(TAG, "Failed to allocate IR controller");
  166. return false;
  167. }
  168. FURI_LOG_I(TAG, "IR controller allocated");
  169. infrared_controller_set_team(app->ir_controller, game_state_get_team(app->game_state));
  170. FURI_LOG_D(TAG, "IR controller team set");
  171. app->need_redraw = true;
  172. return true;
  173. }
  174. int32_t laser_tag_app(void* p) {
  175. UNUSED(p);
  176. FURI_LOG_I(TAG, "Laser Tag app starting");
  177. LaserTagApp* app = laser_tag_app_alloc();
  178. if(!app) {
  179. FURI_LOG_E(TAG, "Failed to allocate application");
  180. return -1;
  181. }
  182. FURI_LOG_D(TAG, "LaserTagApp allocated successfully");
  183. InputEvent event;
  184. bool running = true;
  185. while(running) {
  186. FURI_LOG_D(TAG, "Start of main loop iteration");
  187. FuriStatus status = furi_message_queue_get(app->event_queue, &event, 100);
  188. if(status == FuriStatusOk) {
  189. FURI_LOG_D(TAG, "Received input event: type=%d, key=%d", event.type, event.key);
  190. if(event.type == InputTypePress || event.type == InputTypeRepeat) {
  191. if(app->state == LaserTagStateSplashScreen ||
  192. app->state == LaserTagStateTeamSelect) {
  193. switch(event.key) {
  194. case InputKeyLeft:
  195. FURI_LOG_I(TAG, "Red team selected");
  196. game_state_set_team(app->game_state, TeamRed);
  197. if(!laser_tag_app_enter_game_state(app)) {
  198. running = false;
  199. }
  200. break;
  201. case InputKeyRight:
  202. FURI_LOG_I(TAG, "Blue team selected");
  203. game_state_set_team(app->game_state, TeamBlue);
  204. if(!laser_tag_app_enter_game_state(app)) {
  205. running = false;
  206. }
  207. break;
  208. case InputKeyBack:
  209. FURI_LOG_I(TAG, "Back key pressed, exiting");
  210. running = false;
  211. break;
  212. default:
  213. break;
  214. }
  215. } else {
  216. switch(event.key) {
  217. case InputKeyBack:
  218. FURI_LOG_I(TAG, "Back key pressed, exiting");
  219. running = false;
  220. break;
  221. case InputKeyOk:
  222. FURI_LOG_I(TAG, "OK key pressed, firing laser");
  223. laser_tag_app_fire(app);
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. }
  230. } else if(status == FuriStatusErrorTimeout) {
  231. FURI_LOG_D(TAG, "No input event, continuing");
  232. } else {
  233. FURI_LOG_E(TAG, "Failed to get input event, status: %d", status);
  234. }
  235. if(app->state == LaserTagStateGame && app->ir_controller) {
  236. if(infrared_controller_receive(app->ir_controller)) {
  237. FURI_LOG_D(TAG, "Hit received, processing");
  238. laser_tag_app_handle_hit(app);
  239. }
  240. if(game_state_is_game_over(app->game_state)) {
  241. FURI_LOG_I(TAG, "Game over, notifying user with error sequence");
  242. notification_message(app->notifications, &sequence_error);
  243. running = false;
  244. }
  245. }
  246. if(app->need_redraw) {
  247. FURI_LOG_D(TAG, "Updating viewport");
  248. view_port_update(app->view_port);
  249. app->need_redraw = false;
  250. }
  251. FURI_LOG_D(TAG, "End of main loop iteration");
  252. furi_delay_ms(10);
  253. }
  254. FURI_LOG_I(TAG, "Laser Tag app exiting");
  255. laser_tag_app_free(app);
  256. return 0;
  257. }