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

public class liar_mole : MonoBehaviour
{
 int flg=-1;int count=0;int spacewait=0;int level=0;int score=0;int times=0;int miss=0;int speed=200;
 int[] mxx=new int[4];int[] myy=new int[4];
 int[] mt=new int[4];int[] mj=new int[4];int[] mw=new int[4];
 int hplace;int hwait;int nmwait;
 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 e;int e1;int a1;int a2;int moves;int mousex;int mousey;
 float stimef;float ntimef;float stime2f;float ntime2f;float xf;float yf;
 string c1;string c2;string c3;string c4;string d1;

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

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  obj[1]=GameObject.Find("hammer"); // 1  hammer
  obj[2]=GameObject.Find("mole"); // 2  mole-moto
  for (i=1;i<=10;i++) {obj[i+2]=GameObject.Find("liar-mole-sei-"+i);} // 3-12 sei-moto
  obj[13]=GameObject.Find("title"); // 13 title
  obj[14]=GameObject.Find("ya-r-32x32"); // 14 ya
  obj[15]=GameObject.Find("gameend-mes-1"); // 15 gameend-mes-1
  obj[16]=GameObject.Find("gameend-mes-2"); // 16 gameend-mes-2
  obj[17]=GameObject.Find("gameover-mes-1"); // 17 gameover-mes-1
  obj[18]=GameObject.Find("gameover-mes-2"); // 18 gameover-mes-2
  obj[19]=GameObject.Find("liar-mole-batsu"); // 19 batsu-moto
  obj[20]=GameObject.Find("mole-out"); // 20  mole-out-moto
  for (i=0;i<=3;i++) {obj[i+26]=Object.Instantiate(obj[20]) as GameObject;} // 26-29 mole-out-copy
  for (i=0;i<=3;i++) {obj[i+30]=Object.Instantiate(obj[2]) as GameObject;} // 30-33 mole-copy
  for (i=0;i<=2;i++) {obj[i+34]=Object.Instantiate(obj[19]) as GameObject;} // 34-36 batsu-copy
  for (i=0;i<=3;i++)
  {
   for (j=1;j<=10;j++) {obj[i*10+j+39]=Object.Instantiate(obj[j+2]) as GameObject;} // 40-79 sei-copy
  }
  for (i=0;i<=9;i++)
  {
   obj[i+100]=GameObject.Find("num-"+i); // 100-109  nums-moto
   for (j=0;j<=4;j++) {obj[j*10+i+110]=Object.Instantiate(obj[i+100]) as GameObject;} // 110-159 nums-copy
  }
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();bgm3=obj[3].GetComponent();
  bgm4=obj[4].GetComponent();bgm5=obj[5].GetComponent();
  style1=new GUIStyle();style1.fontSize=40;
  style2=new GUIStyle();style2.fontSize=16;
  style3=new GUIStyle();style3.fontSize=24;style3.normal.textColor=Color.red; 
  mxx[0]=1;myy[0]=1;mxx[1]=1;myy[1]=-1;mxx[2]=-1;myy[2]=-1;mxx[3]=-1;myy[3]=1;
  for (i=0;i<=3;i++)
  {
   z=i+30;zx=mxx[i]*-70;zy=myy[i]*-70;objput();
   obj[i+30].transform.Rotate(0,0,90*(4-i));
   obj[i+26].transform.Rotate(0,0,90*(4-i));
   for (j=0;j<=9;j++)
   {
    if (j==i) {z=i*10+j+40;zx=mxx[i]*-77;zy=myy[i]*-77;objput();}
    obj[i*10+j+40].transform.Rotate(0,0,90*(4-i));
   }
  }
  alldel();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();
    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.Space) && spacewait<=0) {gamestart();}
   if (Input.GetMouseButtonDown(0) && spacewait<=0)
   {
    if (mousex>=490 && mousex<=789)
    {
     if (mousey>=440 && mousey<=479) {level=0;gamestart();}
     if (mousey>=400 && mousey<=439) {level=1;gamestart();}
     if (mousey>=360 && mousey<=399) {level=2;gamestart();}
     if (mousey>=320 && mousey<=359) {level=3;gamestart();}
    }
   }
   if (flg==0) {z=14;zx=-96;zy=98-level*40;objput();}
  }
  if (flg==1)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
// hammer-move
   if (hwait>0)
   {
    hwait=hwait-1;
    if (hwait<=0)
    {
     obj[1].transform.rotation=Quaternion.identity;
     z=1;zx=0;zy=0;objput();
     hplace=-1;hwait=0;
    }
   }
   xx=0;yy=0;
   if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Keypad8)) {yy=1;}
   if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.Keypad2)) {yy=-1;}
   if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Keypad4)) {xx=-1;}
   if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.Keypad6)) {xx=1;}
   if (Input.GetKey(KeyCode.Keypad7)) {xx=-1;yy=1;}
   if (Input.GetKey(KeyCode.Keypad9)) {xx=1;yy=1;}
   if (Input.GetKey(KeyCode.Keypad1)) {xx=-1;yy=-1;}
   if (Input.GetKey(KeyCode.Keypad3)) {xx=1;yy=-1;}
   if (Input.GetMouseButtonDown(0) && spacewait<=0)
   {
    if (mousex>=530 && mousex<=602) {xx=-1;}
    if (mousex>=679 && mousex<=751) {xx=1;}
    if (mousey>=251 && mousey<=323) {yy=-1;}
    if (mousey>=400 && mousey<=472) {yy=1;}
   }
   if (xx!=0 && yy!=0)
   {
    if (xx==-1 && yy==-1) {hplace=0;}
    if (xx==-1 && yy==1) {hplace=1;}
    if (xx==1 && yy==1) {hplace=2;}
    if (xx==1 && yy==-1) {hplace=3;}
    obj[1].transform.rotation=Quaternion.identity;hwait=20;
    z=1;zx=mxx[hplace]*-40;zy=myy[hplace]*-40;objput();
    if (hplace==1 || hplace==3) {obj[1].transform.Rotate(0,180,0);}
    hithantei();
   }
// mole-move
   for (i=0;i<=3;i++)
   {
    if (mj[i]==5)
    {
     mw[i]=mw[i]-1;
     if (mw[i]<=0) {mj[i]=0;mw[i]=0;}
    }
    if (mj[i]==3 || mj[i]==4)
    {
     mw[i]=mw[i]-4;
     z=i+30;if (mj[i]==4) {z=i+26;}
     zx=mxx[i]*-110+mxx[i]*mw[i];zy=myy[i]*-110+myy[i]*mw[i];objput();
     z=i*10+mt[i]+40;zx=mxx[i]*-117+mxx[i]*mw[i];zy=myy[i]*-117+myy[i]*mw[i];objput();
     if (mw[i]<=0) {mj[i]=5;mw[i]=20;}
    }
    if (mj[i]==2)
    {
     mw[i]=mw[i]-1;
     if (mw[i]<=0) {mj[i]=3;mw[i]=40;}
    }
    if (mj[i]==1)
    {
     mw[i]=mw[i]+2;
     z=i+30;zx=mxx[i]*-110+mxx[i]*mw[i];zy=myy[i]*-110+myy[i]*mw[i];objput();
     z=i*10+mt[i]+40;zx=mxx[i]*-117+mxx[i]*mw[i];zy=myy[i]*-117+myy[i]*mw[i];objput();
     if (mw[i]>=40) {mj[i]=2;mw[i]=speed;hithantei();}
    }
   }
// new-mole-call
   if (times>1)
   {
    nmwait=nmwait-1;
    if (nmwait<=0) {newmole();}
    ee=1;
    for (i=0;i<=3;i++)
    {
     if (mj[i]>0) {ee=0;}
    }
    if (ee==1) {newmole();}
   }
// times
   ntimef=Time.time;
   if (ntimef-stimef>=30f && times>0) {bgm1.Play();stimef=Time.time;}
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f)
   {
    times=times-1;if (times<0) {times=0;}
    timeput();stime2f=stime2f+1f;
   }
   if (times<=0)
   {
    z=15;zx=0;zy=0;objput();
    bgmstop();bgm2.Play();
    flg=8;count=0;
   }
  }
  if (flg==8 || flg==9)
  {
   a=flg*2-1;
   count=count+1;
   if (count%50==0) {z=a+1;zx=-1000;zy=0;objput();z=a;zx=0;objput();}
   if (count%50==25) {z=a+1;zx=0;zy=0;objput();z=a;zx=-1000;objput();}
   if (count>=325) {titleput();flg=0;spacewait=2;}
  }
  if (flg>=1)
  {
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();flg=0;spacewait=2;}
   if (Input.GetMouseButtonDown(0))
   {
    if (mousex>=798 && mousex<=868 && mousey>=208 && mousey<=260) {titleput();flg=0;spacewait=2;}
   }
  }
 }

 void gamestart()
 {
  alldel();
  z=1;zx=0;zy=0;objput();
  hplace=-1;hwait=0;nmwait=1;score=0;miss=0;times=120;
  for (i=0;i<=3;i++) {mt[i]=0;mj[i]=0;mw[i]=0;}
  scoreput();timeput();missput();
  stimef=Time.time;stime2f=Time.time;
  bgmstop();bgm1.Play();
  newmole();flg=1;spacewait=2;
 }

 void newmole()
 {
  ee=-1;
  for (ii=0;ii<=99;ii++)
  {
   r=Random.Range(0,4);
   if (mj[r]==0) {ee=r;break;}
  }
  if (times<=1) {ee=-1;}
  if (ee>=0)
  {
   r=Random.Range(0,5);if (r==0) {mt[ee]=0;}
   else
   {
    a1=5;a2=9;
    if (level==0) {a1=0;a2=3;}
    if (level==1) {a1=2;a2=5;}
    if (level==2) {a1=3;a2=6;}
    r=Random.Range(a1,a2);mt[ee]=r+1;
   }
   mj[ee]=1;mw[ee]=0;
   z=ee+30;zx=mxx[ee]*-110;zy=myy[ee]*-110;objput();
   for (ii=0;ii<=9;ii++) {z=ee*10+ii+40;zx=-1000;zy=0;objput();}
   z=ee*10+mt[ee]+40;zx=mxx[ee]*-117;zy=myy[ee]*-117;objput();
   speed=200-level*50-(120-times)/3;if (speed<20) {speed=20;}
   nmwait=speed+20;
  }
  else {nmwait=1;}
 }

 void hithantei()
 {
  if (hplace>=0)
  {
   if (mj[hplace]==2)
   {
    z=hplace+30;zx=-1000;zy=0;objput();
    z=hplace+26;zx=mxx[hplace]*-70;zy=myy[hplace]*-70;objput();
    mj[hplace]=4;mw[hplace]=40;
    if (mt[hplace]==0)
    {
     miss=miss+1;missput();
     if (miss>=3)
     {
      for (ii=0;ii<=3;ii++) {mj[ii]=9;}
      z=17;zx=0;zy=0;objput();
      bgmstop();bgm3.Play();
      flg=9;count=0;
     }
     else {bgm5.Stop();bgm5.Play();}
    }
    else
    {
     bgm4.Stop();bgm4.Play();
     score=score+1;scoreput();
    }
   }
  }
 }

 void scoreput()
 {
  if (score>999) {score=999;}
  for (ii=100;ii<=129;ii++) {z=ii;zx=-1000;zy=0;objput();}
  z=score/100+100;zx=220;zy=136;objput();
  z=(score%100)/10+110;zx=234;zy=136;objput();
  z=score%10+120;zx=248;zy=136;objput();
 }

 void timeput()
 {
  if (times<0) {times=0;}
  for (ii=130;ii<=159;ii++) {z=ii;zx=-1000;zy=0;objput();}
  z=times/100+130;zx=220;zy=112;objput();
  z=(times%100)/10+140;zx=234;zy=112;objput();
  z=times%10+150;zx=248;zy=112;objput();
 }

 void missput()
 {
  for (ii=1;ii<=3;ii++)
  {
   if (miss>=ii) {zx=ii*32+142;} else {zx=-1000;}
   z=ii+33;zy=80;objput();
  }
 }

 void titleput()
 {
  alldel();z=13;zx=0;zy=0;objput();
  z=14;zx=-96;zy=98-level*40;objput();
  bgmstop();spacewait=2;flg=0;count=0;
 }

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

 void alldel()
 {
  z=0;zx=-2000;zy=0;objput();
  zx=-1000;zy=0;for (i=1;i<=20;i++) {z=i;objput();}
  for (i=26;i<=36;i++) {z=i;objput();}
  for (i=40;i<=79;i++) {z=i;objput();}
  for (i=100;i<=159;i++) {z=i;objput();}
  obj[1].transform.rotation=Quaternion.identity;
 }
}