using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class screensize
{
 [RuntimeInitializeOnLoadMethod]
 static void OnRuntimeMethodLoad()
 {
  Screen.SetResolution(1280,720,false);
 }
}
public class pinpiro : MonoBehaviour
{
 int flg=-1;int count=0;int wait=0;int spacewait=0;int level=1;int autosai=0;
 int score=0;int yubicount=0;int menasi=0;int me=-1;int fire=0;
 int[] yubisx={-334,334,-302,302,-270,270,-238,238,-206,206};
 int[] yubisy={-10,-10,25,25,40,40,25,25,-32,-32};
 int[] yubix=new int[10];int[] yubiy=new int[10];
 int[] sais=new int[3];int[] saiss=new int[3];int[] saik=new int[3];int[] saikk=new int[3];
 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 jj;int kk;int a1;int a2;int a3;
 int e;int e1;int moves;int mousex;int mousey;
 float stimef;float ntimef;float stime2f;float ntime2f;float stime3f;float ntime3f;float stime4f;float ntime4f;
 float xf;float yf;float af;float kakuf;float axf;float ayf;
 string c1;string c2;string c3;string c4;
 string d1;string keyname2="bignyan_mousefix";string[] dts=new string[2];
 int mfx=640;int mfy=360;
 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[200];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;AudioSource bgm4;
 AudioSource bgm5;AudioSource bgm6;AudioSource bgm7;AudioSource bgm8;

 void Start()
 {
  dataload();
  style1=new GUIStyle();style1.fontSize=40;
  style2=new GUIStyle();style2.fontSize=16;
  style3=new GUIStyle();style3.fontSize=24;style3.normal.textColor=Color.red;
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  for (i=1;i<=12;i++) {obj[i]=GameObject.Find("yubi-"+i);} // 1-12  yubi
  for (i=0;i<=11;i++) {obj[i+13]=GameObject.Find("pinpiro-hand-"+i+"-2");} // 13-24  hand
  for (i=1;i<=4;i++) {obj[i+24]=GameObject.Find("pinpiro-button-"+i);} // 25-28  buttons
  for (i=1;i<=6;i++)
  {
   obj[i+28]=GameObject.Find("saikoro-"+i); // 29-34  saikoro-moto
   for (j=1;j<=2;j++) {obj[j*6+i+28]=Object.Instantiate(obj[i+28]) as GameObject;} // 35-46  saikoro-copy
  }
  obj[47]=GameObject.Find("pinpiro-title"); // 47  title
  obj[48]=GameObject.Find("ya-r-32x32"); // 48  ya
  obj[49]=GameObject.Find("gameover-mes-1"); // 49  gameover-mes-blue
  obj[50]=GameObject.Find("gameover-mes-2"); // 50  gameover-mes-green
  obj[97]=GameObject.Find("mousefix-1"); // 97  mousefix-black
  obj[98]=GameObject.Find("mousefix-2"); // 98  mousefix-red
  obj[99]=GameObject.Find("mousefix-moji"); // 99  mousefix-moji
  for (i=0;i<=9;i++)
  {
   obj[i+100]=GameObject.Find("num-16a-"+i); // 100-109  nums-moto
   for (j=1;j<=4;j++) {obj[j*10+i+100]=Object.Instantiate(obj[i+100]) as GameObject;} // 110-149  nums-copy
  }
  alldel();
  z=100;zx=305;zy=-218;objput();z=0;zx=0;zy=0;objput();
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();bgm4=obj[4].GetComponent();
  bgm5=obj[5].GetComponent();bgm6=obj[6].GetComponent();
  bgm7=obj[7].GetComponent();bgm8=obj[8].GetComponent();
  count=500;flg=-1;
 }

 void Update()
 {
  Vector3 mpos=Input.mousePosition;
  xf=mpos.x;yf=mpos.y;
  if (flg>-9) {xf=xf*640/mfx;yf=yf*360/mfy;}
  mousex=(int)xf;mousey=(int)yf;
//  c1="X="+mousex.ToString()+" Y="+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;
   }
  }
  if (flg==0)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (spacewait<=0)
   {
    if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8))
    {
     level=level-1;if (level<0) {level=2;}
    }
    if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
    {
     level=level+1;if (level>2) {level=0;}
    }
    if (Input.GetKeyDown(KeyCode.Space)) {gamestart();}
    if (Input.GetKeyDown(KeyCode.M))
    {
     alldel();z=97;zx=0;zy=0;objput();z=98;objput();
     flg=-9;spacewait=2;
    }
    if (Input.GetMouseButtonDown(0))
    {
     if (mousex>=480 && mousex<=648)
     {
     if (mousey>=420 && mousey<=459) {level=0;gamestart();}
     if (mousey>=380 && mousey<=419) {level=1;gamestart();}
     if (mousey>=340 && mousey<=379) {level=2;gamestart();}
     }
     if (mousex>=513 && mousex<=772 && mousey>=184 && mousey<=217)
     {
      alldel();z=97;zx=0;zy=0;objput();z=98;objput();
      flg=-9;spacewait=2;
     }
    }
   }
   if (flg==0) {z=48;zx=-174;zy=80-level*40;objput();}
  }
  if (flg==1)
  {
   e=0;
   if (spacewait>0) {spacewait=spacewait-1;}
   if (spacewait<=0)
   {
    if (Input.GetKeyDown(KeyCode.Space)) {e=1;}
    if (Input.GetMouseButtonDown(0))
    {
     if (mousex>=280 && mousex<=379 && mousey>=112 && mousey<=166) {e=1;}
    }
   }
   wait=wait-autosai;
   if (autosai>0 && wait<=0) {e=1;}
   if (e>0)
   {
    bgm8.Play();
    newsai();flg=2;count=60;
   }
  }
  if (flg==2)
  {
   for (i=29;i<=46;i++)
   {
    z=i;zx=-2000;zy=0;objput();
    obj[z].transform.rotation=Quaternion.identity;
   }
   count=count-1;
   for (i=0;i<=2;i++)
   {
    saik[i]=saik[i]-4;if (saik[i]<0) {saik[i]=saik[i]+360;}
    saikk[i]=saikk[i]+8;if (saikk[i]>=360) {saikk[i]=saikk[i]-360;}
    xf=Mathf.Cos(saik[i]*Mathf.PI/180)*100;yf=Mathf.Sin(saik[i]*Mathf.PI/180)*100;
    r1=Random.Range(1,6);if (r1>5) {r1=1;}
    saiss[i]=saiss[i]+r1;if (saiss[i]>6) {saiss[i]=saiss[i]-6;}
    if (count<=0) {saiss[i]=sais[i];}
    z=i*6+saiss[i]+28;objput2();
    obj[z].transform.Rotate(0,0,saikk[i]);
   }
   if (count<=0) {hantei();}
  }
  if (flg==3 || flg==9)
  {
   a=yubicount;
   if (fire==0) {fire=1;} else {fire=0;}
   z=11;zx=-2000;zy=0;objput();z=12;objput();
   yubiy[a]=yubiy[a]+4;
   z=a+1;zx=yubix[a];zy=yubiy[a];objput();z=fire+11;objput();
   if (yubiy[a]>=240)
   {
    yubiy[a]=240;
   }
   if (flg==3)
   {
    wait=wait-1;
    if (wait<=0 && yubiy[a]>=240) {yubicount=yubicount+1;flg=1;wait=0;}
   }
  }
  if (flg>=1 && flg<9)
  {
// bgm-replay
   ntimef=Time.time;
   if (ntimef-stimef>=52f) {bgm1.Play();stimef=Time.time;}
   e=0;
   if (Input.GetKeyDown(KeyCode.Return)) {e=1;}
   if (Input.GetMouseButtonDown(0))
   {
    if (mousex>=400 && mousex<=499 && mousey>=112 && mousey<=166) {e=1;}
   }
   if (e>0)
   {
    z=26;zx=-2000;zy=0;objput();z=27;objput();
    if (autosai==0) {autosai=1;wait=0;} else {autosai=0;}
    z=autosai+26;zx=-190;zy=-220;objput();
   }
  }
  if (flg==9)
  {
   ntime2f=Time.time;
   if (ntime2f-stime2f>=0.5f)
   {
    stime2f=stime2f+0.5f;count=count+1;
    z=49;zx=-2000;zy=0;objput();z=50;objput();
    z=count%2+49;zx=0;zy=0;objput();
    if (count>=12) {titleput();}
   }
  }
  if (flg>0)
  {
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();}
   if (Input.GetMouseButtonDown(0))
   {
    if (mousex>=520 && mousex<=619 && mousey>=112 && mousey<=166) {titleput();}
   }
  }
  if (flg==-9)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetMouseButtonDown(0) && spacewait<=0)
   {
    mfx=mousex;mfy=mousey;
    datasave();titleput();
   }
  }
 }

 void gamestart()
 {
  alldel();
  for (i=0;i<=9;i++)
  {
   yubix[i]=yubisx[i];yubiy[i]=yubisy[i];
   z=i+1;zx=yubisx[i];zy=yubisy[i];objput();
  }
  score=0;yubicount=0;autosai=0;menasi=0;scoreput();
  z=25;zx=-310;zy=-220;objput();z=26;zx=-190;objput();z=28;zx=-70;objput();
  bgmstop();bgm1.Play();
  stimef=Time.time;stime2f=Time.time;
  newsai();flg=1;spacewait=2;
 }

 void newsai()
 {
  for (i=29;i<=46;i++)
  {
   z=i;zx=-2000;zy=0;objput();
   obj[z].transform.rotation=Quaternion.identity;
  }
  saik[0]=90;saik[1]=210;saik[2]=330;saikk[0]=0;saikk[1]=120;saikk[2]=240;
  for (i=0;i<=2;i++)
  {
   r1=Random.Range(1,7);if (r1>6) {r1=1;}
   r2=Random.Range(1,7);if (r2>6) {r2=1;}
   sais[i]=r1;saiss[i]=r2;
   r1=Random.Range(0,2);if (r1>2) {r1=1;}
   if (r1==0)
   {
    if (level==0) {sais[i]=i+4;}
    if (level==2) {sais[i]=i+1;}
   }
  }
  for (i=0;i<=99;i++)
  {
   r1=Random.Range(0,3);if (r1>2) {r1=0;}
   r2=Random.Range(0,3);if (r2>2) {r2=1;}
   a=sais[r1];sais[r1]=sais[r2];sais[r2]=a;
  }
 }

 void hantei()
 {
  a1=sais[0];a2=sais[1];a3=sais[2];me=-1;
  if (a1==a2) {me=a3;}
  if (a1==a3) {me=a2;}
  if (a2==a3) {me=a1;}
  if (a1==a2 && a2==a3) {me=20;}
  if (me<1)
  {
   if (a1>=4 && a2>=4 && a3>=4) {me=10;}
   if (a1<=3 && a2<=3 && a3<=3) {me=-2;}
  }
  if (me==-1)
  {
   menasi=menasi+1;
   if (menasi>=3) {me=-2;}
  }
  flg=1;wait=60;
  if (me>=1) {score=score+me;scoreput();}
  if (me>=1 && me<=6) {bgm2.Play();}
  if (me==10) {bgm3.Play();}
  if (me==20) {bgm4.Play();}
  if (me==-1) {bgm5.Play();} else {menasi=0;}
  if (me==-2)
  {
   for (i=13;i<=24;i++) {z=i;zx=-2000;zy=0;objput();}
   if (yubicount%2==0)
   {
    z=yubicount+15;zx=-270;zy=-39;objput();
    z=yubicount+14;zx=270;zy=-39;objput();
   }
   else
   {
    z=yubicount+14;zx=-270;zy=-39;objput();
    z=yubicount+15;zx=270;zy=-39;objput();
   }
   fire=0;
   if (yubicount<9)
   {
    bgm6.Play();flg=3;wait=60;
   }
   else
   {
    z=49;zx=0;zy=0;objput();
    flg=9;stime2f=Time.time;count=0;
    bgmstop();bgm7.Play();
   }
  }
 }

 void scoreput()
 {
  if (score<0) {score=0;}
  if (score>9999) {score=9999;}
  for (i=100;i<=149;i++) {z=i;zx=-2000;zy=0;objput();}
  zy=-218;aa=0;
  a=score/1000;if (a>0) {aa=1;z=a+100;zx=245;objput();}
  a=(score%1000)/100;if (a>0 || aa>0) {aa=1;z=a+110;zx=265;objput();}
  a=(score%100)/10;if (a>0 || aa>0) {aa=1;z=a+120;zx=285;objput();}
  a=score%10;z=a+130;zx=305;objput();
 }

 void titleput()
 {
  alldel();bgmstop();flg=0;spacewait=2;
  z=47;zx=0;zy=0;objput();z=48;zx=-174;zy=80-level*40;objput();
  z=13;zx=-270;zy=-39;objput();z=14;zx=270;zy=-39;objput();
  for (i=0;i<=9;i++) {z=i+1;zx=yubisx[i];zy=yubisy[i];objput();}
  z=100;zx=305;zy=-218;objput();z=99;zx=0;zy=-160;objput();
  bgmstop();flg=0;spacewait=2;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (300,80,100,30),c1,style1);
  GUI.Label (new Rect (300,160,100,30),c2,style2);
  GUI.Label (new Rect (300,240,100,30),c3,style1);
  GUI.Label (new Rect (300,320,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 objput2()
 {
  Vector3 pos=obj[z].transform.position;
  pos.x=xf*0.01f;pos.y=yf*0.01f;
  obj[z].transform.position=pos;
 }

 void bgmstop()
 {
  bgm1.Stop();bgm2.Stop();bgm3.Stop();bgm4.Stop();
  bgm5.Stop();bgm6.Stop();bgm7.Stop();bgm8.Stop();
 }

 void alldel()
 {
  zx=-2000;zy=0;
  for (i=0;i<=50;i++) {z=i;objput();}
  for (i=97;i<=99;i++) {z=i;objput();}
  for (i=100;i<=149;i++) {z=i;objput();}
  for (i=29;i<=46;i++) {obj[i].transform.rotation=Quaternion.identity;}
 }

 void datasave()
 {
  d1=mfx.ToString()+"/"+mfy.ToString();
  PlayerPrefs.SetString(keyname2,d1);
  PlayerPrefs.Save();
 }

 void dataload()
 {
  d1=PlayerPrefs.GetString(keyname2,"nothing");
  if (d1!="nothing")
  {
   dts=d1.Split('/');
   mfx=int.Parse(dts[0]);mfy=int.Parse(dts[1]);
  }
  else
  {
   mfx=640;mfy=360;
  }
 }
}