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

public class go_ninja : MonoBehaviour
{
 int flg=0;int count=0;int level=0;int wait=0;int spacewait=0;
 int kakudo;int kaku;int kyo;int target;int nisenum;
 int[] p=new int[24];int[] nnum=new int[6];
 int i;int j;int z;int zx;int zy;int r;int ee;int a;int y;int ax;int r1;int r2;
 int ii;int jj;int e1;int e2;
 float xf;float yf;
 float stimef;float ntimef;
 string c1;string c2;string c3;string d1;string d2;
 GUIStyle style1;GUIStyle style2;
 GameObject[] obj=new GameObject[313];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes-1"); // 0  ng-mes
  obj[1]=GameObject.Find("fairy-title"); // 1  title
  obj[2]=GameObject.Find("ninja-shuriken"); // 2  shuriken
  obj[3]=GameObject.Find("ninja"); // 3  ninja-moto
  obj[4]=GameObject.Find("ninja-out"); // 4  ninja-out
  for (i=0;i<=7;i++) {obj[i+5]=GameObject.Find("ninja-mes-"+i);} // 5 - 12  mes
  for (i=1;i<=8;i++) {obj[i+12]=GameObject.Find("ninja-moji-"+i);} // 13 - 20  moji
  for (i=0;i<=3;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+21]=GameObject.Find("ninja-colors-"+i+"-"+j);} // 21 - 60  colors-moto
  }
  for (i=0;i<=39;i++)
  {
   for (j=0;j<=5;j++) {obj[i*6+j+61]=Object.Instantiate(obj[i+21]) as GameObject;} // 61 - 300  colors-copy
  }
  for (i=0;i<=5;i++) {obj[i+301]=Object.Instantiate(obj[3]) as GameObject;} // 301 - 306  ninja-copy
  for (i=0;i<=5;i++) {obj[i+307]=Object.Instantiate(obj[19]) as GameObject;} // 307 - 312  moji-shin-copy
  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();
  count=500;level=0;flg=-1;
 }

 void Update ()
 {
  Vector3 mpos=Input.mousePosition;
  xf=mpos.x;yf=mpos.y;
  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.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
   if (Input.GetMouseButtonDown(0))
   {
    a=-1;
    if (xf>546f && xf<710f)
    {
     if (yf>424f && yf<459f) {a=0;}
     if (yf>384f && yf<419f) {a=1;}
     if (yf>344f && yf<379f) {a=2;}
     if (yf>304f && yf<339f) {a=3;}
    }
    if (a>=0) {level=a;gamestart();}
   }
   if (flg==0)
   {
    z=2;zx=-82;zy=60-level*40;objput();
   }
  }
  if (flg==1)
  {
   wait=wait-1;
   if (wait<=0)
   {
    z=5;zx=-1000;zy=0;objput();z=6;zx=0;zy=-200;objput();
    kaku=0;count=Random.Range(10,16);flg=2;
   }
  }
  if (flg==2)
  {
   kaku=kaku+6;if (kaku%72==0) {count=count-1;}
   if (kaku>=360) {kaku=0;}
   for (i=0;i<=4;i++)
   {
    kakudo=(450-kaku-i*72)%360;
    xf=Mathf.Cos(kakudo*Mathf.PI/180)*150;yf=Mathf.Sin(kakudo*Mathf.PI/180)*150;
    z=i+301;objput2();
    for (j=0;j<=3;j++)
    {
     a=p[nnum[i]*4+j];
     z=61+j*60+a*6+i;objput2();
    }
   }
   if (count<=0)
   {
    z=6;zx=-1000;zy=0;objput();z=7;zx=0;zy=-200;objput();
    kyo=150;count=1;flg=3;
   }
  }
  if (flg==3)
  {
   kyo=kyo-2;
   for (i=0;i<=4;i++)
   {
    kakudo=(450-kaku-i*72)%360;
    xf=Mathf.Cos(kakudo*Mathf.PI/180)*kyo;yf=Mathf.Sin(kakudo*Mathf.PI/180)*kyo;
    z=i+301;objput2();
    for (j=0;j<=3;j++)
    {
     a=p[nnum[i]*4+j];
     z=61+j*60+a*6+i;objput2();
    }
   }
   if (kyo<=0)
   {
    for (i=0;i<=4;i++)
    {
     for (j=0;j<=3;j++)
     {
      a=p[nnum[i]*4+j];
      z=61+j*60+a*6+i;zx=-1000;zy=0;objput();
     }
    }
    for (i=0;i<100;i++)
    {
     r1=Random.Range(0,6);r2=Random.Range(0,6);
     a=nnum[r1];nnum[r1]=nnum[r2];nnum[r2]=a;
    }
    z=7;zx=-1000;zy=0;objput();z=8;zx=0;zy=-200;objput();
    kyo=0;count=1;flg=4;
   }
  }
  if (flg==4)
  {
   kyo=kyo+2;
   for (i=0;i<=5;i++)
   {
    kakudo=(420-i*60)%360;
    xf=Mathf.Cos(kakudo*Mathf.PI/180)*kyo;yf=Mathf.Sin(kakudo*Mathf.PI/180)*kyo;
    z=i+301;objput2();
    for (j=0;j<=3;j++)
    {
     a=p[nnum[i]*4+j];
     z=61+j*60+a*6+i;objput2();
    }
   }
   if (kyo>=150)
   {
    z=8;zx=-1000;zy=0;objput();z=9;zx=0;zy=-200;objput();
    wait=100;flg=5;
   }
  }
  if (flg==5)
  {
   wait=wait-1;
   if (wait<=0)
   {
    for (i=0;i<=5;i++)
    {
     kakudo=(420-i*60)%360;
     xf=Mathf.Cos(kakudo*Mathf.PI/180)*150;yf=Mathf.Sin(kakudo*Mathf.PI/180)*150-40.0f;
     z=i+13;objput2();
    }
    z=9;zx=-1000;zy=0;objput();z=10;zx=0;zy=-200;objput();
    flg=6;
   }
  }
  if (flg==6)
  {
   a=0;
   if (Input.GetKeyDown(KeyCode.Keypad1)) {a=1;}
   if (Input.GetKeyDown(KeyCode.Keypad2)) {a=2;}
   if (Input.GetKeyDown(KeyCode.Keypad3)) {a=3;}
   if (Input.GetKeyDown(KeyCode.Keypad4)) {a=4;}
   if (Input.GetKeyDown(KeyCode.Keypad5)) {a=5;}
   if (Input.GetKeyDown(KeyCode.Keypad6)) {a=6;}
   if (Input.GetKeyDown(KeyCode.Alpha1)) {a=1;}
   if (Input.GetKeyDown(KeyCode.Alpha2)) {a=2;}
   if (Input.GetKeyDown(KeyCode.Alpha3)) {a=3;}
   if (Input.GetKeyDown(KeyCode.Alpha4)) {a=4;}
   if (Input.GetKeyDown(KeyCode.Alpha5)) {a=5;}
   if (Input.GetKeyDown(KeyCode.Alpha6)) {a=6;}
   if (Input.GetMouseButtonDown(0))
   {
    if (xf>690f && xf<741f && yf>444f && yf<519f) {a=1;}
    if (xf>764f && xf<815f && yf>314f && yf<389f) {a=2;}
    if (xf>690f && xf<741f && yf>184f && yf<259f) {a=3;}
    if (xf>540f && xf<591f && yf>184f && yf<259f) {a=4;}
    if (xf>464f && xf<515f && yf>314f && yf<389f) {a=5;}
    if (xf>540f && xf<591f && yf>444f && yf<519f) {a=6;}
   }
   if (a>0)
   {
    for (i=0;i<=5;i++) {z=i+13;zx=-1000;zy=0;objput();}
    z=2;zx=0;zy=0;objput();z=10;zx=-1000;zy=0;objput();
    kakudo=(420-(a-1)*60)%360;kyo=0;
    target=a-1;flg=7;
   }
  }
  if (flg==7)
  {
   kyo=kyo+5;
   xf=Mathf.Cos(kakudo*Mathf.PI/180)*kyo;yf=Mathf.Sin(kakudo*Mathf.PI/180)*kyo;
   z=2;objput2();
   if (kyo>=150)
   {
    z=target+301;zx=-1000;zy=0;objput();
    xf=Mathf.Cos(kakudo*Mathf.PI/180)*150;yf=Mathf.Sin(kakudo*Mathf.PI/180)*150;
    z=4;objput2();
    a=0;
    for (i=0;i<=5;i++)
    {
     kakudo=(420-i*60)%360;
     xf=Mathf.Cos(kakudo*Mathf.PI/180)*150;yf=Mathf.Sin(kakudo*Mathf.PI/180)*150-40.0f;
     if (nnum[i]==nisenum) {z=20;} else {z=307+a;a=a+1;}
     objput2();
    }
    if (nnum[target]==nisenum)
    {
     bgm1.Stop();bgm2.Play();
     z=11;zx=0;zy=-200;objput();
    }
    else
    {
     bgm1.Stop();bgm3.Play();
     z=12;zx=0;zy=-200;objput();
    }
    wait=300;flg=8;
   }
  }
  if (flg==8)
  {
   wait=wait-1;
   if (wait<=0)
   {
    titleput();
    flg=0;
   }
  }
 }

 void gamestart()
 {
  alldel();spacewait=2;
  newmon();
  for (i=0;i<=4;i++)
  {
   kakudo=(450-i*72)%360;
   xf=Mathf.Cos(kakudo*Mathf.PI/180)*150;yf=Mathf.Sin(kakudo*Mathf.PI/180)*150;
   z=i+301;objput2();
   for (j=0;j<=3;j++)
   {
    a=p[nnum[i]*4+j];
    z=61+j*60+a*6+i;objput2();
   }
  }
  z=5;zx=0;zy=-200;objput();
  bgm1.Play();
  wait=100;flg=1;
 }

 void newmon()
 {
  for (i=0;i<=23;i++) {p[i]=0;}
  e1=0;
  for (ii=0;ii<2;ii++)
  {
   for (i=0;i<=5;i++)
   {
    for (jj=0;jj<2;jj++)
    {
     e2=0;
     for (j=0;j<=3;j++)
     {
      r=Random.Range(1,10);
      p[i*4+j]=r;
     }
     if (p[i*4]==p[i*4+1]) {e2=1;}
     if (p[i*4]==p[i*4+2]) {e2=1;}
     if (p[i*4]==p[i*4+3]) {e2=1;}
     if (p[i*4+1]==p[i*4+2]) {e2=1;}
     if (p[i*4+1]==p[i*4+3]) {e2=1;}
     if (p[i*4+2]==p[i*4+3]) {e2=1;}
     if (e2>0) {jj=0;} else {jj=11;}
    }
    if (level==0) {p[i*4+1]=p[i*4];p[i*4+2]=p[i*4];p[i*4+3]=p[i*4];}
    if (level==1) {p[i*4+1]=p[i*4];p[i*4+3]=p[i*4+2];}
    if (level==2) {p[i*4+3]=p[i*4+2];}
   }
   e1=0;
   for (i=0;i<=5;i++)
   {
    for (j=0;j<=5;j++)
    {
     if (i!=j)
     {
      if (p[i*4]==p[j*4] && p[i*4+1]==p[j*4+1] && p[i*4+2]==p[j*4+2] && p[i*4+3]==p[j*4+3]) {e1=1;}
     }
    }
   }
   if (e1>0) {ii=0;} else {ii=11;}
  }
  for (i=0;i<=5;i++) {nnum[i]=i;}
  for (i=0;i<100;i++)
  {
   r1=Random.Range(0,6);r2=Random.Range(0,6);
   a=nnum[r1];nnum[r1]=nnum[r2];nnum[r2]=a;
  }
  nisenum=nnum[5];
 }

 void titleput()
 {
  alldel();
  z=1;zx=0;zy=0;objput();z=2;zx=-82;zy=60;objput();z=127;
  c1="";
 }

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