xxxxxxxxxx
99
class Unit {
constructor(id, poolId, position = createVector(0, 0), size = 10) {
this.pos = position;
this.size = size;
this.id = id;
this.collision = size * 0.5;
this.poolId = poolId;
this.fill = noise(this.pos.x, this.pos.y, this.id) * 220;
}
move(dir) {
this.pos.x += dir.x;
this.pos.y += dir.y;
}
absorb(obj) {
this.setSize(this.size + (obj.size * 0.3) );
this.fill = (this.fill + obj.fill) * 0.5;
}
setSize(value) {
this.size = value;
this.collision = this.size * 0.5;
}
checkCollide(obj) {
if(obj.id === this.id)
return false;
let d = dist(obj.pos.x, obj.pos.y, this.pos.x, this.pos.y);
let s = obj.collision + this.collision;
return d <= s;
}
sizeDecay(){
this.setSize(this.size - this.size * 0.01);
}
} // end Unit class
class Spool {
constructor(spoolId, min, max, content = {}) {
this.id = spoolId;
this.min = min;
this.max = max;
this.content = content;
this.fitness = Object.keys(this.content);
this.length = this.fitness.length;
this.worst = -1;
this.target = {
pos: {
x: 300,
y: 300
},
id: -1
};
} // end constructor();
addItem(id, object) {
this.content[id] = object;
this.length++;
if (this.length >= this.max) {
this.updateFitness();
this.deleteItem(this.worst.id);
}
} // end addItem();
deleteItem(id) {
if (id === -1)
return -1;
delete this.content[id];
this.length--;
} // end deleteItem();
updateFitness() {
this.fitness = Object.keys(this.content);
let worst = this.content[this.fitness[0]];
for (let i of this.fitness) {
this.content[i].quality = this.comparator(this.target, this.content[i]);
if (this.content[i].quality && this.content[i].quality < worst.quality) {
worst = this.content[i];
}
}
this.worst = worst;
} // end updateFitness();
comparator(target, cur) {
return (2000 - dist(target.pos.x, target.pos.y, cur.pos.x, cur.pos.y)) * cur.size;
} // end default comparator();
changeComparator(fun) {
this.comparator = fun;
} // end changeComparator();
setTarget(target) {
this.target = target;
} // end setTarget();
map(func) {
for (let i of Object.keys(this.content)) {
if(this.content[i])
func(this.content[i]);
}
}
} // end Spool class