//ブロックカウントゲーム

//ブロックカウントと落ちてくるブロックのクラスを宣言
BlockQuiz blockQuiz;
Block[] block = new Block[40];


void blockSetup() {
  norma = 5;
  blockQuiz = new BlockQuiz();
  menu++;
}


void blockCountQuiz() {
  blockQuiz.display();
}


//ブロックカウントゲームのクラス
class BlockQuiz {
  boolean isDown = false; //trueにするとブロックが落ちてくる
  boolean isAnswer = false; //正解ならtrue
  boolean isEffect = false; //trueにするとエフェクトを表示する
  int effectTime = 0;
  int blockNumber; //落ちてくるブロックの個数
  int type; //問題の種類 0:□をカウント 1:△をカウント 2:○をカウント
  int[] counter = {
    0, 0, 0, 0
  };//□△○の数と回答をカウント

  BlockQuiz() {
    standby();
  }

  void standby() {
    //難易度の設定
    float degree = random(10);
    //難易度によってブロックの個数を決定
    if (degree <= 3) blockNumber = int(random(5, 12));
    else if (degree <= 6) blockNumber = int(random(8, 16));
    else if (degree <= 8) blockNumber = int(random(10, 20));
    else if (degree <= 9) blockNumber = int(random(14, 24));
    else if (degree <= 10) blockNumber = int(random(20, 30));
    //ブロックのクラスを宣言
    for (int i = 0; i < blockNumber; i++) {
      block[i] = new Block();
      counter[block[i].type]++;
    }
    type = int(random(2.99)); //問題の種類を設定
  }

  void display() {
    //ブロックが落ちてくる画面
    if (isDown) {
      for (int i = 0; i < blockNumber;i++) {
        block[i].display(); //ブロック描画
      }

      fill(#01DFD7);
      stroke(0);
      strokeWeight(2);
      rect(width/2, height*0.1, width/5, height*0.1); 

      fill(0);
      textSize(normal);
      text(counter[3], width/2, height*0.1);

      //カウント、決定ボタン描画
      fill(255);
      stroke(0);
      strokeWeight(5);
      rect(width/4, height*0.95, width/2, height*0.1);
      rect(width*3/4, height*0.95, width/2, height*0.1);
      fill(0);
      textSize(normal);
      text("カウント", width/4, height*0.95, width/2, height*0.1);
      text("決定", width*3/4, height*0.95, width/2, height*0.1);

      if (isEffect) effect();
    }

    //ブロック落下前
    else {
      textSize(king);
      if (type == 0) {
        fill(#ff0000);
        text("□", width/2, height/2);
      }
      if (type == 1) {
        fill(#01DF01);
        text("△", width/2, height/2);
      }
      if (type == 2) {
        fill(#0000ff);
        text("○", width/2, height/2);
      }   
      textSize(small);
      fill(0);
      text("この図形の個数をカウント!", width/2, height*0.7);
    }
  }

  boolean judgement() {
    if (counter[3] == counter[type]) return true;
    return false;
  }

  void effect() {
    textSize(big);
    if (isAnswer) { //正解の場合
      noFill();
      stroke(255, 0, 0);
      strokeWeight(20);
      ellipse(width/2, height/2, width*0.8, width*0.8);
    }
    else {//不正解の場合
      stroke(0, 0, 255);
      strokeWeight(20);
      line(width*0.2, height*0.2, width*0.8, height*0.8);
      line(width*0.8, height*0.2, width*0.2, height*0.8);
    }

    //エフェクトの表示時間調整
    effectTime++;
    if (effectTime >= 60){
      if(norma == 0) menu++;
      else blockQuiz = new BlockQuiz();
    }
  }

  void mouseReleased() {
    if (isDown && isEffect == false) {
      if (mouseY>height*0.9) {
        //「カウント」
        if (mouseX < width/2) counter[3]++;
        //「OK」
        else {
          isAnswer = judgement();
          if(isAnswer) norma--;
          isEffect = true;
        }
      }
    }
    else isDown = true;
  }
}


//ブロック
class Block {
  float x = random(width*0.1, width*0.9); //x座標、画面内にランダム
  float y = random(height*(-0.5), height*(-0.2)); //y座標、画面より上にランダム
  int type = int(random(2.99)); //種類、0:□、1:△、2:○
  color blockColor = color(random(0, 220), random(0, 220), random(0, 220)); //RGBランダム
  float speed = random(height*0.008, height*0.02); //落下スピード
  boolean isDown = false; //trueだとブロック落下

  void display() {
    if (isDown == false) {
      if (random(20) < 1) isDown = true; //ランダムで落下開始
    }
    stroke(198);
    strokeWeight(2);
    fill(blockColor);
    if (type == 0) rect(x, y, 40, 40);
    if (type == 1) triangle(x, y, x+20, y+30, x-20, y+30);
    if (type == 2) ellipse(x, y, 40, 40);
    if (isDown) y+=speed;
  }
}