var pause; var drawing; var doing; let frames; var x1, y1, fx, fy, lx, ly; var removeButton; var players = []; var hk, vk; function setup() { createCanvas(1920, 850); background(51); frameRate(72); level = new Map(); refresh(); drawing = true; var checkbox; var drawingbox; checkbox = createCheckbox('pause', false); drawingbox = createCheckbox('drawing', true); checkbox.changed(pauseCheck); drawingbox.changed(drawCheck); sightSlider = createSlider(1, 100, 50); velocitySlider = createSlider(1, 10, 3); rotationSpeedSlider = createSlider(0, 10, 1); removeButton = createButton('refresh'); removeButton.mousePressed(refresh); tf.setBackend('cpu'); walls = []; } function refresh() { pause = false; drawing = false; doing = false; frames = 0; players = []; makeNewPlayers(10); //players.push(new Player(createVector(500, 450))); //walls.push(new Wall(100, 100, 200, 200)); } function draw() { clear(); background(51); if (!pause) { if (!drawing) { background(51); if (players.length <= 4) { makeNewPlayers(10); } checkClick(); filterPlayers(); for (let p of players) { p.update(vk, hk); } //print(getDistances(player.distances, player.distances.length, player.sight).push(player.frontalVelocity)); } else { if (doing === true) { if (p5.Vector.dist(createVector(x1, y1), createVector(mouseX, mouseY)) > 10) { walls.push(new Wall(x1, y1, mouseX, mouseY)); x1 = mouseX; y1 = mouseY; lx = mouseX; ly = mouseY; } } } //print(player.frontalVelocity); } level.show(); sortPlayers(); for (let i = 0; i < players.length; i++) { if (i > 0) players[i].show(false); else players[i].show(true); } //drawNeuralNet(players[0].distances); } function mouseClicked() { if (drawing && mouseY <= 850) { if (doing === false) { doing = true; fx = mouseX; fy = mouseY; x1 = mouseX; y1 = mouseY; } else { walls.push(new Wall(fx, fy, lx, ly)); doing = false; } } } function pauseCheck() { if (this.checked()) { pause = true; } else { pause = false; } } function drawCheck() { if (this.checked()) { drawing = true; } else { drawing = false; } } class Map { constructor() {}; show() { for (let w of walls) { w.show(); } } } function exportWalls() { var values = []; for (let i = 0; i < walls.length; i++) { values.push([walls[i].a.x,walls[i].a.y,walls[i].b.x,walls[i].a.y]); } saveJSON(JSON.stringify(values), 'track.json'); //localStorage.setItem('values', values); } function drawNeuralNet(layer1) { noStroke(); for (let i = 0; i < layer1.length; i++) { fill(layer1[i] * 255); ellipse(1860, 850 / layer1.length * i + 50, 50, 50); } } function makeNewPlayers(n) { for (let i = 0; i < n; i++) players.push(new Player(createVector(100, 500))); } function checkClick() { hk = null; vk = null; if (keyIsDown(LEFT_ARROW)) { hk = '0'; } else if (keyIsDown(RIGHT_ARROW)) { hk = '1'; } if (keyIsDown(UP_ARROW)) { vk = '1'; } else if (keyIsDown(DOWN_ARROW)) { vk = '0'; } } function filterPlayers() { players = players.filter(function (pl) { return pl.dead == false; }); } function sortPlayers() { players.sort(function (a, b) { return b.fitness - a.fitness }); }