using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class screensize
{
 [RuntimeInitializeOnLoadMethod]
 static void OnRuntimeMethodLoad()
 {
  Screen.SetResolution(1280,720,false);
 }
}
public class kogitte : MonoBehaviour
{
 int flg=-1;int plnum=0;
 int count=0;int wait=0;int spacewait=0;int level=0;int score=0;int times=0;int outflg=0;
 int px;int py;int lpm=0;int pmcount=0;int shield=0;int kgt;int puyo=0;int missilewait=0;
 int[] spd=new int[3] {20,15,10};int[] maxkg=new int[3] {15,20,10};
 int[] kgx=new int[20];int[] kgy=new int[20];int[] kais=new int[5];
 int[] mx=new int[20];int[] my=new int[20];int[] mj=new int[20];int[] mw=new int[20];
 int[] shw=new int[5];int[] shy=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 a1;int a2;int a3;int a4;
 int e;int e1;int moves;int shot;int mousex;int mousey;
 float pxf;float pxxf;float pyf;float pyyf;
 float stimef;float ntimef;float stime2f;float ntime2f;float stime3f;float ntime3f;float stime4f;float ntime4f;
 float xf;float yf;float af;float kakuf;float axf;float ayf;float a1f;float a2f;
 string c1;string c2;string c3;string c4;
 string d;string dd;string d1;string d2;string d3;string d4;
 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;style1.normal.textColor=Color.red;
  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<=6;i++) {obj[i]=GameObject.Find("gama-"+i);} // 1-6  gamas
  for (i=1;i<=3;i++) {obj[i+6]=GameObject.Find("face-"+i);} // 7-9  face-moto
  for (i=1;i<=10;i++) {obj[i+9]=GameObject.Find("kabu-"+i);} // 10-19  kabus
  obj[20]=GameObject.Find("kogitte"); // 20  kogitte-moto
  obj[21]=GameObject.Find("shohin"); // 21  shohin-moto
  obj[22]=GameObject.Find("kai"); // 22  kai-moto
  obj[23]=GameObject.Find("missile"); // 23  missile-moto
  obj[24]=GameObject.Find("title"); // 24  title
  obj[25]=GameObject.Find("ya-r-32x32"); // 25  ya
  for (i=1;i<=3;i++) {obj[i+25]=GameObject.Find("kogitte-info-"+i);} // 26-28  info
  for (i=1;i<=2;i++) {obj[i+28]=GameObject.Find("shields-"+i);} // 29-30  shield
  obj[31]=GameObject.Find("gameover-mes-1"); // 31  gameover-mes-blue
  obj[32]=GameObject.Find("gameover-mes-2"); // 32  gameover-mes-green
  obj[33]=GameObject.Find("gameend-mes-1"); // 33  gameend-mes-blue
  obj[34]=GameObject.Find("gameend-mes-2"); // 34  gameend-mes-green
  for (i=0;i<=99;i++) {obj[i+100]=Object.Instantiate(obj[22]) as GameObject;} // 100-199  kai-copy
  for (i=0;i<=19;i++) {obj[i+200]=Object.Instantiate(obj[20]) as GameObject;} // 200-219  kogitte-copy
  for (i=0;i<=19;i++) {obj[i+220]=Object.Instantiate(obj[23]) as GameObject;} // 220-239  missile-copy
  for (i=0;i<=4;i++) {obj[i+240]=Object.Instantiate(obj[21]) as GameObject;} // 240-244  shohin-copy
  for (i=1;i<=3;i++)
  {
   for (j=0;j<=4;j++) {obj[i*5+j+240]=Object.Instantiate(obj[i+6]) as GameObject;} // 245-259  face-copy
  }
  for (i=0;i<=9;i++)
  {
   obj[i+500]=GameObject.Find("num-16a-"+i); // 500-509  nums-moto
   for (j=1;j<=3;j++) {obj[j*10+i+500]=Object.Instantiate(obj[i+500]) as GameObject;} // 510-539  nums-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();
  alldel();
  z=0;zx=0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update()
 {
  Vector3 mpos=Input.mousePosition;
  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=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 (flg==0)
   {
    zx=-2000;zy=0;
    for (i=0;i<=2;i++) {z=i+1;objput();z=i+26;objput();}
    z=25;zx=-100;zy=178-level*40;objput();
    z=plnum+1;zx=-85;zy=-36;objput();
    z=plnum+26;zx=90;zy=-43;objput();
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
  }
  if (flg==1)
  {
// gama-move
   moves=0;shot=0;
   if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.Keypad6)) {moves=moves+1;}
   if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Keypad4)) {moves=moves+-1;}
   if (moves>0)
   {
    px=px+spd[plnum];
    if (px>=450)
    {
     if (kgt0)
    {
     if (pmcount<=50) {shot=1;}
     pmcount=0;
    }
    else {pmcount=pmcount+1;}
    lpm=-1;
   }
   if (moves==0)
   {
    pmcount=pmcount+1;
    if (plnum==2)
    {
     z=29;zx=-2000;zy=0;objput();z=30;objput();
     z=puyo%2+29;zx=px;zy=145;objput();
     shield=1;
    }
   }
   else
   {
    if (plnum==2)
    {
     z=29;zx=-2000;zy=0;objput();z=30;objput();
     shield=0;
    }
   }
   z=plnum+1;zx=px;zy=py;objput();
   if (shot>0) {newkogitte();}
// kogitte-move
   for (i=0;i<=19;i++)
   {
    if (kgx[i]>=-999)
    {
     kgy[i]=kgy[i]-5;
     if (kgy[i]<=70)
     {
      a=(kgx[i]+500)/200;
      if (a>=0 && a<=4)
      {
       if (kais[a]>0)
       {
        kais[a]=kais[a]-10;
        if (kais[a]<0) {kais[a]=0;}
        kaiput(a);bgm4.Play();
       }
      }
      kgx[i]=-2000;
     }
     z=i+200;zx=kgx[i];zy=kgy[i];objput();
    }
   }
// new-shohin
   for (i=0;i<=4;i++)
   {
    if (shw[i]>0)
    {
     shw[i]=shw[i]-1;
     if (shw[i]<=0)
     {
      shy[i]=-75;
      z=i+240;zx=i*200-400;zy=-75;objput();
     }
    }
   }
// shohin-move
   for (i=0;i<=4;i++)
   {
    if (shw[i]<=0)
    {
     shy[i]=shy[i]-5;
     if (shy[i]<=-180)
     {
      r1=Random.Range(5,15);kais[i]=kais[i]+r1;kaiput(i);
      r1=Random.Range(0,100);
      shw[i]=(4-level)*100+r1;
      z=i+240;zx=-2000;zy=0;objput();
      outhantei();
      if (outflg==0) {bgm6.Play();}
     }
     else {z=i+240;zx=i*200-400;zy=shy[i];objput();}
    }
   }
   missilewait=missilewait-1;
   if (missilewait<=0) {newmissile();}
// missile-move
   for (i=0;i<=19;i++)
   {
    if (mx[i]>=-999)
    {
     my[i]=my[i]+5;
     if (my[i]>=300) {mx[i]=-2000;my[i]=0;}
     if (shield>0)
     {
      if (px-mx[i]>=-78 && px-mx[i]<=78 && my[i]>=120 && my[i]<=168) {mx[i]=-2000;my[i]=0;}
     }
     z=i+220;zx=mx[i];zy=my[i];objput();
     if (mx[i]>-999)
     {
      ax=px-mx[i];ay=py-my[i];af=Mathf.Sqrt(ax*ax+ay*ay);
      if (af<=70f) {outflg=2;}
     }
    }
   }
   if (outflg==2)
   {
    z=plnum+1;zx=-2000;zy=0;objput();
    z=plnum+4;zx=px;zy=py;objput();
    gameover();
   }
  }
  if (flg>=1 && flg<=1)
  {
// bgm-replay
   ntimef=Time.time;
   if (ntimef-stimef>=86f) {bgm1.Play();stimef=Time.time;}
// time-plus
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f)
   {
    stime2f=stime2f+1f;
    times=times-1;scoreput();
    if (times<=0)
    {
     for (i=1;i<=3;i++) {obj[i].transform.localScale=new Vector3(1,1,1);}
     z=33;zx=0;zy=0;objput();
     bgmstop();bgm3.Play();
     stime2f=Time.time;flg=9;count=0;
    }
   }
// puyo
   ntime3f=Time.time;
   if (ntime3f-stime3f>=0.5f)
   {
    stime3f=stime3f+0.5f;
    puyo=puyo+1;if (puyo>3) {puyo=0;}
    a1f=1f;a2f=1f;
    if (puyo==1) {a1f=1.1f;a2f=0.9f;}
    if (puyo==3) {a1f=0.9f;a2f=1.1f;}
    for (i=1;i<=3;i++) {obj[i].transform.localScale=new Vector3(a1f,a2f,1);}
   }
  }
  if (flg==8 || flg==9)
  {
   ntime2f=Time.time;
   if (ntime2f-stime2f>=0.5f)
   {
    stime2f=stime2f+0.5f;count=count+1;
    for (i=31;i<=34;i++) {z=i;zx=-2000;zy=0;objput();}
    z=count%2+flg*2+15;zx=0;zy=0;objput();
    if (count>=12) {titleput();}
   }
  }
  if (flg>0)
  {
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();}
  }
 }

 void gamestart()
 {
  c1="";outflg=0;moves=0;shot=0;
  z=24;zx=-2000;zy=0;objput();z=25;objput();
  for (i=0;i<=2;i++)
  {
   z=i+1;objput();obj[i].transform.localScale=new Vector3(1,1,1);
   z=i+26;objput();
  }
  z=plnum+1;zx=0;zy=215;objput();
  times=120;scoreput();
  px=0;py=215;lpm=0;pmcount=0;shield=0;kgt=maxkg[plnum];puyo=0;missilewait=(4-level)*50;
  for (i=0;i<=19;i++) {kgx[i]=-2000;mx[i]=-2000;}
  for (i=0;i<=4;i++) {kais[i]=0;shw[i]=1;}
  stimef=Time.time;stime2f=Time.time;stime3f=Time.time;
  bgmstop();bgm1.Play();
  flg=1;
 }

 void newkogitte()
 {
  if (kgt>0)
  {
   ee=-1;
   for (i=0;i<=19;i++)
   {
    if (kgx[i]<-999) {ee=i;break;}
   }
   if (ee>=0)
   {
    kgx[ee]=px;kgy[ee]=185;kgt=kgt-1;
    z=ee+200;zx=px;zy=185;objput();
   }
  }
 }

 void newmissile()
 {
  ee=-1;
  for (i=0;i<=19;i++)
  {
   if (mx[i]<-999) {ee=i;break;}
  }
  if (ee>=0)
  {
   r1=Random.Range(-450,450);
   mx[ee]=r1;my[ee]=-298;mj[ee]=1;mw[ee]=0;
   z=ee+220;zx=r1;zy=-298;objput();
   missilewait=(4-level)*50;
  }
 }

 void kaiput(int n)
 {
  for (ii=0;ii<=9;ii++)
  {
   z=n*10+ii+100;zx=-2000;zy=0;
   if (kais[n]>ii*10)
   {
    if (ii<=4) {zx=n*200+ii*40-480;zy=112;} else {zx=n*200+ii*40-680;zy=150;}
   }
   objput();
  }
  z=n+255;zx=-2000;zy=0;objput();z=n+250;objput();z=n+245;objput();
  if (kais[n]>30) {z=n+250;}
  if (kais[n]>60) {z=n+255;}
  zx=n*200-400;zy=0;objput();
 }

 void outhantei()
 {
  for (ii=0;ii<=4;ii++)
  {
   if (kais[ii]>100)
   {
    z=ii+255;zx=-2000;zy=0;objput();z=ii+250;objput();z=ii+245;objput();z=ii+10;objput();
    for (jj=0;jj<=9;jj++)
    {
     z=ii*10+jj+100;zx=-2000;zy=0;objput();
    }
    z=ii+15;zx=ii*200-400;zy=0;objput();
    outflg=1;
   }
  }
  if (outflg==1) {gameover();}
 }

 void gameover()
 {
  z=31;zx=0;zy=0;objput();
  bgmstop();bgm2.Play();
  stime2f=Time.time;flg=8;count=0;
 }

 void scoreput()
 {
  for (i=500;i<=539;i++) {z=i;zx=-2000;zy=0;objput();}
  zy=300;aa=0;
  a=times/100;if (a>0) {aa=1;z=a+500;zx=540;objput();}
  a=(times%100)/10;if (a>0 || aa>0) {aa=1;z=a+510;zx=560;objput();}
  a=times%10;z=a+520;zx=580;objput();
 }

 void titleput()
 {
  alldel();
  times=0;scoreput();
  z=24;zx=0;zy=0;objput();
  z=25;zx=-100;zy=178-level*40;objput();
  z=plnum+1;zx=-85;zy=-36;objput();
  z=plnum+26;zx=90;zy=-43;objput();
  for (i=0;i<=4;i++)
  {
   z=i+10;zx=i*200-400;zy=0;objput();z=i+245;objput();
  }
  stime2f=Time.time;
  bgmstop();flg=0;spacewait=2;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (300,80,100,30),c1,style1);
  GUI.Label (new Rect (300,160,100,30),c2,style2);
  GUI.Label (new Rect (300,240,100,30),c3,style1);
  GUI.Label (new Rect (300,320,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 objput2()
 {
  Vector3 pos=obj[z].transform.position;
  pos.x=xf*0.01f;pos.y=yf*0.01f;
  obj[z].transform.position=pos;
 }

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

 void alldel()
 {
  zx=-2000;zy=0;
  for (i=0;i<=34;i++) {z=i;objput();}
  for (i=100;i<=259;i++) {z=i;objput();}
  for (i=500;i<=539;i++) {z=i;objput();}
  for (i=1;i<=3;i++) {obj[i].transform.localScale=new Vector3(1,1,1);}
 }
}