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

public class whale : MonoBehaviour
{
 int flg=0;int count=0;int level=0;int plnum=0;int wait=0;int spacewait=0;int outflg=0;
 int px;int py;int pha=3;int maxhp;int kyori;int minikyori;
 int fishnum;int fishwait;int fishkira;int wmwait;
 int wavex=0;
 int[] tx=new int[30];int[] tc=new int[30];int[] tw=new int[30];
 int[] tdm=new int[30];int[] tdmw=new int[30];int[] tdx=new int[5];int[] nwwait=new int[5];
 int[] fishx=new int[20];int[] fishy=new int[20];
 int[] hax=new int[10];int[] hay=new int[10];
 int i;int j;int z;int zx;int zy;int r;int ee;int a;int y;int ax;int r1;int r2;
 float stimef;float ntimef;
 string c1;string c2;string c3;string d1;string d2;
 GUIStyle style1;GUIStyle style2;
 GameObject[] obj=new GameObject[128];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;AudioSource bgm4;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes-1"); // 0  ng-mes
  for (i=1;i<=6;i++) {obj[i]=GameObject.Find("same-"+i);} // 1 - 6  sames
  for (i=1;i<=5;i++)
  {
   for (j=0;j<=5;j++) {obj[i*6+j+1]=GameObject.Find("w-s"+j+"-d"+i);} // 7 - 36  whales
  }
  for (i=1;i<=5;i++)
  {
   for (j=0;j<=5;j++) {obj[i*6+j+31]=GameObject.Find("w-out"+j+"-d"+i);} // 37 - 66  whales-out 
  }
  for (i=1;i<=5;i++) {obj[i+66]=GameObject.Find("whale-wave-"+i);} // 67 - 71  waves
  obj[72]=GameObject.Find("whale-p-dan"); // 72  p-dan-moto
  for (i=0;i<=19;i++) {obj[i+73]=Object.Instantiate(obj[72]) as GameObject;} // 73 - 92  p-dan-copy
  for (i=1;i<=3;i++) {obj[i+92]=GameObject.Find("fish-"+i);} // 93 fish-1-moto  94 fish-2  95 fish-3
  obj[96]=GameObject.Find("gao"); // 96  gao-moto
  for (i=1;i<=5;i++) {obj[i+96]=Object.Instantiate(obj[96]) as GameObject;} // 97 - 101  gao-copy
  for (i=0;i<=18;i++) {obj[i+102]=Object.Instantiate(obj[93]) as GameObject;} // 102 - 120  fish-1-copy
  obj[121]=GameObject.Find("whale-heart"); // 121  heart-gameclear
  obj[122]=GameObject.Find("whale-heart-2"); // 122  heart-broken
  obj[123]=GameObject.Find("gameclear-yellow"); // 123  gameclear
  obj[124]=GameObject.Find("gameover-yellow"); // 124  gameover
  obj[125]=GameObject.Find("whale-title"); // 125  title-back
  obj[126]=GameObject.Find("ya-r-32-0"); // 126  ya-ud
  obj[127]=GameObject.Find("ya-r-32-1"); // 127  ya-lr

  for (i=0;i<=4;i++) {z=67+i;zx=0;zy=67-i*70;objput();}
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();bgm4=obj[4].GetComponent();
  style1=new GUIStyle();
  style1.fontSize=80;
  style2=new GUIStyle();
  style2.fontSize=24;
  c1="000.0m";c2="";c3="";
  z=0;zx=0;zy=0;objput();
  count=500;level=0;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)
   {
    z=0;zx=-2000;zy=0;objput();titleput();
    flg=0;
   }
  }
  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.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    plnum=plnum+1;
    if (plnum>2) {plnum=0;}
   }
    if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    plnum=plnum-1;
    if (plnum<0) {plnum=2;}
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0)
    {
     alldel();
     gamestart();
    }
   }
   if (flg==0)
   {
    z=126;zx=-90;zy=105-level*40;objput();
    z=127;zx=plnum*130-190;zy=-60;objput();
   }
  }
  if (flg==1)
  {
   ntimef=Time.time;
   if (ntimef-stimef>=25.0f)
   {
    stimef=Time.time;bgm1.Play();
   }
// player-move
   if (spacewait>0) {spacewait=spacewait-1;}
   if (spacewait<=0)
   {
    if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8))
    {
     if (py>0) {py=py-1;}
    }
    if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
    {
     if (py<4) {py=py+1;}
    }
    if (Input.GetKeyDown(KeyCode.Space))
    {
     if (pha>=1)
     {
      ee=-1;
      for (i=0;i<=9;i++) {if (hax[i]<-999) {ee=i;i=11;}}
      if (ee>=0)
      {
       hax[ee]=pha*5+px+40;hay[ee]=py;
       pha=pha-1;
      }
     }
    }
   }
// fish-move
   fishwait=fishwait-1;
   if (fishwait<=0) {newfish();}
   if (fishkira==0) {fishkira=1;} else {fishkira=0;}
   for (i=0;i<=19;i++)
   {
    if (fishx[i]>-999)
    {
     fishx[i]=fishx[i]-10;
     if (fishx[i]<=-340) {fishx[i]=-1000;}
     if (fishy[i]==py && fishx[i]>=-300 && fishx[i]<=-220)
     {
      if (i==19) {pha=10;}
      else {pha=pha+1;if (pha>10) {pha=10;}}
      fishx[i]=-1000;
     }
     if (i<=18) {z=i+102;zx=fishx[i];zy=104-fishy[i]*70;objput();}
     else
     {
      z=94;zx=-1000;zy=0;objput();z=95;objput();
      if (fishx[i]>-999) {z=fishkira+94;zx=fishx[i];zy=104-fishy[i]*70;objput();}
     }
    }
   }
// whales-move
   wmwait=wmwait-1;
   if (wmwait<=0)
   {
    wavex=wavex-1;
    if (wavex<=-16) {wavex=16;}
    for (i=0;i<=4;i++) {z=67+i;zx=wavex;zy=67-i*70;objput();}
    for (i=0;i<=4;i++)
    {
     for (j=0;j<=5;j++)
     {
      a=i*6+j;
      if (tx[a]>-999)
      {
       maxhp=j*2*(level+1);if (j==0) {maxhp=1;}
       if (j>0 && tdm[a]=10 && tx[a+1]<-999) {tx[a]=-1000;tx[a+1]=300;}
         else {obj[a+7].transform.Rotate(0,180,0);tw[a]=20;}
        }
       }
       if (j==5 && tdm[a]=maxhp)
       {
        tc[a]=tc[a]+1;
        if ((j==0 || j==1) && tc[a]>=25) {tx[a]=-1000;tc[a]=0;}
        if (j==2 && tc[a]>=26) {tx[a]=-1000;tc[a]=0;}
        if (j==3 && tc[a]>=39) {tx[a]=-1000;tc[a]=0;}
        if ((j==4 || j==5) && tc[a]>=59) {tx[a]=-1000;tc[a]=0;}
       }
       if (j==1 && tx[a]<=-340 && tx[a]>-1000)
       {
        if (tx[a+1]<-999) {tx[a+1]=360;tdm[a+1]=tdm[a];tx[a]=-1000;} else {tx[a]=340;}
       }
       if (j==2 && tx[a]<=-360 && tx[a]>-1000)
       {
        if (tx[a+1]<-999) {tx[a+1]=380;tdm[a+1]=tdm[a];tx[a]=-1000;} else {tx[a]=360;}
       }
       if (j==3 && tx[a]<=-380 && tx[a]>-1000)
       {
        if (tx[a+1]<-999) {tx[a+1]=400;tdm[a+1]=tdm[a];tx[a]=-1000;} else {tx[a]=380;}
       }
       if (j==4 && tx[a]<=-400 && tx[a]>-1000)
       {
        if (tx[a+1]<-999) {tx[a+1]=400;tdm[a+1]=tdm[a];tw[a+1]=100;tx[a]=-1000;} else {tx[a]=400;}
       }
       if (j==5 && tx[a]<=-400 && tx[a]>-1000) {tx[a]=400;tw[a]=100;}
       if (j==4 && tx[a]==240 && tx[a-4]<-999)
       {
        tx[a-4]=300;tc[a-4]=0;tw[a-4]=20;
        obj[a+3].transform.rotation=Quaternion.identity;
       }
       z=a+37;zx=-1000;zy=0;objput();z=a+7;objput();
       if (tdmw[a]>0) {z=a+37;}
       zx=tx[a];zy=140-i*70-tc[a];if (j==1) {zy=zy-10;}
       objput();
      }
     }
    }
// gao-move
    for (i=0;i<=4;i++)
    {
     if (tdx[i]>-999)
     {
      tdx[i]=tdx[i]-5;
      if (tdx[i]<=-370) {tdx[i]=-1000;}
      z=i+97;zx=tdx[i];zy=110-i*70;objput();
     }
    }
    wmwait=5-level;
   }
// pl-dan-move
   for (i=0;i<=9;i++)
   {
    if (hax[i]>-999)
    {
     hax[i]=hax[i]+5;
     if (hax[i]>=328) {hax[i]=-1000;}
     z=i+83;zx=hax[i];zy=106-hay[i]*70;objput();
    }
   }
// hithantei
   for (i=0;i<=9;i++)
   {
    if (hax[i]>-1000)
    {
     y=hay[i];ee=1;
     if (tx[y*6]==300 && tdm[y*6]<=0 && hax[i]>=280 && hax[i]<=320) {tdm[y*6]=1;tdmw[y*6]=20;tc[y*6]=0;ee=0;}
     maxhp=2*(level+1);
     if (tx[y*6+1]>-1000 && tdm[y*6+1]0)
     {
      ax=Mathf.Abs(hax[i]-tx[y*6+1]);
      if (ax<=30)
      {
       tdm[y*6+1]=tdm[y*6+1]+1;tdmw[y*6+1]=20;ee=0;
       if (tdm[y*6+1]>=maxhp) {tc[y*6+1]=0;}
      }
     }
     maxhp=4*(level+1);
     if (tx[y*6+2]>-1000 && tdm[y*6+2]0)
     {
      ax=Mathf.Abs(hax[i]-tx[y*6+2]);
      if (ax<=48)
      {
       tdm[y*6+2]=tdm[y*6+2]+1;tdmw[y*6+2]=20;ee=0;
       if (tdm[y*6+2]>=maxhp) {tc[y*6+2]=0;}
      }
     }
     maxhp=6*(level+1);
     if (tx[y*6+3]>-1000 && tdm[y*6+3]0)
     {
      ax=Mathf.Abs(hax[i]-tx[y*6+3]);
      if (ax<=68)
      {
       tdm[y*6+3]=tdm[y*6+3]+1;tdmw[y*6+3]=20;ee=0;
       if (tdm[y*6+3]>=maxhp) {tc[y*6+3]=0;}
      }
     }
     maxhp=8*(level+1);
     if (tx[y*6+4]>-1000 && tdm[y*6+4]0)
     {
      ax=Mathf.Abs(hax[i]-tx[y*6+4]);
      if (ax<=88)
      {
       tdm[y*6+4]=tdm[y*6+4]+1;tdmw[y*6+4]=20;ee=0;
       if (tdm[y*6+4]>=maxhp) {tc[y*6+4]=0;}
      }
     }
     maxhp=10*(level+1);
     if (tx[y*6+5]>-1000 && tdm[y*6+5]0)
     {
      ax=Mathf.Abs(hax[i]-tx[y*6+5]);
      if (ax<=88)
      {
       tdm[y*6+5]=tdm[y*6+5]+1;tdmw[y*6+5]=20;ee=0;
       if (tdm[y*6+5]>=maxhp) {tc[y*6+5]=0;}
      }
     }
     if (ee<=0) {hax[i]=-1000;z=i+83;zx=-1000;zy=0;objput();}
    }
   }
// new whale
   for (i=0;i<=4;i++)
   {
    if (nwwait[i]>0)
    {
     nwwait[i]=nwwait[i]-1;
     if (nwwait[i]<=0)
     {
      tx[i*6+2]=360;tc[i*6+2]=0;tw[i*6+2]=0;tdm[i*6+2]=0;tdmw[i*6+2]=0;
     }
    }
   }
   for (i=0;i<=4;i++)
   {
    ee=0;
    for (j=0;j<=5;j++)
    {
     if (tx[i*6+j]>-1000) {ee=1;}
    }
    if (ee<=0 && nwwait[i]<=0) {nwwait[i]=1000;}
   }
// outhantei
   maxhp=2*(level+1);
   if (tx[py*6+1]>-1000 && tx[py*6+1]<-220 && tdm[py*6+1]-1000 && tx[py*6+2]<-200 && tdm[py*6+2]-1000 && tx[py*6+3]<-180 && tdm[py*6+3]-1000 && tx[py*6+4]<-160 && tdm[py*6+4]-1000 && tx[py*6+5]<-160 && tdm[py*6+5]-1000 && tdx[py]<-190) {outflg=1;}
// player-put
   z=plnum*2+1;zx=px;zy=106-py*70;objput();
   for (i=0;i<=9;i++)
   {
    if (i=10) {kyori=kyori+1;minikyori=0;}
   if (kyori>=1000)
   {
    kyori=999;minikyori=9;
    zx=-1000;
    for (i=7;i<=66;i++) {z=i;objput();}
    for (i=73;i<=92;i++) {z=i;objput();}
    for (i=94;i<=95;i++) {z=i;objput();}
    for (i=97;i<=120;i++) {z=i;objput();}
    px=-280;
    if (plnum==0) {a=3;} else {a=1;}
    z=plnum*2+1;zx=px;zy=-34;objput();
    z=a;zx=px*-1;objput();obj[a].transform.Rotate(0,180,0);
    flg=2;bgm1.Stop();
   }
   scoreput();
   if (outflg>0)
   {
    z=1;zx=-1000;zy=0;objput();z=3;objput();z=5;objput();
    z=plnum*2+2;zx=px;zy=98-py*70;objput();
    for (i=0;i<=9;i++) {z=i+73;zx=-1000;zy=0;objput();}
    bgm1.Stop();bgm4.Play();
    z=124;zx=0;zy=-85;objput();
    flg=3;wait=300;
   }
  }
  if (flg==2)
  {
   px=px+5;
   z=plnum*2+1;zx=px;zy=-34;objput();z=a;zx=px*-1;objput();
   if (px>=-40)
   {
    if (plnum==0 || plnum==1)
    {
     z=121;zx=0;zy=-10;objput();
     bgm2.Play();
    }
    else
    {
     z=122;zx=0;zy=-10;objput();
     obj[a].transform.Rotate(0,180,0);
     bgm3.Play();
    }
    z=123;zx=0;zy=-85;objput();
    wait=300;flg=3;
   }
  }
  if (flg==3)
  {
   wait=wait-1;
   if (wait<=0)
   {
    for (i=121;i<=124;i++) {z=i;zx=-10000;zy=0;objput();}
    obj[1].transform.rotation=Quaternion.identity;
    obj[3].transform.rotation=Quaternion.identity;
    titleput();flg=0;
   }
  }
 }

 void gamestart()
 {
  alldel();spacewait=2;outflg=0;kyori=0;minikyori=0;
  px=-280;py=2;pha=3;wmwait=5-level;
  fishnum=0;fishwait=1;fishkira=0;
  for (i=0;i<=19;i++) {fishx[i]=-1000;fishy[i]=0;}
  for (i=0;i<=9;i++) {hax[i]=-1000;hay[i]=0;}
  for (i=0;i<=29;i++) {tx[i]=-1000;tc[i]=0;tw[i]=0;tdm[i]=0;tdmw[i]=0;}
  for (i=0;i<=4;i++) {tdx[i]=-1000;}
  for (i=0;i<=4;i++) {nwwait[i]=i*100+1;}
  for (i=0;i<100;i++)
  {
   r1=Random.Range(0,5);r2=Random.Range(0,5);
   a=nwwait[r1];nwwait[r1]=nwwait[r2];nwwait[r2]=a;
  }
  z=plnum*2+1;zx=px;zy=106-py*70;objput();
  for (i=0;i<=9;i++)
  {
   if (i19) {fishnum=0;}
  fishwait=20;
 }

 void scoreput()
 {
  if (kyori>999) {kyori=999;minikyori=9;}
  d1=kyori.ToString();d2=minikyori.ToString();
  if (kyori<100) {d1="0"+d1;}
  if (kyori<10) {d1="0"+d1;}
  c1=d1+"."+d2+"m";
 }

 void OnGUI()
 {
  GUI.Label (new Rect (320,560,100,30),c1,style2);
  GUI.Label (new Rect (955,268,100,30),c2,style2);
  GUI.Label (new Rect (540,470,100,30),c3,style1);
 }

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

 void alldel()
 {
  for (i=1;i<=127;i++) {z=i;zx=-1000;zy=0;objput();obj[i].transform.rotation=Quaternion.identity;}
  z=0;zx=-2000;zy=0;objput();
 }
}