Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const splashScene = '@state/splashScene';
- const loadingScene = '@state/loadingScene';
- const menuScene = '@state/menuScene';
- const gameScene = '@state/gameScene';
- const summaryScene = '@state/summaryScene';
- const shopScene = '@state/shopScene';
- const splashSceneStates = {
- initial: 'loggingIn',
- on: {
- INIT_COMPLETE: { target: `${loadingScene}`, actions: 'switchScene' }
- },
- states: {
- loggingIn: {
- on: {
- DONE: 'initializing'
- }
- },
- initializing: {
- entry: send('INIT_COMPLETE', { delay: 2000 })
- }
- }
- };
- const loadingSceneState = {
- initial: 'unknown',
- on: {
- LOAD_COMPLETE: { target: `${gameScene}`, actions: 'switchScene' },
- SAVE_COMPLETE: { target: `${menuScene}`, actions: 'switchScene' },
- GET_PROFILE_COMPLETE: { target: `${menuScene}`, actions: 'switchScene' }
- },
- states: {
- unknown: {
- on: {
- '': [
- { target: 'getting', cond: ({ loadingFor }) => loadingFor === 'get_profile' },
- { target: 'loading', cond: ({ loadingFor }) => loadingFor === 'resume_game' },
- { target: 'saving', cond: ({ loadingFor }) => loadingFor === 'save_game' }
- ]
- }
- },
- getting: {
- entry: send('GET_PROFILE_COMPLETE', { delay: 2000 })
- },
- loading: {
- entry: send('LOAD_COMPLETE', { delay: 2000 })
- },
- saving: {
- entry: send('SAVE_COMPLETE', { delay: 2000 })
- }
- }
- };
- const menuSceneStates = {
- initial: 'idle',
- on: {
- RESUME_SAVE_GAME: {
- target: `${loadingScene}`,
- actions: assign((context, event) => ({
- loadingFor: 'resume_game'
- }))
- },
- START_NEW_GAME: `${summaryScene}`
- },
- states: {
- idle: {
- on: {
- TOUCH_PLAY_BUTTON: [
- {
- actions: 'showResumeSavedGameDialog',
- cond: ({ data }) => data !== undefined
- },
- { actions: send('START_NEW_GAME') }
- ],
- }
- }
- }
- };
- const gameSceneStates = {
- initial: 'preparing',
- on: {
- EXIT_GAME: {
- target: `${loadingScene}`,
- actions: [
- 'setLoadingFor',
- 'switchScene'
- ]
- },
- NEXT_LEVEL: {
- target: `${summaryScene}`,
- actions: ['setSummaryFor', 'switchScene']
- },
- END_LEVEL: {
- target: `${summaryScene}`,
- actions: ['setSummaryFor', 'switchScene']
- }
- },
- states: {
- preparing: {
- on: {
- PREPARE_COMPLETE: 'playing'
- }
- },
- playing: {
- on: {
- PAUSE: {
- target: 'paused',
- actions: 'showPauseDialog'
- },
- END: 'ended'
- }
- },
- paused: {
- on: {
- PLAY: 'playing',
- NEXT: { actions: send('NEXT_LEVEL', {
- summaryFor: 'complete_level',
- toScene: 'SummaryScene'}
- ) },
- QUIT: {
- actions: send('EXIT_GAME', {
- loadingFor: 'save_game',
- toScene: 'MenuScene'}
- )
- }
- }
- },
- ended: {
- entry: send('END_LEVEL', { toScene: 'SummaryScene' })
- }
- }
- };
- const summarySceneStates = {
- initial: 'unknown',
- on: {
- START_PLAYING: `${gameScene}`,
- CONTINUE_PLAYING: `${gameScene}`,
- END_GAME: `${menuScene}`,
- NEXT: `${shopScene}`
- },
- states: {
- unknown: {
- on: {
- '': [
- {
- target: 'gameOver',
- cond: ({ summaryFor }) => summaryFor === 'game_over'
- },
- {
- target: 'completed',
- cond: ({ summaryFor }) => summaryFor === 'complete_level'
- },
- { target: 'goalTarget' }
- ]
- }
- },
- goalTarget: {
- entry: send('START_PLAYING', { delay: 2000 })
- },
- gameOver: {
- entry: send('END_GAME', { delay: 2000 })
- },
- completed: {
- entry: send('NEXT', { delay: 2000 })
- }
- }
- };
- const shopSceneStates = {
- on: {
- DONE: {
- target: `${summaryScene}`,
- actions: 'setSummaryFor'
- }
- },
- entry: send('DONE', { delay: 2000, purpose: 'goal_target' })
- };
- const switchScene = (_context, { toScene }) => {
- console.log(`switch to :`, toScene);
- };
- const showResumeSavedGameDialog = (ctx, event) => {
- console.log(`showResumeSavedGameDialog`);
- };
- const showPauseDialog = (ctx, event) => {
- console.log('show Pause Dialog');
- };
- const setLoadingFor = assign({
- loadingFor: (_ctx, { purpose }) => purpose
- });
- const setSummaryFor = assign({
- summaryFor: (_ctx, { purpose }) => purpose
- });
- const goldMinerMachine = Machine({
- id: 'goldMinerMachine',
- initial: `${splashScene}`,
- context: {
- loadingFor: 'get_profile'
- },
- states: {
- [splashScene]: {
- ...splashSceneStates
- },
- [loadingScene]: {
- ...loadingSceneState
- },
- [menuScene]: {
- ...menuSceneStates
- },
- [gameScene]: {
- ...gameSceneStates
- },
- [summaryScene]: {
- ...summarySceneStates
- },
- [shopScene]: {
- ...shopSceneStates
- }
- }
- }, {actions: {
- switchScene,
- setLoadingFor,
- setSummaryFor
- }} );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement