game.c 14 KB

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