scorched_tanks_game_app.c 12 KB

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