Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used promises to store sprite #33

Merged
merged 1 commit into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/extensions/botch/botch_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

// </LOAD COSTUMES METHODS>
Expand Down
48 changes: 32 additions & 16 deletions src/extensions/botch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions src/extensions/botch/organism.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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--) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down