scene_dolphin_state.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <furi.h>
  2. #include "scene.h"
  3. #include "assets/emotes.h"
  4. static uint16_t roll_new(uint16_t prev, uint16_t max) {
  5. uint16_t val = 999;
  6. while(val != prev) {
  7. val = random() % max;
  8. break;
  9. }
  10. return val;
  11. }
  12. static void scene_proceed_action(SceneState* state) {
  13. furi_assert(state);
  14. state->prev_action = state->action;
  15. state->action = (state->prev_action != state->next_action) ?
  16. state->next_action :
  17. roll_new(state->next_action, ACTIONS_NUM);
  18. state->action_timeout = default_timeout[state->action];
  19. }
  20. static void scene_dolphin_go_to_poi(SceneState* state) {
  21. furi_assert(state);
  22. if(state->player_global.x < state->poi) {
  23. state->player_flipped = false;
  24. state->player_v.x = SPEED_X / 2;
  25. } else if(state->player_global.x > state->poi) {
  26. state->player_flipped = true;
  27. state->player_v.x = -SPEED_X / 2;
  28. }
  29. }
  30. static void scene_action_handler(SceneState* state) {
  31. furi_assert(state);
  32. if(state->action == MINDCONTROL && state->player_v.x != 0) {
  33. state->action_timeout = default_timeout[state->action];
  34. }
  35. if(state->action_timeout > 0) {
  36. state->action_timeout--;
  37. } else {
  38. if(random() % 1000 > 500) {
  39. state->next_action = roll_new(state->prev_action, ACTIONS_NUM);
  40. state->poi = roll_new(state->player_global.x, WORLD_WIDTH / 4);
  41. }
  42. }
  43. }
  44. void dolphin_scene_update_state(SceneState* state, uint32_t t, uint32_t dt) {
  45. furi_assert(state);
  46. scene_action_handler(state);
  47. UNUSED(dialogues_list);
  48. switch(state->action) {
  49. case WALK:
  50. if(state->player_global.x == state->poi) {
  51. state->player_v.x = 0;
  52. scene_proceed_action(state);
  53. } else {
  54. scene_dolphin_go_to_poi(state);
  55. }
  56. break;
  57. case EMOTE:
  58. state->player_flipped = false;
  59. if(state->action_timeout == 0) {
  60. scene_proceed_action(state);
  61. state->emote_id = roll_new(state->previous_emote, SIZEOF_ARRAY(emotes_list));
  62. break;
  63. }
  64. case INTERACT:
  65. if(state->action_timeout == 0) {
  66. if(state->prev_action == MINDCONTROL) {
  67. state->action = MINDCONTROL;
  68. } else {
  69. scene_proceed_action(state);
  70. }
  71. }
  72. break;
  73. case SLEEP:
  74. if(state->poi != 154) { // temp
  75. state->poi = 154;
  76. } else if(state->player_global.x != state->poi) {
  77. scene_dolphin_go_to_poi(state);
  78. } else {
  79. state->player_v.x = 0;
  80. if(state->action_timeout == 0) {
  81. state->poi = roll_new(state->player_global.x, WORLD_WIDTH / 4);
  82. scene_proceed_action(state);
  83. }
  84. break;
  85. }
  86. default:
  87. if(state->action_timeout == 0) {
  88. scene_proceed_action(state);
  89. }
  90. break;
  91. }
  92. }