using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class screensize
{
 [RuntimeInitializeOnLoadMethod]
 static void OnRuntimeMethodLoad()
 {
  Screen.SetResolution(1280,720,false);
 }
}
public class kaeru_kawaru : MonoBehaviour
{
 int flg=-1;int backx=-15;int backcount=0;int stage=0;int laststage=0;int lastnum=-1;int plnum=0;int movecount=0;
 int count=0;int wait=0;int spacewait=0;int level=0;int score=0;int times=0;int outflg=0;int clearflg=0;int fieldclose=0;
 int px;int py;int ph;int pleaf;

 int[] tx=new int[100];int[] ty=new int[100];int[] tt=new int[100];
 int[] th=new int[100];int[] tm=new int[100];int[] tcount=new int[100];
 string[] stagedatas=new string[100];string[] datas=new string[100];
 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 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;
 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;

 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<=8;i++) {obj[i]=GameObject.Find("kaeru-"+i);} // 1-8  kaerus
  for (i=0;i<=2;i++) {obj[i+9]=GameObject.Find("leaf-"+i);} // 9-11  leaf-moto
  obj[12]=GameObject.Find("igaball-1"); // 12  igaball-moto
  obj[13]=GameObject.Find("kaeru-wave"); // 13  wave-moto
  obj[14]=GameObject.Find("arrow"); // 14  arrow
  obj[15]=GameObject.Find("stageclear-mes-270-yellow"); // 15  stageclear-mes-yellow
  obj[16]=GameObject.Find("stageclear-mes-270-orange"); // 16  stageclear-mes-orange
  obj[17]=GameObject.Find("allclear-mes-280-yellow"); // 17  allclear-mes-yellow
  obj[18]=GameObject.Find("allclear-mes-280-orange"); // 18  allclear-mes-orange
  obj[19]=GameObject.Find("kaeru-title"); // 19  title
  obj[20]=GameObject.Find("kaeru-titleback"); // 20  titleback
  obj[21]=GameObject.Find("kaeru-titlewaku"); // 21  titlewaku
  for (i=1;i<=9;i++) {obj[i+21]=GameObject.Find("kaeru-anten-"+i);} // 22-30  field-close
  obj[31]=GameObject.Find("kaeru-input-moji"); // 31  input-moji
  for (i=0;i<=399;i++) {obj[i+100]=Object.Instantiate(obj[10]) as GameObject;} // 100-499  leaf-1-copy
  for (i=0;i<=199;i++) {obj[i+500]=Object.Instantiate(obj[11]) as GameObject;} // 500-699  leaf-2-copy
  for (i=0;i<=99;i++) {obj[i+700]=Object.Instantiate(obj[12]) as GameObject;} // 700-799  igaball-copy
  for (i=0;i<=99;i++) {obj[i+800]=Object.Instantiate(obj[13]) as GameObject;} // 800-899  wave-copy
  obj[999]=GameObject.Find("kaeru-back"); // 999  moving-back
  for (i=0;i<=9;i++)
  {
   obj[i+1200]=GameObject.Find("num-16a-"+i); // 1200-1209  nums-moto
   for (j=1;j<=9;j++) {obj[j*10+i+1200]=Object.Instantiate(obj[i+1200]) as GameObject;} // 1210-1299  nums-copy
  }
  for (i=0;i<=99;i++) {stagedatas[i]="nothing";}
  stagedatas[0]="009200/105200/205500/309500/end";
  stagedatas[1]="011500/103200/203500/507500/607200/111200/end";
  stagedatas[2]="009200/105200/205521/309511/end";
  stagedatas[3]="009000/105100/206421/404421/509331/609531/end";
  stagedatas[4]="001500/201200/113212/413512/end";
  stagedatas[5]="013500/201200/703311/107200/207500/709431/end";
  stagedatas[6]="014000/102100/601621/503621/306600/206200/707421/108200/508600/310600/509041/611041/end";
  stagedatas[7]="014600/101100/202621/305531/206121/706611/208621/109211/710131/311531/212121/712611/114211/end";
  stagedatas[8]="011100/101100/201621/307641/607400/207121/711512/413511/213311/end";
  stagedatas[9]="007400/101100/201700/702421/303700/603100/704421/205700/605100/707211/309621/409441/311700/611100/712421/413600/113100/end";
  stagedatas[10]="013100/301600/401141/202141/704321/605621/505541/105111/306131/608611/408311/609331/209121/711431/513500/413300/end";
  stagedatas[11]="001000/201600/301400/201200/704313/707531/707111/710333/313600/413400/313200/413000/end";
  stagedatas[12]="013600/201100/703111/706321/207600/107100/708321/709611/end";
  stagedatas[13]="014600/102000/202700/601541/102541/503541/404221/205600/505000/707500/707200/309532/312700/511221/312221/613221/412000/end";
  stagedatas[14]="011200/101100/201600/701341/203500/703341/103100/705211/107511/307411/207200/709331/711511/313600/713421/413100/end";
  stagedatas[15]="000700/202700/100313/707631/707111/314433/414000/end";
  stagedatas[16]="011400/101100/401641/202641/104100/404421/205421/707631/707131/709411/513600/412141/213141/end";
  stagedatas[17]="007500/201600/402321/203321/502111/406021/207021/707421/607300/613631/513531/412341/213341/end";
  stagedatas[18]="013600/101100/701321/201600/702631/702131/303600/203100/704611/704443/704111/205600/105100/706631/706323/706131/307600/207100/708611/708443/708111/209600/109100/710631/710323/710131/311600/211100/712611/712111/713441/113100/end";
  stagedatas[19]="013000/101100/600611/701631/201400/701341/201021/703111/404741/205741/504411/204100/705212/706432/407400/107100/708131/310631/610431/510141/611141/712411/113512/413412/313312/414500/114212/end";
  for (i=0;i<=99;i++)
  {
   if (stagedatas[i]=="nothing") {break;}
   else {laststage=i;}
  }
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();bgm4=obj[4].GetComponent();
  count=500;flg=-1;
  alldel();
  z=0;zx=0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update()
 {
  Vector3 mpos=Input.mousePosition;
  backcount=backcount+1;
  if (backcount>=2)
  {
   backcount=0;backx=backx+1;
   if (backx>=15) {backx=-15;}
   z=999;zx=backx;zy=0;objput();
  }
  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))
   {
    stage=stage+1;if (stage>laststage) {stage=0;}
   }
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
   {
    stage=stage-1;if (stage<0) {stage=laststage;}
   }
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    if (plnum==0) {plnum=1;} else {plnum=0;}
   }
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    if (plnum==0) {plnum=1;} else {plnum=0;}
   }
   scoreput();if (flg==0) {z=21;zx=plnum*84-43;zy=48;objput();}
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
  }
  if (flg==1) // kaeru-on-leaf
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   px=tx[pleaf];py=ty[pleaf];
   z=plnum*4+1;zx=px;zy=py;objput();z=14;objput();
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {flg=2;}
   }
  }
  if (flg==2) // kaeru-jumping
  {
   outflg=0;clearflg=0;
   if (ph==1) {py=py+10;if (py>=210) {outflg=1;}}
   if (ph==2) {px=px+10;if (px>=420) {outflg=1;}}
   if (ph==3) {py=py-10;if (py<=-210) {outflg=1;}}
   if (ph==4) {px=px-10;if (px<=-420) {outflg=1;}}
   z=plnum*4+1;zx=px;zy=py;objput();z=14;objput();
   for (i=0;i<=99;i++)
   {
    if (tt[i]>=0 && tt[i]<=6 && i!=pleaf)
    {
     ax=px-tx[i];ay=py-ty[i];
     af=Mathf.Sqrt(ax*ax+ay*ay);
     if (af<=50f)
     {
      if (tt[i]>=1)
      {
       pleaf=i;px=tx[i];py=ty[i];
       if (tt[i]>=1 && tt[i]<=4) {ph=tt[i];}
       if (tt[i]==5) {ph=ph+1;if (ph>4) {ph=1;}}
       if (tt[i]==6) {ph=ph-1;if (ph<1) {ph=4;}}
       z=plnum*4+1;zx=px;zy=py;objput();z=14;objput();
       turns();flg=1;break;
      }
      else {clearflg=1;}
     }
    }
    if (tt[i]>=7)
    {
     ax=px-tx[i];ay=py-ty[i];
     af=Mathf.Sqrt(ax*ax+ay*ay);
     if (af<=40f) {outflg=1;}
    }
   }
   if (clearflg>0) {px=tx[0];py=ty[0];stageclear();}
   if (outflg>0 && clearflg==0)
   {
    z=plnum*4+1;zx=-2000;zy=0;objput();z=14;objput();
    z=plnum*4+4;zx=px;zy=py;objput();
    bgmstop();bgm4.Play();
    flg=5;wait=150;
   }
  }
  if (flg==3) // field-close
  {
   if (wait>0) {wait=wait-1;}
   if (wait<=0)
   {
    for (i=22;i<=30;i++) {z=i;zx=-2000;zy=0;objput();}
    fieldclose=fieldclose+1;wait=2;
    if (fieldclose<=9) {z=fieldclose+21;zx=0;zy=0;objput();}
     else {z=30;zx=0;zy=0;objput();newstage();}
   }
  }
  if (flg==4) // field-open
  {
   if (wait>0) {wait=wait-1;}
   if (wait<=0)
   {
    for (i=22;i<=30;i++) {z=i;zx=-2000;zy=0;objput();}
    fieldclose=fieldclose-1;wait=2;
    if (fieldclose>=1) {z=fieldclose+21;zx=0;zy=0;objput();}
    else
    {
     flg=1;spacewait=2;
     bgmstop();bgm1.Play();stimef=Time.time;
     stime2f=Time.time;times=0;scoreput();
    }
   }
  }
  if (flg==5) // miss-wait
  {
   wait=wait-1;
   if (wait<=0)
   {
    z=4;zx=-2000;zy=0;objput();z=8;objput();
    px=tx[1];py=ty[1];ph=tt[1];pleaf=1;
    z=plnum*4+1;zx=px;zy=py;objput();z=14;objput();
    
    bgmstop();bgm1.Play();stimef=Time.time;
    stime2f=Time.time;times=0;scoreput();
    turns();flg=1;spacewait=2;
   }
  }
  if (flg==1 || flg==2 || flg>=5) // leaf-move
  {
   movecount=movecount+1;
   if (movecount>=1)
   {
    movecount=0;
    for (i=0;i<=99;i++)
    if (tt[i]>=1 && tt[i]<=7)
    {
     if (th[i]>0)
     {
      tcount[i]=tcount[i]+1;
      if (th[i]==1) {ty[i]=ty[i]+1;}
      if (th[i]==2) {tx[i]=tx[i]+1;}
      if (th[i]==3) {ty[i]=ty[i]-1;}
      if (th[i]==4) {tx[i]=tx[i]-1;}
      z=tt[i]*100+i;zx=tx[i];zy=ty[i];objput();
      if (tcount[i]>=tm[i]*120)
      {
       tcount[i]=0;
       if (th[i]==1) {a=3;}
       if (th[i]==2) {a=4;}
       if (th[i]==3) {a=1;}
       if (th[i]==4) {a=2;}
       th[i]=a;
      }
      if (tt[i]>=1 && tt[i]<=6)
      {
       z=i+800;objput();
       a=450-th[i]*90;
       obj[z].transform.rotation=Quaternion.identity;obj[z].transform.Rotate(0,0,a);
      }
     }
    }
   }
  }
  if (flg>=1 && flg<=2)
  {
// bgm-replay
   ntimef=Time.time;
   if (ntimef-stimef>=12f) {bgm1.Play();stimef=Time.time;}
// time-plus
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f)
   {
    stime2f=stime2f+1f;
    times=times+1;scoreput();
   }
  }
  if (flg==9) // stage-clear
  {
   ntime2f=Time.time;
   if (ntime2f-stime2f>=0.4f)
   {
    stime2f=stime2f+0.4f;count=count+1;
    if (stage=16)
    {
     if (stage0)
  {
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();}
  }
 }

 void gamestart()
 {
  alldel();z=31;zx=-260;zy=-290;objput();
  fieldclose=0;z=22;zx=0;zy=0;objput();
  flg=3;wait=2;
 }

 void newstage()
 {
  alldel();z=30;zx=0;zy=0;objput();z=31;zx=-260;zy=-290;objput();
  for (i=0;i<=99;i++) {tx[i]=-2000;ty[i]=0;tt[i]=-1;th[i]=0;tm[i]=0;tcount[i]=0;}
  datas=stagedatas[stage].Split('/');lastnum=-1;
  for (i=0;i<=99;i++)
  {
   if (datas[i]=="end") {break;}
   else
   {
    lastnum=lastnum+1;d=datas[i];
    d1=d.Substring(0,1);tt[i]=int.Parse(d1);
    d1=d.Substring(1,1);a1=int.Parse(d1);
    d2=d.Substring(2,1);a2=int.Parse(d2);
    tx[i]=(a1*10+a2)*60-420;
    d1=d.Substring(3,1);a1=int.Parse(d1);
    ty[i]=a1*60-210;
    d1=d.Substring(4,1);th[i]=int.Parse(d1);
    d1=d.Substring(5,1);tm[i]=int.Parse(d1);
    tcount[i]=tm[i]*60;
    if (i==0) {z=9;} else {z=tt[i]*100+i;}
    zx=tx[i];zy=ty[i];objput();
    if (tt[i]>=1 && tt[i]<=6)
    {
     if (th[i]>0)
     {
      z=i+800;objput();
      a=450-th[i]*90;
      obj[z].transform.rotation=Quaternion.identity;obj[z].transform.Rotate(0,0,a);
     }
    }
    else {z=i+800;zx=-2000;zy=0;objput();}
   }
  }
  px=tx[1];py=ty[1];ph=tt[1];pleaf=1;
  z=plnum*4+1;zx=px;zy=py;objput();z=14;objput();
  turns();times=0;scoreput();
  flg=4;fieldclose=9;wait=2;
 }

 void turns()
 {
  a=450-ph*90;
  obj[14].transform.rotation=Quaternion.identity;obj[14].transform.Rotate(0,0,a);
  for (i=500;i<=599;i++)
  {
   obj[i].transform.rotation=Quaternion.identity;
   obj[i].transform.Rotate(0,0,a);
  }
  for (i=600;i<=699;i++)
  {
   obj[i].transform.rotation=Quaternion.identity;
   obj[i].transform.Rotate(0,180,ph*90-90);
  }
 }

 void stageclear()
 {
  z=1;zx=-2000;zy=0;objput();z=5;objput();z=14;objput();
  z=2;zx=px-30;zy=py;objput();z=6;zx=px+30;zy=py;objput();
  if (stage999) {times=999;}
  zy=284;aa=0;
  a=times/100;if (a>0) {aa=1;z=a+1230;zx=432;objput();}
  a=(times%100)/10;if (a>0 || aa>0) {aa=1;z=a+1240;zx=452;objput();}
  a=times%10;z=a+1250;zx=472;objput();
 }

 void titleput()
 {
  alldel();
  times=0;scoreput();
  z=19;zx=0;zy=0;objput();z=20;objput();
  z=1;zx=-43;zy=48;objput();z=5;zx=43;zy=48;objput();
  z=21;zx=plnum*84-43;zy=48;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();
 }

 void alldel()
 {
  zx=-2000;zy=0;
  for (i=0;i<=31;i++) {z=i;objput();obj[i].transform.rotation=Quaternion.identity;}
  for (i=100;i<=899;i++) {z=i;objput();obj[i].transform.rotation=Quaternion.identity;}
  for (i=200;i<=299;i++) {obj[i].transform.Rotate(0,0,270);}
  for (i=300;i<=399;i++) {obj[i].transform.Rotate(0,0,180);}
  for (i=400;i<=499;i++) {obj[i].transform.Rotate(0,0,90);}
  for (i=600;i<=699;i++) {obj[i].transform.Rotate(0,180,0);}
  for (i=1200;i<=1299;i++) {z=i;objput();}
 }
}