xxxxxxxxxx
1060
//Making an object move toward the cursor -- https://stackoverflow.com/questions/48250639/making-an-object-move-toward-the-cursor-javascript-p5-js
let pillars = [];
let player1;
let pillarsNum = 1;
let sceneNum = -1; //set scene
let playerLives = 3;
let playerDirection;
let playerAttack = false;
//intro scene -1
let intro;
//goblins in scene 0
let goblin;
let goblinNum = 4;
//demons in scene 1
let demon;
let demonNum = 4;
//Key in scene 1
let keyCheck;
let keyState = false;
let keyInsert = false;
//boss in scene 2
let boss;
let song;
let song2;
let song3;
function preload() {
//music
song = loadSound('assets/music/the_legendary_hero.mp3');
song2 = loadSound('assets/music/Underworld.mp3');
song3 = loadSound('assets/music/move_your_feet.mp3');
//loading all three images
player1_ = loadImage('assets/img/player1.png');
goblin_ = loadImage('assets/img/goblin.png');
demon_ = loadImage('assets/img/demon.png');
boss_ = loadImage('assets/img/boss.png');
flame_ = loadImage('assets/img/flame.png');
cave_0 = loadImage('assets/background/cave_0.png');
cave_1 = loadImage('assets/background/cave_1.png');
cave_2 = loadImage('assets/background/cave_2.png');
outside_ = loadImage('assets/background/outside_0.jpg');
rock_0 = loadImage('assets/img/Rock Pile_1.png');
rock_1 = loadImage('assets/img/aqua_rock.png');
rock_2 = loadImage('assets/img/pillar.png');
door_ = loadImage('assets/img/pixel_door_.png');
sword_right = loadImage('assets/img/sword_normal.png');
sword_left = loadImage('assets/img/sword_left.png');
sword_down = loadImage('assets/img/sword_down.png');
lock_ = loadImage('assets/img/lock_.png');
key_ = loadImage('assets/img/key_.png');
mario_ = loadImage('assets/img/mario_8bit.png');
}
function setup() {
createCanvas(600, 400);
intro = new Intro();
boss = new Boss(color(255, 0, 0));
demon = new Demon(demonNum, color(255, 0, 0));
goblin = new Goblin(goblinNum, color(255, 0, 0));
pillars = new Pillar(color(random(255), random(255), random(255)));
//Walls so player cannot leave canvas
walls = new Walls();
player1 = new Player();
keyCheck = new KeyCheck();
//Play intro song
if(sceneNum == -1){
song.play();
}
}
function draw() {
//background(220);
//Set background depending on which scene you're in
if (sceneNum == 0) {
background(cave_0, 600, 400);
} else if (sceneNum == 1) {
background(cave_1, 600, 400);
} else if (sceneNum == 2) {
background(cave_2, 600, 400);
} else if (sceneNum == 3) {
background(outside_, 600, 400);
}
intro.scene();
//pillar
//
pillars.body();
pillars.checkCollision();
pillars.nextScene();
//player
//
player1.body();
player1.move();
player1.home();
player1.attack();
//goblin
//
goblin.body();
//goblin.move();
demon.body();
boss.body();
walls.build();
keyCheck.location();
}
class Player {
constructor() {
this.x = width / 2;
this.y = height - 50;
this.w = 30;
this.h = 30;
}
body() {
//Main player
push();
translate(-16, -18);
if (sceneNum != -1) {
image(player1_, this.x, this.y, this.w, this.h);
}
pop();
}
//Movement functionality
move() {
if (keyIsDown(37)) {
this.x -= 2;
playerDirection = "left";
}
if (keyIsDown(38)) {
this.y -= 2;
playerDirection = "up";
}
if (keyIsDown(39)) {
this.x += 2;
playerDirection = "right";
}
if (keyIsDown(40)) {
this.y += 2;
playerDirection = "down";
}
}
attack() {
//if spacebar is pressed then attack
if (keyIsDown(32)) {
//Determine which direction to attack
switch (playerDirection) {
case "left":
playerAttack = true;
//rect(player1.x - 45, player1.y - 15, 30, 30);
image(sword_left, player1.x - 45, player1.y - 15, 30, 30);
break;
case "up":
playerAttack = true;
image(sword_right, player1.x - 15, player1.y - 45, 30, 30);
break;
case "right":
playerAttack = true;
//rect(player1.x + 15, player1.y - 15, 30, 30);
image(sword_right, player1.x + 15, player1.y - 15, 30, 30);
break;
case "down":
playerAttack = true;
//rect(player1.x - 15, player1.y + 15, 30, 30);
image(sword_down, player1.x - 15, player1.y + 15, 30, 30);
break;
}
} else { //if spacebar isn't pressed then attack is off
playerAttack = false;
}
}
home() {
//create doors for next and prev scenes
if (sceneNum == 0 || sceneNum == 1 || sceneNum == 2) { //Next Scene Box
/* old shape to represet door
push();
fill(random(255));
rect(width / 2 - 20, height - 20, 20, 20);
pop();
*/
image(door_, width / 2 - 20, 0, 40, 40);
}
if (sceneNum == 1 || sceneNum == 2) { //Prev Scene Box
image(door_, width / 2 - 20, height - 40, 40, 40);
}
if (sceneNum > 3) { //go back to first scene if scenes go past 3rd
sceneNum = -1;
}
}
}
class Pillar {
constructor(c) { // a special method that creates the pillar object
this.w = 50;
this.h = 50;
this.c = c;
this.x = 0;
}
body() {
//Set pillars in each scene
//fill(this.c);
//rect(this.x, this.y, this.w, this.h);
if (sceneNum == 0) {
//rect(50, 50, this.w, this.h);
image(rock_0, 50, 50, this.w, this.h);
image(rock_0, 200, 50, this.w, this.h);
image(rock_0, 350, 50, this.w, this.h);
image(rock_0, 500, 50, this.w, this.h);
image(rock_0, 50, 150, this.w, this.h);
image(rock_0, 200, 150, this.w, this.h);
image(rock_0, 350, 150, this.w, this.h);
image(rock_0, 500, 150, this.w, this.h);
image(rock_0, 50, 250, this.w, this.h);
image(rock_0, 200, 250, this.w, this.h);
image(rock_0, 350, 250, this.w, this.h);
image(rock_0, 500, 250, this.w, this.h);
} else if (sceneNum == 1) {
//rect(50, 50, this.w, this.h);
image(rock_1, 50, 50, this.w, this.h);
image(rock_1, 100, 50, this.w, this.h);
image(rock_1, 150, 50, this.w, this.h);
image(rock_1, 200, 50, this.w, this.h);
image(rock_1, 250, 50, this.w, this.h);
image(rock_1, 300, 50, this.w, this.h);
image(rock_1, 350, 50, this.w, this.h);
image(rock_1, 400, 50, this.w, this.h);
image(rock_1, 450, 50, this.w, this.h);
image(rock_1, 500, 50, this.w, this.h);
image(rock_1, 500, 100, this.w, this.h);
image(rock_1, 500, 150, this.w, this.h);
image(rock_1, 500, 200, this.w, this.h);
image(rock_1, 500, 250, this.w, this.h);
//rect(500, 300, this.w, this.h);
image(rock_1, 450, 250, this.w, this.h);
image(rock_1, 400, 250, this.w, this.h);
image(rock_1, 350, 250, this.w, this.h);
image(rock_1, 300, 250, this.w, this.h);
image(rock_1, 250, 250, this.w, this.h);
image(rock_1, 200, 250, this.w, this.h);
image(rock_1, 150, 250, this.w, this.h);
image(rock_1, 100, 250, this.w, this.h);
//rect(50, 300, this.w, this.h);
image(rock_1, 50, 250, this.w, this.h);
image(rock_1, 50, 200, this.w, this.h);
image(rock_1, 50, 150, this.w, this.h);
image(rock_1, 100, 150, this.w, this.h);
image(rock_1, 150, 150, this.w, this.h);
image(rock_1, 200, 150, this.w, this.h);
image(rock_1, 250, 150, this.w, this.h);
image(rock_1, 300, 150, this.w, this.h);
image(rock_1, 350, 150, this.w, this.h);
image(rock_1, 400, 150, this.w, this.h);
} else if (sceneNum == 2) {
//rect(250, 150, this.w, this.h);
image(rock_2, 250, 150, this.w, this.h);
image(rock_2, 300, 150, this.w, this.h);
image(rock_2, 250, 200, this.w, this.h);
image(rock_2, 300, 200, this.w, this.h);
} else if (sceneNum == 3) {
push();
textSize(32);
fill(random(255), random(255), random(255))
text('You have escaped from the cave!!!', 50, 100);
text('Be free young bird and fly baybee!!!', 50, 200);
pop();
}
}
checkCollision() {
//prevent player from moving past pillars/obstacles
let d = int(dist(player1.x, player1.y, 500, 250));
/* Show player location on canvas
if (sceneNum != 3) {
text(("player1 X = " + player1.x + " " + "player1 Y = " + player1.y), 10, 40);
text(d, 10, 20);
} */
if (sceneNum == 0) { //Scene 0 pillars
if (player1.x + player1.w / 2 > 50 && player1.x < 50 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 200 && player1.x < 200 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 350 && player1.x < 350 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 50 && player1.x < 50 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 200 && player1.x < 200 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 350 && player1.x < 350 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 50 && player1.x < 50 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 200 && player1.x < 200 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 350 && player1.x < 350 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h) {
if (keyIsDown(37)) {
this.x -= 2;
player1.x += 30;
//console.log("X: " + this.x + " Y: " + this.y);
}
if (keyIsDown(38)) {
this.y -= 2;
player1.y += 12;
}
if (keyIsDown(39)) {
this.x += 2;
player1.x -= 12;
}
if (keyIsDown(40)) {
this.y += 2;
player1.y -= 12;
}
}
} else if (sceneNum == 1) { //Scene 1 pillars
if (player1.x + player1.w / 2 > 50 && player1.x < 50 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 100 && player1.x < 100 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 150 && player1.x < 150 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 200 && player1.x < 200 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 250 && player1.x < 250 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 300 && player1.x < 300 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 350 && player1.x < 350 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 400 && player1.x < 400 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 450 && player1.x < 450 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 50 && player1.y < 50 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 100 && player1.y < 100 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 200 && player1.y < 200 + this.h ||
player1.x + player1.w / 2 > 500 && player1.x < 500 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 450 && player1.x < 450 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 400 && player1.x < 400 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 350 && player1.x < 350 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 300 && player1.x < 300 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 250 && player1.x < 250 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 200 && player1.x < 200 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 150 && player1.x < 150 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 100 && player1.x < 100 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 50 && player1.x < 50 + this.w && player1.y + player1.h / 2 > 250 && player1.y < 250 + this.h ||
player1.x + player1.w / 2 > 50 && player1.x < 50 + this.w && player1.y + player1.h / 2 > 200 && player1.y < 200 + this.h ||
player1.x + player1.w / 2 > 50 && player1.x < 50 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 100 && player1.x < 100 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 150 && player1.x < 150 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 200 && player1.x < 200 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 250 && player1.x < 250 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 300 && player1.x < 300 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 350 && player1.x < 350 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 400 && player1.x < 400 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h
) {
if (keyIsDown(37)) {
this.x -= 2;
player1.x += 30;
}
if (keyIsDown(38)) {
this.y -= 2;
player1.y += 12;
}
if (keyIsDown(39)) {
this.x += 2;
player1.x -= 12;
}
if (keyIsDown(40)) {
this.y += 2;
player1.y -= 12;
}
}
} else if (sceneNum == 2) { //Scene 2 pillars
if (player1.x + player1.w / 2 > 250 && player1.x < 250 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 300 && player1.x < 300 + this.w && player1.y + player1.h / 2 > 150 && player1.y < 150 + this.h ||
player1.x + player1.w / 2 > 250 && player1.x < 250 + this.w && player1.y + player1.h / 2 > 200 && player1.y < 200 + this.h ||
player1.x + player1.w / 2 > 300 && player1.x < 300 + this.w && player1.y + player1.h / 2 > 200 && player1.y < 200 + this.h
) {
if (keyIsDown(37)) {
this.x -= 2;
player1.x += 30;
}
if (keyIsDown(38)) {
this.y -= 2;
player1.y += 12;
}
if (keyIsDown(39)) {
this.x += 2;
player1.x -= 12;
}
if (keyIsDown(40)) {
this.y += 2;
player1.y -= 12;
}
}
}
}
nextScene() {
//Progress through different scenes depending on location
if (sceneNum == 0) {
if (player1.x + player1.w / 2 > width / 2 - 30 && player1.x < width / 2 - 20 + 30 && player1.y + player1.h / 2 > 0 && player1.y < 0 + 30) {
player1.x = width / 2;
player1.y = height - 40;
sceneNum++;
}
} else if (sceneNum == 1) {
if (player1.x + player1.w / 2 > width / 2 - 30 && player1.x < width / 2 - 20 + 30 && player1.y + player1.h / 2 > 0 && player1.y < 0 + 30) {
player1.x = width / 2;
player1.y = height - 40;
sceneNum++;
} else if (player1.x + player1.w / 2 > width / 2 - 20 && player1.x < width / 2 - 20 + 20 + 10 && player1.y + player1.h / 2 > height - 20 && player1.y < height) {
player1.x = width / 2;
player1.y = 0 + 40;
sceneNum--;
}
} else if (sceneNum == 2) {
if (player1.x + player1.w / 2 > width / 2 - 20 && player1.x < width / 2 - 20 + 3 && player1.y + player1.h / 2 > 0 && player1.y < 0 + 30 && keyInsert == true) {
player1.x = width / 2;
player1.y = height - 40;
songThree();
sceneNum++;
} else if (player1.x + player1.w / 2 > width / 2 - 20 && player1.x < width / 2 - 20 + 20 + 10 && player1.y + player1.h / 2 > height - 20 && player1.y < height) {
player1.x = width / 2;
player1.y = 0 + 40;
sceneNum--;
}
}
}
}
class Goblin {
constructor(n, c) {
this.w = 30;
this.h = 30;
this.c = c;
this.n = n; //number of goblins(4)
this.g1x = 0;
this.g1x_ = width;
this.g1y = 0;
this.g1y_ = height;
this.changeDirection_gx = false;
this.changeDirection_gx_ = false;
this.changeDirection_gy = false;
this.changeDirection_gy_ = false;
}
body() {
//Set the goblins lives to true
//If not they are dead
let g1 = true;
let g2 = true;
let g3 = true;
let g4 = true;
//Capture distance between goblin and attack when facing left
//g1
let d1_attack_left = int(dist(150, this.g1y, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d1_attack_up = int(dist(150, this.g1y, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d1_attack_right = int(dist(150, this.g1y, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d1_attack_down = int(dist(150, this.g1y, player1.x - 15, player1.y + 15));
//Capture distance between goblin and attack when facing left
//g2
let d2_attack_left = int(dist(450, this.g1y_, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d2_attack_up = int(dist(450, this.g1y_, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d2_attack_right = int(dist(450, this.g1y_, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d2_attack_down = int(dist(450, this.g1y_, player1.x - 15, player1.y + 15));
//Capture distance between goblin and attack when facing left
//g3
let d3_attack_left = int(dist(this.g1x, 125, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d3_attack_up = int(dist(this.g1x, 125, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d3_attack_right = int(dist(this.g1x, 125, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d3_attack_down = int(dist(this.g1x, 125, player1.x - 15, player1.y + 15));
//Capture distance between goblin and attack when facing left
//g4
let d4_attack_left = int(dist(this.g1x_, 225, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d4_attack_up = int(dist(this.g1x_, 225, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d4_attack_right = int(dist(this.g1x_, 225, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d4_attack_down = int(dist(this.g1x_, 225, player1.x - 15, player1.y + 15));
fill(this.c);
if (sceneNum == 0) {
let y = 0;
for (let x = 0; x < this.n; x++) {
//Goblin #1
if (y == 0) {
//Gobin only alive if true
if (g1 == true) {
//ellipse(150, this.g1y, this.w, this.h);
image(goblin_, 150, this.g1y, this.w, this.h);
}
if (this.g1y > height) { //goblin move back and forth
this.changeDirection_gy = true
} else if (this.g1y <= 0) {
this.changeDirection_gy = false;
}
if (this.g1y >= 0 && this.changeDirection_gy == false) {
this.g1y += 5;
} else if (this.changeDirection_gy == true) {
this.g1y -= 5;
}
//Capture distance between goblin and player
let d = int(dist(150, this.g1y, player1.x, player1.y));
if (d < 20) { //player gets attackand resets
player1.x = width / 2;
player1.y = height - 50;
} else if (d1_attack_left < 20 && playerAttack == true || d1_attack_up < 25 && playerAttack == true || d1_attack_right < 25 && playerAttack == true || d1_attack_down < 25 && playerAttack == true) { //Kill goblin
g1 = false;
this.g1y = 99999;
}
//Goblin #2
} else if (y == 1) {
if (g2 == true) {
//ellipse(450, this.g1y_, this.w, this.h);
image(goblin_, 450, this.g1y_, this.w, this.h);
}
if (this.g1y_ > height) { //goblin move back and forth
this.changeDirection_gy_ = true
} else if (this.g1y_ <= 0) {
this.changeDirection_gy_ = false;
}
if (this.g1y_ >= 0 && this.changeDirection_gy_ == false) {
this.g1y_ += 5;
} else if (this.changeDirection_gy_ == true) {
this.g1y_ -= 5;
}
//Capture distance between goblin and player
let d = int(dist(450, this.g1y_, player1.x, player1.y));
if (d < 20) { //player gets attackand resets
player1.x = width / 2;
player1.y = height - 50;
} else if (d2_attack_left < 20 && playerAttack == true || d2_attack_up < 25 && playerAttack == true || d2_attack_right < 25 && playerAttack == true || d2_attack_down < 25 && playerAttack == true) {//Kill goblin
g2 = false;
this.g1y_ = 99999;
}
//Goblin #3
} else if (y == 2) {
if (g3 == true) {
//ellipse(this.g1x, 125, this.w, this.h);
image(goblin_, this.g1x, 125, this.w, this.h);
}
if (this.g1x > width) { //goblin move back and forth
this.changeDirection_gx = true
} else if (this.g1x <= 0) {
this.changeDirection_gx = false;
}
if (this.g1x >= 0 && this.changeDirection_gx == false) {
this.g1x += 5;
} else if (this.changeDirection_gx == true) {
this.g1x -= 5;
}
let d = int(dist(this.g1x, 125, player1.x, player1.y));
if (d < 20) { //player gets attackand resets
player1.x = width / 2;
player1.y = height - 50;
} else if (d3_attack_left < 20 && playerAttack == true || d3_attack_up < 25 && playerAttack == true || d3_attack_right < 25 && playerAttack == true || d3_attack_down < 25 && playerAttack == true) {//Kill goblin
g3 = false;
this.g1x = 99999;
}
//Gobin #4
} else {
if (g4 == true) {
//ellipse(this.g1x_, 225, this.w, this.h);
image(goblin_, this.g1x_, 225, this.w, this.h);
}
if (this.g1x_ > width) { //goblin move back and forth
this.changeDirection_gx_ = true
} else if (this.g1x_ <= 0) {
this.changeDirection_gx_ = false;
}
if (this.g1x_ >= 0 && this.changeDirection_gx_ == false) {
this.g1x_ += 5;
} else if (this.changeDirection_gx_ == true) {
this.g1x_ -= 5;
}
let d = int(dist(this.g1x_, 225, player1.x, player1.y));
if (d < 20) { //player gets attackand resets
player1.x = width / 2;
player1.y = height - 50;
} else if (d4_attack_left < 20 && playerAttack == true || d4_attack_up < 25 && playerAttack == true || d4_attack_right < 25 && playerAttack == true || d4_attack_down < 25 && playerAttack == true) {//Kill goblin
g4 = false;
this.g1x_ = 99999;
}
}
y++;
}
}
}
}
class Demon {
constructor(n, c) {
this.w = 30;
this.h = 30;
this.c = c;
this.n = n; //number of demons(4)
//set original location of demons
this.shapeLocation_1 = createVector(50, 50);
this.shapeLocation_2 = createVector(550, 50);
this.shapeLocation_3 = createVector(50, 300);
this.shapeLocation_4 = createVector(550, 300);
}
body() {
//Create array which will for target which is player
let target = [];
//Create array which measure distance
let distance = [];
//Create array which map distance from demon to player
let mappedDistance = [];
//Set the demons lives to true
//If not they are dead
let d1_ = true;
let d2_ = true;
let d3_ = true;
let d4_ = true;
//let d1 = int(dist(this.shapeLocation_1.x, this.shapeLocation_1.y, player1.x, player1.y));
//Capture distance between demon and attack when facing left
//g1
let d1_attack_left = int(dist(this.shapeLocation_1.x, this.shapeLocation_1.y, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d1_attack_up = int(dist(this.shapeLocation_1.x, this.shapeLocation_1.y, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d1_attack_right = int(dist(this.shapeLocation_1.x, this.shapeLocation_1.y, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d1_attack_down = int(dist(this.shapeLocation_1.x, this.shapeLocation_1.y, player1.x - 15, player1.y + 15));
//Capture distance between goblin and attack when facing left
//g2
let d2_attack_left = int(dist(this.shapeLocation_2.x, this.shapeLocation_2.y, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d2_attack_up = int(dist(this.shapeLocation_2.x, this.shapeLocation_2.y, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d2_attack_right = int(dist(this.shapeLocation_2.x, this.shapeLocation_2.y, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d2_attack_down = int(dist(this.shapeLocation_2.x, this.shapeLocation_2.y, player1.x - 15, player1.y + 15));
//Capture distance between goblin and attack when facing left
//g3
let d3_attack_left = int(dist(this.shapeLocation_3.x, this.shapeLocation_3.y, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d3_attack_up = int(dist(this.shapeLocation_3.x, this.shapeLocation_3.y, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d3_attack_right = int(dist(this.shapeLocation_3.x, this.shapeLocation_3.y, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d3_attack_down = int(dist(this.shapeLocation_3.x, this.shapeLocation_3.y, player1.x - 15, player1.y + 15));
//Capture distance between goblin and attack when facing left
//g4
let d4_attack_left = int(dist(this.shapeLocation_4.x, this.shapeLocation_4.y, player1.x - 45, player1.y - 15));
//Capture distance between goblin and attack when facing up
let d4_attack_up = int(dist(this.shapeLocation_4.x, this.shapeLocation_4.y, player1.x - 15, player1.y - 45));
//Capture distance between goblin and attack when facing right
let d4_attack_right = int(dist(this.shapeLocation_4.x, this.shapeLocation_4.y, player1.x + 15, player1.y - 15));
//Capture distance between goblin and attack when facing down
let d4_attack_down = int(dist(this.shapeLocation_4.x, this.shapeLocation_4.y, player1.x - 15, player1.y + 15));
fill(this.c);
if (sceneNum == 1) {
for (let x = 0; x < this.n; x++) {
switch (x) {
case 0:
//Move demon towards player
target[x] = createVector(player1.x, player1.y);
distance[x] = target[x].dist(this.shapeLocation_1);
mappedDistance[x] = map(distance[x], 100, 0, 0.01, 0.01);
target[x].sub(this.shapeLocation_1);
target[x].mult(mappedDistance[x]);
this.shapeLocation_1.add(target[x]);
//Demon only alive if true
if (d1_ == true) {
//ellipse(this.shapeLocation_1.x, this.shapeLocation_1.y, this.w, this.h);
image(demon_, this.shapeLocation_1.x, this.shapeLocation_1.y, this.w, this.h);
}
let d1 = int(dist(this.shapeLocation_1.x, this.shapeLocation_1.y, player1.x, player1.y));
if (d1 < 30) { //Player hit then reset
sceneNum = 0;
player1.x = width / 2;
player1.y = height - 50;
} else if (d1_attack_left < 30 && playerAttack == true || d1_attack_up < 30 && playerAttack == true || d1_attack_right < 40 && playerAttack == true || d1_attack_down < 30 && playerAttack == true) { //Demon attacked then die
d1_ = false;
this.shapeLocation_1.x = -6400;
}
break;
case 1:
//Move demon towards player
target[x] = createVector(player1.x, player1.y);
distance[x] = target[x].dist(this.shapeLocation_2);
mappedDistance[x] = map(distance[x], 100, 0, 0.009, 0.009);
target[x].sub(this.shapeLocation_2);
target[x].mult(mappedDistance[x]);
this.shapeLocation_2.add(target[x]);
//ellipse(this.shapeLocation_2.x, this.shapeLocation_2.y, this.w, this.h);
image(demon_, this.shapeLocation_2.x, this.shapeLocation_2.y, this.w, this.h);
let d2 = int(dist(this.shapeLocation_2.x, this.shapeLocation_2.y, player1.x, player1.y));
if (d2 < 30) { //Player hit then reset
sceneNum = 0;
player1.x = width / 2;
player1.y = height - 50;
} else if (d2_attack_left < 30 && playerAttack == true || d2_attack_up < 30 && playerAttack == true || d2_attack_right < 40 && playerAttack == true || d2_attack_down < 30 && playerAttack == true) {//Demon attacked then die
d2_ = false;
this.shapeLocation_2.x = 6400;
}
break;
case 2:
//Move demon towards player
target[x] = createVector(player1.x, player1.y);
distance[x] = target[x].dist(this.shapeLocation_3);
mappedDistance[x] = map(distance[x], 100, 0, 0.008, 0.008);
target[x].sub(this.shapeLocation_3);
target[x].mult(mappedDistance[x]);
this.shapeLocation_3.add(target[x]);
//ellipse(this.shapeLocation_3.x, this.shapeLocation_3.y, this.w, this.h);
image(demon_, this.shapeLocation_3.x, this.shapeLocation_3.y, this.w, this.h);
let d3 = int(dist(this.shapeLocation_3.x, this.shapeLocation_3.y, player1.x, player1.y));
if (d3 < 30) { //Player hit then reset
sceneNum = 0;
player1.x = width / 2;
player1.y = height - 50;
} else if (d3_attack_left < 30 && playerAttack == true || d3_attack_up < 30 && playerAttack == true || d3_attack_right < 40 && playerAttack == true || d3_attack_down < 30 && playerAttack == true) {//Demon attacked then die
d3_ = false;
this.shapeLocation_3.y = -6400;
}
break;
case 3:
//Move demon towards player
target[x] = createVector(player1.x, player1.y);
distance[x] = target[x].dist(this.shapeLocation_4);
mappedDistance[x] = map(distance[x], 100, 0, 0.007, 0.007);
target[x].sub(this.shapeLocation_4);
target[x].mult(mappedDistance[x]);
this.shapeLocation_4.add(target[x]);
//ellipse(this.shapeLocation_4.x, this.shapeLocation_4.y, this.w, this.h);
image(demon_, this.shapeLocation_4.x, this.shapeLocation_4.y, this.w, this.h);
let d4 = int(dist(this.shapeLocation_4.x, this.shapeLocation_4.y, player1.x, player1.y));
if (d4 < 30) { //Player hit then reset
sceneNum = 0;
player1.x = width / 2;
player1.y = height - 50;
} else if (d4_attack_left < 30 && playerAttack == true || d4_attack_up < 30 && playerAttack == true || d4_attack_right < 40 && playerAttack == true || d4_attack_down < 30 && playerAttack == true) {//Demon attacked then die
d4_ = false;
this.shapeLocation_4.y = 6400;
}
break;
}
}
}
}
}
class Boss {
constructor(c) {
this.w = 100;
this.h = 100;
this.c = c;
//Set original location of boss
this.shapeLocation = createVector(width / 2, height / 2);
this.blast = 100;
}
body() {
fill(this.c);
if (sceneNum == 2 && keyInsert == false) {
//set target for player
var target = createVector(player1.x, player1.y);
//get distance between target and boss
var distance = target.dist(this.shapeLocation);
var test_blast_x = this.shapeLocation.x;
//map distance between boss and player
var mappedDistance = map(distance, 100, 0, 1.25, 1);
target.sub(this.shapeLocation);
target.normalize();
target.mult(mappedDistance);
this.shapeLocation.add(target);
push();
translate(-50, -50);
//Boss follows payer x coordinate
image(boss_, this.shapeLocation.x, 100, this.w, this.h);
pop();
push();
translate(-10, 0);
//flame aims for player
image(flame_, test_blast_x, this.blast, 23, 23);
pop();
//blast shoots up if player is above boss
if (player1.y < 100) {
this.blast -= 2;
if (this.blast <= 0) {
this.blast = 100;
}
} else { //blast resets if reaches end of canvas
this.blast += 4;
if (this.blast >= 400) {
this.blast = 100;
}
}
//measure distance between blast and player
let d_boss = int(dist(this.shapeLocation.x, 100, player1.x, player1.y));
let d_boss_blast = int(dist(test_blast_x, this.blast, player1.x, player1.y));
//is blast reaches player game resets
if (d_boss_blast < 20 || d_boss < 50) {
sceneNum = 0;
player1.x = width / 2;
player1.y = height - 50;
}
} else if (sceneNum == 2 && keyInsert == true) {
//boss dies
}
}
}
class Walls {
constructor(c) {
this.top = 0;
this.btm = 400;
this.left = 0;
this.right = 600;
}
//set walss so player cannot go beyond canvas
build() {
if (player1.x <= 0) {
player1.x += 10;
} else if (player1.x >= 600) {
player1.x -= 10;
} else if (player1.y <= 0) {
player1.y += 10;
} else if (player1.y >= 400) {
player1.y -= 10;
}
}
}
class KeyCheck {
constructor(c) {
this.h = 20;
this.w = 20;
this.x = 125;
this.y = 225;
this.x_ = width / 2;
this.y_ = 140;
}
location() {
//set key in scene 1
if (sceneNum == 1) {
push();
fill('yellow');
translate(-10,-10);
//ellipse(this.x, this.y, this.h, this.w);
image(key_, this.x, this.y, this.h, this.w);
pop();
//if player gets key they can kill the boss
if (player1.x + player1.w / 2 > this.x && player1.x < this.x + this.w && player1.y + player1.h / 2 > this.y && player1.y < this.y + this.h) {
keyState = true;
this.y = 9999;
}
} else if (sceneNum == 2) {
//if key is inserted into lock then boss dies
if (keyInsert == false){
push();
fill('yellow');
//ellipse(this.x_, this.y_, this.h, this.w);
translate(-10,-10);
image(lock_, this.x_, this.y_, this.h, this.w);
pop();
}
if (player1.x + player1.w / 2 > this.x_ && player1.x < this.x_ + this.w && player1.y + player1.h / 2 > this.y_ && player1.y < this.y_ + this.h && keyState == true) {
keyInsert = true;
}
}
}
}
class Intro { //Intro message and song
constructor(c) {
this.fade_0 = 0;
this.fade_1 = 0;
this.fade_2 = 0;
this.fade_3 = 0;
this.fadeAmount = 1;
}
scene(){
textSize(15)
if (sceneNum == -1){
background(0);
fill(255, 255, 255, this.fade_0);
text("You have awoken in a cave", 20,50);
fill(255, 255, 255, this.fade_1);
text("You don't know how you got here but you need to escape", 20,150);
fill(255, 255, 255, this.fade_2);
text("You must have no fear!", 20,250);
fill(255, 255, 255, this.fade_3);
text("Press enter to continue...", 20,350);
if (this.fade_0<0) this.fadeAmount=1;
//if (fade>255) fadeAmount=-10;
if (this.fade_0 >= 150) {
this.fade_1 += this.fadeAmount;
}
if (this.fade_1 >= 150) {
this.fade_2 += this.fadeAmount;
}
if (this.fade_2 >= 150) {
this.fade_3 += this.fadeAmount;
}
this.fade_0 += this.fadeAmount;
//print(this.fade_0);
if (keyIsDown(32)) {
song.stop();
songTwo();
sceneNum++;
}
}
}
}
function songTwo() { //after intro, loop dungeon song
song2.loop();
}
function songThree() { //When player leaves cave play victory song
song2.stop();
song3.play();
}