xxxxxxxxxx
147
class Arrow {
constructor(brain) {
if (brain) {
this.brain = brain.copy();
} else {
this.brain = new NeuralNetwork(6, 12, 2);
}
this.pos = createVector(width / 2, 0);
this.vel = createVector(0, 0);
this.angle = HALF_PI;
this.acc = p5.Vector.fromAngle(this.angle, 10);
this.color = color(255, 0, 0);
this.len = width / 16;
this.popNum = 0;
this.score = 0;
this.bombHit = false;
this.fitness = 0;
this.balloonID = 0;
this.bestBalloon = null;
this.distance = 0;
}
dispose() {
this.brain.dispose();
}
mutate() {
this.brain.mutate(0.1);
}
think(balloons) {
// Find the best color
let colors = [];
let bestColor = 0;
for (let i = balloons.length - 1; i >= 0; i--) {
colors[i] = balloons[i].col;
}
bestColor = Math.min.apply(Math, colors);
for (let i = balloons.length - 1; i >= 0; i--) {
if (balloons[i].col <= bestColor) {
this.bestBalloon = balloons[i];
this.balloonID = this.bestBalloon.id;
}
}
let inputs = [];
let output = [];
inputs[0] = this.bestBalloon.pos.x / width;
inputs[1] = map(this.vel.x, -5, 5, 0, 1);
inputs[2] = map(this.vel.y, -5, 5, 0, 1);
inputs[3] = this.pos.x / width;
inputs[4] = this.pos.y / height;
inputs[5] = this.angle / PI;
output = this.brain.predict(inputs);
if (dist(this.pos.x, this.pos.y, width / 2, 0) < 200) {
this.distance = abs(this.pos.x - this.bestBalloon.pos.x);
if (output[0] > output[1]) {
this.angle += 0.03;
} else {
this.angle -= 0.03;
}
}
this.acc = p5.Vector.fromAngle(this.angle, 2);
}
update() {
this.pos.add(this.vel);
this.vel.add(this.acc);
this.vel.limit(5);
this.acc.mult(0);
this.distanceScore();
}
show() {
stroke(0);
fill(this.color);
push();
translate(this.pos.x, this.pos.y);
ellipse(0, 0, width / 48);
rotate(this.angle);
fill(0);
rect(0, 0, this.len, 0);
pop();
// textSize(20);
// text(round(this.score), this.pos.x, this.pos.y);
}
offscreen() {
if ((this.pos.x > width + 60) ||
(this.pos.x < -60) ||
(this.pos.y > height + 60) ||
(this.pos.y < -60)) {
return true;
}
}
distanceScore(){
let distance = abs(this.pos.x - this.bestBalloon.pos.x);
if(this.distance - distance >= 0){
this.score += 0 ;
} else {
this.score -= 0 ;
}
}
calcScore(balloonColor) {
//this.popNum++;
if (balloonColor.toString() == this.color.toString()) {
this.score += 100;
} else if (balloonColor.toString() == color(0).toString()) {
this.score += 100;
this.bombHit = false;
} else {
this.score += 100;
}
//this.score += this.popNum * this.score;
for (let i = 0; i < players.length; i++) {
if (this.color.toString() == players[i].color.toString()) {
players[i].getScore(round(this.score));
}
}
}
}