Maybe this was already discussed somewhere, I myself could not find an exact answer how to approach my problem:
I have a mutliniear «story» which executes code in each segment. I wrote a state machine, which initiates a new segment, whenever another segment calls that one. Each segment has an onEnter-, an onCheck and an onLeave function. As the name already says, the onEnter executes when the segment is called, the onCheck checks some input until some conditions are fullfilled (if yes they will lead to another segment) and onLeave executes just before the next segment is called.
Currently I just wrote a javascript object kinda like this:
var flow = {
seg1: {
onEnter: function() {
this.say('Seg1');
},
onCheck: function(data) {
if (data.condition) {
machine.callNextSeg('seg2');
} else if (data.condition2) {
machine.callNextSeg('seg3');
}
},
onLeave: function() {
}
},
seg2: {
onEnter: function() {
this.say('Seg2');
},
onGestureCheck: function(data) {
},
onLeave: function() {
}
},
seg3: {
onEnter: function() {
this.say('Seg3');
},
onGestureCheck: function(data) {
},
onLeave: function() {
}
}
};
The example is a bit simplified for understanding, but the code inside would be a little more complex.
I would rather like to have a JSON file, which is loaded, parsed and creates such an object. The JSON File should use a more simple and more abstract syntax to write. Especially to make it quicker.
I imagine something like this:
{
"seg1": {
"onEnter": {
"say": 'Seg1'
},
"onCheck": {
"condition1": "seg2",
"condition2": "seg3"
},
"onLeave": {
}
},
"seg2": {
"onEnter": {
"say": 'Seg2'
},
"onCheck": {
},
"onLeave": {
}
}
}
The conditions are booleans and if true the described segment should be called.
Would that be easy to parse the json, create an object, and create functions inside it for each onEnter, onCheck and onLeave? And also write the necessary if clauses?
Thanks for hints into the right direction.
cheers
J.