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

public class akumade_tsubo : MonoBehaviour
{
 int flg=-1;int count=0;int spacewait=0;int level=0;int plnum=0;int score=0;int times=0;int wait=0;
 int movewait=0;int getwait=0;int coinwait=0;int cgnum=-1;int hpwait=20;int patowait=100;int outflg=0;
 int px=0;int py=0;int nowmove;int puyo;
 int[] hx=new int[48];int[] hy=new int[48];int[] ht=new int[48];int[] hhp=new int[48];
 int[] tx=new int[10];int[] ty=new int[10];int[] tm=new int[10];
 int[] moveok=new int[5];int[] movepts=new int[3];int[] mgwait=new int[3];int[] dmg=new int[5];
 int i;int j;int k;int z;int zx;int zy;int r;int ee;int a;int aa;int aaa;int x;int y;int r1;int r2;int ax;int ay;
 int xx;int yy;int ii;int jj;int kk;
 int e;int e1;int a1;int a2;int moves;int mousex;int mousey;
 float stimef;float ntimef;float stime2f;float ntime2f;float stime3f;float ntime3f;float xf;float yf;float af;
 float stime4f;float ntime4f;
 string c1;string c2;string c3;string c4;
 string d1;string d2;string d3;string datas;

 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[2000];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;
 AudioSource bgm4;AudioSource bgm5;AudioSource bgm6;

 void Start()
 {
  style1=new GUIStyle();style1.fontSize=40;
  style2=new GUIStyle();style2.fontSize=16;
  style3=new GUIStyle();style3.fontSize=24;style3.normal.textColor=Color.red;
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  for (i=1;i<=12;i++) {obj[i]=GameObject.Find("tsubo-"+i);} // 1-12  tsubo
  for (i=1;i<=16;i++) {obj[i+12]=GameObject.Find("house-"+i);} // 13-28  house-moto
  obj[29]=GameObject.Find("tsubo-meter"); // 29  meter-moto
  obj[30]=GameObject.Find("pato"); // 30  pato-moto
  for (i=1;i<=3;i++) {obj[i+30]=GameObject.Find("tsubo-"+i+"-t");} // 31-33  tsubo-select
  obj[34]=GameObject.Find("tsubo-title"); // 34  title
  obj[35]=GameObject.Find("ya-r-32x32"); // 35  ya
  obj[36]=GameObject.Find("tsubo-waku"); // 36  waku
  for (i=1;i<=3;i++) {obj[i+36]=GameObject.Find("tsubo-info-"+i);} // 37-39  tsubo-select
  obj[40]=GameObject.Find("coin"); // 40  coin
  obj[41]=GameObject.Find("button-esc"); // 41  button-esc
  obj[42]=GameObject.Find("gameend-mes-1"); // 42  gameend-blue
  obj[43]=GameObject.Find("gameend-mes-3"); // 43  gameend-yellow
  obj[44]=GameObject.Find("gameover-mes-1"); // 44  gameover-blue
  obj[45]=GameObject.Find("gameover-mes-3"); // 45  gameover-yellow
  for (i=1;i<=5;i++) {obj[i+45]=GameObject.Find("tsubo-back-"+i);} // 46-50  back-moto
  for (i=0;i<=9;i++) {obj[i+51]=Object.Instantiate(obj[30]) as GameObject;} // 51 - 60  pato-copy
  for (i=0;i<=10;i++)
  {
   obj[i+100]=GameObject.Find("num-"+i); // 100-110  nums-moto
   for (j=0;j<=4;j++) {obj[j*11+i+111]=Object.Instantiate(obj[i+100]) as GameObject;} // 111-165  nums-copy
  }
  alldel();
  for (i=0;i<=7;i++)
  {
   for (j=0;j<=13;j++)
   {
    if (i%2==0)
    {
     if (j==4 || j==9) {a=48;} else {a=46;}
    }
    if (i==1 || i==3 || i==5)
    {
     if (j==4 || j==9) {a=49;} else {a=47;}
    }
    if (i==7)
    {
     if (j==4 || j==9) {a=50;} else {a=47;}
    }
    obj[i*14+j+180]=Object.Instantiate(obj[a]) as GameObject; // 180 - 291  back-copy
    z=i*14+j+180;zx=j*54-351;zy=189-i*54;objput();
   }
  }
  for (i=1;i<=16;i++)
  {
   for (j=0;j<=11;j++)
   {
    obj[i*12+j+288]=Object.Instantiate(obj[i+12]) as GameObject; // 300 - 491  house-copy
   }
  }
  for (i=0;i<=47;i++) {obj[i+492]=Object.Instantiate(obj[29]) as GameObject;} // 492 - 539  meter-copy
  housereset();
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();bgm3=obj[3].GetComponent();
  bgm4=obj[4].GetComponent();bgm5=obj[5].GetComponent();bgm6=obj[6].GetComponent();
  movepts[0]=54;movepts[1]=27;movepts[2]=18;mgwait[0]=20;mgwait[1]=10;mgwait[2]=30;
  z=0;zx=0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update ()
 {
  Vector3 mpos=Input.mousePosition;
  xf=mpos.x;yf=mpos.y;mousex=(int)xf;mousey=(int)yf;
//  c1="X="+mousex.ToString()+" Y="+mousey.ToString();
  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();
   }
  }
  if (flg==0)
  {
   aa=0;
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) {level=level-1;if (level<0) {level=3;}}
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) {level=level+1;if (level>3) {level=0;}}
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4)) {plnum=plnum-1;if (plnum<0) {plnum=2;}}
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6)) {plnum=plnum+1;if (plnum>2) {plnum=0;}}
   if (Input.GetKeyDown(KeyCode.Space) && spacewait<=0) {gamestart();}
   if (Input.GetMouseButtonDown(0) && spacewait<=0)
   {
    if (mousex>=490 && mousex<=789)
    {
     if (mousey>=478 && mousey<=517) {level=0;gamestart();}
     if (mousey>=438 && mousey<=477) {level=1;gamestart();}
     if (mousey>=398 && mousey<=437) {level=2;gamestart();}
     if (mousey>=358 && mousey<=397) {level=3;gamestart();}
    }
    if (mousey>=292 && mousey<=345)
    {
     if (mousex>=546 && mousex<=599) {plnum=0;}
     if (mousex>=613 && mousex<=666) {plnum=1;}
     if (mousex>=680 && mousex<=733) {plnum=2;}
    }
   }
   if (flg==0)
   {
    for (i=37;i<=39;i++) {z=i;zx=-1000;zy=0;objput();}
    z=35;zx=-102;zy=138-level*40;objput();z=36;zx=plnum*67-67;zy=-41;objput();
    z=plnum+37;zx=188;zy=-18;objput();
   }
  }
  if (flg==1)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
// input
   nowmove=0;
   if (movewait<=0)
   {
    if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Keypad4)) {moves=1;}
    if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.Keypad6)) {moves=2;}
    if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Keypad8)) {moves=3;}
    if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.Keypad2)) {moves=4;}
   }
   for (i=1;i<=4;i++) {moveok[i]=1;}
   if (px<=-351) {moveok[1]=0;}
   if (px>=351) {moveok[2]=0;}
   if (py!=135 && py!=27 && py!=-81 && py!=-189) {moveok[1]=0;moveok[2]=0;}
   if (py>=189) {moveok[3]=0;}
   if (py<=-189) {moveok[4]=0;}
   if (px!=-135 && px!=135) {moveok[3]=0;moveok[4]=0;}
   if (movewait>0) {movewait=movewait-1;}
   if (movewait<=0)
   {
    if (moves==1 && moveok[1]==1) {px=px-movepts[plnum];nowmove=1;}
    if (moves==2 && moveok[2]==1) {px=px+movepts[plnum];nowmove=1;}
    if (moves==3 && moveok[3]==1) {py=py+movepts[plnum];nowmove=1;}
    if (moves==4 && moveok[4]==1) {py=py-movepts[plnum];nowmove=1;}
   }
// player-move
   if (nowmove>0)
   {
    for (i=1;i<=12;i++) {z=i;zx=-1000;zy=0;objput();}
    a1=0;
    if (puyo==1) {a1=3;}
    if (puyo==3) {a1=6;}
    z=plnum+a1+1;zx=px;zy=py;objput();
    getwait=mgwait[plnum];moves=0;movewait=score/10+5;
    if (coinwait<=0) {cgnum=-1;}
   }
// coin-get
   if (cgnum>=0)
   {
    if (coinwait>0)
    {
     coinwait=coinwait-1;
     if (coinwait<=0)
     {
      if (px==hx[cgnum])
      {
       score=score+1;scoreput();bgm5.Play();
      }
      z=40;zx=-1000;zy=0;objput();getwait=mgwait[plnum]+1;
      if (px!=hx[cgnum] || hhp[cgnum]<=0) {cgnum=-1;getwait=0;coinwait=0;}
     }
     else {z=40;zx=hx[cgnum];zy=hy[cgnum]+coinwait*3-54;objput();}
    }
// coin-output
    if (getwait>0)
    {
     getwait=getwait-1;
     if (getwait<=0)
     {
      hhp[cgnum]=hhp[cgnum]-dmg[ht[cgnum]];
      if (hhp[cgnum]<=0)
      {
       hhp[cgnum]=0;houseput();bgm6.Play();
      }
      a=(100-hhp[cgnum])/4;af=(float)hhp[cgnum]/100;z=492+cgnum;scaleset();
      z=40;zx=hx[cgnum];zy=hy[cgnum]-14;objput();
      getwait=0;coinwait=20;
     }
    }
   }
// target
   if (cgnum<0 && coinwait<=0)
   {
    ee=-1;
    for (i=0;i<=47;i++)
    {
     if (px==hx[i] && py==hy[i]-54 && hhp[i]>0) {ee=i;break;}
    }
    if (ee>=0) {getwait=mgwait[plnum];cgnum=ee;}
   }
   if (hpwait>0) {hpwait=hpwait-1;}
// house-hp-recover
   if (hpwait<=0)
   {
    for (i=0;i<=47;i++)
    {
     if (hhp[i]>0 && hhp[i]<100)
     {
      hhp[i]=hhp[i]+2;
      a=(100-hhp[i])/4;af=(float)hhp[i]/100;z=i+492;scaleset();
     }
    }
    hpwait=20;
   }
// pato-move
   outflg=0;
   for (i=0;i<=9;i++)
   {
    if (tx[i]>-999)
    {
     tx[i]=tx[i]+tm[i]*4;
     if (tm[i]>0 && tx[i]>=405) {tx[i]=-1000;}
     if (tm[i]<0 && tx[i]<=-405) {tx[i]=-1000;}
     z=i+51;zx=tx[i];zy=ty[i];objput();
     if (px-tx[i]>=-40 && px-tx[i]<=40 && py-ty[i]>=-36 && py-ty[i]<=18) {outflg=1;}
    }
   }
   if (outflg>0)
   {
    puyo=0;houseput();
    for (i=1;i<=12;i++) {z=i;zx=-1000;zy=0;objput();}
    z=plnum+10;zx=px;zy=py;objput();
    bgmstop();bgm3.Play();
    z=44;zx=0;zy=0;objput();
    flg=9;stime4f=Time.time;count=0;
   }
   else
   {
    if (patowait>0) {patowait=patowait-1;}
    if (patowait<=0) {newpato();}
   }
  }
  if (flg==1 || flg==2)
  {
// times
   ntimef=Time.time;
   if (plnum<2)
   {
    if (ntimef-stimef>=40f && times>0) {bgm1.Play();stimef=Time.time;}
   }
   else
   {
    if (ntimef-stimef>=37f && times>0) {bgm2.Play();stimef=Time.time;}
   }
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f)
   {
    times=times-1;if (times<0) {times=0;}
    timeput();stime2f=stime2f+1f;
   }
   if (flg==1 && times<=0)
   {
    puyo=0;houseput();
    for (i=1;i<=12;i++) {z=i;zx=-1000;zy=0;objput();}
    z=plnum+1;zx=px;zy=py;objput();
    z=40;zx=-1000;zy=0;objput();
    for (i=51;i<=60;i++) {z=i;zx=-1000;zy=0;objput();}
    bgmstop();bgm4.Play();
    z=42;zx=0;zy=0;objput();
    flg=8;stime4f=Time.time;count=0;
   }
  }
  if (flg==1 || flg==2)
  {
   ntime3f=Time.time;
   if (ntime3f-stime3f>=0.5f)
   {
    puyo=puyo+1;if (puyo>=4) {puyo=0;}
    for (i=1;i<=12;i++) {z=i;zx=-1000;zy=0;objput();}
    a1=0;
    if (puyo==1) {a1=3;}
    if (puyo==3) {a1=6;}
    z=plnum+a1+1;zx=px;zy=py;objput();
    houseput();
    stime3f=stime3f+0.5f;
   }
  }
  if (flg>0)
  {
   if (Input.GetMouseButtonDown(0))
   {
    if (mousex>=1024 && mousex<=1094 && mousey>=158 && mousey<=188) {titleput();}
   }
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();}
  }
  if (flg==8 || flg==9)
  {
   ntime4f=Time.time;
   if (ntime4f-stime4f>=0.5f)
   {
    stime4f=Time.time;count=count+1;
    if (flg==8) {a=42;} else {a=44;}
    a=a+count%2;
    for (i=42;i<=45;i++) {z=i;zx=-1000;zy=0;objput();}
    z=a;zx=0;zy=0;objput();
    if (count>=12) {titleput();}
   }
  }
 }

 void gamestart()
 {
  alldel();
  score=0;times=120;scoreput();timeput();
  px=-135;py=27;z=plnum+1;zx=px;zy=py;objput();
  z=41;zx=420;zy=-200;objput();
  moves=0;nowmove=0;getwait=0;movewait=0;coinwait=0;cgnum=-1;hpwait=20;
  patowait=(4-level)*50;
  housereset();
  dmg[1]=10;dmg[2]=20;dmg[3]=30;dmg[4]=40;
  for (i=0;i<1000;i++)
  {
   r1=Random.Range(1,5);if (r1>4) {r1=4;}
   r2=Random.Range(1,5);if (r2>4) {r2=4;}
   a=dmg[r1];dmg[r1]=dmg[r2];dmg[r2]=a;
  }
  for (i=0;i<=9;i++)
  {
   tx[i]=-1000;ty[i]=0;tm[i]=0;
  }
  stimef=Time.time;stime2f=Time.time;stime3f=Time.time;puyo=0;
  if (plnum<2) {bgmstop();bgm1.Play();} else {bgmstop();bgm2.Play();}
  flg=1;spacewait=2;
 }

 void newpato()
 {
  ee=-1;
  for (i=0;i<=9;i++)
  {
   if (tx[i]<-999) {ee=i;break;}
  }
  if (ee>=0)
  {
   r1=Random.Range(1,3);if (r1>2) {r1=2;}
   if (px<=-189) {r1=2;}
   if (px>=189) {r1=1;}
   r2=Random.Range(0,4);if (r2>3) {r2=3;}
   if (r1==1) {tx[ee]=-406;ty[ee]=r2*108-166;tm[ee]=1;}
   else {tx[ee]=406;ty[ee]=r2*108-189;tm[ee]=-1;}
   patowait=(4-level)*50;
  }
 }

 void houseput()
 {
  for (i=300;i<=443;i++) {z=i;zx=-1000;zy=0;objput();}
  a1=0;
  if (puyo==1) {a1=48;}
  if (puyo==3) {a1=96;}
  for (i=0;i<=3;i++)
  {
   aa=i+1;
   for (j=0;j<=2;j++)
   {
    for (k=0;k<=3;k++)
    {
     a=i*12+j*4+k;
     z=a1+aa*12+i*3+j+288;if (hhp[a]<=0) {z=aa*12+i*3+j+432;}
     zx=hx[a];zy=hy[a];objput();
     aa=aa+1;if (aa>=5) {aa=1;}
    }
   }
  }
 }

 void scoreput()
 {
  if (score>999) {score=999;}
  for (ii=100;ii<=132;ii++) {z=ii;zx=-1000;zy=0;objput();}
  a=score/100;if (score<100) {a=10;}
  z=a+100;zx=350;zy=250;objput();
  a=(score%100)/10;if (score<10) {a=10;}
  z=a+111;zx=362;zy=250;objput();
  a=score%10;
  z=a+122;zx=374;zy=250;objput();
 }

 void timeput()
 {
  if (times<0) {times=0;}
  for (ii=133;ii<=165;ii++) {z=ii;zx=-1000;zy=0;objput();}
  a=times/100;if (times<100) {a=10;}
  z=a+133;zx=350;zy=230;objput();
  a=(times%100)/10;if (times<10) {a=10;}
  z=a+144;zx=362;zy=230;objput();
  a=times%10;
  z=a+155;zx=374;zy=230;objput();
 }

 void housereset()
 {
  for (i=0;i<=3;i++)
  {
   aa=i+1;
   for (j=0;j<=2;j++)
   {
    for (k=0;k<=3;k++)
    {
     a=i*12+j*4+k;ht[a]=aa;hhp[a]=100;
     hx[a]=(j*5+k)*54-351;hy[a]=189-i*108;
     z=aa*12+i*3+j+288;zx=hx[a];zy=hy[a];objput();
     z=i*12+j*4+k+492;objput();
     aa=aa+1;if (aa>=5) {aa=1;}
    }
   }
  }
  for (i=0;i<=47;i++)
  {
   a=0;af=1f;z=492+i;scaleset();
  }
 }

 void scaleset()
 {
  Vector3 scales=obj[z].transform.localScale;
  scales.x=af;
  obj[z].transform.localScale=scales;
  zx=hx[z-492]-a;if (af<=0) {zx=-1000;}
  zy=hy[z-492];objput();
 }

 void titleput()
 {
  alldel();
  for (i=300;i<=539;i++) {z=i;zx=-1000;zy=0;objput();}
  housereset();
  z=34;zx=0;zy=0;objput();z=31;zx=-67;zy=-39;objput();z=32;zx=0;objput();z=33;zx=67;objput();
  z=35;zx=-102;zy=138-level*40;objput();z=36;zx=plnum*67-67;zy=-41;objput();
  z=plnum+37;zx=188;zy=-18;objput();
  bgmstop();spacewait=2;flg=0;count=0;puyo=0;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (900,280,100,30),c1,style2);
  GUI.Label (new Rect (900,350,100,30),c2,style2);
  GUI.Label (new Rect (330,200,100,30),c3,style1);
  GUI.Label (new Rect (530,300,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();
 }

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