fix scene playing algorithm

added some test scenes to test deep recursion.
This commit is contained in:
Mário C 2016-10-23 20:37:51 +01:00
parent 2745182e9b
commit c0e321d128
3 changed files with 17 additions and 15 deletions

View File

@ -37,7 +37,8 @@
/* create user input area */ /* create user input area */
document.getElementById('ul-teclas').innerHTML = userChoices.reduce((accumulator, choice) => { document.getElementById('ul-teclas').innerHTML = userChoices.reduce((accumulator, choice) => {
accumulator += `<li class="user-action" id="li-${choice.name}" data-action="${choice.name}">${choice.key}<div>${choice.name}</div></li>`; accumulator +=
`<li class="user-action" id="li-${choice.name}" data-action="${choice.name}">${choice.key}<div>${choice.name}</div></li>`;
return accumulator; return accumulator;
}, ''); }, '');
@ -130,7 +131,7 @@
/* the game starts by calling this */ /* the game starts by calling this */
let main = () => { let main = () => {
return this.scenes.test2.play() return this.scenes.choice.play()
.then(() => this.enableUserInput(userChoices, afterChoice)); .then(() => this.enableUserInput(userChoices, afterChoice));
}; };

View File

@ -1,8 +1,9 @@
(function() { (function() {
'use strict'; 'use strict';
const flattenDeep = require('lodash.flattendeep'); const flattenDeep = require('lodash.flattendeep');
const cloneDeep = require('lodash.clonedeep');
let Scene = function(scene, canvas) { let Scene = function(scene, canvas) {
@ -31,12 +32,14 @@
* this will deal with all the repeat: properties and multi level nesting */ * this will deal with all the repeat: properties and multi level nesting */
let expand = (scene) => { let expand = (scene) => {
/* expand first roll */ /* expand this roll */
console.log('expanding', JSON.stringify(scene, null, 2)); let roll = [];
scene.roll = [].concat.apply([], Array(scene.repeat || 1).fill(scene.roll)); for (let i = 0; i < (scene.repeat || 1); i++) {
roll = roll.concat(cloneDeep(scene.roll));
/* recursively expand rolls */ }
scene.roll = roll;
/* recursively expand child rolls */
let expanded = scene.roll.map((scene) => { let expanded = scene.roll.map((scene) => {
if (scene.hasOwnProperty('roll')) { if (scene.hasOwnProperty('roll')) {
@ -47,7 +50,7 @@
}); });
return expanded; return flattenDeep(expanded);
}; };
@ -61,9 +64,7 @@
return () => new Promise((resolve, reject) => { return () => new Promise((resolve, reject) => {
this.drawFrame(frame.images); this.drawFrame(frame.images);
setTimeout(() => { setTimeout(() => resolve(), frame.duration);
return resolve();
}, frame.duration);
}); });
}); });
@ -75,7 +76,7 @@
/* expand the scene definition (get a flat array of simple { name, duration } /* expand the scene definition (get a flat array of simple { name, duration }
* objects */ * objects */
let expanded = expand(this.scene); let expanded = expand(this.scene);
expanded.forEach(x => console.log(x)); //console.log('expanded', JSON.stringify(expanded, null, 2));
/* transform the array obtained into an array of functions */ /* transform the array obtained into an array of functions */
let functionalized = functionalize(expanded); let functionalized = functionalize(expanded);

View File

@ -1,5 +1,5 @@
{ {
"repeat": 1, "repeat": 10,
"roll": [ "roll": [
{ {
"images": "choice_balloon_vaginal", "images": "choice_balloon_vaginal",
@ -13,7 +13,7 @@
"repeat": 2, "repeat": 2,
"roll": [ "roll": [
{ {
"repeat": 2, "repeat": 3,
"roll": [ "roll": [
{ {
"images": "oral_frame1", "images": "oral_frame1",