space.c 16 KB

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