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

public class room_bomb_alert : MonoBehaviour
{
 int flg=0;int count=0;int level=0;int moveptn=0;int wait=0;int spacewait=0;
 int pkaku;int score1;int score2;int catkk;int catkwait;int catdnum;int catdwait;
 int autodnum;int autodwait;
 int[] catkaku=new int[4];int[] cathp=new int[4];int[] catw=new int[4];
 int[] bkaku=new int[10];int[] bkyo=new int[10];
 int[] tkaku=new int[200];int[] tkyo=new int[200];int[] tkk=new int[200];int[] tw=new int[200];
 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 ii;int jj;int e1;int e2;
 float xf;float yf;float af;float axf;float ayf;float aaxf;float aayf;float zf;
 float stimef;float ntimef;
 string c1;string c2;string c3;string d1;string d2;
 GUIStyle style1;GUIStyle style2;
 GameObject[] obj=new GameObject[245];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes-1"); // 0  ng-mes
  obj[1]=GameObject.Find("room-title"); // 1  title
  obj[2]=GameObject.Find("gameclear-mes-1"); // 2  gameclear
  obj[3]=GameObject.Find("gameover-mes-1"); // 3  gameover
  for (i=1;i<=2;i++) {obj[i+3]=GameObject.Find("bar-"+i);} // 4 - 5  meter
  obj[6]=GameObject.Find("maichi"); // 6  maichi
  obj[7]=GameObject.Find("room-door"); // 7  room-door
  for (i=1;i<=4;i++) {obj[i+7]=GameObject.Find("bomb-"+i);} // 8 - 11  boms
  obj[12]=GameObject.Find("pl-dan"); // 12  pl-dan-moto
  for (i=1;i<=8;i++) {obj[i+12]=GameObject.Find("cat-"+i);} // 13 - 20  cats
  for (i=1;i<=12;i++) {obj[i+20]=GameObject.Find("games-"+i);} // 21 - 32  games-moto
  for (i=0;i<=9;i++) {obj[i+33]=Object.Instantiate(obj[12]) as GameObject;} // 33 - 42  pl-dan-copy
  for (i=0;i<=19;i++) {obj[i+43]=Object.Instantiate(obj[21]) as GameObject;} // 43 - 62  games-1-copy
  for (i=0;i<=19;i++) {obj[i+63]=Object.Instantiate(obj[22]) as GameObject;} // 63 - 82  games-2-copy
  for (i=0;i<=29;i++) {obj[i+83]=Object.Instantiate(obj[23]) as GameObject;} // 83 - 112  games-3-copy
  for (i=0;i<=9;i++) {obj[i+113]=Object.Instantiate(obj[24]) as GameObject;} // 113 - 122  games-4-copy
  for (i=0;i<=9;i++) {obj[i+123]=Object.Instantiate(obj[25]) as GameObject;} // 123 - 132  games-5-copy
  for (i=0;i<=9;i++) {obj[i+133]=Object.Instantiate(obj[26]) as GameObject;} // 133 - 142  games-6-copy
  for (i=0;i<=19;i++) {obj[i+143]=Object.Instantiate(obj[27]) as GameObject;} // 143 - 162  games-1-b-copy
  for (i=0;i<=19;i++) {obj[i+163]=Object.Instantiate(obj[28]) as GameObject;} // 163 - 182  games-2-b-copy
  for (i=0;i<=29;i++) {obj[i+183]=Object.Instantiate(obj[29]) as GameObject;} // 183 - 212  games-3-b-copy
  for (i=0;i<=9;i++) {obj[i+213]=Object.Instantiate(obj[30]) as GameObject;} // 213 - 222  games-4-b-copy
  for (i=0;i<=9;i++) {obj[i+223]=Object.Instantiate(obj[31]) as GameObject;} // 223 - 232  games-5-b-copy
  for (i=0;i<=9;i++) {obj[i+233]=Object.Instantiate(obj[32]) as GameObject;} // 233 - 242  games-6-b-copy
  for (i=1;i<=2;i++) {obj[i+242]=GameObject.Find("ya-r-32-"+i);} // 243 - 244  ya
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();
  style1=new GUIStyle();
  style1.fontSize=80;
  style2=new GUIStyle();
  style2.fontSize=24;
  c1="";c2="";c3="";
  z=0;zx=0;zy=0;objput();
  for (i=1;i<=244;i++) {z=i;zx=-1000;zy=0;objput();}
  z=4;zx=-100;zy=-215;objput();af=0f;scaleset();
  z=5;zx=-100;zy=-245;objput();af=0f;scaleset();
  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.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    moveptn=moveptn-1;
    if (moveptn<0) {moveptn=2;}
   }
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    moveptn=moveptn+1;
    if (moveptn>2) {moveptn=0;}
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
   if (flg==0)
   {
    z=243;zx=-88;zy=104-level*40;objput();
    z=244;zx=-34+moveptn*80;zy=-64;objput();
   }
  }
  if (flg==1)
  {
   ntimef=Time.time;
   if (ntimef-stimef>=70.0f)
   {
    stimef=Time.time;bgm1.Play();
   }
// player-move
   if (spacewait>0) {spacewait=spacewait-1;}
   ak=0;
   if (moveptn==0 || moveptn==1)
   {
    if (moveptn==0) {a=1;} else {a=-1;}
    if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.Keypad6)) {ak=a*-1;}
    if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Keypad8)) {ak=a*-1;}
    if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Keypad4)) {ak=a;}
    if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.Keypad2)) {ak=a;}
   }
   else
   {
    if (pkaku>=0 && pkaku<180)
    {
     if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.Keypad6)) {ak=-1;}
     if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Keypad4)) {ak=1;}
    }
    if (pkaku>=180 && pkaku<360)
    {
     if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.Keypad6)) {ak=1;}
     if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Keypad4)) {ak=-1;}
    }
    if (pkaku>=90 && pkaku<270)
    {
     if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Keypad8)) {ak=-1;}
     if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.Keypad2)) {ak=1;}
    }
    if (pkaku<90 || pkaku>=270)
    {
     if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Keypad8)) {ak=1;}
     if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.Keypad2)) {ak=-1;}
    }
   }
   pkaku=pkaku+ak*5;
   if (pkaku<0) {pkaku=pkaku+360;}
   if (pkaku>=360) {pkaku=pkaku-360;}
   if (Input.GetKeyDown(KeyCode.Space) && spacewait<=0)
   {
    ee=-1;
    for (i=0;i<=9;i++)
    {
     if (bkaku[i]<-999) {ee=i;i=11;}
    }
    if (ee>=0) {bkaku[ee]=pkaku;bkyo[ee]=40;}
   }
// pldan-move
   for (i=0;i<=9;i++)
   {
    if (bkaku[i]>-999)
    {
     bkyo[i]=bkyo[i]+5;
     xf=Mathf.Cos(bkaku[i]*Mathf.PI/180)*bkyo[i];yf=Mathf.Sin(bkaku[i]*Mathf.PI/180)*bkyo[i];
     z=i+33;objput2();
     if (bkyo[i]>=205)
     {
      bkaku[i]=-1000;
      zx=-1000;zy=0;objput();
     }
    }
   }
// cat-move
   catkwait=catkwait-1;
   if (catkwait<=0)
   {
    catkk=catkk*-1;
    catkwait=Random.Range(100,200);
   }
   if (catdwait>0) {catdwait=catdwait-1;}
   for (i=0;i<=3;i++)
   {
    if (catkaku[i]>-999)
    {
     if (cathp[i]<=0)
     {
      catw[i]=catw[i]-1;
      if (catw[i]<=0) {cathp[i]=10;}
     }
     catkaku[i]=catkaku[i]+catkk;
     if (catkaku[i]<0) {catkaku[i]=catkaku[i]+360;}
     if (catkaku[i]>=360) {catkaku[i]=catkaku[i]-360;}
     xf=Mathf.Cos(catkaku[i]*Mathf.PI/180)*170;yf=Mathf.Sin(catkaku[i]*Mathf.PI/180)*170;
     zf=30f;hithantei();
     if (ee>0)
     {
      if (cathp[i]>0)
      {
       cathp[i]=cathp[i]-1;
       if (cathp[i]<=0) {catw[i]=500;}
      }
     }
     if (cathp[i]>0)
     {
      z=i+13;objput2();
      z=i+17;zx=-1000;zy=0;objput();
     }
     else
     {
      z=i+17;objput2();
      z=i+13;zx=-1000;zy=0;objput();
     }
     if (catdwait<=0 && cathp[i]>0)
     {
      if (catdnum%2==0) {z=0;} else {z=1;}
      if (catdnum==10) {z=2;}
      ee=-1;numcall();
      if (ee>=0)
      {
       tkaku[ee]=catkaku[i];tkyo[ee]=160;tkk[ee]=catkk;
       catdnum=catdnum+1;if (catdnum>10) {catdnum=0;}
      }
     }
    }
   }
   if (catdwait<=0) {catdwait=200;}
// new-normal-game
   autodwait=autodwait-1;
   if (autodwait<=0)
   {
    if (autodnum%2==0) {z=3;} else {z=4;}
    if (autodnum==9 || autodnum==19) {z=5;}
    if (autodnum==29) {z=6;}
    ee=-1;numcall();
    if (ee>=0)
    {
     tkaku[ee]=Random.Range(0,360);tkyo[ee]=220;tkk[ee]=1;
     r=Random.Range(0,2);if (r==0) {tkk[ee]=-1;}
    }
    autodnum=autodnum+1;if (autodnum>29) {autodnum=0;}
    autodwait=100;
   }
// games-move
   for (i=0;i<=99;i++)
   {
    if (tkaku[i]>-999)
    {
     if (i<50) {a1=1;} else {a1=2;}
     tkaku[i]=tkaku[i]+tkk[i]*a1;
     if (tkaku[i]<0) {tkaku[i]=tkaku[i]+360;}
     if (tkaku[i]>=360) {tkaku[i]=tkaku[i]-360;}
     tkyo[i]=tkyo[i]-1;
     xf=Mathf.Cos(tkaku[i]*Mathf.PI/180)*tkyo[i];yf=Mathf.Sin(tkaku[i]*Mathf.PI/180)*tkyo[i];
     z=i+43;objput2();
     if (tkyo[i]<=40)
     {
      if (i<20) {score1=score1-2;}
      if (i>=40 && i<70) {score1=score1+1;}
      if (i>=70 && i<80) {score1=score1+2;}
      if (i>=80 && i<90) {score1=score1+5;}
      if (i>=90) {score1=score1+10;}
      if (score1<0) {score1=0;}
      if (score1>100) {score1=100;}
      score2=score2+1;if (score2>100) {score2=100;}
      z=4;af=(float)score1/100;scaleset();
      z=5;af=(float)score2/100;scaleset();
      tkaku[i]=-1000;z=i+43;zx=-1000;zy=0;objput();
     }
     zf=25f;hithantei();
     if (ee>0)
     {
      xf=Mathf.Cos(tkaku[i]*Mathf.PI/180)*tkyo[i];yf=Mathf.Sin(tkaku[i]*Mathf.PI/180)*tkyo[i];
      z=i+143;objput2();
      tkaku[i+100]=tkaku[i];tkyo[i+100]=tkyo[i];tw[i+100]=20;
      tkaku[i]=-1000;z=i+43;zx=-1000;zy=0;objput();
     }
    }
   }
// games-bomb-del
   for (i=100;i<=199;i++)
   {
    if (tkaku[i]>-999)
    {
     tw[i]=tw[i]-1;
     if (tw[i]<=0)
     {
      z=i+43;zx=-1000;zy=0;objput();
      tkaku[i]=-1000;
     }
    }
   }
// player-put
   xf=Mathf.Cos(pkaku*Mathf.PI/180)*40;yf=Mathf.Sin(pkaku*Mathf.PI/180)*40;
   z=6;objput2();
   if (score1>=100)
   {
    bgm1.Stop();bgm2.Play();
    z=2;zx=0;zy=40;objput();
    wait=300;flg=3;
   }
   else
   {
    if (score2>=100)
    {
     bgm1.Stop();
     z=7;zx=-1000;zy=0;objput();
     for (i=8;i<=11;i++)
     {
      z=i;zx=0;zy=0;objput();a=0;scaleset2();
     }
     count=0;wait=2;flg=2;
    }
   }
  }
  if (flg==2)
  {
   for (i=0;i<=3;i++)
   {
    a=count-i*5;if (a<0 || a>10) {a=0;}
    z=i+8;scaleset2();
   }
   wait=wait-1;
   if (wait<=0) {count=count+1;wait=2;}
   if (count>26)
   {
    bgm3.Play();
    z=3;zx=0;zy=40;objput();
    wait=300;flg=3;
   }
  }
  if (flg==3)
  {
   wait=wait-1;
   if (wait<=0)
   {
    titleput();
    flg=0;
   }
  }
 }

 void gamestart()
 {
  alldel();spacewait=2;
  z=7;zx=0;zy=0;objput();
  z=4;zx=-100;zy=-215;objput();a=5;af=0f;scaleset();
  z=5;zx=-100;zy=-245;objput();a=5;af=0f;scaleset();
  pkaku=90;catkk=1;catkwait=Random.Range(100,200);catdnum=0;catdwait=1;autodnum=0;autodwait=51;
  score1=0;score2=0;
  catkaku[0]=90;catkaku[1]=-1000;catkaku[2]=-1000;catkaku[3]=-1000;
  for (i=0;i<=3;i++) {cathp[i]=10;catw[i]=0;}
  if (level==1) {catkaku[1]=270;}
  if (level==2) {catkaku[1]=210;catkaku[2]=330;}
  if (level==3) {catkaku[1]=180;catkaku[2]=270;catkaku[3]=0;}
  for (i=0;i<=9;i++) {bkaku[i]=-1000;bkyo[i]=0;}
  for (i=0;i<=199;i++) {tkaku[i]=-1000;tkyo[i]=0;tkk[i]=0;tw[i]=0;}
  stimef=Time.time;bgm1.Play();
  flg=1;spacewait=2;
 }

 void numcall()
 {
  a1=0;a2=-1;
  if (z==0) {a1=0;a2=20;}
  if (z==1) {a1=20;a2=40;}
  if (z==2) {a1=40;a2=50;}
  if (z==3) {a1=50;a2=70;}
  if (z==4) {a1=70;a2=80;}
  if (z==5) {a1=80;a2=90;}
  if (z==6) {a1=90;a2=100;}
  for (ii=a1;ii<a2;ii++)
  {
   if (tkaku[ii]<-999) {ee=ii;ii=101;}
  }
 }

 void hithantei()
 {
  ee=0;
  for (ii=0;ii<=9;ii++)
  {
   if (bkaku[ii]>-999)
   {
    axf=Mathf.Cos(bkaku[ii]*Mathf.PI/180)*bkyo[ii];ayf=Mathf.Sin(bkaku[ii]*Mathf.PI/180)*bkyo[ii];
    aaxf=xf-axf;aayf=yf-ayf;af=Mathf.Sqrt(aaxf*aaxf+aayf*aayf);
    if (af<zf)
    {
     bkaku[ii]=-1000;z=ii+33;zx=-1000;zy=0;objput();
     ee=1;ii=11;
    }
   }
  }
 }

 void titleput()
 {
  alldel();
  z=1;zx=0;zy=0;objput();
 }

 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 objput2()
 {
  Vector3 pos=obj[z].transform.position;
  pos.x=xf*0.01f;pos.y=yf*0.01f;
  obj[z].transform.position=pos;
 }

 void scaleset()
 {
  Vector3 scales=obj[z].transform.localScale;
  scales.x=af;
  obj[z].transform.localScale=scales;
 }

 void scaleset2()
 {
  Vector3 scales=obj[z].transform.localScale;
  scales.x=a;scales.y=a;
  obj[z].transform.localScale=scales;
 }

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