From a393ce2c03e9b8af9ebfa44703f31b453bdf01c4 Mon Sep 17 00:00:00 2001 From: Raffaele Pojer Date: Mon, 7 Sep 2020 21:16:38 +0200 Subject: [PATCH] Used promises to store sprite, but targets at the root of the tree do not change the cosumes! --- src/extensions/botch/botch_util.js | 7 +++-- src/extensions/botch/index.js | 48 ++++++++++++++++++++---------- src/extensions/botch/organism.js | 11 +++++-- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/extensions/botch/botch_util.js b/src/extensions/botch/botch_util.js index e0034f368de..295fc02ce74 100644 --- a/src/extensions/botch/botch_util.js +++ b/src/extensions/botch/botch_util.js @@ -87,7 +87,7 @@ class BotchUtil { vmCostumes.forEach((costume, i) => { costume.name = `org_${i + 1}`; }); - this.handleNewCostume(vmCostumes, id); // Tolto .then( + return this.handleNewCostume(vmCostumes, id); // Tolto .then( } addCostumeFromBuffer (dataBuffer, id) { @@ -100,17 +100,18 @@ class BotchUtil { costumeFormat_, dataBuffer ); - this.handleCostume([vmCostume], id); + return this.handleCostume([vmCostume], id); } /** * Assign a new costume (SVG) to the selected target (id) * @param {string} fileData string of the svg * @param {string?} id id of the target + * @returns {Promise} when the costumes has been added * @since botch-0.1 */ uploadCostumeEdit (fileData, id) { - this.addCostumeFromBuffer(new Uint8Array((new _TextEncoder()).encode(fileData)), id); + return this.addCostumeFromBuffer(new Uint8Array((new _TextEncoder()).encode(fileData)), id); } // diff --git a/src/extensions/botch/index.js b/src/extensions/botch/index.js index 17c36f36189..ab1f53ebd65 100644 --- a/src/extensions/botch/index.js +++ b/src/extensions/botch/index.js @@ -332,25 +332,26 @@ class Scratch3Botch { // check if it is already set as organism, if true don't delete the sprites if (!(this.organismMap.size > 0 && this.organismMap.get(util.target.id))) { this.botchUtil.deleteClones(util.target.id); - this.botchUtil.deleteAllOrgCostumes(util.target); // TODO mettere nel stop all + this.botchUtil.deleteAllOrgCostumes(util.target); // TO DO mettere nel stop all this.organismMap = new Map(); // check if it is already assigned somewhere if (this.enemiesMap.size > 0 && util.target.id === this.enemiesMap.entries().next().value[0]) { this.enemiesMap = new Map(); } - + // create an organism with the original const org = new Organism(util.target, this.mass, this.maxForce); this.organismMap.set(util.target.id, org); org.setParentVariable(); org.setOrgDna(); - org.assignOrgCostume(); - - this.currentOrgCounter++; - org.currentName = this.currentOrgCounter.toString(); - const p = this.storeSprite(util.target.id, org.currentName); - util.target.setCustomState('storedMd5', p.md5); + org.assignOrgCostume().then(() => { + this.currentOrgCounter++; + org.currentName = this.currentOrgCounter.toString(); + const p = this.storeSprite(util.target.id, org.currentName); + util.target.setCustomState('storedMd5', p.md5); + return p; + }); const state = this.getBotchState(util.target); state.type = Scratch3Botch.ORGANISM_TYPE; } @@ -427,7 +428,14 @@ class Scratch3Botch { this.organismMap.set(newClone.id, org); org.setParentVariable(); org.setOrgDna(); - org.assignOrgCostume(); + org.assignOrgCostume().then(() => { + this.currentOrgCounter++; + org.currentName = this.currentOrgCounter.toString(); + const p = this.storeSprite(newClone.id, org.currentName); + newClone.setCustomState('storedMd5', p.md5); + return p; + }); + // Place behind the original target. newClone.goBehindOther(target); // Set a random size @@ -440,10 +448,6 @@ class Scratch3Botch { const stageH = this.runtime.constructor.STAGE_HEIGHT; newClone.setXY((Math.random() - 0.5) * stageW, (Math.random() - 0.5) * stageH); } - this.currentOrgCounter++; - org.currentName = this.currentOrgCounter.toString(); - const p = this.storeSprite(newClone.id, org.currentName); - newClone.setCustomState('storedMd5', p.md5); const state = this.getBotchState(newClone); state.type = Scratch3Botch.ORGANISM_TYPE; } @@ -585,9 +589,21 @@ class Scratch3Botch { newClone.clearEffects(); org.childNumber++; newOrg.currentName = `${org.currentName}.${org.childNumber}`; - const p = this.storeSprite(newClone.id, newOrg.currentName); - newClone.setCustomState('storedMd5', p.md5); - newOrg.setParentVariable(org.target.getCustomState('storedMd5')); + /* newOrg.prom.then(() => { + const p = this.storeSprite(newClone.id, newOrg.currentName); + newClone.setCustomState('storedMd5', p.md5); + newOrg.setParentVariable(org.target.getCustomState('storedMd5')); + return p; + }); */ + + newOrg.assignOrgCostume().then(() => { + const p = this.storeSprite(newClone.id, newOrg.currentName); + newClone.setCustomState('storedMd5', p.md5); + newOrg.setParentVariable(org.target.getCustomState('storedMd5')); + return p; + }); + + newOrg.setOrgDna(); this.organismMap.set(newClone.id, newOrg); } diff --git a/src/extensions/botch/organism.js b/src/extensions/botch/organism.js index 3579688978e..f5fa566a578 100644 --- a/src/extensions/botch/organism.js +++ b/src/extensions/botch/organism.js @@ -10,6 +10,7 @@ const Vector2 = require('./vector2'); const MathUtil = require('../../util/math-util'); const svgGen = require('./svg-generator'); const BotchUtil = require('./botch_util'); +const log = require('../../util/log'); /** * @since botch-0.1 @@ -39,6 +40,7 @@ class Organism { this.storage = this.runtime.storage; this.svgGen = new svgGen(100, 100); this.botchUtil = new BotchUtil(this.runtime); + this.prom = null; // vehicle proprieties this.acceleration = new Vector2(0, 0); this.velocity = new Vector2(this.botchUtil.rdn(-2, 2), this.botchUtil.rdn(-2, 2)); @@ -102,7 +104,11 @@ class Organism { this.svg = this.svgGen.generateOrgSVG3( 100, this.foodAttraction, this.enemyAttraction, this.max_att, this.foodSight, this.enemySight, this.max_perception, svgPoints_, mutation); - this.botchUtil.uploadCostumeEdit(this.svg, this.target.id); + /* this.prom = this.botchUtil.uploadCostumeEdit(this.svg, this.target.id).then( + obj => { + log.log(obj); + } + ); */ } this.target.setSize(this.size); /* for (let i = this.target.getCostumes().length - 1; i >= 0; i--) { @@ -166,10 +172,11 @@ class Organism { /** * assign the new generated costume to the target + * @returns {Promise} when the costume is uploaded */ assignOrgCostume () { - this.botchUtil.uploadCostumeEdit(this.svg, this.target.id); this.target.setSize(this.size); + return this.botchUtil.uploadCostumeEdit(this.svg, this.target.id); } /**