space.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <gui/elements.h>
  6. #include <furi_hal.h>
  7. #include <math.h>
  8. #include <yapinvaders_icons.h>
  9. #define PROJECTILES_MAX 10
  10. #define ENEMIES_MAX 12
  11. #define BARRIERS_MAX 5
  12. #define BARRIER_WIDTH 10
  13. #define BARRIER_HEIGHT 3
  14. #define PROJECTILE_WIDTH 4
  15. #define PROJECTILE_HEIGHT 8
  16. #define ENEMY_PROJECTILE_SPEED .5
  17. #define ENEMY_PROJECTILES_MAX 10
  18. int enemy_movement_direction = 1;
  19. int move_down_step = 0;
  20. int enemy_move_counter = 0;
  21. int enemy_move_threshold = 20;
  22. bool leftKeyPressed = false;
  23. bool rightKeyPressed = false;
  24. typedef struct { int x, y; } Point;
  25. typedef struct {
  26. Point position;
  27. } Player;
  28. typedef struct {
  29. Point position;
  30. bool active;
  31. } Projectile;
  32. typedef struct {
  33. Point position;
  34. bool active;
  35. } Enemy;
  36. typedef struct {
  37. Point position;
  38. bool active;
  39. } Barrier;
  40. typedef struct {
  41. Player player;
  42. Projectile projectiles[PROJECTILES_MAX];
  43. int projectiles_count;
  44. bool game_over;
  45. Enemy enemies[ENEMIES_MAX];
  46. int current_wave;
  47. bool waiting_for_restart;
  48. bool running;
  49. int score;
  50. Barrier barriers[BARRIERS_MAX];
  51. Projectile enemy_projectiles[ENEMY_PROJECTILES_MAX];
  52. int enemy_projectiles_count;
  53. } GameState;
  54. GameState game_state;
  55. void deactivate_all_enemies(GameState* state);
  56. void game_state_init(GameState* state) {
  57. state->player.position = (Point){64, 55};
  58. state->projectiles_count = 0;
  59. for(int i = 0; i < PROJECTILES_MAX; ++i) {
  60. state->projectiles[i].active = false;
  61. }
  62. for(int i = 0; i < BARRIERS_MAX; ++i) {
  63. state->barriers[i].position.x = 7 + i * (128 / BARRIERS_MAX);
  64. state->barriers[i].position.y = 50;
  65. state->barriers[i].active = true;
  66. }
  67. for(int p = 0; p < ENEMY_PROJECTILES_MAX; ++p) {
  68. if(state->enemy_projectiles[p].active) {
  69. state->enemy_projectiles[p].position.y += ENEMY_PROJECTILE_SPEED;
  70. if(state->enemy_projectiles[p].position.y > 128) {
  71. state->enemy_projectiles[p].active = false;
  72. state->enemy_projectiles_count--;
  73. }
  74. }
  75. }
  76. state->running = true;
  77. state->game_over = false;
  78. state->current_wave = 0;
  79. state->waiting_for_restart = false;
  80. state->score = 0;
  81. }
  82. void handle_input(GameState* state, InputEvent* input_event) {
  83. if(input_event->type == InputTypeShort) {
  84. switch(input_event->key) {
  85. case InputKeyLeft:
  86. state->player.position.x = (state->player.position.x - 1) < 0 ? 0 : state->player.position.x - 1;
  87. break;
  88. case InputKeyRight:
  89. state->player.position.x = (state->player.position.x + 1) > 124 ? 124 : state->player.position.x + 1;
  90. break;
  91. case InputKeyOk:
  92. if(state->projectiles_count < PROJECTILES_MAX) {
  93. for(int i = 0; i < PROJECTILES_MAX; ++i) {
  94. if(!state->projectiles[i].active) {
  95. state->projectiles[i].position = (Point){state->player.position.x, state->player.position.y};
  96. state->projectiles[i].active = true;
  97. state->projectiles_count++;
  98. break;
  99. }
  100. }
  101. }
  102. break;
  103. case InputKeyBack:
  104. state->game_over = true;
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. }
  111. void initialize_enemies(GameState* state) {
  112. int rows = 2;
  113. int cols = ENEMIES_MAX / rows;
  114. int horizontalSpacing = 128 / cols;
  115. int verticalSpacing = 10;
  116. for(int i = 0; i < ENEMIES_MAX; ++i) {
  117. state->enemies[i].position.x = (i % cols) * horizontalSpacing + (horizontalSpacing / 2);
  118. state->enemies[i].position.y = (i / cols) * verticalSpacing + 10;
  119. state->enemies[i].active = true;
  120. }
  121. state->current_wave++;
  122. }
  123. void update_enemy_positions(GameState* state) {
  124. enemy_move_counter++;
  125. if(enemy_move_counter >= enemy_move_threshold) {
  126. bool changeDirection = false;
  127. int newDirection = enemy_movement_direction;
  128. for(int i = 0; i < ENEMIES_MAX; ++i) {
  129. if(state->enemies[i].active) {
  130. if ((state->enemies[i].position.x <= 0 && enemy_movement_direction < 0) ||
  131. (state->enemies[i].position.x >= 124 && enemy_movement_direction > 0)) {
  132. changeDirection = true;
  133. newDirection *= -1;
  134. break;
  135. }
  136. }
  137. }
  138. for(int i = 0; i < ENEMIES_MAX; ++i) {
  139. if(state->enemies[i].active) {
  140. if (!changeDirection) {
  141. state->enemies[i].position.x += enemy_movement_direction;
  142. }
  143. }
  144. }
  145. if (changeDirection) {
  146. for(int i = 0; i < ENEMIES_MAX; ++i) {
  147. if(state->enemies[i].active) {
  148. state->enemies[i].position.x += newDirection;
  149. state->enemies[i].position.y += 5;
  150. }
  151. }
  152. enemy_movement_direction = newDirection;
  153. }
  154. enemy_move_counter = 0;
  155. }
  156. }
  157. void enemy_shoot(GameState* state) {
  158. int activeEnemies[ENEMIES_MAX];
  159. int activeCount = 0;
  160. for(int e = 0; e < ENEMIES_MAX; ++e) {
  161. if(state->enemies[e].active) {
  162. activeEnemies[activeCount++] = e;
  163. }
  164. }
  165. if(activeCount > 0) {
  166. int chosenOne = activeEnemies[rand() % activeCount];
  167. for(int p = 0; p < ENEMY_PROJECTILES_MAX; ++p) {
  168. if(!state->enemy_projectiles[p].active) {
  169. state->enemy_projectiles[p].position = (Point){state->enemies[chosenOne].position.x + 5, state->enemies[chosenOne].position.y + 10}; // Assuming enemy size for adjustment
  170. state->enemy_projectiles[p].active = true;
  171. state->enemy_projectiles_count++;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. void handle_collisions(GameState* state) {
  178. int playerHitboxMargin = 2;
  179. for(int p = 0; p < PROJECTILES_MAX; ++p) {
  180. if(state->projectiles[p].active) {
  181. for(int e = 0; e < ENEMIES_MAX; ++e) {
  182. if(state->enemies[e].active) {
  183. if(abs(state->projectiles[p].position.x - state->enemies[e].position.x) < 10 / 2 + 1 &&
  184. abs(state->projectiles[p].position.y - state->enemies[e].position.y) < 10 / 2 + 1) {
  185. state->projectiles[p].active = false;
  186. state->enemies[e].active = false;
  187. state->projectiles_count--;
  188. state->score += 10;
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. for(int p = 0; p < PROJECTILES_MAX; ++p) {
  196. if(state->projectiles[p].active) {
  197. for(int b = 0; b < BARRIERS_MAX; ++b) {
  198. if(state->barriers[b].active &&
  199. abs(state->projectiles[p].position.x - state->barriers[b].position.x) < 3 &&
  200. abs(state->projectiles[p].position.y - state->barriers[b].position.y) < 3) {
  201. state->projectiles[p].active = false;
  202. state->barriers[b].active = false;
  203. break;
  204. }
  205. }
  206. }
  207. }
  208. for(int e = 0; e < ENEMIES_MAX; ++e) {
  209. if(state->enemies[e].active) {
  210. for(int b = 0; b < BARRIERS_MAX; ++b) {
  211. if(state->barriers[b].active &&
  212. abs(state->enemies[e].position.x - state->barriers[b].position.x) < 10 &&
  213. abs(state->enemies[e].position.y - state->barriers[b].position.y) < 10) {
  214. state->enemies[e].active = false;
  215. state->barriers[b].active = false;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. for(int p = 0; p < ENEMY_PROJECTILES_MAX; ++p) {
  222. if(state->enemy_projectiles[p].active) {
  223. int projectileCenterX = state->enemy_projectiles[p].position.x + 2;
  224. int projectileCenterY = state->enemy_projectiles[p].position.y + 4;
  225. int playerCenterX = state->player.position.x + 5;
  226. int playerCenterY = state->player.position.y + 5;
  227. if(abs(projectileCenterX - playerCenterX) < (5 - playerHitboxMargin) &&
  228. abs(projectileCenterY - playerCenterY) < (5 - playerHitboxMargin)) {
  229. deactivate_all_enemies(&game_state);
  230. state->game_over = true;
  231. state->enemy_projectiles[p].active = false;
  232. }
  233. }
  234. }
  235. for(int p = 0; p < ENEMY_PROJECTILES_MAX; ++p) {
  236. if(state->enemy_projectiles[p].active) {
  237. for(int b = 0; b < BARRIERS_MAX; ++b) {
  238. if(state->barriers[b].active &&
  239. abs(state->enemy_projectiles[p].position.x - state->barriers[b].position.x) < (BARRIER_WIDTH / 2 + PROJECTILE_WIDTH / 2) &&
  240. (state->enemy_projectiles[p].position.y + PROJECTILE_HEIGHT >= state->barriers[b].position.y) &&
  241. (state->enemy_projectiles[p].position.y <= state->barriers[b].position.y + BARRIER_HEIGHT)) {
  242. state->enemy_projectiles[p].active = false;
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. bool all_enemies_destroyed(GameState* state) {
  250. for(int i = 0; i < ENEMIES_MAX; ++i) {
  251. if(state->enemies[i].active) {
  252. return false;
  253. }
  254. }
  255. return true;
  256. }
  257. void render_game_over_screen(Canvas* canvas, GameState* state) {
  258. char score_msg[30];
  259. canvas_clear(canvas);
  260. canvas_set_font(canvas, FontSecondary);
  261. canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, "Game Over!");
  262. snprintf(score_msg, sizeof(score_msg), "Score: %d", state->score);
  263. canvas_draw_str_aligned(canvas, 64, 24, AlignCenter, AlignCenter, score_msg);
  264. canvas_draw_str_aligned(canvas, 64, 40, AlignCenter, AlignCenter, "Press OK to restart");
  265. }
  266. void deactivate_all_enemies(GameState* state) {
  267. for(int i = 0; i < ENEMIES_MAX; ++i) {
  268. state->enemies[i].active = false;
  269. }
  270. }
  271. void update_game_state(GameState* state) {
  272. static int shoot_counter = 0;
  273. for(int i = 0; i < PROJECTILES_MAX; ++i) {
  274. if(state->projectiles[i].active) {
  275. state->projectiles[i].position.y -= 2;
  276. if(state->projectiles[i].position.y < 0) {
  277. state->projectiles[i].active = false;
  278. state->projectiles_count--;
  279. }
  280. }
  281. }
  282. if(++shoot_counter >= 30) {
  283. enemy_shoot(state);
  284. shoot_counter = 0;
  285. }
  286. for(int i = 0; i < ENEMIES_MAX; ++i) {
  287. if(state->enemies[i].active) {
  288. if(abs(state->enemies[i].position.x - state->player.position.x) < 10 &&
  289. abs(state->enemies[i].position.y - state->player.position.y) < 10) {
  290. deactivate_all_enemies(state);
  291. state->game_over = true;
  292. return;
  293. }
  294. }
  295. }
  296. for(int p = 0; p < ENEMY_PROJECTILES_MAX; ++p) {
  297. if(state->enemy_projectiles[p].active) {
  298. state->enemy_projectiles[p].position.y += 2;
  299. if(state->enemy_projectiles[p].position.y > 128) {
  300. state->enemy_projectiles[p].active = false;
  301. state->enemy_projectiles_count--;
  302. }
  303. }
  304. }
  305. if(all_enemies_destroyed(state)) {
  306. initialize_enemies(state);
  307. }
  308. update_enemy_positions(state);
  309. handle_collisions(state);
  310. }
  311. void render_callback(Canvas* const canvas, void* ctx) {
  312. GameState* state = (GameState*)ctx;
  313. if (!canvas || !state) return;
  314. if(state->game_over) {
  315. render_game_over_screen(canvas, state);
  316. } else {
  317. canvas_clear(canvas);
  318. char score_text[30];
  319. snprintf(score_text, sizeof(score_text), "Score: %d", state->score);
  320. canvas_set_font(canvas, FontSecondary);
  321. canvas_draw_str_aligned(canvas, 64, 0, AlignCenter, AlignTop, score_text);
  322. canvas_draw_icon(canvas, state->player.position.x, state->player.position.y, &I_yapinvader);
  323. for(int i = 0; i < PROJECTILES_MAX; ++i) {
  324. if(state->projectiles[i].active) {
  325. canvas_draw_circle(canvas, state->projectiles[i].position.x, state->projectiles[i].position.y, 1);
  326. }
  327. }
  328. }
  329. for(int i = 0; i < ENEMIES_MAX; ++i) {
  330. if(state->enemies[i].active) {
  331. canvas_draw_icon(canvas, state->enemies[i].position.x, state->enemies[i].position.y, &I_yap);
  332. }
  333. }
  334. for(int i = 0; i < BARRIERS_MAX; ++i) {
  335. if(state->barriers[i].active) {
  336. canvas_draw_rbox(canvas, state->barriers[i].position.x, state->barriers[i].position.y, 10, 3, 1);
  337. }
  338. }
  339. for(int p = 0; p < ENEMY_PROJECTILES_MAX; ++p) {
  340. if(state->enemy_projectiles[p].active) {
  341. canvas_draw_rbox(canvas, state->enemy_projectiles[p].position.x - 1, state->enemy_projectiles[p].position.y - 4, 4, 8, 1);
  342. }
  343. }
  344. }
  345. static void input_callback(InputEvent* input_event, void* ctx) {
  346. GameState* state = (GameState*)ctx;
  347. if (!state) return;
  348. if (input_event->key == InputKeyBack && input_event->type == InputTypeShort) {
  349. state->running = false;
  350. return;
  351. }
  352. if (state->game_over) {
  353. if (input_event->key == InputKeyOk && input_event->type == InputTypeShort) {
  354. game_state_init(state);
  355. initialize_enemies(state);
  356. state->game_over = false;
  357. }
  358. } else {
  359. handle_input(state, input_event);
  360. }
  361. if (input_event->type == InputTypePress) {
  362. switch(input_event->key) {
  363. case InputKeyLeft:
  364. leftKeyPressed = true;
  365. break;
  366. case InputKeyRight:
  367. rightKeyPressed = true;
  368. break;
  369. default:
  370. break;
  371. }
  372. }
  373. if (input_event->type == InputTypeRelease) {
  374. switch(input_event->key) {
  375. case InputKeyLeft:
  376. leftKeyPressed = false;
  377. break;
  378. case InputKeyRight:
  379. rightKeyPressed = false;
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. }
  386. void update_player_position(GameState* state) {
  387. if(leftKeyPressed) {
  388. state->player.position.x = (state->player.position.x - 1) > 0 ? state->player.position.x - 1 : 0;
  389. }
  390. if(rightKeyPressed) {
  391. state->player.position.x = (state->player.position.x + 1) < 124 ? state->player.position.x + 1 : 124;
  392. }
  393. }
  394. int32_t yapinvaders_app(void) {
  395. Gui* gui = furi_record_open(RECORD_GUI);
  396. ViewPort* view_port = view_port_alloc();
  397. view_port_draw_callback_set(view_port, render_callback, &game_state);
  398. view_port_input_callback_set(view_port, input_callback, &game_state);
  399. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  400. game_state_init(&game_state);
  401. initialize_enemies(&game_state);
  402. while (game_state.running) {
  403. if (game_state.game_over) {
  404. if (game_state.waiting_for_restart) {
  405. game_state_init(&game_state);
  406. initialize_enemies(&game_state);
  407. game_state.waiting_for_restart = false;
  408. }
  409. } else {
  410. update_player_position(&game_state);
  411. update_game_state(&game_state);
  412. view_port_update(view_port);
  413. }
  414. furi_delay_ms(30);
  415. }
  416. gui_remove_view_port(gui, view_port);
  417. view_port_free(view_port);
  418. furi_record_close(RECORD_GUI);
  419. return 0;
  420. }