xxxxxxxxxx
52
var oShip;
var cooldownCount = 70;
class Player {
constructor() {
this.pos = createVector(width / 2, height - fieldHeight * 2);
this.w = sShip.width / 2.5;
this.h = sShip.height / 2.5;
this.dir = createVector(0, -this.h / 2);
this.vel = 2;
this.cooldown = 0;
}
display() {
push();
strokeWeight(2);
translate(this.pos.x, this.pos.y);
if (this.cooldown < 0) line(0, 0, 0, -this.h / 1.5);
image(sShip, 0, 0, this.w, this.h);
pop();
}
control() {
// MOVING
this.pos.x += (keyIsDown(68)) ? this.vel * 2 : 0;
this.pos.x -= (keyIsDown(65)) ? this.vel * 2: 0;
// CHECK EDGES
this.pos.x = constrain(this.pos.x, fieldWidth + this.w / 2, width - fieldWidth - this.w / 2);
// SHOOTING
if (keyIsDown(80)) this.shoot();
// COOLDOWN REDUCE
this.cooldown--;
}
shoot() {
if (this.cooldown < 0) {
const p = p5.Vector.add(this.pos, this.dir);
bullets.push(new Bullet(p, this.dir));
if (effect) blast.play();
this.cooldown = cooldownCount;
if (0.25 > random())
for (let enemy of enemies) {
enemy.pos.x -= eVel * eWidth / 6;
if (enemy.attacking == -1) enemy.currentAnchor -= eVel * eWidth / 6;
}
}
}
}