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

public class sekai_hana : MonoBehaviour
{
 int flg=0;int count=0;int spacewait=0;int level=0;int score=0;int nbyou=0;int shop=0;
 int komakazu=0;int komacount=0;int putban=0;int loser=0;int ans=-1;int mojinum=0;int kaitou=-1;
 int kh=1;int kcount=15;
 int[] p=new int[25];int[] selenum=new int[2];
 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 e1;int px;int py;int a1;int a2;int moves;int mousex;int mousey;
 float stimef;float ntimef;float xf;float yf;float stime2f;float ntime2f;
 string c1;string c2;string c3;string c4;
 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[281];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;AudioSource bgm4;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  obj[1]=GameObject.Find("hana-shutter"); // 1  shutter
  obj[2]=GameObject.Find("yane-1"); // 2  yane-1
  obj[3]=GameObject.Find("yane-2"); // 3  yane-2
  obj[4]=GameObject.Find("hana-shutter-moji-1"); // 4  kaiten
  obj[5]=GameObject.Find("hana-shutter-moji-2"); // 5  heiten
  obj[6]=GameObject.Find("hana-title"); // 6  title
  obj[7]=GameObject.Find("ya-1"); // 7  ya-1
  obj[8]=GameObject.Find("ya-2"); // 8  ya-2
  obj[9]=GameObject.Find("hana-selected-1"); // 9  selected-1
  obj[10]=GameObject.Find("hana-selected-2"); // 10  selected-2
  obj[11]=GameObject.Find("maru-1"); // 11  maru-1
  obj[12]=GameObject.Find("maru-2"); // 12  maru-2
  obj[13]=GameObject.Find("batu-1"); // 13  batu-1
  obj[14]=GameObject.Find("batu-2"); // 14  batu-2
  for (i=0;i<=9;i++)
  {
   obj[i+20]=GameObject.Find("num-"+i); // 20-29  nums-moto
  }
  for (i=0;i<=2;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+30]=Object.Instantiate(obj[j+20]) as GameObject;} // 20-59 nums
  }
  for (i=1;i<=50;i++)
  {
   obj[i+99]=GameObject.Find("hana-"+i); // 100-149  hanas-1st
   obj[i+149]=Object.Instantiate(obj[i+99]) as GameObject; // 150-199  hanas-2nd
  }
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();bgm4=obj[4].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="";
  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=mousex.ToString()+" "+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.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    if (shop==0) {shop=1;} else {shop=0;}
   }
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    if (shop==0) {shop=1;} else {shop=0;}
   }
   z=2;zx=-1000;zy=0;objput();z=3;objput();z=shop+2;zx=0;zy=150;objput();
   z=7;zx=-90;zy=124-level*40;objput();z=8;zx=shop*160-124;zy=-42;objput();
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
  }
  if (flg>=1 && flg<99)
  {
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f)
   {
    nbyou=nbyou-1;if (nbyou<0) {nbyou=0;}
    timeput();stime2f=stime2f+1f;
    if (nbyou<=0)
    {
     bgmstop();bgm4.Play();
     mojinum=2;flg=4;count=level*5+10;
    }
   }
   for (i=100;i<=199;i++) {obj[i].transform.Rotate(0,0,kh);}
   kcount=kcount+1;
   if (kcount>=30) {kh=kh*-1;kcount=0;}
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();flg=0;spacewait=2;}
  }
  if (flg==1)
  {
   count=count+1;
   z=1;zx=0;zy=count*10;objput();
   if (mojinum==1) {z=4;objput();}
   if (count>=level*5+10)
   {
    if (mojinum==1) {z=4;zx=-1000;zy=0;objput();mojinum=0;}
    flg=2;
   }
  }
  if (flg==2)
  {
   kaitou=-1;
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetMouseButtonDown(0)) {pic();}
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    px=px+1;
    if (px>4) {px=0;}
   }
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    px=px-1;
    if (px<0) {px=4;}
   }
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8))
   {
    py=py+1;
    if (py>level+1) {py=0;}
   }
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
   {
    py=py-1;
    if (py<0) {py=level+1;}
   }
   z=9;zx=px*50-100;zy=py*50-100;objput();
   if (Input.GetKeyDown(KeyCode.Space)) {kaitou=py*5+px;}
   if (kaitou>=0)
   {
    if (selenum[0]<0) {selenum[0]=kaitou;z=10;zx=px*50-100;zy=py*50-100;objput();}
    else
    {
     z=9;zx=-1000;zy=0;objput();z=10;objput();
     if (selenum[0]==kaitou) {selenum[0]=-1;}
     else
     {
      selenum[1]=kaitou;e1=0;
      for (i=0;i=300)
   {
    titleput();
    count=0;flg=0;spacewait=2;
   }
  }
 }

 void gamestart()
 {
  alldel();z=shop+2;zx=0;zy=150;objput();score=0;nbyou=50;scoreput();timeput();
  mojinum=1;kh=1;kcount=15;newstage();px=0;py=0;
  stimef=Time.time;stime2f=Time.time;bgmstop();bgm1.Play();
  flg=1;spacewait=2;
 }

 void newstage()
 {
  zx=-1000;zy=0;for (i=9;i<=14;i++) {z=i;objput();}
  for (i=100;i<=199;i++) {z=i;objput();}
  selenum[0]=-1;selenum[1]=-1;
  for (i=0;i<=24;i++) {p[i]=i+1;}
  for (i=0;i<1000;i++)
  {
   r1=Random.Range(0,25);r2=Random.Range(0,25);
   a=p[r1];p[r1]=p[r2];p[r2]=a;
  }
  a=level*5+10;for (i=a;i<=24;i++) {p[i]=0;}
  p[0]=p[1];ans=p[0];
  for (i=0;i<1000;i++)
  {
   r1=Random.Range(0,level*5+10);r2=Random.Range(0,level*5+10);
   a=p[r1];p[r1]=p[r2];p[r2]=a;
  }
  e1=0;
  for (i=0;i=515 && mousex<=764 && mousey>=235 && mousey<=484)
  {
   ax=mousex-515;ay=mousey-235;x=ax/50;y=ay/50;
   if (y<=level+1) {px=x;py=y;kaitou=py*5+px;}
  }
 }

 void scoreput()
 {
  for (i=20;i<=39;i++) {z=i;zx=-1000;zy=0;objput();}
  z=score/10+20;zx=200;zy=212;objput();
  z=score%10+30;zx=212;zy=212;objput();
 }

 void timeput()
 {
  for (i=40;i<=59;i++) {z=i;zx=-1000;zy=0;objput();}
  z=nbyou/10+40;zx=200;zy=190;objput();
  z=nbyou%10+50;zx=212;zy=190;objput();
 }

 void titleput()
 {
  alldel();z=6;zx=0;zy=0;objput();z=shop+2;zx=0;zy=150;objput();
  z=7;zx=-90;zy=124-level*40;objput();z=8;zx=shop*160-124;zy=-42;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();
 }

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