//"Battle of Bunnies"
//This is a very simple shooting game, but it does require some skills, especially the time limit is only 10 seconds.
//The player gets to adjust both the shooting angle and the inititial velocity (which can also be known as the power).
//The target is moving, which adds some difficulty to the game.
//I personally like shooting games a lot. That is why I chose to make a shooting game for my project. I intend to creat
//a simple game that is not too easy to win. For a shooting game, there are at least two variables making the 
//result unpredictable. And for this project, I even looked up my roommate's physics textbook to find the projectile
//motion equations in order to achieve the motion effect.
//In terms of concept, my idea is very kiddy. I just want it to be a cute game with friendly colors.
//For the bunnies, haku actually means white, and gurei means gray, I didn't name them out of nowhere. :p


//Anyways, game starts here!

projectile shoot;  //controls the shooting
Target target;     //the target

int begin;               // Time the event/game begins
boolean active;          // Flag for event/game status
boolean done;            // Flag for "game over"

boolean spaceRelease=false;
boolean spacePress=false;  // hit spacebar to shoot
boolean win=false;      

//for the projectile
float angle=0.0, Vi=0.0; // angle and power changed by player
float t=0.0, g=0.098;    // time and gravity
float A;                 // angle for aiming

// Default images to begin and end the event/game.
// Make your own images to fit your concept.
PImage beginImage;       // Start with this image
PImage loseImage;         // End with this image
PImage winImage;
PImage BG;
PImage meter;
PImage water;
PImage haku;
PImage gurei;
PImage gurei2;
PFont AlbaSuper;


void setup() 
{
  size(600, 600);
  smooth();
  noStroke();
  framerate(60);
  active = false;        // Don't begin with action
  done = false;          // The event/game has not finished
  beginImage = loadImage("begin.png");
  loseImage = loadImage("lose.png");
  winImage = loadImage("win.png");
  BG=loadImage("bunnyBG4.png");
  meter=loadImage("meter2.png");
  water=loadImage("water.png");
  haku=loadImage("haku.png");
  gurei=loadImage("gurei.png");
  gurei2=loadImage("gurei2.png");
  AlbaSuper=loadFont("KabelITCbyBT-Ultra-48.vlw");
  
  shoot=new projectile(100, 375);
  target=new Target(370,0);
}


// Control the series of events.

void draw() 
{
  background(126);
  
  if(active == true) {
    eventGame();         // Run the event/game
    timer();             // Time the event/game
 } else {
    if(done == true) {
     endScreen();       // Show the "end" screen
    } else {
     beginScreen();     // Show the "first" screen
    } 
  }
}



void eventGame() 
{ 
image(BG, 0,0);

pushMatrix(); target.move(); target.display(); popMatrix();
meter();

smooth();
 
if(spaceRelease==true){ 
  shoot.move();
  shoot.display();  
}
pushMatrix();  bow();  popMatrix();

 
 image(water,0, 460);
 
 if(shoot.xpos>=(target.xpos+100-20)&& shoot.xpos<=(target.xpos+100+20)&&
 shoot.ypos>=(target.ypos+375-20)&&shoot.ypos<=(target.ypos+375+20)
 )
 {  win=true;    }
 

}

void mousePressed() 
{
  // Begin the event/game when the mouse is clicked
  // and the event/game is not already happening
  if(active == false) {
   
    active = true;
    begin = millis();
    spaceRelease=false;
    spacePress=false;
    t=0.0;
    Vi=0.0;
    shoot.Xi=100;
    shoot.Yi=375;
    win=false;

  } 
  
  
}


 
void keyPressed() 
{
  if(key==' ') { 
  spacePress=true;
  //as spacebar is pressed, power is going up and angle follows the mouse angle
  if(Vi*10<130&&spaceRelease==false)
  {Vi=Vi+0.5;  }
  angle=atan2(mouseY-shoot.Yi, mouseX-shoot.Xi); 
  } 
  

  
  if(key==CODED){
    spacePress=false;
    if(keyCode==LEFT&&shoot.Xi>20&&spaceRelease==false) { shoot.Xi=shoot.Xi-1.0; }
    if(keyCode==RIGHT&&shoot.Xi<150&&spaceRelease==false){ shoot.Xi=shoot.Xi+1.0; }
  }

}


    

void keyReleased() 
{
  if (spacePress==true){
  spaceRelease=true;
  spacePress=false;
  }
}

void timer() 
{
  int curTime = millis();
  if(curTime > begin + 10000) {
    active = false;
    done = true;
  }
  noStroke();
  fill(255);
  rect(0, height-5, width, 5);
  fill(144, 200,47);
  rect(0, height-5, (curTime-begin)/16.667, 5);
}

// Displays when the game/event begins
void beginScreen() {
  image(beginImage,0,0);
}

// Displays when the 10 seconds are over

void endScreen() {
  if(win==true){
  // the wining screen
  image(winImage, 0,0);
  handClap();
 
  }
  else{
  // the losing screen
  image(loseImage, 0,0);
  smooth();
  fill(126);
  ellipse(265, 320, 60, 60);
  translate(70,0);
  ellipse(265, 320, 60, 60);
  fill(255);
  
 //watery eyes
 pushMatrix(); eyeRoll(PI); popMatrix();
 pushMatrix(); translate(-70, 0);  eyeRoll(PI); popMatrix();
 pushMatrix(); translate(70, 170); scale(0.5); eyeRoll(0); popMatrix();
 pushMatrix(); translate(65, 166); scale(0.5); eyeRoll(0); popMatrix();
 pushMatrix(); translate(140, 170); scale(0.5); eyeRoll(0); popMatrix();
 pushMatrix(); translate(135, 166); scale(0.5); eyeRoll(0); popMatrix();
 

  }
 
      
}

//the bow that the bunny is holding

 void bow(){
  A=atan2(mouseY-shoot.Yi, mouseX-shoot.Xi); //same as angle
  fill(255);
  translate(shoot.Xi,shoot.Yi);
  image(haku,-52,-50);
  rotate(A);
  fill(138,91,41);
  ellipse(40, 0, 15,15);
   
}

//displaying the angle and the power
void meter(){
  fill(255);
  image(meter, -5, -10);
  rect(85, 57, 130, 10);
  fill(255,255,0);
  rect(85,57, 10*Vi,10);
  
  float a=degrees(-A);
  String b=nf(a,2,1);
  fill(138,91,41);
  textAlign(LEFT);
  textFont(AlbaSuper);
  textSize(20);
  text(b, 22, 57);
  
}

// for the bunny eye rolling on loseScreen
float roll=0.5;          

void eyeRoll(float P){
  translate(265, 320);
  roll=roll-0.01;
  pushMatrix();
  rotate(P+0.5*cos(roll));
  ellipse(10, 10, 26, 26);
  popMatrix();
  }

//set globals for the handClap function
float speed=2.5; 
int Rdirection=1, Ldirection=1;
float Rx=220, Ry=400, Lx=380, Ly=400;

//hand clapping for wining screen
void handClap(){
  stroke(197, 198, 189); strokeWeight(4);
  fill(255);
  smooth();
  Rx=Rx+speed*Rdirection;
  ellipse(Rx, Ry, 50,50);
if ((Rx+25)>=300){Rdirection=-1;}
if ((Rx-25)<200) { Rdirection=1;}
  Lx=Lx-speed*Ldirection;
  ellipse(Lx, Ly, 50, 50);
if ((Lx+25)>400) {Ldirection=1;}
if ((Lx-25)<=300) {Ldirection=-1;}

}
//the target

class Target{
  float xpos, ypos, speed=2.85;
  int direction=1;
  
  Target(float x, float y){
    xpos=x; ypos=y;
  }
  
  void move(){
   
 
    ypos=ypos-speed*direction;
    if((ypos-20)<=-180||win==true){ direction=-1;}
    if((ypos+20)>=15) { direction=1;}
  
  }
  
  void display(){
    fill(126);
   translate(60, 330);
   if(win==true){ image(gurei2, xpos, ypos); }
   
   else{
   
    image(gurei, xpos, ypos);
   }
   
  }
    
}

//the shooting

class projectile{
  float xpos, ypos, Vx, Vy, Xi, Yi;
  
  projectile(float x, float y){
    Xi=x; Yi=y;
  }

  void move(){
   //the projectile motion formulas
    t=t+1.5;
    Vx=Vi*cos(-angle);
    Vy=Vi*sin(-angle);
    xpos=Xi+Vx*t;
    ypos=Yi-Vy*t+0.5*g*t*t;
   
  }
  
  void display(){
    if(win==false){
    fill(255,255,0);
    ellipse(xpos, ypos, 20,20);
    }
  }
  

}

//the end
Project 3: