game.c 13 KB

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