Created
April 16, 2020 19:28
-
-
Save christianhg/15582ab43cd031fbb01e62accf5b32ce to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetchMachine = Machine({ | |
id: 'snake', | |
context: {}, | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
UP: { target: 'moving.up' }, | |
RIGHT: { target: 'moving.right' }, | |
DOWN: { target: 'moving.down' }, | |
LEFT: { target: 'moving.left' }, | |
}, | |
}, | |
moving: { | |
on: { | |
SPACE: { target: '#snake.paused' }, | |
UP: { actions: ['queueUp'] }, | |
RIGHT: { actions: ['queueRight'] }, | |
DOWN: { actions: ['queueDown'] }, | |
LEFT: { actions: ['queueLeft'] }, | |
}, | |
states: { | |
up: { | |
entry: ['resetQueue'], | |
on: { | |
TICK: [ | |
{ target: '#snake.dead', cond: 'boundUp' }, | |
{ target: '#snake.dead', cond: 'snakeUp' }, | |
{ | |
cond: 'appleUp', | |
target: '.unlocked', | |
actions: ['growUp', 'updateApples', 'notifyUpdate'], | |
}, | |
{ target: '.unlocked', actions: ['moveUp', 'notifyUpdate'] }, | |
], | |
}, | |
initial: 'locked', | |
states: { | |
locked: {}, | |
unlocked: { | |
on: { | |
'': [ | |
{ cond: 'rightQueued', target: '#snake.moving.right' }, | |
{ cond: 'leftQueued', target: '#snake.moving.left' }, | |
], | |
}, | |
}, | |
}, | |
}, | |
right: { | |
entry: ['resetQueue'], | |
on: { | |
TICK: [ | |
{ target: '#snake.dead', cond: 'boundRight' }, | |
{ target: '#snake.dead', cond: 'snakeRight' }, | |
{ | |
cond: 'appleRight', | |
target: '.unlocked', | |
actions: ['growRight', 'updateApples', 'notifyUpdate'], | |
}, | |
{ | |
target: '.unlocked', | |
actions: ['moveRight', 'notifyUpdate'], | |
}, | |
], | |
}, | |
initial: 'locked', | |
states: { | |
locked: {}, | |
unlocked: { | |
on: { | |
'': [ | |
{ cond: 'upQueued', target: '#snake.moving.up' }, | |
{ cond: 'downQueued', target: '#snake.moving.down' }, | |
], | |
}, | |
}, | |
}, | |
}, | |
down: { | |
entry: ['resetQueue'], | |
on: { | |
TICK: [ | |
{ target: '#snake.dead', cond: 'boundDown' }, | |
{ target: '#snake.dead', cond: 'snakeDown' }, | |
{ | |
cond: 'appleDown', | |
target: '.unlocked', | |
actions: ['growDown', 'updateApples', 'notifyUpdate'], | |
}, | |
{ | |
target: '.unlocked', | |
actions: ['moveDown', 'notifyUpdate'], | |
}, | |
], | |
}, | |
initial: 'locked', | |
states: { | |
locked: {}, | |
unlocked: { | |
on: { | |
'': [ | |
{ cond: 'rightQueued', target: '#snake.moving.right' }, | |
{ cond: 'leftQueued', target: '#snake.moving.left' }, | |
], | |
}, | |
}, | |
}, | |
}, | |
left: { | |
entry: ['resetQueue'], | |
on: { | |
TICK: [ | |
{ target: '#snake.dead', cond: 'boundLeft' }, | |
{ target: '#snake.dead', cond: 'snakeLeft' }, | |
{ | |
cond: 'appleLeft', | |
target: '.unlocked', | |
actions: ['growLeft', 'updateApples', 'notifyUpdate'], | |
}, | |
{ | |
target: '.unlocked', | |
actions: ['moveLeft', 'notifyUpdate'], | |
}, | |
], | |
}, | |
initial: 'locked', | |
states: { | |
locked: {}, | |
unlocked: { | |
on: { | |
'': [ | |
{ cond: 'upQueued', target: '#snake.moving.up' }, | |
{ cond: 'downQueued', target: '#snake.moving.down' }, | |
], | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
dead: { | |
entry: ['notifyUpdate'], | |
on: { | |
SPACE: { target: 'idle', actions: ['reset', 'notifyUpdate'] }, | |
}, | |
}, | |
paused: { | |
entry: ['notifyUpdate'], | |
on: { | |
SPACE: { target: 'idle', actions: ['notifyUpdate'] }, | |
ESCAPE: { target: 'idle', actions: ['reset', 'notifyUpdate'] }, | |
}, | |
}, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment