zombiez.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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, void* ctx) {
  154. FuriMessageQueue* event_queue = ctx;
  155. furi_assert(event_queue);
  156. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  157. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  158. }
  159. static void tick(PluginState* const plugin_state) {
  160. if(plugin_state->input_shoot && (plugin_state->projectiles_count < PROJECTILES_MAX)) {
  161. Projectile* p = (Projectile*)malloc(sizeof(Projectile));
  162. p->position.x = plugin_state->player.position.x;
  163. p->position.y = plugin_state->player.position.y;
  164. size_t idx = plugin_state->projectiles_count;
  165. plugin_state->projectiles[idx] = p;
  166. plugin_state->projectiles_count += 1;
  167. }
  168. for(int i = 0; i < ZOMBIES_MAX; ++i) {
  169. if(!plugin_state->zombies[i]) {
  170. Zombie* z = (Zombie*)malloc(sizeof(Zombie));
  171. //z->hp = 20;
  172. z->position.x = 126;
  173. z->position.y = MIN_Y + (rand() % (MAX_Y - MIN_Y));
  174. plugin_state->zombies[i] = z;
  175. plugin_state->zombies_count += 1;
  176. }
  177. }
  178. for(int i = 0; i < PROJECTILES_MAX; ++i) {
  179. Projectile* p = plugin_state->projectiles[i];
  180. if(p != NULL) {
  181. p->position.x += 2;
  182. for(int i = 0; i < ZOMBIES_MAX; ++i) {
  183. Zombie* z = plugin_state->zombies[i];
  184. if(z != NULL) {
  185. if( // projectile close enough to zombie
  186. (((z->position.x - p->position.x) <= 2) &&
  187. ((z->position.y - p->position.y) <= 4)) &&
  188. (((p->position.x - z->position.x) <= 2) &&
  189. ((p->position.y - z->position.y) <= 6))) {
  190. //z->hp -= 5;
  191. //if(z->hp <= 0) {
  192. plugin_state->zombies_count -= 1;
  193. free(z);
  194. plugin_state->zombies[i] = NULL;
  195. plugin_state->score++;
  196. //if(plugin_state->score % 15 == 0) dolphin_deed(getRandomDeed());
  197. //}
  198. } else if(z->position.x <= WALL_X && z->position.x > 0) { // zombie got to the wall
  199. plugin_state->zombies_count -= 1;
  200. free(z);
  201. plugin_state->zombies[i] = NULL;
  202. if(plugin_state->player.hp > 0) {
  203. plugin_state->player.hp--;
  204. } else {
  205. plugin_state->game_state = GameStateGameOver;
  206. }
  207. } else {
  208. if(furi_get_tick() % 2 == 0) z->position.x--;
  209. }
  210. }
  211. }
  212. if(p->position.x >= 128) {
  213. free(p);
  214. plugin_state->projectiles[i] = NULL;
  215. }
  216. }
  217. }
  218. plugin_state->input_shoot = false;
  219. }
  220. static void timer_callback(void* ctx) {
  221. furi_assert(ctx);
  222. FuriMessageQueue* event_queue = ctx;
  223. PluginEvent event = {.type = EventTypeTick};
  224. furi_message_queue_put(event_queue, &event, 0);
  225. }
  226. static void zombiez_state_init(PluginState* const plugin_state) {
  227. plugin_state->player.position.x = PLAYER_START_X;
  228. plugin_state->player.position.y = PLAYER_START_Y;
  229. plugin_state->player.hp = 20;
  230. plugin_state->projectiles_count = 0;
  231. plugin_state->zombies_count = 0;
  232. plugin_state->score = 0;
  233. for(int i = 0; i < PROJECTILES_MAX; i++) {
  234. plugin_state->projectiles[i] = NULL;
  235. }
  236. for(int i = 0; i < ZOMBIES_MAX; i++) {
  237. plugin_state->zombies[i] = NULL;
  238. }
  239. plugin_state->game_state = GameStatePlaying;
  240. plugin_state->input_shoot = false;
  241. }
  242. int32_t zombiez_game_app(void* p) {
  243. UNUSED(p);
  244. uint32_t return_code = 0;
  245. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  246. PluginState* plugin_state = malloc(sizeof(PluginState));
  247. zombiez_state_init(plugin_state);
  248. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  249. if(!plugin_state->mutex) {
  250. FURI_LOG_E("Zombiez", "cannot create mutex\r\n");
  251. return_code = 255;
  252. goto free_and_exit;
  253. }
  254. // Set system callbacks
  255. ViewPort* view_port = view_port_alloc();
  256. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  257. view_port_input_callback_set(view_port, input_callback, event_queue);
  258. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  259. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 22);
  260. // Open GUI and register view_port
  261. Gui* gui = furi_record_open(RECORD_GUI);
  262. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  263. // Call dolphin deed on game start
  264. dolphin_deed(DolphinDeedPluginGameStart);
  265. PluginEvent event;
  266. bool isRunning = true;
  267. while(isRunning) {
  268. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  269. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  270. if(event_status == FuriStatusOk) {
  271. if(event.type == EventTypeKey) {
  272. if(event.input.type == InputTypePress) {
  273. switch(event.input.key) {
  274. case InputKeyUp:
  275. if(plugin_state->player.position.y > MIN_Y &&
  276. plugin_state->game_state == GameStatePlaying) {
  277. plugin_state->player.position.y--;
  278. }
  279. break;
  280. case InputKeyDown:
  281. if(plugin_state->player.position.y < MAX_Y &&
  282. plugin_state->game_state == GameStatePlaying) {
  283. plugin_state->player.position.y++;
  284. }
  285. break;
  286. case InputKeyOk:
  287. if(plugin_state->projectiles_count < PROJECTILES_MAX &&
  288. plugin_state->game_state == GameStatePlaying) {
  289. plugin_state->input_shoot = true;
  290. }
  291. break;
  292. case InputKeyBack:
  293. break;
  294. default:
  295. break;
  296. }
  297. } else if(
  298. event.input.type == InputTypeRepeat &&
  299. plugin_state->game_state == GameStatePlaying) {
  300. switch(event.input.key) {
  301. case InputKeyUp:
  302. if(plugin_state->player.position.y > (MIN_Y + 1)) {
  303. plugin_state->player.position.y -= 4;
  304. }
  305. break;
  306. case InputKeyDown:
  307. if(plugin_state->player.position.y < (MAX_Y - 1)) {
  308. plugin_state->player.position.y += 4;
  309. }
  310. break;
  311. default:
  312. break;
  313. }
  314. } else if(event.input.type == InputTypeLong) {
  315. if(event.input.key == InputKeyOk) {
  316. if(plugin_state->game_state == GameStateGameOver) {
  317. zombiez_state_init(plugin_state);
  318. } else if(plugin_state->projectiles_count >= PROJECTILES_MAX) {
  319. plugin_state->projectiles_count = 0;
  320. plugin_state->player.hp++;
  321. }
  322. } else if(event.input.key == InputKeyBack) {
  323. isRunning = false;
  324. }
  325. }
  326. } else if(event.type == EventTypeTick) {
  327. tick(plugin_state);
  328. }
  329. }
  330. furi_mutex_release(plugin_state->mutex);
  331. view_port_update(view_port);
  332. }
  333. furi_timer_free(timer);
  334. view_port_enabled_set(view_port, false);
  335. gui_remove_view_port(gui, view_port);
  336. furi_record_close(RECORD_GUI);
  337. view_port_free(view_port);
  338. furi_mutex_free(plugin_state->mutex);
  339. free_and_exit:
  340. free(plugin_state);
  341. furi_message_queue_free(event_queue);
  342. return return_code;
  343. }