Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. const splashScene = '@state/splashScene';
  2. const loadingScene = '@state/loadingScene';
  3. const menuScene = '@state/menuScene';
  4. const gameScene = '@state/gameScene';
  5. const summaryScene = '@state/summaryScene';
  6. const shopScene = '@state/shopScene';
  7.  
  8. const splashSceneStates = {
  9. initial: 'loggingIn',
  10. on: {
  11. INIT_COMPLETE: { target: `${loadingScene}`, actions: 'switchScene' }
  12. },
  13. states: {
  14. loggingIn: {
  15. on: {
  16. DONE: 'initializing'
  17. }
  18. },
  19. initializing: {
  20. entry: send('INIT_COMPLETE', { delay: 2000 })
  21. }
  22. }
  23. };
  24.  
  25. const loadingSceneState = {
  26. initial: 'unknown',
  27. on: {
  28. LOAD_COMPLETE: { target: `${gameScene}`, actions: 'switchScene' },
  29. SAVE_COMPLETE: { target: `${menuScene}`, actions: 'switchScene' },
  30. GET_PROFILE_COMPLETE: { target: `${menuScene}`, actions: 'switchScene' }
  31. },
  32. states: {
  33. unknown: {
  34. on: {
  35. '': [
  36. { target: 'getting', cond: ({ loadingFor }) => loadingFor === 'get_profile' },
  37. { target: 'loading', cond: ({ loadingFor }) => loadingFor === 'resume_game' },
  38. { target: 'saving', cond: ({ loadingFor }) => loadingFor === 'save_game' }
  39. ]
  40. }
  41. },
  42. getting: {
  43. entry: send('GET_PROFILE_COMPLETE', { delay: 2000 })
  44. },
  45. loading: {
  46. entry: send('LOAD_COMPLETE', { delay: 2000 })
  47. },
  48. saving: {
  49. entry: send('SAVE_COMPLETE', { delay: 2000 })
  50. }
  51. }
  52. };
  53.  
  54. const menuSceneStates = {
  55. initial: 'idle',
  56. on: {
  57.  
  58. RESUME_SAVE_GAME: {
  59. target: `${loadingScene}`,
  60. actions: assign((context, event) => ({
  61. loadingFor: 'resume_game'
  62. }))
  63. },
  64. START_NEW_GAME: `${summaryScene}`
  65. },
  66. states: {
  67. idle: {
  68. on: {
  69. TOUCH_PLAY_BUTTON: [
  70. {
  71. actions: 'showResumeSavedGameDialog',
  72. cond: ({ data }) => data !== undefined
  73. },
  74. { actions: send('START_NEW_GAME') }
  75. ],
  76. }
  77. }
  78. }
  79. };
  80.  
  81. const gameSceneStates = {
  82. initial: 'preparing',
  83. on: {
  84. EXIT_GAME: {
  85. target: `${loadingScene}`,
  86. actions: [
  87. 'setLoadingFor',
  88. 'switchScene'
  89. ]
  90. },
  91. NEXT_LEVEL: {
  92. target: `${summaryScene}`,
  93. actions: ['setSummaryFor', 'switchScene']
  94. },
  95. END_LEVEL: {
  96. target: `${summaryScene}`,
  97. actions: ['setSummaryFor', 'switchScene']
  98.  
  99. }
  100. },
  101. states: {
  102. preparing: {
  103. on: {
  104. PREPARE_COMPLETE: 'playing'
  105. }
  106. },
  107. playing: {
  108. on: {
  109. PAUSE: {
  110. target: 'paused',
  111. actions: 'showPauseDialog'
  112. },
  113. END: 'ended'
  114. }
  115. },
  116. paused: {
  117. on: {
  118. PLAY: 'playing',
  119. NEXT: { actions: send('NEXT_LEVEL', {
  120. summaryFor: 'complete_level',
  121. toScene: 'SummaryScene'}
  122. ) },
  123. QUIT: {
  124. actions: send('EXIT_GAME', {
  125. loadingFor: 'save_game',
  126. toScene: 'MenuScene'}
  127. )
  128. }
  129. }
  130. },
  131. ended: {
  132. entry: send('END_LEVEL', { toScene: 'SummaryScene' })
  133. }
  134. }
  135. };
  136.  
  137. const summarySceneStates = {
  138. initial: 'unknown',
  139. on: {
  140. START_PLAYING: `${gameScene}`,
  141. CONTINUE_PLAYING: `${gameScene}`,
  142. END_GAME: `${menuScene}`,
  143. NEXT: `${shopScene}`
  144. },
  145. states: {
  146. unknown: {
  147. on: {
  148. '': [
  149. {
  150. target: 'gameOver',
  151. cond: ({ summaryFor }) => summaryFor === 'game_over'
  152. },
  153. {
  154. target: 'completed',
  155. cond: ({ summaryFor }) => summaryFor === 'complete_level'
  156. },
  157. { target: 'goalTarget' }
  158. ]
  159. }
  160. },
  161. goalTarget: {
  162. entry: send('START_PLAYING', { delay: 2000 })
  163. },
  164. gameOver: {
  165. entry: send('END_GAME', { delay: 2000 })
  166. },
  167. completed: {
  168. entry: send('NEXT', { delay: 2000 })
  169. }
  170. }
  171. };
  172.  
  173. const shopSceneStates = {
  174. on: {
  175. DONE: {
  176. target: `${summaryScene}`,
  177. actions: 'setSummaryFor'
  178. }
  179. },
  180. entry: send('DONE', { delay: 2000, purpose: 'goal_target' })
  181. };
  182.  
  183. const switchScene = (_context, { toScene }) => {
  184. console.log(`switch to :`, toScene);
  185. };
  186.  
  187. const showResumeSavedGameDialog = (ctx, event) => {
  188. console.log(`showResumeSavedGameDialog`);
  189. };
  190.  
  191. const showPauseDialog = (ctx, event) => {
  192. console.log('show Pause Dialog');
  193. };
  194.  
  195. const setLoadingFor = assign({
  196. loadingFor: (_ctx, { purpose }) => purpose
  197. });
  198.  
  199. const setSummaryFor = assign({
  200. summaryFor: (_ctx, { purpose }) => purpose
  201. });
  202.  
  203. const goldMinerMachine = Machine({
  204. id: 'goldMinerMachine',
  205. initial: `${splashScene}`,
  206. context: {
  207. loadingFor: 'get_profile'
  208. },
  209. states: {
  210. [splashScene]: {
  211. ...splashSceneStates
  212. },
  213. [loadingScene]: {
  214. ...loadingSceneState
  215. },
  216. [menuScene]: {
  217. ...menuSceneStates
  218. },
  219. [gameScene]: {
  220. ...gameSceneStates
  221. },
  222. [summaryScene]: {
  223. ...summarySceneStates
  224. },
  225. [shopScene]: {
  226. ...shopSceneStates
  227. }
  228. }
  229.  
  230. }, {actions: {
  231. switchScene,
  232. setLoadingFor,
  233. setSummaryFor
  234. }} );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement