game.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "game.h"
  2. #include "flip_world.h"
  3. #include "flip_world_icons.h"
  4. Wall walls[] = {
  5. WALL(true, 12, 0, 3),
  6. WALL(false, 3, 3, 17),
  7. WALL(false, 23, 3, 6),
  8. WALL(true, 3, 4, 57),
  9. WALL(true, 28, 4, 56),
  10. WALL(false, 4, 7, 5),
  11. WALL(false, 12, 7, 13),
  12. WALL(true, 8, 8, 34),
  13. WALL(true, 12, 8, 42),
  14. WALL(true, 24, 8, 8),
  15. WALL(true, 16, 11, 8),
  16. WALL(false, 17, 11, 4),
  17. WALL(true, 20, 12, 22),
  18. WALL(false, 6, 17, 2),
  19. WALL(true, 24, 19, 15),
  20. WALL(true, 16, 22, 16),
  21. WALL(false, 4, 24, 1),
  22. WALL(false, 21, 28, 2),
  23. WALL(false, 6, 33, 2),
  24. WALL(false, 13, 34, 3),
  25. WALL(false, 17, 37, 11),
  26. WALL(true, 16, 41, 14),
  27. WALL(false, 20, 41, 5),
  28. WALL(true, 20, 45, 12),
  29. WALL(true, 24, 45, 12),
  30. WALL(false, 4, 46, 2),
  31. WALL(false, 9, 46, 3),
  32. WALL(false, 6, 50, 3),
  33. WALL(true, 12, 53, 7),
  34. WALL(true, 8, 54, 6),
  35. WALL(false, 4, 60, 19),
  36. WALL(false, 26, 60, 6),
  37. };
  38. // Global variables to store camera position
  39. static int camera_x = 0;
  40. static int camera_y = 0;
  41. // Background rendering function
  42. static void background_render(Canvas *canvas, Vector pos)
  43. {
  44. // Clear the canvas
  45. canvas_clear(canvas);
  46. // Calculate camera offset to center the player
  47. camera_x = pos.x - (SCREEN_WIDTH / 2);
  48. camera_y = pos.y - (SCREEN_HEIGHT / 2);
  49. // Clamp camera position to prevent showing areas outside the world
  50. camera_x = CLAMP(camera_x, WORLD_WIDTH - SCREEN_WIDTH, 0);
  51. camera_y = CLAMP(camera_y, WORLD_HEIGHT - SCREEN_HEIGHT, 0);
  52. // Draw the outer bounds adjusted by camera offset
  53. canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
  54. // Draw other elements adjusted by camera offset
  55. // Static Dot at (72, 40)
  56. canvas_draw_dot(canvas, 72 - camera_x, 40 - camera_y);
  57. // Static Circle at (16, 16) with radius 4
  58. canvas_draw_circle(canvas, 16 - camera_x, 16 - camera_y, 4);
  59. // Static 8x8 Rectangle Frame at (96, 48)
  60. canvas_draw_frame(canvas, 96 - camera_x, 48 - camera_y, 8, 8);
  61. // Static earth icon at (112, 56)
  62. canvas_draw_icon(canvas, 112 - camera_x, 56 - camera_y, &I_icon_earth);
  63. // static home icon at (128, 24)
  64. canvas_draw_icon(canvas, 128 - camera_x, 24 - camera_y, &I_icon_home);
  65. // static menu icon at (144, 24)
  66. canvas_draw_icon(canvas, 144 - camera_x, 24 - camera_y, &I_icon_info);
  67. // static man icon at (160, 56)
  68. canvas_draw_icon(canvas, 160 - camera_x, 56 - camera_y, &I_icon_man);
  69. // static plant icon at (176, 32)
  70. canvas_draw_icon(canvas, 176 - camera_x, 32 - camera_y, &I_icon_plant);
  71. // static tree icon at (104, 40)
  72. canvas_draw_icon(canvas, 192 - camera_x, 40 - camera_y, &I_icon_tree);
  73. // static woman icon at (208, 32)
  74. canvas_draw_icon(canvas, 208 - camera_x, 32 - camera_y, &I_icon_woman);
  75. }
  76. /****** Entities: Player ******/
  77. typedef struct
  78. {
  79. Vector trajectory; // Direction player would like to move.
  80. float radius; // collision radius
  81. int8_t dx; // x direction
  82. int8_t dy; // y direction
  83. Sprite *sprite; // player sprite
  84. } PlayerContext;
  85. // Forward declaration of player_desc, because it's used in player_spawn function.
  86. static const EntityDescription player_desc;
  87. static void player_spawn(Level *level, GameManager *manager)
  88. {
  89. Entity *player = level_add_entity(level, &player_desc);
  90. // Set player position.
  91. // Depends on your game logic, it can be done in start entity function, but also can be done here.
  92. entity_pos_set(player, (Vector){64, 32});
  93. // Add collision box to player entity
  94. // Box is centered in player x and y, and it's size is 10x10
  95. entity_collider_add_rect(player, 10, 10);
  96. // Get player context
  97. PlayerContext *player_context = entity_context_get(player);
  98. // Load player sprite
  99. player_context->sprite = game_manager_sprite_load(manager, "player.fxbm");
  100. }
  101. static void player_update(Entity *self, GameManager *manager, void *context)
  102. {
  103. UNUSED(context);
  104. // Get game input
  105. InputState input = game_manager_input_get(manager);
  106. // Get player position
  107. Vector pos = entity_pos_get(self);
  108. // Control player movement
  109. if (input.held & GameKeyUp)
  110. pos.y -= 2;
  111. if (input.held & GameKeyDown)
  112. pos.y += 2;
  113. if (input.held & GameKeyLeft)
  114. pos.x -= 2;
  115. if (input.held & GameKeyRight)
  116. pos.x += 2;
  117. // Clamp player position to screen bounds, considering player sprite size (10x10)
  118. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  119. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  120. // Set new player position
  121. entity_pos_set(self, pos);
  122. // Control game exit
  123. if (input.pressed & GameKeyBack)
  124. {
  125. game_manager_game_stop(manager);
  126. }
  127. }
  128. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  129. {
  130. // Get player context
  131. PlayerContext *player = context;
  132. // Get player position
  133. Vector pos = entity_pos_get(self);
  134. // Draw background (updates camera_x and camera_y)
  135. background_render(canvas, pos);
  136. // Draw player sprite relative to camera
  137. canvas_draw_sprite(canvas, player->sprite, pos.x - camera_x - 5, pos.y - camera_y - 5);
  138. // Get game context
  139. GameContext *game_context = game_manager_game_context_get(manager);
  140. // Draw score (optional)
  141. UNUSED(game_context);
  142. // canvas_printf(canvas, 0, 7, "Score: %lu", game_context->score);
  143. }
  144. static const EntityDescription player_desc = {
  145. .start = NULL, // called when entity is added to the level
  146. .stop = NULL, // called when entity is removed from the level
  147. .update = player_update, // called every frame
  148. .render = player_render, // called every frame, after update
  149. .collision = NULL, // called when entity collides with another entity
  150. .event = NULL, // called when entity receives an event
  151. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  152. };
  153. /****** Entities: Target ******/
  154. static Vector random_pos(void)
  155. {
  156. return (Vector){rand() % (SCREEN_WIDTH - 8) + 4, rand() % (SCREEN_HEIGHT - 8) + 4};
  157. }
  158. static void target_start(Entity *self, GameManager *manager, void *context)
  159. {
  160. UNUSED(context);
  161. UNUSED(manager);
  162. // Set target position
  163. entity_pos_set(self, random_pos());
  164. // Add collision circle to target entity
  165. // Circle is centered in target x and y, and it's radius is 3
  166. entity_collider_add_circle(self, 3);
  167. }
  168. static void target_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  169. {
  170. UNUSED(context);
  171. UNUSED(manager);
  172. // Get target position
  173. Vector pos = entity_pos_get(self);
  174. // Draw target relative to the camera
  175. canvas_draw_disc(canvas, pos.x - camera_x, pos.y - camera_y, 3);
  176. }
  177. static void target_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  178. {
  179. UNUSED(context);
  180. // Check if target collided with player
  181. if (entity_description_get(other) == &player_desc)
  182. {
  183. // Increase score
  184. GameContext *game_context = game_manager_game_context_get(manager);
  185. game_context->score++;
  186. // Move target to new random position
  187. entity_pos_set(self, random_pos());
  188. }
  189. }
  190. static const EntityDescription target_desc = {
  191. .start = target_start, // called when entity is added to the level
  192. .stop = NULL, // called when entity is removed from the level
  193. .update = NULL, // called every frame
  194. .render = target_render, // called every frame, after update
  195. .collision = target_collision, // called when entity collides with another entity
  196. .event = NULL, // called when entity receives an event
  197. .context_size = 0, // size of entity context, will be automatically allocated and freed
  198. };
  199. /****** Entities: Wall ******/
  200. static uint8_t wall_index;
  201. static void wall_start(Entity *self, GameManager *manager, void *context);
  202. typedef struct
  203. {
  204. float width;
  205. float height;
  206. } WallContext;
  207. static void wall_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  208. {
  209. UNUSED(manager);
  210. UNUSED(self);
  211. UNUSED(canvas);
  212. UNUSED(context);
  213. // WallContext *wall = context;
  214. // Vector pos = entity_pos_get(self);
  215. // Draw the wall relative to the camera
  216. // canvas_draw_box(
  217. // canvas,
  218. // pos.x - camera_x - (wall->width / 2),
  219. // pos.y - camera_y - (wall->height / 2),
  220. // wall->width,
  221. // wall->height);
  222. }
  223. static void wall_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  224. {
  225. WallContext *wall = context;
  226. // Check if wall collided with player
  227. if (entity_description_get(other) == &player_desc)
  228. {
  229. // Increase score
  230. GameContext *game_context = game_manager_game_context_get(manager);
  231. game_context->score++;
  232. PlayerContext *player = (PlayerContext *)entity_context_get(other);
  233. if (player)
  234. {
  235. if (player->dx || player->dy)
  236. {
  237. Vector pos = entity_pos_get(other);
  238. // TODO: Based on where we collided, we should still slide across/down the wall.
  239. UNUSED(wall);
  240. if (player->dx)
  241. {
  242. FURI_LOG_D(
  243. "Player",
  244. "Player collided with wall, dx: %d. center:%f,%f",
  245. player->dx,
  246. (double)pos.x,
  247. (double)pos.y);
  248. pos.x -= player->dx;
  249. player->dx = 0;
  250. }
  251. if (player->dy)
  252. {
  253. FURI_LOG_D(
  254. "Player",
  255. "Player collided with wall, dy: %d. center:%f,%f",
  256. player->dy,
  257. (double)pos.x,
  258. (double)pos.y);
  259. pos.y -= player->dy;
  260. player->dy = 0;
  261. }
  262. entity_pos_set(other, pos);
  263. FURI_LOG_D("Player", "Set to center:%f,%f", (double)pos.x, (double)pos.y);
  264. }
  265. }
  266. else
  267. {
  268. FURI_LOG_D("Player", "Player collided with wall, but context null.");
  269. }
  270. }
  271. else
  272. {
  273. // HACK: Wall touching other items destroys each other (to help find collider issues)
  274. Level *level = game_manager_current_level_get(manager);
  275. level_remove_entity(level, self);
  276. level_remove_entity(level, other);
  277. }
  278. }
  279. static const EntityDescription wall_desc = {
  280. .start = wall_start, // called when entity is added to the level
  281. .stop = NULL, // called when entity is removed from the level
  282. .update = NULL, // called every frame
  283. .render = wall_render, // called every frame, after update
  284. .collision = wall_collision, // called when entity collides with another entity
  285. .event = NULL, // called when entity receives an event
  286. .context_size =
  287. sizeof(WallContext), // size of entity context, will be automatically allocated and freed
  288. };
  289. static void wall_start(Entity *self, GameManager *manager, void *context)
  290. {
  291. UNUSED(manager);
  292. WallContext *wall = context;
  293. // TODO: We can get the current number of items from the level (instead of wall_index).
  294. if (wall_index < COUNT_OF(walls))
  295. {
  296. if (walls[wall_index].horizontal)
  297. {
  298. wall->width = walls[wall_index].length * 2;
  299. wall->height = 1 * 2;
  300. }
  301. else
  302. {
  303. wall->width = 1 * 2;
  304. wall->height = walls[wall_index].length * 2;
  305. }
  306. entity_pos_set(
  307. self,
  308. (Vector){
  309. walls[wall_index].x + wall->width / 2, walls[wall_index].y + wall->height / 2});
  310. entity_collider_add_rect(self, wall->width, wall->height);
  311. wall_index++;
  312. }
  313. }
  314. /****** Level ******/
  315. static void level_alloc(Level *level, GameManager *manager, void *context)
  316. {
  317. UNUSED(manager);
  318. UNUSED(context);
  319. // Add player entity to the level
  320. player_spawn(level, manager);
  321. // Add first target entity to the level
  322. level_add_entity(level, &target_desc);
  323. // Add wall entities to the level
  324. wall_index = 0;
  325. for (size_t i = 0; i < COUNT_OF(walls); i++)
  326. {
  327. level_add_entity(level, &wall_desc);
  328. }
  329. }
  330. static const LevelBehaviour level = {
  331. .alloc = level_alloc, // called once, when level allocated
  332. .free = NULL, // called once, when level freed
  333. .start = NULL, // called when level is changed to this level
  334. .stop = NULL, // called when level is changed from this level
  335. .context_size = 0, // size of level context, will be automatically allocated and freed
  336. };
  337. /****** Game ******/
  338. /*
  339. Write here the start code for your game, for example: creating a level and so on.
  340. Game context is allocated (game.context_size) and passed to this function, you can use it to store your game data.
  341. */
  342. static void game_start(GameManager *game_manager, void *ctx)
  343. {
  344. UNUSED(game_manager);
  345. // Do some initialization here, for example you can load score from storage.
  346. // For simplicity, we will just set it to 0.
  347. GameContext *game_context = ctx;
  348. game_context->score = 0;
  349. // Add level to the game
  350. game_manager_add_level(game_manager, &level);
  351. }
  352. /*
  353. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  354. You don't need to free level, sprites or entities, it will be done automatically.
  355. Also, you don't need to free game_context, it will be done automatically, after this function.
  356. */
  357. static void game_stop(void *ctx)
  358. {
  359. UNUSED(ctx);
  360. // GameContext *game_context = ctx;
  361. // Do some deinitialization here, for example you can save score to storage.
  362. // For simplicity, we will just print it.
  363. // FURI_LOG_I("Game", "Your score: %lu", game_context->score);
  364. }
  365. /*
  366. Your game configuration, do not rename this variable, but you can change its content here.
  367. */
  368. const Game game = {
  369. .target_fps = 30, // target fps, game will try to keep this value
  370. .show_fps = false, // show fps counter on the screen
  371. .always_backlight = true, // keep display backlight always on
  372. .start = game_start, // will be called once, when game starts
  373. .stop = game_stop, // will be called once, when game stops
  374. .context_size = sizeof(GameContext), // size of game context
  375. };