zombiez.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <dolphin/dolphin.h>
  6. //ORIGINAL REPO: https://github.com/Dooskington/flipperzero-zombiez
  7. //AUTHORS: https://github.com/Dooskington | https://github.com/DevMilanIan
  8. #include "zombiez.h"
  9. #define ZOMBIES_MAX 3
  10. #define ZOMBIES_WIDTH 5
  11. #define ZOMBIES_HEIGHT 8
  12. #define PROJECTILES_MAX 10
  13. #define MIN_Y 5
  14. #define MAX_Y 58
  15. #define WALL_X 16
  16. #define PLAYER_START_X 8
  17. #define PLAYER_START_Y (MAX_Y - MIN_Y) / 2
  18. typedef enum {
  19. EventTypeTick,
  20. EventTypeKey,
  21. } EventType;
  22. typedef struct {
  23. EventType type;
  24. InputEvent input;
  25. } PluginEvent;
  26. typedef enum { GameStatePlaying, GameStateGameOver } GameState;
  27. typedef struct {
  28. int x;
  29. int y;
  30. } Point;
  31. typedef struct {
  32. Point position;
  33. int hp;
  34. } Player;
  35. typedef struct {
  36. Point position;
  37. int hp;
  38. } Zombie;
  39. typedef struct {
  40. Point position;
  41. } Projectile;
  42. typedef struct {
  43. FuriMutex* mutex;
  44. GameState game_state;
  45. Player player;
  46. size_t zombies_count;
  47. Zombie* zombies[ZOMBIES_MAX];
  48. size_t projectiles_count;
  49. Projectile* projectiles[PROJECTILES_MAX];
  50. uint16_t score;
  51. bool input_shoot;
  52. } PluginState;
  53. static void render_callback(Canvas* const canvas, void* ctx) {
  54. furi_assert(ctx);
  55. const PluginState* plugin_state = ctx;
  56. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  57. canvas_draw_frame(canvas, 0, 0, 128, 64);
  58. canvas_set_font(canvas, FontPrimary);
  59. canvas_draw_str_aligned(
  60. canvas,
  61. plugin_state->player.position.x,
  62. plugin_state->player.position.y,
  63. AlignCenter,
  64. AlignCenter,
  65. "@");
  66. canvas_draw_line(canvas, WALL_X, 0, WALL_X, 64);
  67. canvas_draw_line(canvas, WALL_X + 2, 4, WALL_X + 2, 59);
  68. for(int i = 0; i < PROJECTILES_MAX; ++i) {
  69. Projectile* p = plugin_state->projectiles[i];
  70. if(p != NULL) {
  71. canvas_draw_disc(canvas, p->position.x, p->position.y, 3);
  72. }
  73. }
  74. for(int i = 0; i < ZOMBIES_MAX; ++i) {
  75. Zombie* z = plugin_state->zombies[i];
  76. if(z != NULL) {
  77. for(int h = 0; h < ZOMBIES_HEIGHT; h++) {
  78. for(int w = 0; w < ZOMBIES_WIDTH; w++) {
  79. // Switch animation
  80. int zIdx = 0;
  81. if(z->position.x % 2 == 0) {
  82. zIdx = 1;
  83. }
  84. // Draw zombie pixels
  85. if(zombie_array[zIdx][h][w] == 1) {
  86. int x = z->position.x + w;
  87. int y = z->position.y + h;
  88. canvas_draw_dot(canvas, x, y);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. int heart;
  95. if((plugin_state->player.hp - 10) > 5) { // 16, 17, 18, 19, 20
  96. heart = 0;
  97. } else if((plugin_state->player.hp - 5) > 5) { // 11, 12, 13, 14, 15
  98. heart = 1;
  99. } else if((plugin_state->player.hp - 3) > 2) { // 6, 7, 8, 9, 10
  100. heart = 2;
  101. } else if(plugin_state->player.hp > 0) { // 1, 2, 3, 4, 5
  102. heart = 3;
  103. } else { // 0
  104. heart = 4;
  105. }
  106. // visual representation of health
  107. for(int h = 0; h < 5; h++) {
  108. for(int w = 0; w < 5; w++) {
  109. if(heart_array[heart][h][w] == 1) {
  110. int x = 124 - w;
  111. int y = 56 + h;
  112. canvas_draw_dot(canvas, x, y);
  113. }
  114. }
  115. }
  116. // buffer hp + score
  117. char hpBuffer[8];
  118. char scoreBuffer[14];
  119. if(plugin_state->game_state == GameStatePlaying) {
  120. // display ammo / reload
  121. if(plugin_state->projectiles_count >= PROJECTILES_MAX) {
  122. canvas_draw_str_aligned(canvas, 24, 10, AlignLeft, AlignCenter, "RELOAD");
  123. } else {
  124. for(uint8_t i = 0; i < (PROJECTILES_MAX - plugin_state->projectiles_count); i++) {
  125. canvas_draw_box(canvas, 24 + (4 * i), 6, 2, 4);
  126. }
  127. }
  128. // display hp + score
  129. snprintf(hpBuffer, sizeof(hpBuffer), "%u", plugin_state->player.hp);
  130. canvas_draw_str_aligned(canvas, 118, 62, AlignRight, AlignBottom, hpBuffer);
  131. snprintf(scoreBuffer, sizeof(scoreBuffer), "%u", plugin_state->score);
  132. canvas_draw_str_aligned(canvas, 126, 10, AlignRight, AlignBottom, scoreBuffer);
  133. }
  134. // Game Over banner
  135. if(plugin_state->game_state == GameStateGameOver) {
  136. // Screen is 128x64 px
  137. canvas_set_color(canvas, ColorWhite);
  138. canvas_draw_box(canvas, 34, 20, 62, 24);
  139. canvas_set_color(canvas, ColorBlack);
  140. canvas_draw_frame(canvas, 34, 20, 62, 24);
  141. canvas_set_font(canvas, FontPrimary);
  142. canvas_draw_str(canvas, 37, 31, "Game Over");
  143. canvas_set_font(canvas, FontSecondary);
  144. snprintf(scoreBuffer, sizeof(scoreBuffer), "Score: %u", plugin_state->score);
  145. canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, scoreBuffer);
  146. }
  147. //char* info = (char*)malloc(16 * sizeof(char));
  148. //asprintf(&info, "%d, %d", plugin_state->x, plugin_state->y);
  149. //canvas_draw_str_aligned(canvas, 32, 16, AlignLeft, AlignBottom, info);
  150. //free(info);
  151. furi_mutex_release(plugin_state->mutex);
  152. }
  153. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  154. furi_assert(event_queue);
  155. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  156. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  157. }
  158. static void tick(PluginState* const plugin_state) {
  159. if(plugin_state->input_shoot && (plugin_state->projectiles_count < PROJECTILES_MAX)) {
  160. Projectile* p = (Projectile*)malloc(sizeof(Projectile));
  161. p->position.x = plugin_state->player.position.x;
  162. p->position.y = plugin_state->player.position.y;
  163. size_t idx = plugin_state->projectiles_count;
  164. plugin_state->projectiles[idx] = p;
  165. plugin_state->projectiles_count += 1;
  166. }
  167. for(int i = 0; i < ZOMBIES_MAX; ++i) {
  168. if(!plugin_state->zombies[i]) {
  169. Zombie* z = (Zombie*)malloc(sizeof(Zombie));
  170. //z->hp = 20;
  171. z->position.x = 126;
  172. z->position.y = MIN_Y + (rand() % (MAX_Y - MIN_Y));
  173. plugin_state->zombies[i] = z;
  174. plugin_state->zombies_count += 1;
  175. }
  176. }
  177. for(int i = 0; i < PROJECTILES_MAX; ++i) {
  178. Projectile* p = plugin_state->projectiles[i];
  179. if(p != NULL) {
  180. p->position.x += 2;
  181. for(int i = 0; i < ZOMBIES_MAX; ++i) {
  182. Zombie* z = plugin_state->zombies[i];
  183. if(z != NULL) {
  184. if( // projectile close enough to zombie
  185. (((z->position.x - p->position.x) <= 2) &&
  186. ((z->position.y - p->position.y) <= 4)) &&
  187. (((p->position.x - z->position.x) <= 2) &&
  188. ((p->position.y - z->position.y) <= 6))) {
  189. //z->hp -= 5;
  190. //if(z->hp <= 0) {
  191. plugin_state->zombies_count -= 1;
  192. free(z);
  193. plugin_state->zombies[i] = NULL;
  194. plugin_state->score++;
  195. //if(plugin_state->score % 15 == 0) dolphin_deed(getRandomDeed());
  196. //}
  197. } else if(z->position.x <= WALL_X && z->position.x > 0) { // zombie got to the wall
  198. plugin_state->zombies_count -= 1;
  199. free(z);
  200. plugin_state->zombies[i] = NULL;
  201. if(plugin_state->player.hp > 0) {
  202. plugin_state->player.hp--;
  203. } else {
  204. plugin_state->game_state = GameStateGameOver;
  205. }
  206. } else {
  207. if(furi_get_tick() % 2 == 0) z->position.x--;
  208. }
  209. }
  210. }
  211. if(p->position.x >= 128) {
  212. free(p);
  213. plugin_state->projectiles[i] = NULL;
  214. }
  215. }
  216. }
  217. plugin_state->input_shoot = false;
  218. }
  219. static void timer_callback(void* ctx) {
  220. furi_assert(ctx);
  221. FuriMessageQueue* event_queue = ctx;
  222. PluginEvent event = {.type = EventTypeTick};
  223. furi_message_queue_put(event_queue, &event, 0);
  224. }
  225. static void zombiez_state_init(PluginState* const plugin_state) {
  226. plugin_state->player.position.x = PLAYER_START_X;
  227. plugin_state->player.position.y = PLAYER_START_Y;
  228. plugin_state->player.hp = 20;
  229. plugin_state->projectiles_count = 0;
  230. plugin_state->zombies_count = 0;
  231. plugin_state->score = 0;
  232. for(int i = 0; i < PROJECTILES_MAX; i++) {
  233. plugin_state->projectiles[i] = NULL;
  234. }
  235. for(int i = 0; i < ZOMBIES_MAX; i++) {
  236. plugin_state->zombies[i] = NULL;
  237. }
  238. plugin_state->game_state = GameStatePlaying;
  239. plugin_state->input_shoot = false;
  240. }
  241. int32_t zombiez_game_app(void* p) {
  242. UNUSED(p);
  243. uint32_t return_code = 0;
  244. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  245. PluginState* plugin_state = malloc(sizeof(PluginState));
  246. zombiez_state_init(plugin_state);
  247. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  248. if(!plugin_state->mutex) {
  249. FURI_LOG_E("Zombiez", "cannot create mutex\r\n");
  250. return_code = 255;
  251. goto free_and_exit;
  252. }
  253. // Set system callbacks
  254. ViewPort* view_port = view_port_alloc();
  255. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  256. view_port_input_callback_set(view_port, input_callback, event_queue);
  257. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  258. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
  259. // Open GUI and register view_port
  260. Gui* gui = furi_record_open(RECORD_GUI);
  261. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  262. // Call dolphin deed on game start
  263. dolphin_deed(DolphinDeedPluginGameStart);
  264. PluginEvent event;
  265. bool isRunning = true;
  266. while(isRunning) {
  267. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  268. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  269. if(event_status == FuriStatusOk) {
  270. if(event.type == EventTypeKey) {
  271. if(event.input.type == InputTypePress) {
  272. switch(event.input.key) {
  273. case InputKeyUp:
  274. if(plugin_state->player.position.y > MIN_Y &&
  275. plugin_state->game_state == GameStatePlaying) {
  276. plugin_state->player.position.y--;
  277. }
  278. break;
  279. case InputKeyDown:
  280. if(plugin_state->player.position.y < MAX_Y &&
  281. plugin_state->game_state == GameStatePlaying) {
  282. plugin_state->player.position.y++;
  283. }
  284. break;
  285. case InputKeyOk:
  286. if(plugin_state->projectiles_count < PROJECTILES_MAX &&
  287. plugin_state->game_state == GameStatePlaying) {
  288. plugin_state->input_shoot = true;
  289. }
  290. break;
  291. case InputKeyBack:
  292. break;
  293. default:
  294. break;
  295. }
  296. } else if(
  297. event.input.type == InputTypeRepeat &&
  298. plugin_state->game_state == GameStatePlaying) {
  299. switch(event.input.key) {
  300. case InputKeyUp:
  301. if(plugin_state->player.position.y > (MIN_Y + 1)) {
  302. plugin_state->player.position.y -= 4;
  303. }
  304. break;
  305. case InputKeyDown:
  306. if(plugin_state->player.position.y < (MAX_Y - 1)) {
  307. plugin_state->player.position.y += 4;
  308. }
  309. break;
  310. default:
  311. break;
  312. }
  313. } else if(event.input.type == InputTypeLong) {
  314. if(event.input.key == InputKeyOk) {
  315. if(plugin_state->game_state == GameStateGameOver) {
  316. zombiez_state_init(plugin_state);
  317. } else if(plugin_state->projectiles_count >= PROJECTILES_MAX) {
  318. plugin_state->projectiles_count = 0;
  319. plugin_state->player.hp++;
  320. }
  321. } else if(event.input.key == InputKeyBack) {
  322. isRunning = false;
  323. }
  324. }
  325. } else if(event.type == EventTypeTick) {
  326. tick(plugin_state);
  327. }
  328. }
  329. furi_mutex_release(plugin_state->mutex);
  330. view_port_update(view_port);
  331. }
  332. furi_timer_free(timer);
  333. view_port_enabled_set(view_port, false);
  334. gui_remove_view_port(gui, view_port);
  335. furi_record_close(RECORD_GUI);
  336. view_port_free(view_port);
  337. furi_mutex_free(plugin_state->mutex);
  338. free_and_exit:
  339. free(plugin_state);
  340. furi_message_queue_free(event_queue);
  341. return return_code;
  342. }