game.c 13 KB

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