diff --git a/app/director.js b/app/director.js index 2758277..81b0c0a 100644 --- a/app/director.js +++ b/app/director.js @@ -2,7 +2,10 @@ 'use strict'; - const mapValues = require('lodash.mapvalues'); + const _ = { + mapValues: require('lodash/mapvalues'), + sample: require('lodash/sample') + }; const Scene = require('./scene'); let Director = function(canvas) { @@ -26,7 +29,7 @@ /* map object with scene definitions obtained above into object * containing Scene objects with scene name as a key */ - this.scenes = mapValues(scenes, (value, key) => { + this.scenes = _.mapValues(scenes, (value, key) => { return new Scene(value, this.canvas); }); @@ -124,7 +127,11 @@ /* then play the chosen scene */ .then(() => this.scenes[scene].play()) /* then play the reinaldo scene */ - .then(() => this.scenes.reinaldo.play()) + .then(() => { + /* set valor property for reinaldo scene */ + this.scenes.reinaldo.properties.valor_acto = _.sample([1000, 2000, 3000]); + return this.scenes.reinaldo.play(); + }) /* then start all over again */ .then(main); }; diff --git a/app/scene.js b/app/scene.js index cc4e0cc..f4e0ca4 100644 --- a/app/scene.js +++ b/app/scene.js @@ -2,8 +2,10 @@ 'use strict'; - const flattenDeep = require('lodash.flattendeep'); - const cloneDeep = require('lodash.clonedeep'); + const _ = { + flattenDeep: require('lodash/flattendeep'), + cloneDeep: require('lodash/clonedeep') + }; let Scene = function(scene, canvas) { @@ -13,6 +15,8 @@ /* the html5 canvas object */ this.canvas = canvas; + /* scene properties */ + this.properties = {}; }; /* draw one of the specified frames in this scene */ @@ -24,6 +28,22 @@ }); }; + /* */ + Scene.prototype.testCondition = function(scene) { + + if (!scene.if) { + return true; + } + + /* check if all conditions specified in the 'if' property match + * the values stored in this scene's properties attribute */ + return Object.keys(scene.if).reduce((accumulator, condition) => { + let value = scene.if[condition]; + return accumulator && this.properties[condition] === scene.if[condition]; + }, true); + + }; + /* play this scene on the given canvas */ Scene.prototype.play = function() { @@ -32,10 +52,16 @@ * this will deal with all the repeat: properties and multi level nesting */ let expand = (scene) => { + /* test the if property for this scene. if it fails, filter it out + * by returning an empty array as result of roll expansion */ + if (!this.testCondition(scene)) { + return []; + } + /* expand this roll */ let roll = []; for (let i = 0; i < (scene.repeat || 1); i++) { - roll = roll.concat(cloneDeep(scene.roll)); + roll = roll.concat(_.cloneDeep(scene.roll)); } scene.roll = roll; @@ -50,7 +76,7 @@ }); - return flattenDeep(expanded); + return _.flattenDeep(expanded); }; @@ -76,7 +102,7 @@ /* expand the scene definition (get a flat array of simple { name, duration } * objects */ let expanded = expand(this.scene); - //console.log('expanded', JSON.stringify(expanded, null, 2)); + /* transform the array obtained into an array of functions */ let functionalized = functionalize(expanded); diff --git a/assets/scenes/reinaldo.json b/assets/scenes/reinaldo.json index e491481..6b35e45 100644 --- a/assets/scenes/reinaldo.json +++ b/assets/scenes/reinaldo.json @@ -9,19 +9,31 @@ "duration": 1500 }, { - "if": { "valor": 1000 }, - "images": "reinaldo_balloon_sao_1000", - "duration": 1500 + "if": { "valor_acto": 1000 }, + "roll": [ + { + "images": "reinaldo_balloon_sao_1000", + "duration": 1500 + } + ] }, { - "if": { "valor": 2000 }, - "images": "reinaldo_balloon_sao_2000", - "duration": 1500 + "if": { "valor_acto": 2000 }, + "roll": [ + { + "images": "reinaldo_balloon_sao_2000", + "duration": 1500 + } + ] }, { - "if": { "valor": 3000 }, - "images": "reinaldo_balloon_sao_3000", - "duration": 1500 + "if": { "valor_acto": 3000 }, + "roll": [ + { + "images": "reinaldo_balloon_sao_3000", + "duration": 1500 + } + ] }, { "images": "reinaldo_balloon_nao_tenho", diff --git a/package.json b/package.json index 1fb72ce..6e9caf5 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,7 @@ "homepage": "https://github.com/falsovsky/paradise.js#readme", "dependencies": { "es6-promise": "^4.0.5", - "lodash.flatten": "^4.4.0", - "lodash.flattendeep": "^4.4.0", - "lodash.mapvalues": "^4.6.0" + "lodash": "^4.16.4" }, "devDependencies": { "babel-core": "^6.17.0",