int status; 

PFont myfont; //<>//

float x = 0;
float x2 = 250;

float waterx = 0;
float water2x = 0;
float water3x = 0;

float nutrix = 500;
float nutri2x = 500;
float nutri3x = 500;

int reternw = 0;
int countw = 0;
int countn = 0;

int waterpoint = 0;
int nutripoint = 0;

float prsun = 0;//太陽回転の角度

int raywidth = 200;
int rayheight = 200;
int raywidth2 = 0;
int rayheight2 = 0;

float angle = 0;//花の回転の角度

float cloud1x = 260;
float cloud1y = 265;
float cloud2x = -300;
float cloud2y = 300;

int r = 0;
int g = 0;
int b = 0;
int sunr = 245;
int sung = 201;
int sunb = 79;


void setup(){
  size(500,500);
  frameRate(200);
  myfont = createFont("Yu Gothic",48);
  textFont(myfont);//
  textAlign(CENTER,CENTER);
  
  status = 0;//プログラミング入門授業紹介よりA_Story_of_a_Manを参考
}            //どんな状態を示すのか確認する。

void draw(){

  if(status == 0){
    title();
  }
  
  if(status == 1){
    title_keyinput();
   }
   
  if(status == 2){
    asagao();
    mouseCheck();
   }

}

 
///////////////////////////////////////////////////////////////////////////////////


void title(){
  textAlign(CENTER,CENTER);
  background(255);
  
  stroke(250,248,204);
  strokeWeight(50);
  noFill();
  rect(0,0,500,500);
  noStroke();
  fill(78,192,222,100);
  rect(80,195,70,20);
  fill(255,174,44,100);
  rect(156,195,99,20);
  
  textSize(25);
  fill(0);
  text("花のレシピ",250,120);
  textSize(15);
  text("植物を育てましょう",250,170);
  text("wキーで水、nキーで栄養剤を与えることができます",250,200);
  text("成長は全部で三段階",250,230);
  text("どんな花が咲くでしょうか？",250,260);
  textSize(13);
  text("sキーを押して始める",250,350);
   
  status = 1;  //キー入力ステータスへ
}

void title_keyinput(){
  if(key == 's'){
    status = 2;  //育成画面ステータスへ
  }
}

void mouseCheck(){
   if(mousePressed == true){//太陽をクリックしている間、太陽が赤くなり光が出て、花が踊る
     float distance = dist(mouseX, mouseY, 0, 0);
     if(distance <= 100){
       sunr = 255;
       sung = 85;
       sunb = 85;
       
       angle += 0.015;
       
       if(raywidth > 1414){
         raywidth = 0;
         rayheight = 0;
       }
      
       if(raywidth2 > 1414){
         raywidth2 = 0;
         rayheight2 = 0;
       }
       
       raywidth += 2;
       rayheight += 2;
       sunray(raywidth, rayheight);
       
       raywidth2 += 4;
       rayheight2 += 4;
       sunray2(raywidth2, rayheight2);      
     }
   }
   
   if(mousePressed == false) {
     raywidth = 200;
     rayheight = 200;
     
     raywidth2 = 0;
     rayheight2 = 0;
     
     sunr = 245;
     sung = 201;
     sunb = 79;
   }
}

void keyPressed(){
  if(key == 'w' && countw+countn < 3){
    countw++;
  }
  if(key == 'n' && countw+countn < 3){
    countn++;
  }
}

void asagao(){
  
  background(250,248,204);
    

  fill(220,235,242);
  noStroke();
  rect(0,0,500,435);
  
  cloud1x += 0.02;
  cloud2x += 0.04;
  
  if(cloud1x >= 500){
    cloud1x = -260;
    cloud1y = random(150,350);
  }
  cloud1(cloud1x,cloud1y);
  
  if(cloud2x >= 500){
    cloud2x = -250;
    cloud2y = random(150,350);
  }
  cloud2(cloud2x,cloud2y);
  
  fill(0);
  textSize(12);
  text("rキーを押してタイトルに戻る",400,480);
  
  pipe();
  
  if(countw >= 1 && countw <= 3 && waterx <= 250){
    waterx++;
    water(waterx);
  }
  
  if(countw >= 2 && countw <= 3 && water2x <= 250){
    water2x++;
    water2(water2x);
  }
  
  if(countw == 3 && water3x <= 250){
    water3x++;
    water3(water3x);
  }
  
  if(waterx == 250 || water2x == 250 || water3x == 250){//ｘ座標が中心に到達すると成長する
    waterpoint++;
  }
  

  
  if(countn >= 1&& countn <= 3 && nutrix >= 250){
    nutrix--;  
    nutrition(nutrix);
  }
  
  if(countn >= 2 && countn <= 3 && nutri2x >= 250){
    nutri2x--;  
    nutrition2(nutri2x);
    }
    
  if(countn == 3 && nutri3x >= 250){
    nutri3x--;  
    nutrition3(nutri3x);
  }
  
  if(nutrix == 250 || nutri2x == 250 || nutri3x == 250){//ｘ座標が中心に到達すると成長する
    nutripoint++;
  }
  

  
  if(waterpoint == 1 && nutripoint == 0){ //水で発芽する
    r=138;
    g=198;
    b=145;
    sprout(r,g,b);
  }
  
  if(waterpoint == 0 && nutripoint == 1){ //栄養剤で発芽する
    r=25;
    g=126;
    b=26;
    sprout(r,g,b);
  }
  
  if(waterpoint == 2 && nutripoint == 0){ //第二段階（水だけ）になる
    waterplant();  
  }
  
  if(waterpoint == 0 && nutripoint == 2){ //第二段階（栄養だけ）になる
    nutriplant();
  }
  
  if(waterpoint == 1 && nutripoint == 1){ //第二段階（バランス良好）になる
    goodplant();
  }
     
  if(waterpoint == 2 && nutripoint == 1){ //第三段階（花・水多め）
    beautyflower();
  }
  
  if(waterpoint == 1 && nutripoint == 2){ //第三段階（花・栄養多め）
    normalflower();
  }
    
  if(waterpoint == 0 && nutripoint == 3){  //第三段階（花・栄養だけ）
    nutriflower();
  }
  
  if(waterpoint == 3 && nutripoint == 0){  //第三段階（花・水だけ）
    waterflower();
  }
    
  pipeshadow();//管の影と植木鉢
  pot();
  
  rotate(prsun);//太陽を回転させる
  
  sun(sunr, sung, sunb);
  prsun += 0.0009;
  
  if(key == 'r'){  
    waterx = 0;//初期化
    water2x = 0;
    water3x = 0;
  
    nutrix = 500;
    nutri2x = 500;
    nutri3x = 500;
  
    reternw = 0;
  
    countw = 0;
    countn = 0;
  
    waterpoint = 0;
    nutripoint = 0;
    
    status = 0;//タイトルへ
  }
}


///////////////////////////////////
void water(float pwaterx){     //水
  noStroke();
  fill(78,192,222);
  ellipse(pwaterx,400,20,20);
}

void water2(float pwater2x){
  noStroke();
  fill(78,192,222);
  ellipse(pwater2x,400,20,20);
}

void water3(float pwater3x){
  noStroke();
  fill(78,192,222);
  ellipse(pwater3x,400,20,20);
}

void nutrition(float pnutrix){  //栄養剤
  noStroke();
  fill(255,174,44);
  ellipse(pnutrix,380,20,20);
}

void nutrition2(float pnutri2x){   
  noStroke();
  fill(255,174,44);
  ellipse(pnutri2x,380,20,20);
}
  
void nutrition3(float pnutri3x){  
  noStroke();
  fill(255,174,44);
  ellipse(pnutri3x,380,20,20);
}
  
void pot(){  
  fill(137,120,99,100);
  quad(210,450,290,450,300,500,200,500);//植木鉢の影
  fill(188,116,21);
  noStroke();
  quad(200,350,300,350,290,450,210,450);//植木鉢
  ellipse(250,450,80,20);
}

void sprout(int pr, int pg, int pb){  //第一段階（芽）（色はasagao内で指定）
  fill(pr,pg,pb);
  stroke(pr,pg,pb);
  strokeWeight(3);
  line(250,350,250,320);
  strokeWeight(2);
  ellipse(237,320,20,10);
  ellipse(263,320,20,10);
}

void waterplant(){//第二段階（水２）
  strokeWeight(4);
  fill(173,209,177);
  stroke(173,209,177);
  line(250,350,250,280);
  ellipse(230,315,30,10);
  ellipse(270,315,30,10);
  ellipse(230,280,30,10);
  ellipse(270,280,30,10);
}

void nutriplant(){//第二段階（栄養２）
  strokeWeight(4);
  fill(25,126,26);
  stroke(25,126,26);
  line(250,350,250,270);
  ellipse(232,330,30,10);
  ellipse(268,330,30,10);
  ellipse(232,310,30,10);
  ellipse(268,310,30,10);
  ellipse(232,290,30,10);
  ellipse(268,290,30,10);
  ellipse(232,270,30,10);
  ellipse(268,270,30,10);
}

void goodplant(){  //第二段階（水１栄養１）
  strokeWeight(3);
  fill(138,198,145);
  stroke(138,198,145);
  line(250,350,250,300);
  ellipse(235,320,25,10);
  ellipse(265,320,25,10);
  ellipse(235,300,25,10);
  ellipse(265,300,25,10);
}

void beautyflower(){  //花（水２栄養１）
  noStroke();
  strokeWeight(3);
  fill(138,198,145);
  stroke(138,198,145);
  
  line(250,350,250,290);
  line(250,350,215,320);
  line(215,320,215,240);
  line(250,350,285,315);
  line(285,315,285,250);
  ellipse(235,320,25,10);
  ellipse(265,320,25,10);
  ellipse(235,300,25,10);
  ellipse(265,300,25,10);
  ellipse(200,270,25,10);
  ellipse(230,270,25,10);
  ellipse(300,280,25,10);
  ellipse(270,280,25,10);
  
  pushMatrix();//真ん中の花の中心
  translate(250,270);
  rotate(angle);
  noStroke();
  fill(46,57,240);
  ellipse(0,0,40,40);
  fill(255);
  for (int i = 0; i < 5; i++){
    rotate(PI/2.5);
    triangle(0,-10,-5,0,5,0);
  }
  popMatrix();
  
  pushMatrix();//左の花の中心
  translate(215,230);
  rotate(-angle);
  noStroke();
  fill(46,57,240);
  ellipse(0,0,50,50);
  fill(255);
  for (int i = 0; i < 5; i++){
    rotate(PI/2.5);
    triangle(0,-10,-5,0,5,0);
    rect(0,0,2,25);
  }
  popMatrix();
  
  pushMatrix();//右の花の中心
  translate(285,250);
  rotate(-angle);
  noStroke();
  fill(46,57,240);
  ellipse(0,0,30,30);
  fill(133,137,191);
  ellipse(0,0,20,20);
  fill(255);
  
  for (int i = 0; i < 5; i++){  
    rotate(PI/2.5);
    triangle(0,-10,-2,0,2,0);
  }
  popMatrix();
  
  fill(227,180,110);
  textSize(13);
  text("Flowers dance for the sun",100,460);
  text("太陽をクリックしてみよう",100,480);
}

void nutriflower(){  //花（栄養３）
  strokeWeight(4);
  fill(25,126,26);
  stroke(25,126,26);
  line(250,350,250,250);
  ellipse(232,330,30,10);
  ellipse(268,330,30,10);
  ellipse(232,310,30,10);
  ellipse(268,310,30,10);
  ellipse(232,290,30,10);
  ellipse(268,290,30,10);
  ellipse(232,270,30,10);
  ellipse(268,270,30,10);

  pushMatrix();
  translate(250,250);
  rotate(-angle);
  noStroke();
  fill(46,57,240);
  ellipse(0,0,28,28);
  //ellipse();
  fill(255);
  for (int i = 0; i < 5; i++){
    rotate(PI/2.5);
    triangle(0,-10,-2.5,0,2.5,0);
  }
  popMatrix();
}

void waterflower(){  //花（水３）
  strokeWeight(4);
  fill(173,209,177);
  stroke(173,209,177);
  line(250,350,250,250);
  ellipse(230,315,30,10);
  ellipse(270,315,30,10);
  ellipse(230,280,30,10);
  ellipse(270,280,30,10);

  pushMatrix();
  translate(250,245);
  rotate(angle);
  noStroke();
  fill(147,152,211);
  ellipse(0,0,50,50);
  fill(255);
  for (int i = 0; i < 5; i++){
    rotate(PI/2.5);
    triangle(0,-15,-5,0,5,0);
  }
  popMatrix();
}

void normalflower(){  //花（水１栄養２）
  strokeWeight(4);
  fill(25,126,26);
  stroke(25,126,26);
  line(250,350,250,250);
  ellipse(230,330,30,10);
  ellipse(270,330,30,10);
  ellipse(230,310,30,10);
  ellipse(270,310,30,10);
  ellipse(230,290,30,10);
  ellipse(270,290,30,10);
  ellipse(230,270,30,10);
  ellipse(270,270,30,10);

  pushMatrix();
  translate(250,240);
  rotate(angle);
  noStroke();
  fill(46,57,240);
  ellipse(0,0,50,50);
  fill(255);
  for (int i = 0; i < 5; i++){
    rotate(PI/2.5);
    triangle(0,-15,-5,0,5,0);
  }
  popMatrix();
}

void sun(int psunr, int psung, int psunb){
  noStroke();
  fill(psunr,psung,psunb);
  ellipse(0,0,200,200);
  pushMatrix();
  for(int j = 0; j < 24; j++){   
    rotate(PI/12);
    rect(125,0,60,10);
  }
  popMatrix();
}

void sunray(int praywidth, int prayheight){
  stroke(247,119,55,30);
  noFill();
  strokeWeight(30);
  ellipse(0,0,praywidth,rayheight);//太陽の光１
  ellipse(0,0,praywidth+100,prayheight+100);
}

void sunray2(int praywidth2, int prayheight2){//太陽の光２
  stroke(234,51,14,30);
  noFill();
  strokeWeight(30);
  ellipse(0,0,praywidth2,prayheight2);
  ellipse(0,0,praywidth2+100,prayheight2+100);
  ellipse(0,0,praywidth2+200,prayheight2+200);
}

void pipe(){  //管
  noStroke();
  fill(224,211,186);
  rect(-2,389,250,22);
  rect(250,368,250,22);
}

void pipeshadow(){//管の影
  noStroke();
  fill(103,80,36,10);
  rect(-2,404,250,7);
  rect(250,383,250,7);
}

void cloud1(float pcloud1x, float pcloud1y){
  fill(255);
  noStroke();
  ellipse(pcloud1x+130,pcloud1y,260,20);
  ellipse(pcloud1x+90,pcloud1y-25,90,70);
  ellipse(pcloud1x+160,pcloud1y-25,120,70);
  ellipse(pcloud1x+140,pcloud1y-65,100,100);
  ellipse(pcloud1x+200,pcloud1y-45,70,70);
  ellipse(pcloud1x+190,pcloud1y-15,50,40);
}

void cloud2(float pcloud2x, float pcloud2y){
  fill(255);
  noStroke();
  ellipse(pcloud2x+125,pcloud2y,130,10);
  ellipse(pcloud2x+75,pcloud2y-8,20,20);
  ellipse(pcloud2x+95,pcloud2y-12,30,30);
  ellipse(pcloud2x+130,pcloud2y-15,52,40);
  ellipse(pcloud2x+160,pcloud2y-7,25,20);
}

/*statusという変数や、55から70行目のdrawの部分の書き方は
http://www2.kobe-u.ac.jp/~tnishida/misc/processing-state.htmlのコードを参考にし、一部変更して使いました。*/
