Side Scroll Adventure

Run

//---------Variables---------\\
var ground;
var sun;
var player;
var left, right;
right = false;
left = false;
var attack;
attack = false;
var interact;
interact = false;
var drawXYlines
var click;
click = false;
var scene;
scene = 1
var missionRecieved;
var missionFinished;
missionFinished = false
var messageStarted;
var messageFinished;
var sign;
//"Knight" is right knight
var keycardsCollected;
keycardsCollected = 0
var keycardsTotal;
keycardsTotal = 1
var Knight;
var building;
var leftknight;
var KNIGHT;
var dude;
var message;
message = ["word"]
var action = function(){}
var counter = 0
var message1;
var message1  = ["(Peasant) Greetings Fair Knight!", "Thank goodness you've come!", "(You) what is it kind stranger?", "(Peasant) There is a horrible Dragon, and he is ravaging our town!", " (You) Has he been eating villagers!?", "(Peasant) Worse! He's over charging for outdoor grills!", "(You) ???", "(Peasant) You See, He's the only supplier of the cooking grills we want.", "So Hes increased the price by almost 500%!", "(You) Thats Horrible!", "(Peasant) Indeed!", "So you see our plight?", "(You) Yes, I see now.", "I must slay this dragon!", "where is his cave?", "(Peasant) His Cave is not a cave", "It is a building of tremendous sature", "to gain access, you must find a keycard.", "(You) Where might I find one?", "(Peasant) I believe the gaurd dropped it on his way to the post", "(You) Then onward!"]
var message2;
var message2 = ["(Gaurd) Halt!", "none shall pass!", "So Go away.", ". . .", "Please."]
var SWORD;
var rightSword;
var leftSword;
var attackTimer;
attackTimer = 0
var leftEnemy;
var rightEnemy;
var enemy;
var enemyHealth;
enemyHealth = 10
var dudeHealth
dudeHealth = 10
var buildingUnlock;
buildingUnlock = false
var enemy2
var dragonHealth
dragonHealth = 50
var playerHealth
playerHealth = 100
var setup = function(){
var interact = false

rightDragon = loadImage("right_dragon.png")
leftDragon = loadImage("left_dragon.png")
rightEnemy = loadImage("OIPI64I0PAP.jpg")
leftEnemy = loadImage("EvilKnight.jpg")
leftSword = loadImage("SWORD.png")
rightSword = loadImage("mini-letter-opener-sword-6001-2-1.png");
Knight = loadImage("rar.png");
building = loadImage("building.png")
leftknight = loadImage("PaP.png")
ground = 350;  
KNIGHT = Knight
SWORD = rightSword
sun = {
  x: 400,
  y: 100,
  size: 100,
  yv:1
};
player = {
  x: 150,
  y: 250,
  width: 50,
  height: 100,
  health: 100
};
Sword = {
  x: 175,
  y: 225,
  width: 50,
  height: 75
};
dude = {
  x:150,
  y: 250,
   width: 50,
  height: 100,
  health: 10,
  scene: 2
};
sign = {
  x: 325,
  y: 250,
  width: 100,
  height: 50
};  
door = {
  x: 225,
  y:275,
  width: 50,
  height: 75
};
keycard = {
  x: round(random(0, 500)),
  y: ground - 10,
  width: 20,
  height: 20,
  color: color(random(0,255), random(0,255), random(0,255)),
  scene: round(random(1, 3))
};
enemy = {
  x:450,
  y: 250,
   width: 50,
  height: 100,
  health: 10,
  scene: 1
};
enemy2 = {
  x:450,
  y: 250,
   width: 50,
  height: 100,
  health: 50,
  scene: 102
}

};

var draw = function(){
stroke(0)
strokeWeight(1)
drawScene();
sunset();
drawPlayer();  
movePlayer();
drawSword();
moveSword();
enemyHealth();
dragonHealth();
dudeHealth();
playerHealth();
//do this function last
if (click){
 drawXYlines();
}
if(missionRecieved && !missionFinished){
  showMission("Find Keycards");
  drawKeycards();
  collectKeycards();
  }
};

console.log(round(random(0, 100)));
//------Helper Functions------\\
//Purpose: x/y debug lines
var drawXYlines = function(){

strokeWeight(2);
textSize(12)
stroke(255, 0, 0);
line(0, mouseY, mouseX, mouseY);

stroke(0, 0, 255);
line(mouseX, 0, mouseX, mouseY);

noStroke();
  fill(255);
  rect(mouseX - 25, mouseY/2-10, 50, 20)
  rect(mouseX/2 - 25, mouseY-10, 50, 20)
  fill(0)
  text("Y: " + mouseY, mouseX -15, mouseY/2+5)
  text("X: " + mouseX, mouseX/2 -15, mouseY+5)  
  
};

// purpose: Draw the background scene
var drawScene = function(){
//backround color

if(scene === 1){
background(0, 153-sun.y * 0.6, 255-sun.y * 0.6)  

//sun
fill(255, 200, 0)
ellipse(sun.x, sun.y, sun.size, sun.size);

fill(0, 200, 0)  
//fillcolor
 rect(0,ground, 500, 500 - ground); 

drawEnemy()


//150, 250, 50, 100

} else if (scene === 2){
   //blue sky
  background(0, 153-sun.y * 0.6, 255-sun.y * 0.6)  

  
  //sun
fill(255, 200, 0)
ellipse(sun.x, sun.y, sun.size, sun.size);

//mountains
fill(150, 150, 150)
triangle(0, 500, 400, 500, 200, 200)
triangle(100, 500, 300, 150, 500, 500)

//ground
fill(0, 200, 0);
rect(0, ground, 500, 500 - ground);

drawDude()

} else if (scene === 3){
   //blue sky
   background(0, 153-sun.y * 0.6, 255-sun.y * 0.6)  


//sun
fill(255, 200, 0)
ellipse(sun.x, sun.y, sun.size, sun.size);

fill(100, 100, 100,);
rect(120, 100, 250, 350);

image(building, 0, -135, 500, 500);

//drawDoor()

//ground
fill(0, 200, 0);
rect(0, ground, 500, 500 - ground);


drawSign(225, 275, 50, 75)

} else if (scene === 101){

background(179,179,179)  

fill(130,130,130)  
//fillcolor
 rect(0,ground, 500, 500 - ground); 
} else if (scene === 102){

background(179,179,179)  

fill(130,130,130)  
//fillcolor
 rect(0,ground, 500, 500 - ground); 
 
 fill(255, 0, 0)
ellipse(100, 100, 100, 100);

drawEnemy2()
moveEnemy2();

} else if (scene === 666){
 background(0, 0, 0,);
 textSize(36);
 fill(255, 255, 255);
 text("Game Over", 135, 200);
} else if (scene === 777){
background(0, 0, 0,);
 textSize(36);
 fill(255, 255, 255);
 text("You Win!", 135, 200);

 } else {
 background(0, 0, 0,);
 textSize(36);
 fill(255, 255, 255);
 text("No secrets Here...", 135, 200);
 }
};

// Purpose: Make the sun set
var sunset = function(){
  sun.y += sun.yv;
  sun.x += 0.1;
sun.size += 0.15*sun.yv;

if (sun.y > 600){
  sun.x = 15;
  sun.yv = -1;
}

if(sun.y<-100){
  sun.x = 400;
  sun.yv = 1;
}
};

// purpose: draw dude
var drawDude = function(){
if(dude.x > player.x)
image(leftknight, dude.x, dude.y, dude.width, dude.height);  
if(dude.x < player.x) 
image(Knight, dude.x, dude.y, dude.width, dude.height);  
};

//purpose: player health
var playerHealth = function(){
if (playerIsOverLapping(enemy2) === true && scene == 102){
player.health -= 1
}

if (player.health == 0){
enemy2.x = 1000
scene = 666
}
};

// purpose: draw enemy
var drawEnemy = function(){
if(enemy.x > player.x)
image(leftEnemy, enemy.x, enemy.y, enemy.width, enemy.height);  
if(enemy.x < player.x) 
image(rightEnemy, enemy.x, enemy.y, enemy.width, enemy.height);  
};

// purpose: draw enemy2
var drawEnemy2 = function(){
if(enemy2.x > player.x)
image(leftDragon, enemy2.x, enemy2.y, enemy2.width, enemy2.height);

if(enemy2.x < player.x) 
image(rightDragon, enemy2.x, enemy2.y, enemy2.width, enemy2.height);  
};

//purpose: move enemy2
var moveEnemy2 = function(){
if(enemy2.x > player.x && scene == 102)
enemy2.x -= 2.5

if(enemy2.x < player.x && scene == 102) 
enemy2.x += 2.5
};

//purpose: enemy health
var enemyHealth = function(){
if (swordIsOverlapping(enemy) === true && attack === true){
enemy.health -= 1
}

if (enemy.health == 0){
enemy.x = 1000
}
};

//purpose: dude health
var dudeHealth = function(){
if (swordIsOverlapping(dude) === true && attack === true){
dude.health -= 1
}

if (dude.health == 0){
dude.x = 1000
scene = 666
}
};

//purpose: Dragon health
var dragonHealth = function(){
if (swordIsOverlapping(enemy2) === true && attack === true){
enemy2.health -= 1
}

if (enemy2.health == 0){
enemy2.x = 1000
scene = 777
}
};

// purpose: draw player
var drawPlayer = function(){
image(KNIGHT, player.x, player.y, player.width, player.height);
  
}; 

//Purpose: draw sword
var drawSword = function(){

if (attack == true){
  attackTimer+=1
  var xOffset = 0;
  var dir = 1
  if(SWORD == leftSword){
    xOffset = Sword.width
    dir = -1
  }
pushMatrix()
translate(Sword.x + xOffset, Sword.y+Sword.height);
attackTimer == 0
rotate( radians(attackTimer * 7 * dir) )
image(SWORD, -xOffset, -Sword.height, Sword.width, Sword.height)
popMatrix()
if(attackTimer > 19){
  attack = false
//  frameCount
}
} else{
  image(SWORD, Sword.x, Sword.y, Sword.width, Sword.height);
}
}; 

//purpose: draw door
var drawDoor = function(){
  fill(0, 0, 0)
  rect( 225, 275, 50, 75)
  
  
};

// purpose: Move the PLayer
var movePlayer = function(){

 if(right){
  player.x += 4;
KNIGHT = Knight
}
if(left){
player.x -= 4; 
KNIGHT = leftknight
}
if(player.x > 600){
   player.x = -100;
scene += 1;   
}
if(player.x < -100){
   player.x = 500;
scene -= 1;  
}
if (scene === 3){
  //(player.x + player.width > sign.x && player.x < sign.x + sign.width){
if (playerIsOverLapping(sign)){     
  showOption("Genuine Dragon Grills \n Home of the famous \n Drago Cracklesmith \n the last Dragon! ");
if(interact){
   bubble("this must be the place.")
}
}
if (playerIsOverLapping(door)){     
  showOption("Enter");
if(interact && keycardsCollected == true){
  scene = 101
  missionFinished = true
}else {
 bubble ("This door requires a Keycard.")
  }
 }
}
if (scene === 2)
  //(player.x + player.width > dude.x && player.x < dude.x + dude.width){
if (playerIsOverLapping(dude)){
  message = message1
  showOption("Talk");
  action = function(){missionRecieved = true};
if(interact && (!messageStarted || messageFinished)){
messageStarted = true;
messageFinished = false
counter = 0
interact = false;
}
if(messageStarted && !messageFinished){
  message = message
 playMessage();
}
}
if (scene === 1)
  //(player.x + player.width > dude.x && player.x < dude.x + dude.width){
if (playerIsOverLapping(enemy)){
  
  if(player.x>enemy.x && scene == enemy.scene){
player.x = enemy.x
}
  message = message2
  showOption("Talk");
if(interact && (!messageStarted || messageFinished)){
messageStarted = true;
messageFinished = false
counter = 0
interact = false;
}
if(messageStarted && !messageFinished){
  message = message
 playMessage();
    }
   }
 };
 
// purpose: Move the Sword
var moveSword = function(){

if(right){
  Sword.x = player.x +25;
SWORD = rightSword
}
if(left){
Sword.x = player.x -25; 
SWORD = leftSword 
}
};

//Purpose:Add a sign to the scene
var drawSign = function(){
  //brown color
  fill(150, 75, 0);
  //main sign
   rect(sign.x + sign.width/2- 10, sign.y, 20, ground - sign.y)
   //sign post
   rect(sign.x, sign.y, sign.width, sign.height)
   //lines on the sign
   line(sign.x + 10, sign.y +10, sign.x + sign.width - 10, sign.y+ 10)
   line(sign.x + 10, sign.y +20, sign.x + sign.width - 10, sign.y+ 20)
   line(sign.x + 10, sign.y +30, sign.x + sign.width - 10, sign.y + 30) 
};

//Purpose: Dislpay what the player is thinking 
var bubble = function(thought){
  var w = 100+textWidth(thought)*0.1;
  var h = floor(textWidth(thought)/(w*0.9))*25+50;
    //thought bubble
   textSize(12); 
    fill(255, 255, 255);
   ellipse(player.x,player.y - 70, w, h);
  textAlign(CENTER,CENTER);
  fill(0);
  text(thought, player.x-w*0.45, player.y-h/2 - 70, w*0.9, h);
    //little bubbles
   fill(255, 255, 255)
    ellipse(player.x - 10, player.y - 20, 20, 20);
     ellipse(player.x - 0, player.y + 5, 10, 10);
     //words
     fill(0, 0, 0);
     //text( thought, player.x - 50, player.y - 75);
 };
 
//Purpose: show what interactions the player can do  
var showOption = function(message){
 textAlign(CORNER)
  fill(255, 255, 255)
  textSize(20)
  text(message, 290, 400)
};

//Purpose: Tell if the player is on top of another object
var playerIsOverLapping = function(object){
     var isOverLapping = false;

if(player.x + player.width > object.x && player.x < object.x + object.width){     
isOverLapping = true;
  
}

    return isOverLapping;
};

//Purpose: tell if sword is on top of another object
var swordIsOverlapping = function(object){
     var IsOverlapping = false;

if(Sword.x + Sword.width > object.x && Sword.x < object.x + object.width && scene == object.scene){     
IsOverlapping = true;
  
}

    return IsOverlapping;
};

//Purpose: Show the villager's message

//show the enemys message
var playMessage = function(){
  
 //background
  fill(0, 0, 0, 50);
  rect(0, 0, 500, 500);
fill(255);
rect(10, 10, 480, 200, 20);
textSize(24);
fill(0);
textAlign(CORNER,CORNER);
text(message[counter], 100, 100, 300, 300);
if(interact){
  interact = false;
  counter += 1
}
if(counter >= message.length){
messageFinished = true 
action()
//missionRecieved = true
  }
};

//purpose: Show what the player should be doing 
var showMission =function(mission){
  textAlign(CORNER, CORNER)
  fill(255);
  textSize(24);
  text("Mission: " + mission, 50, 50);
};

//Purpose: Display the keycards in the correct scene
var drawKeycards = function(){
// 249, 45, 42 
if (scene === keycard.scene && !keycard.collected){
  fill(keycard.color);
  rect(keycard.x, keycard.y, keycard.width, keycard.height);
}  
};

//Purpose: Count the number of keycards collected
var collectKeycards = function(){
  
  fill(255);
  textSize(24);
  text("Keycards Collected: " + keycardsCollected + "/" + keycardsTotal, 20, 450);

if(playerIsOverLapping(keycard) && scene === keycard.scene && !keycard.collected){
bubble ("this must be the key card");

if (interact){
  keycard.collected = true
keycardsCollected =+ 1;
 interact === false;
    }
  }
if (missionFinished == true){
missionRecieved = false
}
};

//Purpose: Unlock the building
var buildingUnlock = function(){
if (keycardsCollected == 1){
buildingUnlock = true  
}
if (buildingUnlock == true){
missionFinished == true
}
if (buildingUnlock == true && playerIsOverlapping(door) && interact == true){
scene = 101
missionFinished = true
}
}

//----User Input Functions----\\
var mousePressed= function(){
   click = true;
}
var mouseReleased = function(){
   click = false;
}
var keyPressed = function(){
if(keyCode === RIGHT){
 right = true;
}
if(keyCode === LEFT){
  left = true
}
if(keyCode === 32 && attack === false){
  attack = true
  attackTimer = 0
}
  if(keyCode === ENTER){  
  interact = !interact;
}
}
var keyReleased = function(){
if(keyCode === RIGHT){
 right = false;
}
if(keyCode === LEFT){
  left = false
}
if(keyCode === ENTER){  
  interact = false
}
};