using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class corona_chan : MonoBehaviour
{
 int flg=0;int count=0;int spacewait=0;int level=0;
 int[] tx=new int[30];int[] ty=new int[30];int[] txx=new int[30];int[] tyy=new int[30];
 int[] tj=new int[30];int[] tc=new int[30];
 int[] window=new int[6];int[] wx=new int[6];int[] wy=new int[6];
 float[] timecf=new float[4];
 int i;int j;int z;int zx;int zy;int r;int ee;int a;int aa;int x;int y;int r1;int r2;
 int ii;int jj;int e1;int e2;int n1;int n2;int ax;int ay;int aax;int aay;int cx;int cy;
 int a1;int a2;int a3;int mx;int my;int mt;int mc;int mxx;int myy;
 int moves;int movex;int movey;int kazu;int temp;
 float stimef;float ntimef;
 string c1;string c2;string c3;string c4;
 string d;string d1;string d2;string savedata;string keyname;
 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[1600];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;AudioSource bgm4;
 AudioSource bgm5;AudioSource bgm6;AudioSource bgm7;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes-1"); // 0  ng-mes
  for (i=0;i<=5;i++) {obj[i+1]=GameObject.Find("coronachan-window-"+i);} // 1 - 6  window
  obj[7]=GameObject.Find("gameclear-mes-1"); // 7  gameclear-mes
  obj[8]=GameObject.Find("gameover-mes-1"); // 8  gameover-mes
  obj[9]=GameObject.Find("coronachan-title"); // 9  title
  obj[10]=GameObject.Find("coronachan-gekiback"); // 10  gekiback
  obj[11]=GameObject.Find("ya-r-32"); // 11  ya
  for (i=1;i<=4;i++) {obj[i+11]=GameObject.Find("coronachan-"+i);} // 12 - 15  coronachan-moto
  for (i=1;i<=4;i++) {obj[i+15]=GameObject.Find("coronachan-s-"+i);} // 16 - 19  coronachan-s-moto
  for (i=1;i<=10;i++) {obj[i+19]=GameObject.Find("coronachan-serifu-"+i);} // 20 - 29  geki-serifu
  for (i=1;i<=4;i++)
  {
   for (j=0;j<=29;j++) {obj[i*30+j+70]=Object.Instantiate(obj[i+11]) as GameObject;} // 100 - 219  chara-copy
  }
  for (i=1;i<=4;i++)
  {
   for (j=0;j<=3;j++) {obj[i*4+j+216]=Object.Instantiate(obj[i+15]) as GameObject;} // 220 - 235  geki-copy
  }
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();bgm4=obj[4].GetComponent();
  bgm5=obj[5].GetComponent();bgm6=obj[6].GetComponent();
  bgm7=obj[7].GetComponent();
  style1=new GUIStyle();
  style1.fontSize=80;
  style2=new GUIStyle();
  style2.fontSize=24;
  style3=new GUIStyle();
  style3.fontSize=24;style3.normal.textColor=Color.red; 
  c1=" 0";c2="24";c3="";c4="";
  wx[0]=-304;wy[0]=0;wx[1]=304;wy[1]=0;wx[2]=-150;wy[2]=204;
  wx[3]=150;wy[3]=204;wx[4]=-150;wy[4]=-204;wx[5]=150;wy[5]=-204;
  timecf[0]=32.2f;timecf[1]=16.2f;timecf[2]=11.2f;timecf[3]=8.4f;
  alldel();z=0;zx=0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update ()
 {
  if (flg==-1)
  {
   count=count-1;
   if (Input.GetMouseButtonDown(0)) {count=0;}
   if (Input.GetMouseButtonDown(1)) {count=0;}
   if (Input.GetKeyDown(KeyCode.Space)) {count=0;spacewait=2;}
   if (Input.GetKeyDown(KeyCode.Return)) {count=0;}
   if (count<=0)
   {
    titleput();
    flg=0;spacewait=2;
   }
  }
  if (flg==0)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8))
   {
    level=level-1;
    if (level<0) {level=4;}
   }
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
   {
    level=level+1;
    if (level>4) {level=0;}
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0)
    {
     z=9;zx=-1000;zy=0;objput();z=11;objput();
     if (level<=3) {gamestart();}
     else
     {
      stimef=Time.time;bgmstop();bgm5.Play();
      z=10;zx=0;zy=0;objput();z=20;zx=0;zy=-60;objput();z=220;zx=0;zy=38;objput();
      flg=10;count=0;
     }
    }
   }
   if (flg==0)
   {
    z=11;zx=-120;zy=100-level*40;objput();
   }
  }
  if (flg==1)
  {
// window-open-close
   moves=0;movex=0;movey=0;
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4)) {movex=1;}
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6)) {movex=2;}
   if (Input.GetKey(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) {movey=1;}
   if (Input.GetKey(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) {movey=2;}
   if (movex>0) {moves=movey*2+movex;}
   if (moves>0)
   {
    a=moves;
    if (window[a-1]==0) {window[a-1]=1;z=a;zx=-1000;zy=0;objput();}
    else
    {
     ee=0;
     for (i=0;i<=29;i++)
     {
      if (tx[i]>-999 && tj[i]==a+10) {ee=1;}
     }
     if (ee==0) {window[a-1]=0;z=a;zx=wx[a-1];zy=wy[a-1];objput();}
    }
   }
   moves=0;movex=0;movey=0;
// temp-up-down
   a=0;
   for (i=0;i<=5;i++)
   {
    if (window[i]>0) {a=a+1;}
   }
   if (a==0) {temp=temp+1;}
   if (a>=1) {temp=temp-a*4;}
   if (temp>2899) {temp=2899;}
   c2=(temp/100).ToString();
   if (temp<100)
   {
    z=8;zx=0;zy=0;objput();
    bgmstop();bgm6.Play();
    flg=99;count=-1;
   }
// coronachan-move
   for (i=0;i<=29;i++)
   {
    if (tx[i]>-999)
    {
     if (tj[i]>=11 && tj[i]<=16)
     {
      tx[i]=tx[i]+txx[i];ty[i]=ty[i]+tyy[i];tc[i]=tc[i]+1;
      if (tc[i]>=24) {tx[i]=-1000;ty[i]=0;tj[i]=0;}
     }
     if (tj[i]==1)
     {
      tx[i]=tx[i]+txx[i];ty[i]=ty[i]+tyy[i];
      r1=Random.Range(0,6);a1=4;a2=4;
      if (r1==1) {a1=1;}
      if (r1==2) {a1=2;}
      if (r1==3) {a1=3;}
      if (r1==4) {a2=1;}
      if (r1==5) {a2=2;}
      if (r1==6) {a2=3;}
      if (txx[i]<0 && tx[i]<=-280)
      {
       tx[i]=-280;txx[i]=a1;
       if (tyy[i]>0) {tyy[i]=a2;} else {tyy[i]=-a2;}
      }
      if (txx[i]>0 && tx[i]>=280)
      {
       tx[i]=280;txx[i]=-a1;
       if (tyy[i]>0) {tyy[i]=a2;} else {tyy[i]=-a2;}
      }
      if (tyy[i]<0 && ty[i]<=-180)
      {
       ty[i]=-180;tyy[i]=a1;
       if (txx[i]>0) {txx[i]=a2;} else {txx[i]=-a2;}
      }
      if (tyy[i]>0 && ty[i]>=180)
      {
       ty[i]=180;tyy[i]=-a1;
       if (txx[i]>0) {txx[i]=a2;} else {txx[i]=-a2;}
      }
      if (window[0]==1)
      {
       if (tx[i]>=-280 && tx[i]<=-260 && ty[i]>=-30 && ty[i]<=30)
       {tj[i]=11;txx[i]=-2;tyy[i]=0;tc[i]=0;}
      }
      if (window[1]==1)
      {
       if (tx[i]>=260 && tx[i]<=280 && ty[i]>=-30 && ty[i]<=30)
       {tj[i]=12;txx[i]=2;tyy[i]=0;tc[i]=0;}
      }
      if (window[2]==1)
      {
       if (tx[i]>=-180 && tx[i]<=-120 && ty[i]>=160 && ty[i]<=180)
       {tj[i]=13;txx[i]=0;tyy[i]=2;tc[i]=0;}
      }
      if (window[3]==1)
      {
       if (tx[i]>=120 && tx[i]<=180 && ty[i]>=160 && ty[i]<=180)
       {tj[i]=15;txx[i]=0;tyy[i]=2;tc[i]=0;}
      }
      if (window[4]==1)
      {
       if (tx[i]>=-180 && tx[i]<=-120 && ty[i]>=-180 && ty[i]<=-160)
       {tj[i]=13;txx[i]=0;tyy[i]=-2;tc[i]=0;}
      }
      if (window[5]==1)
      {
       if (tx[i]>=120 && tx[i]<=180 && ty[i]>=-180 && ty[i]<=-160)
       {tj[i]=16;txx[i]=0;tyy[i]=-2;tc[i]=0;}
      }
     }
     z=i+100;zx=tx[i];zy=ty[i];objput();
    }
   }
   kazu=0;
   for (i=0;i<=29;i++)
   {
    if (tx[i]>-999 && tj[i]>0) {kazu=kazu+1;}
   }
   c1=kazu.ToString();
   if (kazu<=0)
   {
    z=7;zx=0;zy=0;objput();
    bgmstop();bgm7.Play();
    flg=99;count=-1;
   }
// time-count
   ntimef=Time.time;
   if (ntimef-stimef>=timecf[level])
   {
    bgmstop();
    for (i=0;i<=29;i++)
    {
     if (tx[i]>-999)
     {
      if (tj[i]==1)
      {
       z=i+100;zx=-1000;zy=0;objput();
       z=i+130;zx=tx[i];zy=ty[i];objput();
       tj[i]=2;
      }
     }
    }
    flg=2;count=-1;
   }
  }
  if (flg==2)
  {
   count=count+1;
   if (count>=50)
   {
    for (i=0;i<=29;i++)
    {
     if (tx[i]>-999)
     {
      if (tj[i]==2)
      {
       z=i+130;zx=-1000;zy=0;objput();
       z=i+160;zx=tx[i];zy=ty[i];objput();z=i+190;zx=tx[i]-24;objput();
       tj[i]=3;kazu=kazu+1;
       ee=-1;
       for (j=0;j<=29;j++)
       {
        if (tx[j]<-999) {ee=j;break;}
       }
       if (ee>=0)
       {
        tx[ee]=tx[i];ty[ee]=ty[i];txx[ee]=txx[i]*-1;tyy[ee]=tyy[i]*-1;
        tj[ee]=1;tc[ee]=0;
       }
      }
     }
    }
    c1=kazu.ToString();
    if (kazu>30)
    {
     c1="";c4=kazu.ToString();
     z=8;zx=0;zy=0;objput();
     bgmstop();bgm6.Play();
     flg=99;count=-1;
    }
    else {flg=3;count=-1;}
   }
  }
  if (flg==3)
  {
   count=count+1;
   if (count>=50)
   {
    for (i=0;i<=29;i++)
    {
     if (tx[i]>-999)
     {
      if (tj[i]==3)
      {
       z=i+160;zx=-1000;zy=0;objput();z=i+190;objput();
       tj[i]=1;
      }
      if (tj[i]==1)
      {
       z=i+100;zx=tx[i];zy=ty[i];objput();
      }
     }
    }
    stimef=Time.time;bgmstop();
    if (level==0) {bgm1.Play();}
    if (level==1) {bgm2.Play();}
    if (level==2) {bgm3.Play();}
    if (level==3) {bgm4.Play();}
    flg=1;
   }
  }
  if (flg==10)
  {
   count=count+1;
   if (count%20==0)
   {
    z=220;zx=-1000;zy=0;objput();z=224;objput();
    if (count%40==0) {z=220;zx=0;zy=38;objput();}
    else {z=224;zx=0;zy=38;objput();}
   }
   if (count==320)
   {
    z=220;zx=-1000;zy=0;objput();z=224;objput();
    z=20;zx=-1000;zy=0;objput();z=21;zx=0;zy=-60;objput();
    flg=11;count=-1;
   }
  }
  if (flg==11)
  {
   count=count+1;
   if (count%20==0)
   {
    z=220;zx=-1000;zy=0;objput();z=224;objput();
    if (count%40==0) {z=220;zx=0;zy=38;objput();}
    else {z=224;zx=0;zy=38;objput();}
   }
   if (count==320)
   {
    z=220;zx=-1000;zy=0;objput();z=224;objput();
    z=21;zx=-1000;zy=0;objput();z=22;zx=0;zy=-60;objput();
    z=228;zx=0;zy=38;objput();
    flg=12;count=-1;
   }
  }
  if (flg==12)
  {
   count=count+1;
   if (count==80)
   {
    z=228;zx=-1000;zy=0;objput();
    z=22;zx=-1000;zy=0;objput();z=23;zx=0;zy=-60;objput();
    z=232;zx=0;zy=38;objput();
    flg=13;count=-1;
   }
  }
  if (flg==13)
  {
   count=count+1;
   if (count<=40) {a=count;} else {a=40;}
   z=232;zx=-a;zy=38;objput();z=233;zx=a;zy=38;objput();
   if (count==80)
   {
    z=232;zx=-1000;zy=0;objput();z=233;zx=-1000;zy=0;objput();
    z=23;zx=-1000;zy=0;objput();z=24;zx=0;zy=-60;objput();
    z=220;zx=-80;zy=38;objput();z=221;zx=80;zy=38;objput();
    flg=14;count=-1;
   }
  }
  if (flg==14)
  {
   count=count+1;
   if (count%20==0)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    if (count%40==0) {z=220;zx=-40;zy=38;objput();z=221;zx=40;zy=38;objput();}
    else {z=224;zx=-40;zy=38;objput();z=225;zx=40;zy=38;objput();}
   }
   if (count==320)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    z=24;zx=-1000;zy=0;objput();z=25;zx=0;zy=-60;objput();
    flg=15;count=-1;
   }
  }
  if (flg==15)
  {
   count=count+1;
   if (count%20==0)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    if (count%40==0) {z=220;zx=-40;zy=38;objput();z=221;zx=40;zy=38;objput();}
    else {z=224;zx=-40;zy=38;objput();z=225;zx=40;zy=38;objput();}
   }
   if (count==320)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    z=25;zx=-1000;zy=0;objput();z=26;zx=0;zy=-60;objput();
    flg=16;count=-1;
   }
  }
  if (flg==16)
  {
   count=count+1;
   if (count%20==0)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    if (count%40==0) {z=220;zx=-40;zy=38;objput();z=221;zx=40;zy=38;objput();}
    else {z=224;zx=-40;zy=38;objput();z=225;zx=40;zy=38;objput();}
   }
   if (count==320)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    z=26;zx=-1000;zy=0;objput();z=27;zx=0;zy=-60;objput();
    flg=17;count=-1;
   }
  }
  if (flg==17)
  {
   count=count+1;
   if (count%20==0)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    if (count%40==0) {z=220;zx=-40;zy=38;objput();z=221;zx=40;zy=38;objput();}
    else {z=224;zx=-40;zy=38;objput();z=225;zx=40;zy=38;objput();}
   }
   if (count==320)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    z=27;zx=-1000;zy=0;objput();z=28;zx=0;zy=-60;objput();
    flg=18;count=-1;
   }
  }
  if (flg==18)
  {
   count=count+1;
   if (count%10==0)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    if (count%20==0) {z=220;zx=-40;zy=38;objput();z=221;zx=40;zy=38;objput();}
    else {z=224;zx=-40;zy=38;objput();z=225;zx=40;zy=38;objput();}
   }
   if (count==320)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;zx=-1000;zy=0;objput();z=225;objput();
    z=28;zx=-1000;zy=0;objput();z=22;zx=0;zy=-60;objput();
    z=228;zx=-40;zy=38;objput();z=229;zx=40;zy=38;objput();
    flg=19;count=-1;
   }
  }
  if (flg==19)
  {
   count=count+1;
   if (count==80)
   {
    z=228;zx=-1000;zy=0;objput();z=229;zx=-1000;zy=0;objput();
    z=22;zx=-1000;zy=0;objput();z=23;zx=0;zy=-60;objput();
    z=232;zx=0;zy=38;objput();
    flg=20;count=-1;
   }
  }
  if (flg==20)
  {
   count=count+1;
   if (count<=40) {a=count*2;} else {a=80;}
   z=232;zx=-40;zy=38;objput();z=233;zx=40;zy=38;objput();
   z=234;zx=-40-a;zy=38;objput();z=235;zx=40+a;zy=38;objput();
   if (count==80)
   {
    z=232;zx=-1000;zy=0;objput();z=233;objput();z=234;objput();z=235;objput();
    z=23;zx=-1000;zy=0;objput();z=29;zx=0;zy=-60;objput();
    z=220;zx=-80;zy=38;objput();z=221;zx=80;zy=38;objput();
    flg=21;count=-1;
   }
  }
  if (flg==21)
  {
   count=count+1;
   if (count%20==0)
   {
    z=220;zx=-1000;zy=0;objput();z=221;objput();z=224;objput();z=225;objput();
    z=222;zx=-1000;zy=0;objput();z=223;objput();z=226;objput();z=227;objput();
    if (count%40==0)
    {
     z=220;zx=-40;zy=38;objput();z=221;zx=40;zy=38;objput();
     z=222;zx=-120;zy=38;objput();z=223;zx=120;zy=38;objput();
    }
    else
    {
     z=224;zx=-40;zy=38;objput();z=225;zx=40;zy=38;objput();
     z=226;zx=-120;zy=38;objput();z=227;zx=120;zy=38;objput();
    }
   }
   if (count==320)
   {
    titleput();flg=0;
   }
  }
  if (flg>=10 && flg<=21)
  {
   ntimef=Time.time;
   if (ntimef-stimef>=28f)
   {
    stimef=Time.time;bgm5.Play();
   }
  }
  if (flg==99)
  {
   count=count+1;
   if (count>=400)
   {
    titleput();
    count=0;flg=0;
   }
  }
 }

 void gamestart()
 {
  alldel();
  for (i=0;i<=29;i++) {tx[i]=-1000;ty[i]=0;txx[i]=0;tyy[i]=0;tj[i]=0;tc[i]=0;}
  for (i=0;i<=5;i++) {window[i]=0;z=i+1;zx=wx[i];zy=wy[i];objput();}
  if (level==0) {tx[0]=0;}
  if (level==1) {tx[0]=-40;tx[1]=40;}
  if (level==2) {tx[0]=-80;tx[1]=0;tx[2]=80;}
  if (level==3) {tx[0]=-120;tx[1]=-40;tx[2]=40;tx[3]=120;}
  for (i=0;i<=level*2+1;i++)
  {
   tx[i]=Random.Range(-280,280);
   ty[i]=Random.Range(-180,180);
   tj[i]=1;
   r1=Random.Range(0,3);txx[i]=1;tyy[i]=1;
   if (r1==1) {txx[i]=-1;}
   if (r1==2) {tyy[i]=-1;}
   if (r1==3) {txx[i]=-1;tyy[i]=-1;}
   r1=Random.Range(0,6);a1=4;a2=4;
   if (r1==1) {a1=1;}
   if (r1==2) {a1=2;}
   if (r1==3) {a1=3;}
   if (r1==4) {a2=1;}
   if (r1==5) {a2=2;}
   if (r1==6) {a2=3;}
   txx[i]=txx[i]*a1;tyy[i]=tyy[i]*a2;
  }
  kazu=level*2+2;temp=2499;
  c1=kazu.ToString();c2=(temp/100).ToString();c4="";
  stimef=Time.time;bgmstop();
  if (level==0) {bgm1.Play();}
  if (level==1) {bgm2.Play();}
  if (level==2) {bgm3.Play();}
  if (level==3) {bgm4.Play();}
  flg=1;
 }

 void titleput()
 {
  alldel();z=9;zx=0;zy=0;objput();z=11;zx=-120;zy=100;objput();
  c1="0";c2="24";c4="";
  for (i=0;i<=5;i++) {z=i+1;zx=wx[i];zy=wy[i];objput();}
  bgmstop();spacewait=2;flg=0;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (930,96,100,30),c1,style2);
  GUI.Label (new Rect (930,118,100,30),c2,style2);
  GUI.Label (new Rect (540,470,100,30),c3,style1);
  GUI.Label (new Rect (930,96,100,30),c4,style3);
 }

 void objput()
 {
  Vector3 pos=obj[z].transform.position;
  pos.x=zx*0.01f;pos.y=zy*0.01f;
  obj[z].transform.position=pos;
 }

 void bgmstop()
 {
  bgm1.Stop();bgm2.Stop();bgm3.Stop();bgm4.Stop();bgm5.Stop();bgm6.Stop();bgm7.Stop();
 }

 void alldel()
 {
  z=0;zx=-2000;zy=0;objput();
  for (i=1;i<=29;i++) {z=i;zx=-1000;zy=0;objput();}
  for (i=100;i<=235;i++) {z=i;zx=-1000;zy=0;objput();}
 }
}