[Processing]誕生日ケーキアニメーション

#こんな
![HappyBirthdayCake.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/554244/354ab7c7-25e1-26e6-670a-40933c8a5831.gif)

#プログラム
今見返すとかなり雑な感じに書いてるなーと思いつつ
でも下手に書き換えると動かなくなるはずだから修正はしない方向で
（友達祝うために当日急遽4時間で作った作品）

```Cake.java
//キーボード入力受けたら開始

Cake cake = new Cake(200,100);
Cake cake2 = new Cake(130,100);
Cake[] candles = new Cake[10];
Cake[] candles2 = new Cake[10];
Cake candle = new Cake(5,100);

float[] canX = new float[10];
float[] canZ = new float[10];
float[] radX = new float[361];
float[] radY = new float[361];
int berNum = 0;
int berNum2 = 0;
int timer = 0;
float rotate = 0;
boolean light = true;
boolean start = false;

void setup(){
  //size(1800,900,P3D);
  fullScreen(P3D);
  frameRate(30);
  noStroke();
  textAlign(CENTER);
  
  
  for(int i=0;i<canX.length;i++){
    float a = radians(i * 36);
    canX[i] = cos(a);
    canZ[i] = sin(a);
  }
  for(int i=0;i<=360;i++){
    float rad = radians(i);
    radY[i] = cos(rad);
    radX[i] = (0.72f + 0.1f * cos(rad)) * sin(rad);
  }
  candle.r = 360;
  for(int i=0;i<candles.length;i++){
    candles[i] = new Cake(5,50);
    candles[i].r = 360;
    candles2[i] = new Cake(5,50);
    candles2[i].r = 360;
  }
}//setup end }

void draw(){
  if(light){
    background(100,100,255);
    perspective();
    lights();
    if(!start) return ;
  
    pushMatrix();
      translate(width/2,height/2);
      rotateX(-10);
      rotateY(rotate);
      fill(255);
      cake.Caker();
      pushMatrix();
        translate(0,cake.hei/2 + cake2.hei/2);
        cake2.Caker();
      popMatrix();
      
      pushMatrix(); //ろうそくたち
        fill(255,255,0);
        if(berNum2 == 360){
          translate(0,cake.hei - candles[0].hei+candles[0].h/2);
          for(int i=0;i<candles.length;i++){
            pushMatrix();
              translate(canX[i] * (cake.radius/4f)*3,0,canZ[i] *(cake.radius/4f)*3);
              candles[i].Caker();
            popMatrix();
          }
        }
        if(candles[0].h == candles[0].hei){
          translate(0,cake2.hei-candles[0].h/2 + candles2[0].h/2);
          for(int i=0;i<candles2.length;i++){
            pushMatrix();
              translate(canX[i]* (cake2.radius/4f)*3,0,canZ[i]* (cake2.radius/4f)*3);
              candles2[i].Caker();
            popMatrix();
          }
        }
        if(candles2[0].h == candles[0].hei){
          fill(220,0,0);
          translate(0,candle.h/2 - candles2[0].h/2);
          candle.Caker();
        }
      popMatrix();//ろうそくたち end
      
      pushMatrix(); //ベリーたち
        fill(255,0,0);
        translate(0,cake.hei/2 + 10);
        for(int i=0;i<berNum/20;i++){
          float rad = radians(i * 20);
          pushMatrix();
            translate(cos(rad) * (cake.radius - 10),0,sin(rad) * (cake.radius - 10));
            rotateY(-rotate);
            StBerry();
          popMatrix();
        }
        translate(0,cake2.hei);
        for(int i=0;i<berNum2/20;i++){
          float rad = radians(i * 20);
          pushMatrix();
            translate(cos(rad) * (cake2.radius - 10),0,sin(rad) * (cake2.radius - 10));
            rotateY(-rotate);
            StBerry();
          popMatrix();
        }
      popMatrix(); //ベリーたち
      
      pushMatrix(); //ほのお
      if(candle.h == candle.hei && light){
        fill(255,100,0);
        translate(0,10 + cake.hei/2 + candles[0].hei);
        for(int i=0;i<candles.length;i++){
          pushMatrix();
            translate(canX[i] * (cake.radius/4f)*3,0,canZ[i] *(cake.radius/4f)*3);
            rotateY(-rotate);
            StBerry();
          popMatrix();
        }
        translate(0,cake2.hei/2 + candles[0].hei);
        for(int i=0;i<candles2.length;i++){
          pushMatrix();
            translate(canX[i]* (cake2.radius/4f)*3,0,canZ[i]* (cake2.radius/4f)*3);
            rotateY(-rotate);
            StBerry();
          popMatrix();
        }
        translate(0,candle.hei/2);
        rotateY(-rotate);
        StBerry();
      }
      popMatrix(); //ほのお
    popMatrix(); //translate(width/2,height/2); end
  
    if(cake.h < cake.hei){
      cake.h ++;
    }
    else if(cake.r < 360){
      cake.r++;
    }
    else if(cake2.h < cake2.hei){
      cake2.h++;
    }
    else if(cake2.r < 360){
      cake2.r++;
    }
    else if(berNum < 360){
      berNum++;
    }
    else if(berNum2 < 360){
      berNum2++;
    }
    else if(candles[0].h < candles[0].hei){
      for(Cake c : candles){
        c.h++;
      } 
    }
    else if(candles2[0].h < candles2[0].hei){
      for(Cake c : candles2){
        c.h++;
      }
    }
    else if(candle.h < candle.hei){
      candle.h++;
      timer = millis()/1000 + 3;
    }
    else if(millis()/1000 < timer){
      light = false;
    }
    else{
      light = true;
      textSize(50);
      fill(255,0,0);
      text("HAPPY BIRTHDAY!!\n",width/2,50);
      fill(255,255,0);
      text("HAPPY BIRTHDAY!!\n",width/2 - 5,50);
      rotate += 0.01f;
    }
  }//if(light){
  else {
    background(0);
    if(millis()/1000 >= timer) light = true;
  }
}

class Cake {
  float radius = 0;
  float hei;
  float h = 0;
  float r = 0;
  Cake(float rad , float hei){
    radius = rad;
    this.hei = hei;
    r = 0;
  }
  
  //円柱メソッド
  void Caker(){
    
    float x=0,y=0,z=0;
    
    pushMatrix();
    
      //上面と底面
      for(int i = -1;i<=1;i++){
        if(i == 0) continue;
        
        beginShape(TRIANGLE_FAN);
          y = i * h / 2;
          vertex(0, y, 0);
          for(int deg = 0; deg <= r; deg ++){
            x = cos(radians(deg)) * radius;
            z = sin(radians(deg)) * radius;
            vertex(x, y, z);
          }
        endShape(); 
        
      }
  
      //側面
      beginShape(TRIANGLE_STRIP);
        for(int deg = 0; deg <= r; deg ++){
          x = cos(radians(deg)) * radius;
          y = -h / 2;
          z = sin(radians(deg)) * radius;
          vertex(x, y, z);
          
          y = h / 2;
          vertex(x, y, z);
        }
      endShape();
      if(r < 360){
        pushMatrix();
         translate(0,-hei/2+h/2);
          beginShape(QUAD_STRIP);
            vertex(radius,h/2,0);
            vertex(radius,-h/2,0);
            vertex(0,h/2,0);
            vertex(0,-h/2,0);
            if(r>0){
              vertex(x,h/2,z);
              vertex(x,-h/2,z);
            }
          endShape();
        popMatrix();
      }
    popMatrix();
  }//   Caker end }
  
} // Cake end }

void StBerry(){StBerry(1);}
void StBerry(float ratio){
  pushMatrix();
    scale(10 * ratio);
    rotateZ(PI);
    beginShape(TRIANGLE_FAN);
      for(int i=0;i<radX.length;i++){
        vertex(radX[i],radY[i]);
      }
    endShape();
  popMatrix();
}

void mousePressed(){
  start = true;
}
```