-
Hello! What I am trying to do: I want to make platform for different games based on xstate. Workflow example: {
id: 'platform_machine',
initial: 'loading',
states: {
loading: {
invoke: {
src: 'loadingService',
},
onDone: 'running'
},
running: {
invoke: {
src: 'gameService'
},
onDone: 'exit',
},
exit: {
type: 'final'
},
}
} Starting service const service = interpret(platformMachine.withConfig({
services: {
loadingService: loadingService,
gameService: gameService
}
}))
.start() Platform machine loading resources, then run specific game. Simplified game machine config: {
id: 'game',
initial: 'idle',
states: {
idle: {
on: {
START: 'game'
}
},
game: {
entry: 'onGameStart'
},
exit: {
type: 'final'
}
}
} Game import { createMachine } from 'xstate';
export default class Game extends DisplayObject {
buildMachine() {
return createMachine(config, {
actions: {
onGameStart: this.onGameStart.bind(this)
}
});
}
onGameStart() {
/// ....
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
Yes, XState can be used to design games - we know several ppl using it this way.
I dont understand your question - this is specific to the project you are working on.
A machine in XState is like a template for a running services. You could compare it to being a class and to make its instances you can use the |
Beta Was this translation helpful? Give feedback.
Yes, XState can be used to design games - we know several ppl using it this way.
I dont understand your question - this is specific to the project you are working on.
A machine in XState is like a template for a running services. You could compare it to being a class and to make its instances you can use the
interpret
API which will return an Interpreter instance to u which has asend
method. Its possible to usetransition
method of the Machine but its a pure function so no actions etc will be fired and u would end up reimplementing the interpreter