scorched_tanks_game_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. // That's a filthy workaround but sin(player.aimAngle) breaks it all... If you're able to fix it, please do create a PR!
  7. double scorched_tanks_sin[91] = {
  8. 0.000, -0.017, -0.035, -0.052, -0.070, -0.087, -0.105, -0.122, -0.139, -0.156, -0.174, -0.191,
  9. -0.208, -0.225, -0.242, -0.259, -0.276, -0.292, -0.309, -0.326, -0.342, -0.358, -0.375, -0.391,
  10. -0.407, -0.423, -0.438, -0.454, -0.469, -0.485, -0.500, -0.515, -0.530, -0.545, -0.559, -0.574,
  11. -0.588, -0.602, -0.616, -0.629, -0.643, -0.656, -0.669, -0.682, -0.695, -0.707, -0.719, -0.731,
  12. -0.743, -0.755, -0.766, -0.777, -0.788, -0.799, -0.809, -0.819, -0.829, -0.839, -0.848, -0.857,
  13. -0.866, -0.875, -0.883, -0.891, -0.899, -0.906, -0.914, -0.921, -0.927, -0.934, -0.940, -0.946,
  14. -0.951, -0.956, -0.961, -0.966, -0.970, -0.974, -0.978, -0.982, -0.985, -0.988, -0.990, -0.993,
  15. -0.995, -0.996, -0.998, -0.999, -0.999, -1.000, -1.000};
  16. double scorched_tanks_cos[91] = {
  17. 1.000, 1.000, 0.999, 0.999, 0.998, 0.996, 0.995, 0.993, 0.990, 0.988, 0.985, 0.982, 0.978,
  18. 0.974, 0.970, 0.966, 0.961, 0.956, 0.951, 0.946, 0.940, 0.934, 0.927, 0.921, 0.914, 0.906,
  19. 0.899, 0.891, 0.883, 0.875, 0.866, 0.857, 0.848, 0.839, 0.829, 0.819, 0.809, 0.799, 0.788,
  20. 0.777, 0.766, 0.755, 0.743, 0.731, 0.719, 0.707, 0.695, 0.682, 0.669, 0.656, 0.643, 0.629,
  21. 0.616, 0.602, 0.588, 0.574, 0.559, 0.545, 0.530, 0.515, 0.500, 0.485, 0.469, 0.454, 0.438,
  22. 0.423, 0.407, 0.391, 0.375, 0.358, 0.342, 0.326, 0.309, 0.292, 0.276, 0.259, 0.242, 0.225,
  23. 0.208, 0.191, 0.174, 0.156, 0.139, 0.122, 0.105, 0.087, 0.070, 0.052, 0.035, 0.017, 0.000};
  24. double scorched_tanks_tan[91] = {
  25. 0.000, -0.017, -0.035, -0.052, -0.070, -0.087, -0.105, -0.123, -0.141, -0.158, -0.176,
  26. -0.194, -0.213, -0.231, -0.249, -0.268, -0.287, -0.306, -0.325, -0.344, -0.364, -0.384,
  27. -0.404, -0.424, -0.445, -0.466, -0.488, -0.510, -0.532, -0.554, -0.577, -0.601, -0.625,
  28. -0.649, -0.674, -0.700, -0.727, -0.754, -0.781, -0.810, -0.839, -0.869, -0.900, -0.932,
  29. -0.966, -1.000, -1.036, -1.072, -1.111, -1.150, -1.192, -1.235, -1.280, -1.327, -1.376,
  30. -1.428, -1.483, -1.540, -1.600, -1.664, -1.732, -1.804, -1.881, -1.963, -2.050, -2.144,
  31. -2.246, -2.356, -2.475, -2.605, -2.747, -2.904, -3.078, -3.271, -3.487, -3.732, -4.011,
  32. -4.331, -4.704, -5.144, -5.671, -6.313, -7.115, -8.144, -9.513, -11.429, -14.298, -19.077,
  33. -28.627, -57.254, -90747.269};
  34. typedef struct {
  35. // +-----x
  36. // |
  37. // |
  38. // y
  39. uint8_t x;
  40. uint8_t y;
  41. } Point;
  42. typedef struct {
  43. unsigned char hp;
  44. int aimAngle;
  45. unsigned char locationX;
  46. bool isShooting;
  47. } Tank;
  48. typedef struct {
  49. Point ground[128];
  50. Tank player;
  51. Tank enemy;
  52. bool isPlayerTurn;
  53. unsigned char trajectoryY[128];
  54. unsigned char trajectoryAnimationStep;
  55. Point bulletPosition;
  56. } Game;
  57. typedef enum {
  58. EventTypeTick,
  59. EventTypeKey,
  60. } EventType;
  61. typedef struct {
  62. EventType type;
  63. InputEvent input;
  64. } ScorchedTanksEvent;
  65. int scorched_tanks_random(int min, int max) {
  66. return min + rand() % ((max + 1) - min);
  67. }
  68. void scorched_tanks_generate_ground(Game* game_state) {
  69. unsigned char ScreenWith = 128;
  70. // unsigned char ScreenHeight = 64;
  71. auto lastHeight = 45;
  72. for(unsigned char a = 0; a < ScreenWith; a++) {
  73. auto diffHeight = scorched_tanks_random(-2, 3);
  74. auto changeLength = scorched_tanks_random(1, 6);
  75. if(diffHeight == 0) {
  76. changeLength = 1;
  77. }
  78. for(int b = 0; b < changeLength; b++) {
  79. if(a + b < ScreenWith) {
  80. auto index = a + b;
  81. auto newPoint = lastHeight + diffHeight;
  82. newPoint = newPoint < 35 ? 35 : newPoint;
  83. newPoint = newPoint > 55 ? 55 : newPoint;
  84. game_state->ground[index].x = index;
  85. game_state->ground[index].y = newPoint;
  86. lastHeight = newPoint;
  87. } else {
  88. a += b;
  89. break;
  90. }
  91. }
  92. a += changeLength - 1;
  93. }
  94. }
  95. void scorched_tanks_calculate_trajectory(Game* game_state) {
  96. if(game_state->player.isShooting) {
  97. int x0 = game_state->player.locationX;
  98. int y0 = game_state->ground[game_state->player.locationX].y - 3;
  99. int v0 = 50;
  100. int g = 32;
  101. int angle = game_state->player.aimAngle;
  102. if(x0 + game_state->trajectoryAnimationStep > 128 ||
  103. game_state->bulletPosition.x > 128 ||
  104. game_state->bulletPosition.y > game_state->ground[game_state->bulletPosition.x].y) {
  105. game_state->player.isShooting = false;
  106. game_state->bulletPosition.x = 0;
  107. game_state->bulletPosition.y = 0;
  108. return;
  109. }
  110. auto x = game_state->trajectoryAnimationStep;
  111. auto y = y0 + x * scorched_tanks_tan[angle] -
  112. g * x * x / -(2 * v0 * v0 * scorched_tanks_cos[angle] * scorched_tanks_cos[angle]);
  113. x += x0;
  114. if(x % 4 == 0) {
  115. game_state->trajectoryY[x] = y;
  116. }
  117. game_state->bulletPosition.x = x;
  118. game_state->bulletPosition.y = y;
  119. game_state->trajectoryAnimationStep++;
  120. }
  121. }
  122. void scorched_tanks_init_game(Game* game_state) {
  123. game_state->player.locationX = 20;
  124. game_state->player.aimAngle = 45;
  125. game_state->enemy.locationX = 108;
  126. scorched_tanks_generate_ground(game_state);
  127. }
  128. static void scorched_tanks_render_callback(Canvas* const canvas, void* ctx) {
  129. const Game* game_state = acquire_mutex((ValueMutex*)ctx, 25);
  130. if(game_state == NULL) {
  131. return;
  132. }
  133. canvas_draw_frame(canvas, 0, 0, 128, 64);
  134. canvas_set_color(canvas, ColorBlack);
  135. if(game_state->player.isShooting) {
  136. canvas_draw_dot(canvas, game_state->bulletPosition.x, game_state->bulletPosition.y);
  137. }
  138. for(int a = 1; a < 128; a++) {
  139. canvas_draw_line(
  140. canvas,
  141. game_state->ground[a - 1].x,
  142. game_state->ground[a - 1].y,
  143. game_state->ground[a].x,
  144. game_state->ground[a].y);
  145. if(game_state->trajectoryY[a] != 0) {
  146. canvas_draw_dot(canvas, a, game_state->trajectoryY[a]);
  147. }
  148. }
  149. canvas_draw_disc(
  150. canvas,
  151. game_state->enemy.locationX,
  152. game_state->ground[game_state->enemy.locationX].y - 3,
  153. 3);
  154. canvas_draw_circle(
  155. canvas,
  156. game_state->player.locationX,
  157. game_state->ground[game_state->player.locationX].y - 3,
  158. 3);
  159. auto aimX1 = game_state->player.locationX;
  160. auto aimY1 = game_state->ground[game_state->player.locationX].y - 3;
  161. double sinFromAngle = scorched_tanks_sin[game_state->player.aimAngle];
  162. double cosFromAngle = scorched_tanks_cos[game_state->player.aimAngle];
  163. int aimX2 = aimX1 + 8 * cosFromAngle;
  164. int aimY2 = aimY1 + 8 * sinFromAngle;
  165. canvas_draw_line(canvas, aimX1, aimY1, aimX2, aimY2);
  166. canvas_set_font(canvas, FontSecondary);
  167. canvas_draw_str(canvas, 40, 10, "Scorched Tanks");
  168. char buffer[12];
  169. snprintf(buffer, sizeof(buffer), "a: %u", game_state->player.aimAngle);
  170. canvas_draw_str(canvas, 2, 10, buffer);
  171. release_mutex((ValueMutex*)ctx, game_state);
  172. }
  173. static void scorched_tanks_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  174. furi_assert(event_queue);
  175. ScorchedTanksEvent event = {.type = EventTypeKey, .input = *input_event};
  176. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  177. }
  178. static void scorched_tanks_update_timer_callback(FuriMessageQueue* event_queue) {
  179. furi_assert(event_queue);
  180. ScorchedTanksEvent event = {.type = EventTypeTick};
  181. furi_message_queue_put(event_queue, &event, 0);
  182. }
  183. static void scorched_tanks_move_right(Game* game_state) {
  184. if(game_state->player.locationX < 128 - 3) {
  185. game_state->player.locationX++;
  186. }
  187. }
  188. static void scorched_tanks_move_left(Game* game_state) {
  189. if(game_state->player.locationX > 0 + 3) {
  190. game_state->player.locationX--;
  191. }
  192. }
  193. static void scorched_tanks_aim_up(Game* game_state) {
  194. if(game_state->player.aimAngle < 90) {
  195. game_state->player.aimAngle++;
  196. }
  197. }
  198. static void scorched_tanks_aim_down(Game* game_state) {
  199. if(game_state->player.aimAngle > 0) {
  200. game_state->player.aimAngle--;
  201. }
  202. }
  203. static void scorched_tanks_fire(Game* game_state) {
  204. if(!game_state->player.isShooting) {
  205. game_state->bulletPosition.x = game_state->player.locationX;
  206. game_state->bulletPosition.y = game_state->ground[game_state->player.locationX].y - 3;
  207. game_state->trajectoryAnimationStep = 0;
  208. for(int x = 0; x < 128; x++) {
  209. game_state->trajectoryY[x] = 0;
  210. }
  211. game_state->player.isShooting = true;
  212. }
  213. }
  214. int32_t scorched_tanks_game_app(void* p) {
  215. UNUSED(p);
  216. srand(DWT->CYCCNT);
  217. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(ScorchedTanksEvent));
  218. Game* game_state = malloc(sizeof(Game));
  219. scorched_tanks_init_game(game_state);
  220. ValueMutex state_mutex;
  221. if(!init_mutex(&state_mutex, game_state, sizeof(ScorchedTanksEvent))) {
  222. FURI_LOG_E("ScorchedTanks", "cannot create mutex\r\n");
  223. free(game_state);
  224. return 255;
  225. }
  226. ViewPort* view_port = view_port_alloc();
  227. view_port_draw_callback_set(view_port, scorched_tanks_render_callback, &state_mutex);
  228. view_port_input_callback_set(view_port, scorched_tanks_input_callback, event_queue);
  229. FuriTimer* timer =
  230. furi_timer_alloc(scorched_tanks_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  231. furi_timer_start(timer, 2000);
  232. // Open GUI and register view_port
  233. Gui* gui = furi_record_open(RECORD_GUI);
  234. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  235. ScorchedTanksEvent event;
  236. for(bool processing = true; processing;) {
  237. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 50);
  238. if(event.type == EventTypeKey) { // && game->isPlayerTurn
  239. if(event.input.type == InputTypeRepeat || event.input.type == InputTypeShort) {
  240. switch(event.input.key) {
  241. case InputKeyUp:
  242. scorched_tanks_aim_up(game_state);
  243. break;
  244. case InputKeyDown:
  245. scorched_tanks_aim_down(game_state);
  246. break;
  247. case InputKeyRight:
  248. scorched_tanks_move_right(game_state);
  249. break;
  250. case InputKeyLeft:
  251. scorched_tanks_move_left(game_state);
  252. break;
  253. case InputKeyOk:
  254. scorched_tanks_fire(game_state);
  255. break;
  256. case InputKeyBack:
  257. processing = false;
  258. break;
  259. }
  260. }
  261. } else if(event.type == EventTypeTick) {
  262. scorched_tanks_calculate_trajectory(game_state);
  263. }
  264. view_port_update(view_port);
  265. release_mutex(&state_mutex, game_state);
  266. }
  267. furi_timer_free(timer);
  268. view_port_enabled_set(view_port, false);
  269. gui_remove_view_port(gui, view_port);
  270. furi_record_close(RECORD_GUI);
  271. view_port_free(view_port);
  272. furi_message_queue_free(event_queue);
  273. delete_mutex(&state_mutex);
  274. free(game_state);
  275. return 0;
  276. }