Skip to content

Commit

Permalink
Rewrite the generate organism svg, refers to #15
Browse files Browse the repository at this point in the history
    - there was a bug that generates svg with a very large dimension

1. The block reproduce works with the first generated
    so it can be clicked after the set as organism,
 2. Boundaries with organism now are much harder,
 1, 2 refers to #28
  • Loading branch information
raffaelepojer committed Aug 27, 2020
1 parent bd2596f commit 0e92b2d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 114 deletions.
31 changes: 20 additions & 11 deletions src/extensions/botch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Scratch3Botch {
// utils
this.botchUtil = new BotchUtil(this.runtime);
this.currentOrgCounter = 0;
this.originalOrg = null; // this is the clone that hide the original when set as organism

// since that the project is loaded at the startup
// for some reasons the storage is not already defined and it needs to
Expand Down Expand Up @@ -330,7 +331,11 @@ class Scratch3Botch {
}

this.organismMap = new Map();
this.organismMap.set(util.target.id, new Organism(util.target));
const org = new Organism(util.target);
this.organismMap.set(util.target.id, org);

org.currentName = this.currentOrgCounter.toString();

util.target.setVisible(false);
this.createOrganismClone(util.target, 0);

Expand Down Expand Up @@ -396,7 +401,6 @@ class Scratch3Botch {
// newClone.setSize((Math.random() * 100) + 40);
if (i === 0) { // the first clone is placed where is the original
newClone.setXY(target.x, target.y);
target.setCostume(org.target.currentCostume); // don't know why this does not work!
} else {
// place the new clone in a random position
const stageW = this.runtime.constructor.STAGE_WIDTH;
Expand All @@ -409,6 +413,10 @@ class Scratch3Botch {
newClone.setCustomState('storedMd5', p.md5);
const state = this.getBotchState(newClone);
state.type = Scratch3Botch.ORGANISM_TYPE;

if (target.isOriginal) {
this.originalOrg = org;
}
}
}

Expand Down Expand Up @@ -537,15 +545,17 @@ class Scratch3Botch {
reproduceChild (args, util) {
const mr = MathUtil.clamp(Cast.toNumber(args.MR), 0, 100);
if (this.organismMap.size > 0 && this.organismMap.get(util.target.id)) {
const org = this.organismMap.get(util.target.id);
if (org && !org.target.isOriginal) { // Only the clones are managed
if (org.health > 0) {
const newOrg = org.clone(mr);
let org = this.organismMap.get(util.target.id);
if (util.target.isOriginal) {
org = this.originalOrg;
}
if (org) {
if (org.health >= 0) {
const newClone = this.botchUtil.createClone(org.target);
if (newClone) {
this.runtime.addTarget(newClone);
const newOrg = org.clone(mr, newClone);
newClone.clearEffects();
newOrg.target = newClone; // assign the new target to new organism
org.childNumber++;
newOrg.currentName = `${org.currentName}.${org.childNumber}`;
const p = this.storeSprite(newClone.id, newOrg.currentName);
Expand All @@ -558,15 +568,14 @@ class Scratch3Botch {
}
} else if (this.enemiesMap.size > 0 && this.enemiesMap.get(util.target.id)) {
const enemy = this.enemiesMap.get(util.target.id);
if (enemy && !enemy.target.isOriginal) {
const newOrg = enemy.clone(mr);
if (enemy) {
const newClone = this.botchUtil.createClone(enemy.target);
if (newClone) {
this.runtime.addTarget(newClone);
const newOrg = enemy.clone(mr, newClone);
newClone.clearEffects();
newOrg.target = newClone;
this.enemiesMap.set(newClone.id, newOrg);
}
this.enemiesMap.set(newClone.id, newOrg);
}
}
}
Expand Down
34 changes: 19 additions & 15 deletions src/extensions/botch/organism.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Organism {
* @param {RenderedTarget} target_ target (sprite)
* @param {number} mass_ mass of the vehicle
* @param {number} maxForce_ max force of the vehicle
* @param {string} svgPoints_ svg points of the organism
* @param {Array} svgPoints_ svg points of the organism
* @param {number} mutation how the organism can mutate [0 - 100]
* @param {number} foodAttraction food attraction
* @param {number} foodSight food sight
Expand Down Expand Up @@ -57,8 +57,8 @@ class Organism {
this.versionName = '';
this.max_att = 5;
this.max_perception = 150;
this.max_size = 140;
this.min_size = 30;
this.max_size = 110;
this.min_size = 50;
this.currentName = '';
this.childNumber = 0;
// "dna" parameter
Expand All @@ -74,14 +74,18 @@ class Organism {
const sizeScaled = MathUtil.scale(mutation, 0, 100, 0, this.max_size);
this.foodAttraction = foodAttraction;
this.foodAttraction += this.botchUtil.rdn(-attScaled, attScaled);
this.foodAttraction = MathUtil.clamp(this.foodAttraction, -this.max_att, this.max_att);
this.foodSight = foodSight;
this.foodSight += this.botchUtil.rdn(-perScaled, perScaled);
this.foodSight = MathUtil.clamp(this.foodSight, 0, this.max_perception);
this.enemyAttraction = enemyAttraction;
this.enemyAttraction += this.botchUtil.rdn(-attScaled, attScaled);
this.enemyAttraction = MathUtil.clamp(this.enemyAttraction, -this.max_att, this.max_att);
this.enemySight = enemySight;
this.enemySight += this.botchUtil.rdn(-perScaled, perScaled);
this.enemySight = MathUtil.clamp(this.enemySight, 0, this.max_perception);
this.size = orgSize;
this.size += this.botchUtil.rdn(-sizeScaled / 2, sizeScaled / 2);
this.size += this.botchUtil.rdn(-sizeScaled / 3, sizeScaled / 3);
this.size = MathUtil.clamp(this.size, this.min_size, this.max_size);
} else {
this.foodAttraction = this.botchUtil.rdn(-this.max_att, this.max_att); // food attraction
Expand All @@ -93,7 +97,7 @@ class Organism {

// if is not defined, do not generate
if (svgPoints_) {
this.svg = this.svgGen.generateOrgSVG2(
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);
Expand All @@ -105,6 +109,9 @@ class Organism {
} */

this.target.setSize(this.size);
} else {
this.svg = this.svgGen.generateOrgSVG3(100, this.foodAttraction, this.enemyAttraction, this.max_att,
this.foodSight, this.enemySight, this.max_perception);
}

// values found empirically
Expand All @@ -129,11 +136,11 @@ class Organism {
* @since botch-0.2
*/
stepOrganism (enemiesMap) {
this.boundaries(
this.runtime.constructor.STAGE_WIDTH - 50,
this.runtime.constructor.STAGE_HEIGHT - 50);
this.refreshArgs(this.mass, this.maxForce);
this.behaveGeneralOrganism(enemiesMap);
this.boundaries(
this.runtime.constructor.STAGE_WIDTH - 30,
this.runtime.constructor.STAGE_HEIGHT - 30);
this.update();
this.breathe();
}
Expand All @@ -158,12 +165,8 @@ class Organism {
* assign the new generated costume to the target
*/
assignOrgCostume () {
this.svg = this.svgGen.generateOrgSVG2(100, this.foodAttraction, this.enemyAttraction, this.max_att,
this.foodSight, this.enemySight, this.max_perception);
this.botchUtil.uploadCostumeEdit(this.svg, this.target.id);
// this.area = this.svgGen.calcOrgMass();
this.target.setSize(this.size);
// this.health = MathUtil.scale(this.area, 2000, (this.svgGen.width) ** 2, 1, 3);
}

/**
Expand Down Expand Up @@ -394,11 +397,12 @@ class Organism {
* Create a clone of itself "Parthenogenesis"
* when called it return always a new child
* @param {number} mutation how the organism will mutate [0 - 100]
* @param {target} target new clone target
* @returns {Organism} new copy Organism
* @since botch-0.2
*/
clone (mutation) {
const newOrg = new Organism(this.target, 1, 0.5, this.svgGen.getOrgPoints(), mutation,
clone (mutation, target) {
const newOrg = new Organism(target, this.mass, 0.5, this.svgGen.getOrgPoints(), mutation,
this.foodAttraction, this.foodSight, this.enemyAttraction, this.enemySight, this.size);
return newOrg;
}
Expand Down Expand Up @@ -518,7 +522,7 @@ class Organism {
desired.normalize();
desired.mult(this.maxSpeed);
const steer = Vector2.sub(desired, this.velocity);
steer.limit(this.maxForce);
// steer.limit(this.maxForce);
this.applyForce(steer);
}
}
Expand Down
Loading

0 comments on commit 0e92b2d

Please sign in to comment.