state.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <furi.h>
  2. #include "dolphin_scene/dolphin_scene.h"
  3. #include "dolphin_scene/dolphin_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 dolphin_actions_proceed(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 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 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_dolphin_state(SceneState* state, uint32_t t, uint32_t dt) {
  45. furi_assert(state);
  46. action_handler(state);
  47. switch(state->action) {
  48. case WALK:
  49. if(state->player_global.x == state->poi) {
  50. state->player_v.x = 0;
  51. dolphin_actions_proceed(state);
  52. } else {
  53. dolphin_go_to_poi(state);
  54. }
  55. break;
  56. case EMOTE:
  57. state->player_flipped = false;
  58. if(state->action_timeout == 0) {
  59. dolphin_actions_proceed(state);
  60. state->emote_id = roll_new(state->previous_emote, ARRSIZE(emotes_list));
  61. break;
  62. }
  63. case INTERACT:
  64. if(state->action_timeout == 0) {
  65. if(state->prev_action == MINDCONTROL) {
  66. state->action = MINDCONTROL;
  67. } else {
  68. dolphin_actions_proceed(state);
  69. }
  70. }
  71. break;
  72. case SLEEP:
  73. if(state->poi != 154) { // temp
  74. state->poi = 154;
  75. } else if(state->player_global.x != state->poi) {
  76. dolphin_go_to_poi(state);
  77. } else {
  78. state->player_v.x = 0;
  79. if(state->action_timeout == 0) {
  80. state->poi = roll_new(state->player_global.x, WORLD_WIDTH / 4);
  81. dolphin_actions_proceed(state);
  82. }
  83. break;
  84. }
  85. default:
  86. if(state->action_timeout == 0) {
  87. dolphin_actions_proceed(state);
  88. }
  89. break;
  90. }
  91. UNUSED(dialogues_list);
  92. }