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

public class itemon_2020 : MonoBehaviour
{
 int flg=0;int count=0;int level=0;int wait=0;int spacewait=0;int outflg=0;
 int px;int py;int wpnum;int monsnum;int nmwait;int amkazu;
 int keyx;int keyy;int keyw;
 int score1;int score2;
 int[] weapons=new int[13];int[] mrec=new int[5];
 int[] pdx=new int[130];int[] pdy=new int[130];
 int[] tx=new int[172];int[] ty=new int[172];int[] tyy=new int[172];
 int[] thp=new int[172];int[] tw=new int[172];
 int[] bestscore=new int[8];
 int i;int j;int z;int zx;int zy;int r;int ee;int a;int ak;int r1;int r2;
 int a1;int a2;int a3;int a4;int b1;int b2;
 int ii;int jj;int e1;int e2;int e3;int x;int y;int e;int zz;int ax;int ay;
 float af;float stimef;float ntimef;
 string c1;string c2;string c3;string c4;string c5;string c6;string c7;
 string d1;string d2;string d3;string d4;string d5;string monsline;string d;
 string keyname="bignyan-itemon";string datas;
 GUIStyle style1;GUIStyle style2;
 GameObject[] obj=new GameObject[400];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes-1"); // 0  ng-mes
  obj[1]=GameObject.Find("itemon-title"); // 1  title
  obj[2]=GameObject.Find("itemon-gameclear"); // 2  gameclear
  obj[3]=GameObject.Find("itemon-gameover"); // 3  gameover
  obj[4]=GameObject.Find("itemon-select"); // 4  select
  for (i=1;i<=2;i++) {obj[i+4]=GameObject.Find("player-"+i);} // 5 - 6  player
  for (i=0;i<=13;i++) {obj[i+7]=GameObject.Find("ghost-"+i);} // 7 - 20  ghost-moto
  for (i=0;i<=3;i++) {obj[i+21]=GameObject.Find("itemon-bomb-"+i);} // 21 - 24  bomb-moto
  obj[25]=GameObject.Find("itemon-key"); // 25  key-moto
  for (i=0;i<=4;i++) {obj[i+26]=GameObject.Find("light-"+i);} // 26 - 30  light-moto
  obj[31]=GameObject.Find("itemon-box"); // 31  box-moto
  obj[32]=GameObject.Find("itemon-waku-1"); // 32  waku-1-moto
  obj[33]=GameObject.Find("itemon-waku-2"); // 33  waku-2
  for (i=1;i<=12;i++) {obj[i+33]=GameObject.Find("itemon-d-"+i);} // 34 - 45  dams-moto
  obj[46]=GameObject.Find("missile"); // 46  missile-moto
  obj[47]=GameObject.Find("ya-r-32"); // 47  ya
  for (i=0;i<=4;i++)
  {
   for (j=0;j<=4;j++) {obj[i*5+j+48]=Object.Instantiate(obj[i+26]) as GameObject;} // 48 - 72  light-copy
  }
  for (i=0;i<=10;i++) {obj[i+73]=Object.Instantiate(obj[31]) as GameObject;} // 73 - 83  box-copy
  for (i=0;i<=12;i++) {obj[i+84]=Object.Instantiate(obj[32]) as GameObject;} // 84 - 96  waku-1-copy
  for (i=1;i<=13;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+87]=Object.Instantiate(obj[i+33]) as GameObject;} // 97 - 226  dans-copy
  }
  for (i=0;i<=12;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+227]=Object.Instantiate(obj[i+7]) as GameObject;} // 227 - 356  ghost-copy
  }
  obj[357]=Object.Instantiate(obj[20]) as GameObject; // 357  ghost-13-copy
  for (i=0;i<=3;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+358]=Object.Instantiate(obj[i+21]) as GameObject;} // 358 - 397  bomb-copy
  }
  obj[398]=Object.Instantiate(obj[21]) as GameObject; // 398  bomb-13-copy
  obj[399]=GameObject.Find("itemon-newrecord"); // 399  newrecord
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();
  style1=new GUIStyle();
  style1.fontSize=16;
  style2=new GUIStyle();
  style2.fontSize=24;
  c1="";c2="";c7="";
  d1="0101201203120412034d";d2="0505605607560856078d";d3="0909a09a0b9a0c9a0bcd";
  d4="012340567809abc0159d";d5="026a037b048c015948c0";monsline=d1+d2+d3+d4+d5;
  alldel();
  z=84;zx=-305;zy=137;objput();z=34;objput();
  for (i=0;i<=10;i++) {z=i+73;zx=i*32-273;zy=137;objput();}
  for (i=0;i<=4;i++) {z=i+48;zx=i*32+111;zy=137;objput();}
  z=1;zx=0;zy=15;objput();z=0;zx=0;zy=0;objput();
  weapons[0]=1;for (i=1;i<=12;i++) {weapons[i]=0;}
  for (i=0;i<=3;i++) {bestscore[i]=0;bestscore[i+4]=0;}
  dataload();
  bestscoredel();count=500;level=0;flg=-1;
 }

 void Update ()
 {
  if (flg==-2)
  {
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    x=x-1;
    if (x<1) {x=12;}
   }
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    x=x+1;
    if (x>12) {x=1;}
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (x<12)
    {
     if (weapons[x]<=0)
     {
      z=x+72;zx=-1000;zy=0;objput();
      z=x+84;zx=x*32-305;zy=137;objput();z=x+34;objput();
      weapons[x]=1;
     }
     else
     {
      z=x+72;zx=x*32-305;zy=137;objput();
      z=x+84;zx=-1000;zy=0;objput();z=x+34;objput();
      weapons[x]=0;
     }
     a=0;
     for (i=1;i<=11;i++)
     {
      if (weapons[i]>0) {a=a+1;}
     }
     if (a>=11)
     {
      z=46;zx=79;zy=137;objput();z=96;objput();
      weapons[12]=1;
     }
     else
     {
      z=46;zx=-1000;zy=0;objput();z=96;objput();
      weapons[12]=0;
     }
    }
    else
    {
     if (weapons[12]>0) {weaponreset();}
     else
     {
      z=46;zx=79;zy=137;objput();z=96;objput();
      weapons[12]=1;
      for (i=1;i<=11;i++)
      {
       z=i+84;zx=i*32-305;zy=137;objput();z=i+34;objput();
       z=i+72;zx=-1000;zy=0;objput();
       weapons[i]=1;
      }
     }
    }
   }
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
   {
    z=4;zx=-1000;zy=0;objput();titleput();
    obj[47].transform.Rotate(0,0,270);
    flg=0;spacewait=2;count=0;
   }
   if (flg==-2)
   {
    z=47;zx=x*32-305;zy=105;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)
   {
    z=0;zx=-2000;zy=0;objput();titleput();
    flg=0;
   }
  }
  if (flg==0)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (spacewait<=0)
   {
    if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8))
    {
     if (level>0) {level=level-1;} else {count=count+1;}
    }
    if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
    {
     if (level<5) {level=level+1;}
     count=0;
    }
   }
   if (count>=10)
   {
    z=1;zx=-1000;zy=0;objput();z=4;zx=0;zy=0;objput();
    obj[47].transform.Rotate(0,0,90);
    x=1;flg=-2;bestscoredel();
   }
   if (Input.GetKeyDown(KeyCode.Space) && spacewait<=0)
   {
    if (level<=3) {gamestart();}
    if (level==4) {weaponreset();datasave();}
    if (level==5)
    {
     for (i=0;i<=3;i++) {bestscore[i]=0;bestscore[i+4]=0;}
     bestscoreput();datasave();
    }
   }
   if (flg==0)
   {
    z=47;zx=-140;zy=130-level*40;objput();
   }
  }
  if (flg==1)
  {
   outflg=0;
   ntimef=Time.time;
   if (ntimef-stimef>=16.5f)
   {
    stimef=Time.time;bgm1.Play();
   }
// player-move
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Keypad8)) {py=py+4;}
   if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.Keypad2)) {py=py-4;}
   if (py>104) {py=104;}
   if (py<-104) {py=-104;}
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    for (i=0;i<=12;i++)
    {
     wpnum=wpnum+1;if (wpnum>12) {wpnum=0;}
     if (weapons[wpnum]>0) {i=101;}
    }
    z=33;zx=wpnum*32-305;zy=137;objput();
   }
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    for (i=0;i<=12;i++)
    {
     wpnum=wpnum-1;if (wpnum<0) {wpnum=12;}
     if (weapons[wpnum]>0) {i=101;}
    }
    z=33;zx=wpnum*32-305;zy=137;objput();
   }
   if (Input.GetKeyDown(KeyCode.Space) && spacewait<=0)
   {
    zz=wpnum;numcall();
    if (ee>=0)
    {
     pdx[ee]=-280;pdy[ee]=py;
     if (wpnum>=12) {score2=score2+10;} else {score2=score2+1;}
     if (score2>9999) {score2=9999;}
     scoreput();
    }
   }
// pl-dan-move
   for (i=0;i<=129;i++)
   {
    if (pdx[i]>-999)
    {
     pdx[i]=pdx[i]+5;
     if (pdx[i]>325) {pdx[i]=-1000;}
     z=i+97;zx=pdx[i];zy=pdy[i];objput();
    }
   }
// mons-move
   for (i=0;i<=129;i++)
   {
    if (tx[i]>-999)
    {
     tx[i]=tx[i]-2;ty[i]=ty[i]+tyy[i];
     if (ty[i]>=104) {ty[i]=104;tyy[i]=-1;}
     if (ty[i]<=-104) {ty[i]=-104;tyy[i]=1;}
     if (tx[i]<-334) {tx[i]=-1000;amkazu=amkazu+1;}
     ee=-1;x=tx[i];y=ty[i];hithantei();
     if (ee>=0)
     {
      e1=Mathf.FloorToInt(i/10);e2=0;damages();
      thp[i]=thp[i]-e2;
      if (thp[i]<=0)
      {
       amkazu=amkazu+1;score1=score1+1;scoreput();
       if (i<10) {zz=0;}
       if (i>=10 && i<50) {zz=1;}
       if (i>=50 && i<90) {zz=2;}
       if (i>=90 && i<130) {zz=3;}
       getitem();
       if (e<0)
       {
        numcall3();
        if (e3>=131)
        {
         tx[e3]=tx[i];ty[e3]=ty[i];tw[e3]=20;
         z=e3+227;zx=tx[e3];zy=ty[e3];objput();
         tx[i]=-1000;ty[i]=0;
        }
       }
       else
       {
        keyx=tx[i];keyy=ty[i];keyw=40;
        z=25;zx=keyx;zy=keyy;objput();
        tx[i]=-1000;ty[i]=0;
       }
      }
     }
     ax=tx[i]-px;ay=ty[i]-py;
     af=Mathf.Sqrt(ax*ax+ay*ay);
     if (af<20f) {outflg=1;}
     z=i+227;zx=tx[i];zy=ty[i];objput();
    }
   }
   if (tx[130]>-999)
   {
    tx[130]=tx[130]-8;ty[130]=ty[130]+tyy[130]*2;
    if (ty[130]>=104) {ty[130]=104;tyy[130]=-1;}
    if (ty[130]<=-104) {ty[130]=-104;tyy[130]=1;}
    if (tx[130]<-334) {tx[130]=-1000;amkazu=amkazu+1;}
    ee=-1;x=tx[130];y=ty[130];hithantei();
    if (ee>=0)
    {
     amkazu=amkazu+1;score1=score1+1;scoreput();
     thp[130]=0;zz=0;getitem();
     if (e<=0)
     {
      tx[171]=tx[130];ty[171]=ty[130];tw[171]=20;
      z=398;zx=tx[130];zy=ty[130];objput();
      tx[130]=-1000;ty[130]=0;
     }
     else
     {
      keyx=tx[130];keyy=ty[130];keyw=40;
      z=25;zx=keyx;zy=keyy;objput();
      tx[130]=-1000;ty[130]=0;
     }
    }
    ax=tx[130]-px;ay=ty[130]-py;
    af=Mathf.Sqrt(ax*ax+ay*ay);
    if (af<20f) {outflg=1;}
    z=357;zx=tx[130];zy=ty[130];objput();
   }
// bomb-del
   for (i=131;i<=171;i++)
   {
    if (tx[i]>-999)
    {
     tw[i]=tw[i]-1;
     if (tw[i]<=0)
     {
      tx[i]=-1000;
      z=i+227;zx=-1000;zy=0;objput();
     }
    }
   }
// key-del
   if (keyx>-999)
   {
    keyw=keyw-1;
    if (keyw<=0)
    {
     z=25;zx=-1000;zy=0;objput();
     keyx=-1000;
    }
   }
// new-mons
   nmwait=nmwait-1;
   if (nmwait<=0)
   {
    newmons();nmwait=240;
    if (level==1) {nmwait=120;}
    if (level==2) {nmwait=80;}
    if (level==3) {nmwait=60;}
   }
// player-put
   z=5;zx=px;zy=py;objput();
   if (outflg>0)
   {
    z=5;zx=-1000;zy=0;objput();z=6;zx=px;zy=py;objput();
    z=3;zx=0;zy=0;objput();
    newrecord();
    bgm1.Stop();bgm3.Play();
    wait=300;flg=2;
   }
   else
   {
    if (amkazu>=level*100+100)
    {
     z=2;zx=0;zy=0;objput();
     newrecord();
     bgm1.Stop();bgm2.Play();
     wait=300;flg=2;
    }
   }
  }
  if (flg==2)
  {
   wait=wait-1;
   if (wait<=0)
   {
    titleput();
    flg=0;
   }
  }
 }

 void gamestart()
 {
  minialldel();bestscoredel();
  px=-304;py=0;wpnum=0;monsnum=0;nmwait=1;amkazu=0;
  keyx=-1000;keyy=0;keyw=0;score1=0;score2=0;scoreput();
  z=5;zx=px;zy=py;objput();z=33;zx=-305;zy=137;objput();
  for (i=0;i<=129;i++) {pdx[i]=-1000;pdy[i]=0;}
  for (i=0;i<=171;i++) {tx[i]=-1000;ty[i]=0;tyy[i]=0;thp[i]=0;tw[i]=0;}
  for (i=0;i<=4;i++)
  {
   mrec[i]=-1;
   z=i+48;zx=i*32+111;zy=137;objput();
  }
  stimef=Time.time;bgm1.Play();
  flg=1;spacewait=2;
 }

 void newmons()
 {
  if (monsnum=0)
   {
    tx[ee]=334;ty[ee]=Random.Range(-104,105);tyy[ee]=0;
    r=Random.Range(0,4);if (ee==130) {r=3;}
    if (r==0) {tyy[ee]=1;}
    if (r==1) {tyy[ee]=-1;}
    thp[ee]=30;tw[ee]=0;
   }
   monsnum=monsnum+1;scoreput();
  }
 }

 void numcall()
 {
  ee=-1;
  a1=zz*10;a2=zz*10+10;
  for (ii=a1;ii-999)
   {
    ax=pdx[ii]-x;ay=pdy[ii]-y;
    af=Mathf.Sqrt(ax*ax+ay*ay);
    if (af<20f)
    {
     pdx[ii]=-1000;z=ii+97;zx=-1000;zy=0;objput();
     ee=Mathf.FloorToInt(ii/10);break;
    }
   }
  }
 }

 void damages()
 {
  e2=3;
  if (e1==0 || ee==12) {e2=30;}
  else
  {
   a=e1-1;a1=Mathf.FloorToInt(a/4);a2=a%4;
   b1=Mathf.FloorToInt(ee/4);b2=ee%4;
   if (a1==b1 || a2==b2) {e2=10;}
   if (a1==b1 && a2==b2) {e2=30;}
  }
 }

 void getitem()
 {
  for (ii=0;ii<=3;ii++) {mrec[ii]=mrec[ii+1];}
  mrec[4]=zz;
  for (ii=48;ii<=72;ii++) {z=ii;zx=-1000;zy=0;objput();}
  d="";
  for (ii=0;ii<=4;ii++)
  {
   z=mrec[ii]*5+ii+53;zx=ii*32+111;zy=137;objput();
   if (mrec[ii]<0) {d=d+"-";} else {d=d+mrec[ii].ToString();}
  }
  e=-1;
  if (d=="11101" && weapons[1]==0) {e=1;}
  if (d=="10111" && weapons[2]==0) {e=2;}
  if (d=="10001" && weapons[3]==0) {e=3;}
  if (d=="20202" && weapons[4]==0) {e=4;}
  if (d=="20222" && weapons[5]==0) {e=5;}
  if (d=="22202" && weapons[6]==0) {e=6;}
  if (d=="20002" && weapons[7]==0) {e=7;}
  if (d=="30303" && weapons[8]==0) {e=8;}
  if (d=="30333" && weapons[9]==0) {e=9;}
  if (d=="33303" && weapons[10]==0) {e=10;}
  if (d=="30003" && weapons[11]==0) {e=11;}
  if (e>0)
  {
   z=e+72;zx=-1000;zy=0;objput();
   z=e+84;zx=e*32-305;zy=137;objput();z=e+34;objput();
   weapons[e]=1;
   a=0;
   for (ii=1;ii<=11;ii++)
   {
    if (weapons[ii]>0) {a=a+1;}
   }
   if (a>=11)
   {
    z=46;zx=79;zy=137;objput();z=96;objput();
    weapons[12]=1;
   }
  }
 }

 void weaponreset()
 {
  weapons[12]=0;
  z=46;zx=-1000;zy=0;objput();z=96;objput();
  for (i=1;i<=11;i++)
  {
   z=i+72;zx=i*32-305;zy=137;objput();
   z=i+84;zx=-1000;zy=0;objput();z=i+34;objput();
   weapons[i]=0;
  }
 }

 void scoreput()
 {
  if (score2>9999) {score2=9999;}
  c1=score1.ToString("000");
  c2=score2.ToString("0000");
  c7=monsnum.ToString("000")+"/"+(level*100+100).ToString();
 }

 void titleput()
 {
  minialldel();
  c1="000";c2="0000";c7="000/000";bestscoreput();
  z=1;zx=0;zy=15;objput();
 }

 void bestscoreput()
 {
  c3=bestscore[0].ToString("000")+" ("+bestscore[4].ToString("0000")+")";
  c4=bestscore[1].ToString("000")+" ("+bestscore[5].ToString("0000")+")";
  c5=bestscore[2].ToString("000")+" ("+bestscore[6].ToString("0000")+")";
  c6=bestscore[3].ToString("000")+" ("+bestscore[7].ToString("0000")+")";
 }

 void bestscoredel()
 {
  c3="";c4="";c5="";c6="";
 }

 void newrecord()
 {
  ee=-1;
  if (score1>bestscore[level]) {ee=1;}
  if (score1==bestscore[level] && score20)
  {
   z=399;zx=0;zy=75;objput();
   bestscore[level]=score1;bestscore[level+4]=score2;
  }
  datasave();
 }

 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<=399;i++) {z=i;zx=-1000;zy=0;objput();obj[i].transform.rotation=Quaternion.identity;}
  z=0;zx=-2000;zy=0;objput();
 }

 void minialldel()
 {
  z=5;zx=-1000;zy=0;objput();z=6;objput();z=25;objput();
  z=1;objput();z=2;objput();z=3;objput();z=47;objput();z=399;objput();
  z=33;zx=-305;zy=137;objput();
  for (i=48;i<=72;i++) {z=i;zx=-1000;zy=0;objput();}
  for (i=97;i<=399;i++) {z=i;zx=-1000;zy=0;objput();}
 }

 void OnGUI()
 {
  GUI.Label (new Rect (394,486,100,30),c1,style2);
  GUI.Label (new Rect (584,486,100,30),c2,style2);
  GUI.Label (new Rect (670,230,100,30),c3,style1);
  GUI.Label (new Rect (670,270,100,30),c4,style1);
  GUI.Label (new Rect (670,310,100,30),c5,style1);
  GUI.Label (new Rect (670,350,100,30),c6,style1);
  GUI.Label (new Rect (800,486,100,30),c7,style2);
 }

 void datasave()
 {
  datas="";
  for (i=0;i<=12;i++) {datas=datas+weapons[i].ToString();}
  for (i=0;i<=3;i++)
  {
   datas=datas+" ";
   datas=datas+bestscore[i].ToString("000");
  }
  for (i=0;i<=3;i++)
  {
   datas=datas+" ";
   datas=datas+bestscore[i+4].ToString("0000");
  }
  keyname="bignyan-itemon";
  PlayerPrefs.SetString(keyname,datas);
  PlayerPrefs.Save();
 }

 void dataload()
 {
  keyname="bignyan-itemon";
  datas=PlayerPrefs.GetString(keyname,"nothing");
  if (datas=="nothing") {datas="1000000000000 000 000 000 000 0000 0000 0000 0000";}
  for (i=0;i<=12;i++)
  {
   d1=datas.Substring(i,1);a1=int.Parse(d1);
   weapons[i]=a1;
  }
  for (i=0;i<=3;i++)
  {
   d1=datas.Substring(i*4+14,1);a1=int.Parse(d1);
   d2=datas.Substring(i*4+15,1);a2=int.Parse(d2);
   d3=datas.Substring(i*4+16,1);a3=int.Parse(d3);
   bestscore[i]=a1*100+a2*10+a3;
   d1=datas.Substring(i*5+30,1);a1=int.Parse(d1);
   d2=datas.Substring(i*5+31,1);a2=int.Parse(d2);
   d3=datas.Substring(i*5+32,1);a3=int.Parse(d3);
   d4=datas.Substring(i*5+33,1);a4=int.Parse(d4);
   bestscore[i+4]=a1*1000+a2*100+a3*10+a4;
  }
  for (i=1;i<=11;i++)
  {
   if (weapons[i]>0)
   {
    z=i+72;zx=-1000;zy=0;objput();
    z=i+84;zx=i*32-305;zy=137;objput();z=i+34;objput();
   }
   else
   {
    z=i+72;zx=i*32-305;zy=137;objput();
    z=i+84;zx=-1000;zy=0;objput();z=i+34;objput();
   }
  }
  if (weapons[12]>0) {z=46;zx=79;zy=137;objput();z=96;objput();}
  else {z=46;zx=-1000;zy=0;objput();z=96;objput();}
 }
}