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

public class escape_chu : MonoBehaviour
{
 int flg=-1;int count=0;int spacewait=0;int level=0;int score=0;int nbyou=0;int blink=0;
 int px;int py;int oldpx;int oldpy;int newpx;int newpy;int foods;int keynum;int keyflg;int items;
 int m1;int m2;int m3;int m4;int newth;int finds;
 int[] p=new int[375];int[] pw=new int[375];
 int[] tx=new int[2];int[] ty=new int[2];int[] th=new int[2];int[] tw=new int[2];int[] foodj=new int[10];
 string[] datalines=new string[15];
 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 xf;float yf;float stime2f;float ntime2f;float af;
 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;AudioSource bgm6;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  for (i=1;i<=21;i++) {obj[i]=GameObject.Find("escape-chu-"+i);} // 7=cat 8=food 9-12=key 13-16=door 17-21=number
  obj[22]=GameObject.Find("escape-chu-99"); // 22  exit-wall
  obj[23]=GameObject.Find("escape-chu-98"); // 23  exit
  obj[44]=GameObject.Find("escape-chu-title"); // 44 title 
  obj[45]=GameObject.Find("ya-r-32x32"); // 45 ya
  obj[46]=GameObject.Find("gameclear-mes-1"); // 46 gameclear-mes-1
  obj[47]=GameObject.Find("gameclear-mes-2"); // 47 gameclear-mes-2
  obj[48]=GameObject.Find("gameover-mes-1"); // 48 gameover-mes-1
  obj[49]=GameObject.Find("gameover-mes-2"); // 49 gameover-mes-2
  for (i=0;i<=9;i++)
  {
   obj[i+50]=GameObject.Find("num-"+i); // 50-59  nums-moto
   obj[i+60]=Object.Instantiate(obj[i+50]) as GameObject; // 60-69 nums-copy
  }
  obj[70]=Object.Instantiate(obj[50]) as GameObject; // 70 nums-copy
  obj[71]=Object.Instantiate(obj[51]) as GameObject; // 71 nums-copy
  obj[80]=Object.Instantiate(obj[7]) as GameObject; // 80 cat-copy
  obj[81]=Object.Instantiate(obj[7]) as GameObject; // 81 cat-copy
  for (i=0;i<=9;i++)
  {
   obj[i+90]=Object.Instantiate(obj[8]) as GameObject; // 90-99 food-copy
  }
  for (i=0;i<=2;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+100]=Object.Instantiate(obj[j+50]) as GameObject;} // 100-129 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();
  style1=new GUIStyle();style1.fontSize=40;
  style2=new GUIStyle();style2.fontSize=16;
  style3=new GUIStyle();style3.fontSize=24;style3.normal.textColor=Color.red; 
  c1="";c2="";c3="";c4="";
  datalines[0]="1111111111111111111111111";
  datalines[1]="1000000000000000000234561";
  datalines[2]="1011011011011011011011111";
  datalines[3]="1011011011011011011010000";
  datalines[4]="1000000000000000000010000";
  datalines[5]="1011011011011011011010000";
  datalines[6]="1011011011011011011010000";
  datalines[7]="1000000000000000000010000";
  datalines[8]="1011011011011011011010000";
  datalines[9]="1011011011011011011010000";
  datalines[10]="1000000000000000000010000";
  datalines[11]="1011011011011011011010000";
  datalines[12]="1011011011011011011010000";
  datalines[13]="1000000000000000000010000";
  datalines[14]="1111111111111111111110000";
  alldel();z=0;zx=-0;zy=0;objput();
  count=500;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)
   {
// gamestart();
    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 (flg==0) {z=45;zx=-96;zy=98-level*40;objput();}
  }
  if (flg==1)
  {
// player-move
   moveok(0,px,py);newpx=px;newpy=py;
   if ((Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6)) && m1>0) {newpx=px+1;}
   if ((Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4)) && m2>0) {newpx=px-1;}
   if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) && m3>0) {newpy=py-1;}
   if ((Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) && m4>0) {newpy=py+1;}
   if (newpx!=px || newpy!=py)
   {
    oldpx=px;oldpy=py;px=newpx;py=newpy;charaput(2,px,py);
   }
// player-item-get
   a=py*25+px;
   if (p[a]>=11 && p[a]<=14 && pw[a]<=15) {items=p[a]-10;p[a]=0;pw[a]=0;keyflg=2;bgm5.Play();}
   if (p[a]>=20 && p[a]<=29 && pw[a]<=15)
   {
    z=p[a]+70;zx=-1000;zy=0;objput();
    foods=foods+1;foodj[p[a]-20]=0;p[a]=0;pw[a]=0;
    score=score+1;scoreput();bgm4.Play();
   }
   if (items>=1 && items<=4) {charaput(items+8,oldpx,oldpy);}
// player-door-open
   a=py*25+px;
   if (p[a]>=2 && p[a]<=5 && items==p[a]-1)
   {
    z=p[a]+11;zx=-1000;zy=0;objput();z=items+8;objput();
    p[a]=0;keynum=keynum+1;items=0;foods=0;keyflg=0;
    score=score+10;scoreput();bgm6.Play();
   }
// player-get-nezuko
   if (px==22 && py==1 && items<5)
   {
    z=4;zx=-1000;zy=0;objput();z=22;objput();charaput(5,23,1);
    z=23;zx=-320;zy=-192;objput();
    p[325]=0;items=5;oldpx=23;oldpy=1;
   }
   if (items==5) {charaput(5,oldpx,oldpy);}
   outhantei();
// cats-move
   for (i=0;i<=1;i++)
   {
    if (tx[i]>=0)
    {
     moveok(i+1,tx[i],ty[i]);newth=th[i];finds=0;
     d1=m1.ToString()+m2.ToString()+m3.ToString()+m4.ToString();
     r=Random.Range(0,10);
     if (d1=="0000") {newth=0;}
     if (d1=="1000") {newth=1;}
     if (d1=="0100") {newth=2;}
     if (d1=="0010") {newth=3;}
     if (d1=="0001") {newth=4;}
     if (th[i]==1)
     {
      if (d1=="1100" || d1=="1010" || d1=="1001") {newth=1;}
      if (d1=="0110") {newth=3;}
      if (d1=="0101") {newth=4;}
      if (d1=="1110") {if (r<=6) {newth=1;} else {newth=3;}}
      if (d1=="1101") {if (r<=6) {newth=1;} else {newth=4;}}
      if (d1=="0111" || d1=="0011") {if (r<=4) {newth=3;} else {newth=4;}}
      if (d1=="1011" || d1=="1111")
      {
       if (r<=5) {newth=1;}
       if (r==6 || r==7) {newth=3;}
       if (r>=8) {newth=4;}
      }
     }
     if (th[i]==2)
     {
      if (d1=="1100" || d1=="0110" || d1=="0101") {newth=2;}
      if (d1=="1010") {newth=3;}
      if (d1=="1001") {newth=4;}
      if (d1=="1110") {if (r<=6) {newth=2;} else {newth=3;}}
      if (d1=="1101") {if (r<=6) {newth=2;} else {newth=4;}}
      if (d1=="1011" || d1=="0011") {if (r<=4) {newth=3;} else {newth=4;}}
      if (d1=="0111" || d1=="1111")
      {
       if (r<=5) {newth=2;}
       if (r==6 || r==7) {newth=3;}
       if (r>=8) {newth=4;}
      }
     }
     if (th[i]==3)
     {
      if (d1=="0011" || d1=="1010" || d1=="0110") {newth=3;}
      if (d1=="1001") {newth=1;}
      if (d1=="0101") {newth=2;}
      if (d1=="1011") {if (r<=6) {newth=3;} else {newth=1;}}
      if (d1=="0111") {if (r<=6) {newth=3;} else {newth=2;}}
      if (d1=="1101" || d1=="1100") {if (r<=4) {newth=1;} else {newth=2;}}
      if (d1=="1110" || d1=="1111")
      {
       if (r<=5) {newth=3;}
       if (r==6 || r==7) {newth=1;}
       if (r>=8) {newth=2;}
      }
     }
     if (th[i]==4)
     {
      if (d1=="0011" || d1=="1001" || d1=="0101") {newth=4;}
      if (d1=="1010") {newth=1;}
      if (d1=="0110") {newth=2;}
      if (d1=="1011") {if (r<=6) {newth=4;} else {newth=1;}}
      if (d1=="0111") {if (r<=6) {newth=4;} else {newth=2;}}
      if (d1=="1110" || d1=="1100") {if (r<=4) {newth=1;} else {newth=2;}}
      if (d1=="1101" || d1=="1111")
      {
       if (r<=5) {newth=4;}
       if (r==6 || r==7) {newth=1;}
       if (r>=8) {newth=2;}
      }
     }
     if (ty[i]%3==1)
     {
      if (ty[i]==py)
      {
       finds=1;newth=0;
       if (tx[i]px) {newth=2;}
      }
      if (ty[i]==oldpy && items==5)
      {
       finds=1;newth=0;
       if (tx[i]oldpx) {newth=2;}
      }
     }
     if (tx[i]%3==1)
     {
      if (tx[i]==px)
      {
       finds=1;newth=0;
       if (ty[i]py) {newth=3;}
      }
      if (tx[i]==oldpx && items==5)
      {
       finds=1;newth=0;
       if (ty[i]oldpy) {newth=3;}
      }
     }
     if (finds>0 && tw[i]>16-level*4) {tw[i]=16-level*4;}
     tw[i]=tw[i]-1;if (flg>1) {newth=0;}
     if (tw[i]<=0)
     {
      if (newth>0)
      {
       th[i]=newth;
       if (th[i]==1) {tx[i]=tx[i]+1;}
       if (th[i]==2) {tx[i]=tx[i]-1;}
       if (th[i]==3) {ty[i]=ty[i]-1;}
       if (th[i]==4) {ty[i]=ty[i]+1;}
       charaput(i+80,tx[i],ty[i]);tw[i]=80-level*20;
       if (finds>0) {tw[i]=16-level*4;}
      }
     }
     outhantei();
    }
   }
   if (flg==1 && items==5 && px==0)
   {
    bgmstop();bgm3.Play();
    score=score+100+nbyou;scoreput();
    z=46;zx=0;zy=0;objput();
    flg=8;count=0;
   }
  }
  if (flg==1)
  {
   ntime2f=Time.time;
   if (ntime2f-stime2f>=0.5f && blink==1)
   {
    for (i=0;i<=374;i++)
    {
     if (p[i]>=11 && pw[i]>=1 && pw[i]<=5)
     {
      if (p[i]<=15) {z=p[i]-2;} else {z=p[i]+70;}
      charaput(z,i%25,i/25);
     }
    }
    blink=0;
   }
   if (ntime2f-stime2f>=1f)
   {
    if (blink==0)
    {
     for (i=17;i<=21;i++) {z=i;zx=-1000;zy=0;objput();}
     for (i=0;i<=374;i++)
     {
      if (p[i]>=7 && pw[i]>=17 && pw[i]<=20) {charaput(pw[i],i%25,i/25);}
      if (p[i]==7 && pw[i]==16)
      {
       p[i]=0;pw[i]=0;
       tx[1]=10;ty[1]=7;tw[1]=80-level*20;th[1]=Random.Range(1,5);charaput(81,10,7);
      }
      if (p[i]>=11 && pw[i]==16)
      {
       if (p[i]<=15) {z=p[i]-2;} else {z=p[i]+70;}
       charaput(z,i%25,i/25);
      }
      if (p[i]>=11 && pw[i]>=1 && pw[i]<=5)
      {
       if (p[i]<=15) {z=p[i]-2;} else {z=p[i]+70;}
       zx=-1000;zy=0;objput();
      }
      if (p[i]>=11 && p[i]<=14)
      {
       pw[i]=pw[i]-1;
       if (pw[i]<=0) {p[i]=0;keyflg=0;foods=0;}
      }
      if (p[i]>=20)
      {
       pw[i]=pw[i]-1;
       if (pw[i]<=0) {foodj[p[i]-20]=0;p[i]=0;}
      }
      if (p[i]==7) {pw[i]=pw[i]-1;}
     }
     blink=1;
    }
    nbyou=nbyou-1;if (nbyou<0) {nbyou=0;}
    timeput();stime2f=stime2f+1f;
    if (nbyou%2==0 && nbyou>0) {newitem();}
    if (nbyou==65) {newitem();}
    if (nbyou<=0) {gameover(3);}
   }
  }
  if (flg==1)
  {
   ntimef=Time.time;
   if (ntimef-stimef>=60f && nbyou>0) {bgm1.Play();stimef=Time.time;}
  }
  if (flg==8 || flg==9)
  {
   a=flg*2+30;
   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;}}
 }

 void gamestart()
 {
  z=44;zx=-1000;zy=0;objput();z=45;objput();
  nbyou=120;timeput();
  for (i=0;i<=14;i++)
  {
   for (j=0;j<=24;j++)
   {
    d1=datalines[i].Substring(j,1);
    p[i*25+j]=int.Parse(d1);pw[i*25+j]=0;
   }
  }
  for (i=0;i<=9;i++) {foodj[i]=0;}
  px=1;py=13;oldpx=1;oldpy=13;foods=0;keynum=1;items=0;keyflg=0;
  tx[0]=19;ty[0]=2;th[0]=4;tw[0]=80-level*20;tx[1]=-1;ty[1]=0;th[1]=0;tw[1]=80-level*20;
  charaput(2,px,py);charaput(80,tx[0],ty[0]);
  stimef=Time.time;stime2f=Time.time;bgmstop();bgm1.Play();
  flg=1;spacewait=2;newitem();
 }

 void moveok(int n1,int n2,int n3)
 {
  m1=1;m2=1;m3=1;m4=1;a=n3*25+n2;
  if (p[a+1]>=1 && p[a+1]<=6) {m1=0;}
  if (p[a-1]>=1 && p[a-1]<=6) {m2=0;}
  if (p[a-25]>=1 && p[a-25]<=6) {m3=0;}
  if (p[a+25]>=1 && p[a+25]<=6) {m4=0;}
  if (n1==0)
  {
   if (items==1 && p[a-25]==2) {m3=1;}
   if (items==1 && p[a+1]==2) {m1=1;}
   if (items==2 && p[a+1]==3) {m1=1;}
   if (items==3 && p[a+1]==4) {m1=1;}
   if (items==4 && p[a+1]==5) {m1=1;}
  }
  if (n1>0)
  {
   if (tx[n1-1]<=1) {m2=0;}
   for (ii=0;ii>=1;ii++)
   {
    if (tx[ii]==tx[n1-1]+1 && ty[ii]==ty[n1-1]) {m1=0;}
    if (tx[ii]==tx[n1-1]-1 && ty[ii]==ty[n1-1]) {m2=0;}
    if (tx[ii]==tx[n1-1] && ty[ii]==ty[n1-1]-1) {m3=0;}
    if (tx[ii]==tx[n1-1] && ty[ii]==ty[n1-1]+1) {m4=0;}
   }
  }
 }

 void newitem()
 {
  for (ii=0;ii<=2;ii++)
  {
   x=Random.Range(1,20);y=Random.Range(1,14);a=1;
   ax=Mathf.Abs(px-x);ay=Mathf.Abs(py-y);
   if (ax<=3 && ay<=3) {a=0;}
   if (x==19 && y==1) {a=0;}
   if (x==10 && y==7) {a=0;}
   if (p[y*25+x]>0) {a=0;}
   if (a==0) {ii=0;} else {break;}
  }
  if (foods>=1 && keyflg==0 && keynum<=4) {p[y*25+x]=keynum+10;keyflg=1;}
  else
  {
   e=0;for (ii=0;ii<=9;ii++) {if (foodj[ii]==0) {e=ii;break;}}
   p[y*25+x]=e+20;foodj[e]=1;
  }
  if (nbyou==65) {x=10;y=7;p[y*25+x]=7;}
  charaput(21,x,y);pw[y*25+x]=20;
 }

 void outhantei()
 {
  for (ii=0;ii<=1;ii++)
  {
   if (px==tx[ii] && py==ty[ii]) {gameover(1);}
   if (oldpx==tx[ii] && oldpy==ty[ii] && items==5) {gameover(2);}
  }
 }

 void gameover(int n1)
 {
  if (n1==1 || n1==3) {z=2;zx=-1000;zy=0;objput();charaput(3,px,py);}
  if (n1==2 || n1==3 && items==5) {z=5;zx=-1000;zy=0;objput();charaput(6,oldpx,oldpy);}
  z=48;zx=0;zy=0;objput();
  bgmstop();bgm2.Play();
  flg=9;count=0;
 }

 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=260;zy=250;objput();
  z=(score%100)/10+110;zx=274;zy=250;objput();
  z=score%10+120;zx=290;zy=250;objput();
 }

 void timeput()
 {
  for (ii=50;ii<=71;ii++) {z=ii;zx=-1000;zy=0;objput();}
  z=nbyou/100+70;zx=400;zy=250;objput();
  z=(nbyou%100)/10+60;zx=414;zy=250;objput();
  z=nbyou%10+50;zx=428;zy=250;objput();
 }

 void titleput()
 {
  alldel();score=0;scoreput();nbyou=120;timeput();
  charaput(2,1,13);charaput(4,23,1);charaput(80,19,2);charaput(22,0,13);
  charaput(13,19,1);charaput(14,20,1);charaput(15,21,1);charaput(16,22,1);
  z=44;zx=0;zy=0;objput();z=45;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 charaput(int n1,int n2,int n3)
 {
  z=n1;zx=(n2-10)*32;zy=32*(7-n3);objput();
 }

 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();
 }

 void alldel()
 {
  z=0;zx=-2000;zy=0;objput();
  zx=-1000;zy=0;for (i=1;i<=23;i++) {z=i;objput();}
  for (i=44;i<=71;i++) {z=i;objput();}
  for (i=80;i<=81;i++) {z=i;objput();}
  for (i=90;i<=129;i++) {z=i;objput();}
 }
}