scorched_tanks_game_app.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <notification/notification.h>
  7. #include <notification/notification_messages.h>
  8. #define SCREEN_WIDTH 128
  9. #define SCREEN_HEIGHT 64
  10. #define PLAYER_INIT_LOCATION_X 20
  11. #define PLAYER_INIT_AIM 45
  12. #define PLAYER_INIT_POWER 50
  13. #define ENEMY_INIT_LOCATION_X 108
  14. #define TANK_BARREL_LENGTH 8
  15. #define GRAVITY_FORCE (double)0.5
  16. #define MIN_GROUND_HEIGHT 35
  17. #define MAX_GROUND_HEIGHT 55
  18. #define MAX_FIRE_POWER 100
  19. #define MIN_FIRE_POWER 0
  20. #define TANK_COLLIDER_SIZE 3
  21. #define MAX_WIND 10
  22. #define MAX_PLAYER_DIFF_X 20
  23. #define MAX_ENEMY_DIFF_X 20
  24. // That's a filthy workaround but sin(player.aimAngle) breaks it all... If you're able to fix it, please do create a PR!
  25. double scorched_tanks_sin[91] = {
  26. 0.000, -0.017, -0.035, -0.052, -0.070, -0.087, -0.105, -0.122, -0.139, -0.156, -0.174, -0.191,
  27. -0.208, -0.225, -0.242, -0.259, -0.276, -0.292, -0.309, -0.326, -0.342, -0.358, -0.375, -0.391,
  28. -0.407, -0.423, -0.438, -0.454, -0.469, -0.485, -0.500, -0.515, -0.530, -0.545, -0.559, -0.574,
  29. -0.588, -0.602, -0.616, -0.629, -0.643, -0.656, -0.669, -0.682, -0.695, -0.707, -0.719, -0.731,
  30. -0.743, -0.755, -0.766, -0.777, -0.788, -0.799, -0.809, -0.819, -0.829, -0.839, -0.848, -0.857,
  31. -0.866, -0.875, -0.883, -0.891, -0.899, -0.906, -0.914, -0.921, -0.927, -0.934, -0.940, -0.946,
  32. -0.951, -0.956, -0.961, -0.966, -0.970, -0.974, -0.978, -0.982, -0.985, -0.988, -0.990, -0.993,
  33. -0.995, -0.996, -0.998, -0.999, -0.999, -1.000, -1.000};
  34. double scorched_tanks_cos[91] = {
  35. 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,
  36. 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,
  37. 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,
  38. 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,
  39. 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,
  40. 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,
  41. 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};
  42. double scorched_tanks_tan[91] = {
  43. 0.000, -0.017, -0.035, -0.052, -0.070, -0.087, -0.105, -0.123, -0.141, -0.158, -0.176,
  44. -0.194, -0.213, -0.231, -0.249, -0.268, -0.287, -0.306, -0.325, -0.344, -0.364, -0.384,
  45. -0.404, -0.424, -0.445, -0.466, -0.488, -0.510, -0.532, -0.554, -0.577, -0.601, -0.625,
  46. -0.649, -0.674, -0.700, -0.727, -0.754, -0.781, -0.810, -0.839, -0.869, -0.900, -0.932,
  47. -0.966, -1.000, -1.036, -1.072, -1.111, -1.150, -1.192, -1.235, -1.280, -1.327, -1.376,
  48. -1.428, -1.483, -1.540, -1.600, -1.664, -1.732, -1.804, -1.881, -1.963, -2.050, -2.144,
  49. -2.246, -2.356, -2.475, -2.605, -2.747, -2.904, -3.078, -3.271, -3.487, -3.732, -4.011,
  50. -4.331, -4.704, -5.144, -5.671, -6.313, -7.115, -8.144, -9.513, -11.429, -14.298, -19.077,
  51. -28.627, -57.254, -90747.269};
  52. uint8_t scorched_tanks_ground_modifiers[SCREEN_WIDTH] = {
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 28, 26, 24, 22, 20,
  56. 18, 16, 14, 12, 10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  59. typedef struct {
  60. // +-----x
  61. // |
  62. // |
  63. // y
  64. uint8_t x;
  65. uint8_t y;
  66. } Point;
  67. typedef struct {
  68. // +-----x
  69. // |
  70. // |
  71. // y
  72. double x;
  73. double y;
  74. } PointDetailed;
  75. typedef struct {
  76. uint8_t locationX;
  77. uint8_t hp;
  78. int aimAngle;
  79. uint8_t firePower;
  80. } Tank;
  81. typedef struct {
  82. Point ground[SCREEN_WIDTH];
  83. Tank player;
  84. Tank enemy;
  85. bool isPlayerTurn;
  86. bool isShooting;
  87. int windSpeed;
  88. Point trajectory[SCREEN_WIDTH];
  89. uint8_t trajectoryAnimationStep;
  90. PointDetailed bulletPosition;
  91. PointDetailed bulletVector;
  92. FuriMutex* mutex;
  93. } Game;
  94. typedef enum {
  95. EventTypeTick,
  96. EventTypeKey,
  97. } EventType;
  98. typedef struct {
  99. EventType type;
  100. InputEvent input;
  101. } ScorchedTanksEvent;
  102. int scorched_tanks_random(int min, int max) {
  103. return min + rand() % ((max + 1) - min);
  104. }
  105. void scorched_tanks_generate_ground(Game* game_state) {
  106. int lastHeight = 45;
  107. for(uint8_t a = 0; a < SCREEN_WIDTH; a++) {
  108. int diffHeight = scorched_tanks_random(-2, 3);
  109. int changeLength = scorched_tanks_random(1, 6);
  110. if(diffHeight == 0) {
  111. changeLength = 1;
  112. }
  113. for(int b = 0; b < changeLength; b++) {
  114. if(a + b < SCREEN_WIDTH) {
  115. int index = a + b;
  116. int newPoint = lastHeight + diffHeight;
  117. newPoint = newPoint < MIN_GROUND_HEIGHT ? MIN_GROUND_HEIGHT : newPoint;
  118. newPoint = newPoint > MAX_GROUND_HEIGHT ? MAX_GROUND_HEIGHT : newPoint;
  119. game_state->ground[index].x = index;
  120. game_state->ground[index].y = newPoint - scorched_tanks_ground_modifiers[a];
  121. lastHeight = newPoint;
  122. } else {
  123. a += b;
  124. break;
  125. }
  126. }
  127. a += changeLength - 1;
  128. }
  129. }
  130. void scorched_tanks_init_game(Game* game_state) {
  131. game_state->player.locationX = PLAYER_INIT_LOCATION_X +
  132. scorched_tanks_random(0, MAX_PLAYER_DIFF_X) -
  133. MAX_PLAYER_DIFF_X / 2;
  134. game_state->player.aimAngle = PLAYER_INIT_AIM;
  135. game_state->player.firePower = PLAYER_INIT_POWER;
  136. game_state->enemy.aimAngle = PLAYER_INIT_AIM;
  137. game_state->enemy.firePower = PLAYER_INIT_POWER;
  138. game_state->enemy.locationX =
  139. ENEMY_INIT_LOCATION_X + scorched_tanks_random(0, MAX_ENEMY_DIFF_X) - MAX_ENEMY_DIFF_X / 2;
  140. game_state->isPlayerTurn = true;
  141. game_state->windSpeed = scorched_tanks_random(0, MAX_WIND);
  142. for(int x = 0; x < SCREEN_WIDTH; x++) {
  143. game_state->trajectory[x].x = 0;
  144. game_state->trajectory[x].y = 0;
  145. }
  146. scorched_tanks_generate_ground(game_state);
  147. }
  148. void scorched_tanks_calculate_trajectory(Game* game_state) {
  149. if(game_state->isShooting) {
  150. game_state->bulletVector.x += ((double)game_state->windSpeed - MAX_WIND / 2) / 40;
  151. game_state->bulletVector.y += GRAVITY_FORCE;
  152. game_state->bulletPosition.x += game_state->bulletVector.x;
  153. game_state->bulletPosition.y += game_state->bulletVector.y;
  154. int totalDistanceToEnemy = 100;
  155. if(game_state->isPlayerTurn) {
  156. double distanceToEnemyX = game_state->enemy.locationX - game_state->bulletPosition.x;
  157. double distanceToEnemyY = game_state->ground[game_state->enemy.locationX].y -
  158. TANK_COLLIDER_SIZE - game_state->bulletPosition.y;
  159. totalDistanceToEnemy =
  160. sqrt(distanceToEnemyX * distanceToEnemyX + distanceToEnemyY * distanceToEnemyY);
  161. } else {
  162. double distanceToEnemyX = game_state->player.locationX - game_state->bulletPosition.x;
  163. double distanceToEnemyY = game_state->ground[game_state->player.locationX].y -
  164. TANK_COLLIDER_SIZE - game_state->bulletPosition.y;
  165. totalDistanceToEnemy =
  166. sqrt(distanceToEnemyX * distanceToEnemyX + distanceToEnemyY * distanceToEnemyY);
  167. }
  168. if(totalDistanceToEnemy <= TANK_COLLIDER_SIZE) {
  169. game_state->isShooting = false;
  170. scorched_tanks_init_game(game_state);
  171. game_state->isPlayerTurn = !game_state->isPlayerTurn;
  172. return;
  173. }
  174. if(game_state->bulletPosition.x > SCREEN_WIDTH ||
  175. game_state->bulletPosition.y >
  176. game_state->ground[(int)round(game_state->bulletPosition.x)].y) {
  177. game_state->isShooting = false;
  178. game_state->bulletPosition.x = 0;
  179. game_state->bulletPosition.y = 0;
  180. game_state->windSpeed = scorched_tanks_random(0, MAX_WIND);
  181. game_state->isPlayerTurn = !game_state->isPlayerTurn;
  182. return;
  183. }
  184. if(game_state->bulletPosition.y > 0) {
  185. game_state->trajectory[game_state->trajectoryAnimationStep].x =
  186. round(game_state->bulletPosition.x);
  187. game_state->trajectory[game_state->trajectoryAnimationStep].y =
  188. round(game_state->bulletPosition.y);
  189. game_state->trajectoryAnimationStep++;
  190. }
  191. }
  192. }
  193. static void scorched_tanks_draw_tank(Canvas* const canvas, uint8_t x, uint8_t y, bool isPlayer) {
  194. uint8_t lineIndex = 0;
  195. if(isPlayer) {
  196. // Draw tank base
  197. canvas_draw_line(canvas, x - 3, y - lineIndex, x + 3, y - lineIndex);
  198. lineIndex++;
  199. canvas_draw_line(canvas, x - 4, y - lineIndex, x + 4, y - lineIndex);
  200. lineIndex++;
  201. canvas_draw_line(canvas, x - 4, y - lineIndex, x + 4, y - lineIndex);
  202. lineIndex++;
  203. // draw turret
  204. canvas_draw_line(canvas, x - 2, y - lineIndex, x + 1, y - lineIndex);
  205. lineIndex++;
  206. canvas_draw_line(canvas, x - 2, y - lineIndex, x, y - lineIndex);
  207. lineIndex++;
  208. } else {
  209. // Draw tank base
  210. canvas_draw_line(canvas, x - 3, y - lineIndex, x + 3, y - lineIndex);
  211. lineIndex++;
  212. canvas_draw_line(canvas, x - 4, y - lineIndex, x + 4, y - lineIndex);
  213. lineIndex++;
  214. canvas_draw_line(canvas, x - 4, y - lineIndex, x + 4, y - lineIndex);
  215. lineIndex++;
  216. // draw turret
  217. canvas_draw_line(canvas, x - 1, y - lineIndex, x + 2, y - lineIndex);
  218. lineIndex++;
  219. canvas_draw_line(canvas, x, y - lineIndex, x + 2, y - lineIndex);
  220. lineIndex++;
  221. }
  222. }
  223. static void scorched_tanks_render_callback(Canvas* const canvas, void* ctx) {
  224. furi_assert(ctx);
  225. const Game* game_state = ctx;
  226. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  227. canvas_draw_frame(canvas, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  228. canvas_set_color(canvas, ColorBlack);
  229. if(game_state->isShooting) {
  230. canvas_draw_dot(canvas, game_state->bulletPosition.x, game_state->bulletPosition.y);
  231. }
  232. for(int a = 1; a < SCREEN_WIDTH; a++) {
  233. canvas_draw_line(
  234. canvas,
  235. game_state->ground[a - 1].x,
  236. game_state->ground[a - 1].y,
  237. game_state->ground[a].x,
  238. game_state->ground[a].y);
  239. if(game_state->trajectory[a].y != 0) {
  240. canvas_draw_dot(canvas, game_state->trajectory[a].x, game_state->trajectory[a].y);
  241. }
  242. }
  243. scorched_tanks_draw_tank(
  244. canvas,
  245. game_state->enemy.locationX,
  246. game_state->ground[game_state->enemy.locationX].y - TANK_COLLIDER_SIZE,
  247. true);
  248. scorched_tanks_draw_tank(
  249. canvas,
  250. game_state->player.locationX,
  251. game_state->ground[game_state->player.locationX].y - TANK_COLLIDER_SIZE,
  252. false);
  253. int aimX1 = 0;
  254. int aimY1 = 0;
  255. int aimX2 = 0;
  256. int aimY2 = 0;
  257. if(game_state->isPlayerTurn) {
  258. aimX1 = game_state->player.locationX;
  259. aimY1 = game_state->ground[game_state->player.locationX].y - TANK_COLLIDER_SIZE;
  260. double sinFromAngle = scorched_tanks_sin[game_state->player.aimAngle];
  261. double cosFromAngle = scorched_tanks_cos[game_state->player.aimAngle];
  262. aimX2 = aimX1 + TANK_BARREL_LENGTH * cosFromAngle;
  263. aimY2 = aimY1 + TANK_BARREL_LENGTH * sinFromAngle;
  264. aimX1 += 1;
  265. aimX2 += 1;
  266. } else {
  267. aimX1 = game_state->enemy.locationX;
  268. aimY1 = game_state->ground[game_state->enemy.locationX].y - TANK_COLLIDER_SIZE;
  269. double sinFromAngle = scorched_tanks_sin[game_state->enemy.aimAngle];
  270. double cosFromAngle = scorched_tanks_cos[game_state->enemy.aimAngle];
  271. aimX2 = aimX1 + TANK_BARREL_LENGTH * cosFromAngle;
  272. aimY2 = aimY1 + TANK_BARREL_LENGTH * sinFromAngle;
  273. aimX2 = aimX1 - (aimX2 - aimX1);
  274. aimX1 -= 1;
  275. aimX2 -= 1;
  276. }
  277. canvas_draw_line(canvas, aimX1, aimY1 - 3, aimX2, aimY2 - 3);
  278. canvas_set_font(canvas, FontSecondary);
  279. char buffer2[18];
  280. snprintf(buffer2, sizeof(buffer2), "wind: %i", game_state->windSpeed - MAX_WIND / 2);
  281. canvas_draw_str(canvas, 55, 10, buffer2);
  282. if(game_state->isPlayerTurn) {
  283. canvas_draw_str(canvas, 93, 10, "player1");
  284. char buffer[12];
  285. snprintf(buffer, sizeof(buffer), "a: %u", game_state->player.aimAngle);
  286. canvas_draw_str(canvas, 2, 10, buffer);
  287. snprintf(buffer, sizeof(buffer), "p: %u", game_state->player.firePower);
  288. canvas_draw_str(canvas, 27, 10, buffer);
  289. } else {
  290. canvas_draw_str(canvas, 93, 10, "player2");
  291. char buffer[12];
  292. snprintf(buffer, sizeof(buffer), "a: %u", game_state->enemy.aimAngle);
  293. canvas_draw_str(canvas, 2, 10, buffer);
  294. snprintf(buffer, sizeof(buffer), "p: %u", game_state->enemy.firePower);
  295. canvas_draw_str(canvas, 27, 10, buffer);
  296. }
  297. furi_mutex_release(game_state->mutex);
  298. }
  299. static void scorched_tanks_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  300. furi_assert(event_queue);
  301. ScorchedTanksEvent event = {.type = EventTypeKey, .input = *input_event};
  302. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  303. }
  304. static void scorched_tanks_update_timer_callback(FuriMessageQueue* event_queue) {
  305. furi_assert(event_queue);
  306. ScorchedTanksEvent event = {.type = EventTypeTick};
  307. furi_message_queue_put(event_queue, &event, 0);
  308. }
  309. static void scorched_tanks_increase_power(Game* game_state) {
  310. if(game_state->player.firePower < MAX_FIRE_POWER && !game_state->isShooting) {
  311. if(game_state->isPlayerTurn && game_state->player.firePower < MAX_FIRE_POWER) {
  312. game_state->player.firePower++;
  313. }
  314. if(!game_state->isPlayerTurn && game_state->enemy.firePower < MAX_FIRE_POWER) {
  315. game_state->enemy.firePower++;
  316. }
  317. }
  318. }
  319. static void scorched_tanks_decrease_power(Game* game_state) {
  320. if(game_state->player.firePower > MIN_FIRE_POWER && !game_state->isShooting) {
  321. if(game_state->isPlayerTurn && game_state->player.firePower > MIN_FIRE_POWER) {
  322. game_state->player.firePower--;
  323. }
  324. if(!game_state->isPlayerTurn && game_state->enemy.firePower > MIN_FIRE_POWER) {
  325. game_state->enemy.firePower--;
  326. }
  327. }
  328. }
  329. static void scorched_tanks_aim_up(Game* game_state) {
  330. if(!game_state->isShooting) {
  331. if(game_state->isPlayerTurn && game_state->player.aimAngle < 90) {
  332. game_state->player.aimAngle++;
  333. }
  334. if(!game_state->isPlayerTurn && game_state->enemy.aimAngle < 90) {
  335. game_state->enemy.aimAngle++;
  336. }
  337. }
  338. }
  339. static void scorched_tanks_aim_down(Game* game_state) {
  340. if(game_state->player.aimAngle > 0 && !game_state->isShooting) {
  341. if(game_state->isPlayerTurn) {
  342. game_state->player.aimAngle--;
  343. } else {
  344. game_state->enemy.aimAngle--;
  345. }
  346. }
  347. }
  348. const NotificationSequence sequence_long_vibro = {
  349. &message_vibro_on,
  350. &message_delay_500,
  351. &message_vibro_off,
  352. NULL,
  353. };
  354. static void scorched_tanks_fire(Game* game_state) {
  355. if(!game_state->isShooting) {
  356. if(game_state->isPlayerTurn) {
  357. double sinFromAngle = scorched_tanks_sin[game_state->player.aimAngle];
  358. double cosFromAngle = scorched_tanks_cos[game_state->player.aimAngle];
  359. uint8_t aimX1 = game_state->player.locationX;
  360. uint8_t aimY1 =
  361. game_state->ground[game_state->player.locationX].y - TANK_COLLIDER_SIZE;
  362. int aimX2 = aimX1 + TANK_BARREL_LENGTH * cosFromAngle;
  363. int aimY2 = aimY1 + TANK_BARREL_LENGTH * sinFromAngle;
  364. game_state->bulletPosition.x = aimX2;
  365. game_state->bulletPosition.y = aimY2;
  366. game_state->bulletVector.x = scorched_tanks_cos[game_state->player.aimAngle] *
  367. ((double)game_state->player.firePower / 10);
  368. game_state->bulletVector.y = scorched_tanks_sin[game_state->player.aimAngle] *
  369. ((double)game_state->player.firePower / 10);
  370. } else {
  371. double sinFromAngle = scorched_tanks_sin[game_state->enemy.aimAngle];
  372. double cosFromAngle = scorched_tanks_cos[game_state->enemy.aimAngle];
  373. uint8_t aimX1 = game_state->enemy.locationX;
  374. uint8_t aimY1 = game_state->ground[game_state->enemy.locationX].y - TANK_COLLIDER_SIZE;
  375. int aimX2 = aimX1 + TANK_BARREL_LENGTH * cosFromAngle;
  376. int aimY2 = aimY1 + TANK_BARREL_LENGTH * sinFromAngle;
  377. aimX2 = aimX1 - (aimX2 - aimX1);
  378. game_state->bulletPosition.x = aimX2;
  379. game_state->bulletPosition.y = aimY2;
  380. game_state->bulletVector.x = -scorched_tanks_cos[game_state->enemy.aimAngle] *
  381. ((double)game_state->enemy.firePower / 10);
  382. game_state->bulletVector.y = scorched_tanks_sin[game_state->enemy.aimAngle] *
  383. ((double)game_state->enemy.firePower / 10);
  384. }
  385. game_state->trajectoryAnimationStep = 0;
  386. for(int x = 0; x < SCREEN_WIDTH; x++) {
  387. game_state->trajectory[x].x = 0;
  388. game_state->trajectory[x].y = 0;
  389. }
  390. game_state->isShooting = true;
  391. NotificationApp* notification = furi_record_open("notification");
  392. notification_message(notification, &sequence_long_vibro);
  393. notification_message(notification, &sequence_blink_white_100);
  394. furi_record_close("notification");
  395. }
  396. }
  397. int32_t scorched_tanks_game_app(void* p) {
  398. UNUSED(p);
  399. srand(DWT->CYCCNT);
  400. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(ScorchedTanksEvent));
  401. Game* game_state = malloc(sizeof(Game));
  402. scorched_tanks_init_game(game_state);
  403. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  404. if(!game_state->mutex) {
  405. FURI_LOG_E("ScorchedTanks", "cannot create mutex\r\n");
  406. furi_message_queue_free(event_queue);
  407. free(game_state);
  408. return 255;
  409. }
  410. ViewPort* view_port = view_port_alloc();
  411. view_port_draw_callback_set(view_port, scorched_tanks_render_callback, game_state);
  412. view_port_input_callback_set(view_port, scorched_tanks_input_callback, event_queue);
  413. FuriTimer* timer =
  414. furi_timer_alloc(scorched_tanks_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  415. furi_timer_start(timer, 2000);
  416. // Open GUI and register view_port
  417. Gui* gui = furi_record_open(RECORD_GUI);
  418. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  419. ScorchedTanksEvent event;
  420. for(bool processing = true; processing;) {
  421. furi_message_queue_get(event_queue, &event, 50);
  422. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  423. if(event.type == EventTypeKey) { // && game->isPlayerTurn
  424. if(event.input.type == InputTypeRepeat || event.input.type == InputTypeShort) {
  425. switch(event.input.key) {
  426. case InputKeyUp:
  427. scorched_tanks_aim_up(game_state);
  428. break;
  429. case InputKeyDown:
  430. scorched_tanks_aim_down(game_state);
  431. break;
  432. case InputKeyRight:
  433. scorched_tanks_increase_power(game_state);
  434. break;
  435. case InputKeyLeft:
  436. scorched_tanks_decrease_power(game_state);
  437. break;
  438. case InputKeyOk:
  439. scorched_tanks_fire(game_state);
  440. break;
  441. case InputKeyBack:
  442. processing = false;
  443. break;
  444. default:
  445. break;
  446. }
  447. }
  448. } else if(event.type == EventTypeTick) {
  449. scorched_tanks_calculate_trajectory(game_state);
  450. }
  451. view_port_update(view_port);
  452. furi_mutex_release(game_state->mutex);
  453. }
  454. furi_timer_free(timer);
  455. view_port_enabled_set(view_port, false);
  456. gui_remove_view_port(gui, view_port);
  457. furi_record_close(RECORD_GUI);
  458. view_port_free(view_port);
  459. furi_message_queue_free(event_queue);
  460. furi_mutex_free(game_state->mutex);
  461. free(game_state);
  462. return 0;
  463. }