xxxxxxxxxx
77
let asteroids = [];
let player;
let bullets = [];
let keys = {};
const rotationSpeed = 0.1;
function setup() {
createCanvas(600, 600);
angleMode(RADIANS);
for (let i = 0; i < 10; i++) {
asteroids.push(new Asteroid());
}
player = new Player();
background(0);
}
function keyPressed() {
keys[keyCode] = true;
}
function keyReleased() {
keys[keyCode] = false;
}
function draw() {
background(0);
let pressedKeys = Object.keys(keys).filter(key => keys[key])
if (pressedKeys.includes(LEFT_ARROW + "")) {
player.rotate(-rotationSpeed);
} else if (pressedKeys.includes(RIGHT_ARROW + "")) {
player.rotate(rotationSpeed);
}
if (pressedKeys.includes("32") && frameCount % 10 == 0) {
bullets.push(player.shoot());
}
//let newAsteroids = [];
for (let i = asteroids.length - 1; i >= 0; i--) {
let ast = asteroids[i];
let hits = ast.hit(bullets).filter(Boolean);
if (hits.length > 0) {
asteroids.splice(i, 1);
for (let hit of hits) {
asteroids = asteroids.concat(hit.asteroids);
bullets.splice(bullets.indexOf(hit.bullet), 1);
}
} else {
ast.show();
try {
ast.move();
} catch (err) {
console.log(err.message);
ast.noiseSpace = p5.Vector.random2D().mult(100);
}
}
}
player.show();
for (let bul of bullets) {
bul.show();
bul.move();
}
/*if (random(1) < 0.01) {
asteroids = asteroids.concat(asteroids.shift().split())
}*/
//noLoop();
}