xxxxxxxxxx
71
// A basic weapon the player is equiped with from the start.
class Laser {
constructor() {
// Laser originates from the player
this.playerPos = player.getPlayerPos();
// Laser has a random direction and travels in a straight path in that direction
this.angle = random(0, 360);
// to track the position of the laser
this.pos = createVector(this.playerPos.x, this.playerPos.y);
// speed of the laser
this.speed = 3;
// size of the laser
this.radius = 10
// sturdiness defines how strong the laser is. If 0, the laser disappears
this.sturdiness = laserSturdiness;
}
// Laser starts from the player position and the laser is just a circle
show() {
stroke(255);
strokeWeight(2);
fill("black");
circle(this.pos.x, this.pos.y, this.radius);
}
// Laser moves in a straight line defined by the angle
move() {
angleMode(DEGREES);
// calculating the vector of movement
this.pos.x += this.speed * cos(this.angle);
this.pos.y += this.speed * sin(this.angle);
}
// Getter function for laser position.
getLaserPos() {
return this.pos;
}
}
// Upgrading the weapon as the game progresses
function upgrade_weapons() {
// this will increase the number of lasers the player can shoot as time passes.
if (laserCount != 20) {
laserCount -= 10;
}
// Sturdiness of the laser is increased with certain interval, such that the laser will pierce through more aliens
laserSturdiness++;
// a flag so that this function is only called once every few seconds
upgrade_flipper = false;
}
// This function was added because the game started lagging when it went on for a longer period of time.
// It basically deletes all the lasers that are outside the canvas
function delete_lasers_outside() {
// checking for every laser if they are outside the canvas.
for (let i = 0; i < lasers.length; i++) {
if (lasers[i].pos.x < 0 || lasers[i].pos.x > width || lasers[i].pos.y < 0 || lasers[i].pos.y > height) {
// removing the laser from the global lasers array is the same as deleting the laser.
lasers.splice(i, 1);
}
}
}