xxxxxxxxxx
220
// for other neuroevolution examples please
// go to djuliette.github.io
// thanks!
// this generally takes the agents around
// 25 generations to solve the environment
// after it gets lucky and
// randomly makes it through its first gate
// note:I'm aware the drawing of the rects are slightly off
// from the calculated gate openings, that's why I moved the
// target slightly offcenter. I'll fix it at some point :)
//var player;
var population;
var gameCounter = 0;
var gameScore = 0
var obst1 = [];
var obst2 = [];
var moveL;
var moveR;
var obstCounter1 = 160;
var cloudCounter = -200;
var hitObst1;
var hitObst2;
// ga vars
var numInputNodes = 5;
var numHiddenNodes = 15;
var numOutputNodes = 3;
var initPopSize = 300;
var lowest_alive = 0
var allTimeHigh = 0;
var globalMutationRate = 0.1;
var drawBlue = false;
var max_alive = 0;
var maxIndexAlive = 0;
var numAlive = 0;
var stopZ;
var inconsolata;
function preload() {
vanlose = loadFont('assets/Vanlose_SimpleType.otf');
}
function setup() {
createCanvas(1000, 800, WEBGL);
population = new Population(initPopSize);
textFont(vanlose);
textSize(20);
textAlign(LEFT, BOTTOM);
hitObst1 = false;
hitObst2 = false;
//cam.lookAt(500,300,300);
//player = new Player();
// cam.lookAt(1220,-50,1214); // y was - 277
// cam.rotateX(0.225);
// cam.rotateY(0.775);
// cam.rotateZ(-0.164);
// cam.setDistance(35);
cam = createCamera();
cam.setPosition(1000,0,1500);
cam.lookAt(0,0,0);
// cam.rotateX(0.225);
// cam.rotateY(0.775);
// cam.rotateZ(-0.164);
//cam.setDistance(35);
moveL = false;
moveR = false;
}
function draw() {
background(196,230,255);
orbitControl();
if(gameCounter % 10 == 0) {
gameScore++;
}//
gameCounter++;
obstCounter1++;
if(obstCounter1 % 175 == 0) {
obst1.push(new Obstacle());
obst2.push(new Obstacle2());
obstCounter1 = 0;
}
if(!population.done()) {
obst1Show();
obst2Show();
obstCounter1++;
population.updateAlive();
} else {
population.calculateFitness();
population.naturalSelection();
resetObstacles();
}
// draw a cloud
fill(255);
cloudCounter++;
if(cloudCounter > 0 && cloudCounter < 1000) {
push();
translate(300,-400,cloudCounter);
ellipse(0,0,200,25);
ellipse(250,0,250,35);
pop();
}
if(cloudCounter - 100 > 0 && cloudCounter < 500) {
push();
translate(325,-400,cloudCounter*1.5);
ellipse(100,0,250,20);
pop();
}
if(cloudCounter > 1000) {
cloudCounter = -200;
}
//draw ground
fill(130,100,15);
push();
rotateX(PI/2);
translate(-500,-500,-800);
rect(0,0,1500,2000);
pop();
showInfo();
} // draw
function obst1Show() {
for(let i = obst1.length - 1; i>=0; i--) {
let o = obst1[i];
o.move();
o.show();
if(o.offscreen()) {
obst1.splice(i,1);
}
}} // obst1show
function obst2Show() {
for(let i = obst2.length - 1; i>=0; i--) {
let o2 = obst2[i];
o2.move();
o2.show();
if(o2.offscreen()) {
obst2.splice(i,1);
}
}
} // obst1show
function resetObstacles() {
//obst1 = [];
//obst2 = [];
//obst1.splice(0);
//obst2.splice(0);
obst1.length = 0;
obst2.length = 0;
obstCounter1 = 160;
gameScore = 0;
gameCounter = 0;
} //resetObstacles
// function keyPressed() {
// if(keyCode == LEFT_ARROW) {
// moveL = true;
// }
// if(keyCode == RIGHT_ARROW) {
// moveR = true;
// }
// if(key == ' ') {
// player.flap();
// }
// if(key == 'z') {
// stopZ = !stopZ;
// }
// } // keypressed
// function keyReleased() {
// if(keyCode == LEFT_ARROW) {
// moveL = false;
// }
// if(keyCode == RIGHT_ARROW) {
// moveR = false;
// }
// } // keyreleased
function showInfo() {
fill(0);
textAlign(LEFT);
textSize(20);
if(obst1.length >= 1) {
text("Obst.z: " + obst1[0].posz, 830,height - 60,600);
}
text("Gen: " + population.gen,830,240);
if(obst1.length >= 1) {
//text("deltaX: " + ((population.players[0].position.x - (obst2.get(0).w + obst2.get(0).d + 0))/800),30,260);
text("deltaX: " + ((obst2[0].w + obst2[0].d + 35) - population.players[0].position.x)/800,830,260);
text("deltaY: " + ((population.players[0].position.y - (obst1[0].h + obst1[0].d - 200))/800),830,280);
text("deltaZ: " + ((population.players[0].position.z - obst1[0].posz)/700),830,300);
}
text("Score: " + gameScore,830,320);
text("x: " + population.players[lowest_alive].position.x, 830, 340);
text("y: " + population.players[lowest_alive].position.y, 830, 360);
text("z: " + population.players[lowest_alive].position.z, 830, 380);
//cam.endHUD();
}