game.c 12 KB

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