game.c 13 KB

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