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

public class supply_fuel : MonoBehaviour
{
 int flg=0;int count=0;int level=0;int plnum=0;int wait=0;int spacewait=0;int outflg=0;
 int px;int py;int ph;int pfuel;int pdan;int oily;int oilwait;int tdwait;
 int[] tfuel=new int[5];int[] dx=new int[10];int[] dy=new int[10];
 int[] tdx=new int[50];int[] tdy=new int[50];int[] tdj=new int[50];int[] tdw=new int[50];
 int[] speeds={3,4,2};int[] maxfuel={2000,1500,1000};int[] maxdans={20,40,10};
 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 af;
 float stimef;float ntimef;
 string c1;string c2;string c3;string d1;string d2;
 GUIStyle style1;GUIStyle style2;
 GameObject[] obj=new GameObject[154];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes-1"); // 0  ng-mes
  for (i=1;i<=6;i++) {obj[i]=GameObject.Find("supply-"+i);} // 1 - 6  supply
  for (i=1;i<=2;i++) {obj[i+6]=GameObject.Find("tank-"+i);} // 7 - 8  tank-moto
  for (i=1;i<=3;i++) {obj[i+8]=GameObject.Find("dan-"+i);} // 9 - 11  dan-moto
  for (i=1;i<=2;i++) {obj[i+11]=GameObject.Find("supply-fuel-"+i);} // 12 - 13  fuel-meter-moto
  obj[14]=GameObject.Find("fuki"); // 14  fuki
  obj[15]=GameObject.Find("oil"); // 15  oil
  obj[16]=GameObject.Find("whale-title"); // 16  title
  for (i=1;i<=2;i++) {obj[i+16]=GameObject.Find("ya-r-32-"+i);} // 17 - 18  ya-r
  for (i=0;i<=4;i++) {obj[i+19]=Object.Instantiate(obj[7]) as GameObject;} // 19 - 23  tank-sleep-copy
  for (i=0;i<=4;i++) {obj[i+24]=Object.Instantiate(obj[8]) as GameObject;} // 24 - 28  tank-full-copy
  for (i=0;i<=4;i++) {obj[i+29]=Object.Instantiate(obj[12]) as GameObject;} // 29 - 33  meter-red-copy
  for (i=0;i<=4;i++) {obj[i+34]=Object.Instantiate(obj[13]) as GameObject;} // 34 - 38  meter-green-copy
  for (i=0;i<=9;i++) {obj[i+39]=Object.Instantiate(obj[10]) as GameObject;} // 39 - 48  pl-dan-copy
  for (i=0;i<=49;i++) {obj[i+49]=Object.Instantiate(obj[9]) as GameObject;} // 49 - 98  enemy-dan-copy
  for (i=0;i<=49;i++) {obj[i+99]=Object.Instantiate(obj[11]) as GameObject;} // 99 - 148  dan-bomb-copy
  for (i=0;i<=2;i++) {obj[i+149]=GameObject.Find("supply-info-"+i);} // 149 - 151  info
  obj[152]=GameObject.Find("gameclear-mes-1"); // 152  gameover-mes
  obj[153]=GameObject.Find("gameover-mes-1"); // 153  gameclear-mes
  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 ()
 {
  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.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    plnum=plnum+1;
    if (plnum>2) {plnum=0;}
   }
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    plnum=plnum-1;
    if (plnum<0) {plnum=2;}
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0)
    {
     alldel();
     gamestart();
    }
   }
   if (flg==0)
   {
    z=17;zx=-90;zy=104-level*40;objput();z=18;zx=plnum*100-146;zy=-68;objput();
    for (i=149;i<=151;i++) {z=i;zx=-1000;zy=0;objput();}
    z=plnum+149;zx=335;zy=-55;objput();
   }
  }
  if (flg==1)
  {
   ntimef=Time.time;
   if (ntimef-stimef>=27.0f)
   {
    stimef=Time.time;bgm1.Play();
   }
// player-move
   if (spacewait>0) {spacewait=spacewait-1;}
    a=0;
   if (spacewait<=0)
   {
    if (Input.GetKeyDown(KeyCode.Keypad8)) {a=3;}
    if (Input.GetKeyDown(KeyCode.UpArrow)) {a=3;}
    if (Input.GetKeyDown(KeyCode.Keypad2)) {a=4;}
    if (Input.GetKeyDown(KeyCode.DownArrow)) {a=4;}
    if (Input.GetKey(KeyCode.Keypad6)) {a=1;}
    if (Input.GetKey(KeyCode.RightArrow)) {a=1;}
    if (Input.GetKey(KeyCode.Keypad4)) {a=2;}
    if (Input.GetKey(KeyCode.LeftArrow)) {a=2;}
    if (Input.GetKeyDown(KeyCode.Space)) {a=5;}
   }
   if (a==1 && px<246) {px=px+speeds[plnum];ph=1;pfuel=pfuel-1;if (px>246) {px=246;}}
   if (a==2 && px>-346) {px=px-speeds[plnum];ph=2;pfuel=pfuel-1;if (px<-346) {px=-346;}}
   if (a==3 && py>0) {py=py-1;pfuel=pfuel-10;}
   if (a==4 && py<4) {py=py+1;pfuel=pfuel-10;}
   if (a==5) {shot();}
   if (pfuel<0) {pfuel=0;}
// pl-dan-move
   for (i=0;i<=9;i++)
   {
    if (dx[i]>-1000)
    {
     dx[i]=dx[i]+5;if (dx[i]>=406) {dx[i]=-1000;}
     z=39+i;zx=dx[i];zy=95-dy[i]*50;objput();
    }
   }
// t-dan-move
   tdwait=tdwait-1;
   if (tdwait<=0) {newtdan();}
   for (i=0;i<=49;i++)
   {
    if (tdx[i]>-1000)
    {
     if (tdj[i]==1)
     {
      tdx[i]=tdx[i]-5;if (tdx[i]<=-406) {tdx[i]=-1000;}
      z=49+i;zx=tdx[i];zy=95-tdy[i]*50;objput();
     }
     else
     {
      tdw[i]=tdw[i]-1;if (tdw[i]<=0) {tdx[i]=-1000;}
      z=99+i;zx=tdx[i];zy=95-tdy[i]*50;objput();
     }
    }
   }
// dan-hit
   for (i=0;i<=9;i++)
   {
    if (dx[i]>-999)
    {
     for (j=0;j<=49;j++)
     {
      if (tdx[j]>-999 && tdj[j]==1)
      {
       if (Mathf.Abs(dx[i]-tdx[j])<=10 && dy[i]==tdy[j])
       {
        z=39+i;zx=-1000;zy=0;objput();z=49+j;objput();
        z=99+j;zx=tdx[j];zy=95-tdy[j]*50;objput();
        dx[i]=-1000;tdj[j]=2;tdw[j]=20;
        j=101;
       }
      }
     }
    }
   }
// oil-move
   oilwait=oilwait-1;
   if (oilwait<=0)
   {
    if (oily<0)
    {
     oily=Random.Range(0,5);
     z=15;zx=-388;zy=100-oily*50;objput();
     oilwait=500;
    }
    else
    {
     oily=-1;
     z=15;zx=-1000;zy=0;objput();
     oilwait=500;
    }
   }
   z=14;zx=-1000;zy=0;objput();
// oil-player-supply
   if (px<=-346 && py==oily)
   {
    pdan=maxdans[plnum];
    if (pfuelmaxfuel[plnum]) {pfuel=maxfuel[plnum];}
    }
   }
// player-tank-supply
   if (px>=246 && tfuel[py]<200)
   {
    z=14;zx=292;zy=117-py*50;objput();
    tfuel[py]=tfuel[py]+1;pfuel=pfuel-1;
    z=py+34;af=(float)tfuel[py]/200;scaleset();
    if (tfuel[py]>=200)
    {
     z=py+19;zx=-1000;zy=0;objput();
     z=py+24;zx=300;zy=93-py*50;objput();
    }
   }
// out-hantei
   for (i=0;i<=49;i++)
   {
    if (tdy[i]==py && tdx[i]>-1000 && tdj[i]==1)
    {
     if (Mathf.Abs(px-tdx[i])<30)
     {
      z=49+i;zx=-1000;zy=0;objput();
      z=99+i;zx=tdx[i];zy=95-tdy[i]*50;objput();
      outflg=1;
     }
    }
   }
   if (pfuel<=0) {outflg=1;}
// clear-hantei
   a=0;
   for (i=0;i<=4;i++)
   {
    if (tfuel[i]>=200) {a=a+1;}
   }
   if (a>=5 && outflg<=0)
   {
    z=152;zx=0;zy=0;objput();
    z=14;zx=-1000;zy=0;objput();
    for (i=39;i<=148;i++) {z=i;objput();}
    for (i=29;i<=38;i++) {z=i;objput();}
    bgm1.Stop();bgm2.Play();
    flg=2;count=0;
   }
// player-put
   z=plnum+1;zx=px;zy=93-py*50;objput();
   obj[z].transform.rotation=Quaternion.identity;
   if (ph==2) {obj[z].transform.Rotate(0,180,0);}
   z=12;zx=px-26;zy=77-py*50;objput();z=13;objput();af=(float)pfuel/maxfuel[plnum];scaleset();
// game-over
   if (outflg>0)
   {
    z=153;zx=0;zy=0;objput();
    z=plnum+1;zx=-1000;zy=0;objput();
    z=plnum+4;zx=px;zy=93-py*50;objput();
    obj[z].transform.rotation=Quaternion.identity;
    if (ph==2) {obj[z].transform.Rotate(0,180,0);}
    bgm1.Stop();bgm3.Play();
    flg=3;wait=300;
   }
  }
  if (flg==2)
  {
   count=count+1;
   for (i=0;i<=4;i++)
   {
    z=i+24;zx=count+300;zy=93-i*50;objput();
   }
   if (count>=130) {flg=3;wait=170;}
  }
  if (flg==3)
  {
   wait=wait-1;
   if (wait<=0)
   {
    titleput();
    flg=0;
   }
  }
 }

 void gamestart()
 {
  alldel();spacewait=2;
  px=-346;py=2;ph=1;pfuel=maxfuel[plnum];pdan=maxdans[plnum];
  z=plnum+1;zx=px;zy=93-py*50;objput();
  z=12;zx=px-26;zy=77-py*50;objput();z=13;objput();af=1f;scaleset();
  for (i=0;i<=4;i++)
  {
   z=i+19;zx=300;zy=93-i*50;objput();
   z=i+29;zx=274;zy=77-i*50;objput();z=i+34;objput();af=0f;scaleset();
  }
  for (i=0;i<=9;i++) {dx[i]=-1000;dy[i]=0;}
  for (i=0;i<=49;i++) {tdx[i]=-1000;tdy[i]=0;tdj[i]=0;tdw[i]=0;}
  for (i=0;i<=4;i++) {tfuel[i]=0;}
  oily=-1;oilwait=500;tdwait=20*(5-level);
  stimef=Time.time;bgm1.Play();
  flg=1;spacewait=2;outflg=0;
 }

 void shot()
 {
  if (flg==1 && pdan>0 && ph==1)
  {
   ee=-1;
   for (i=0;i<=9;i++)
   {
    if (dx[i]<-999) {ee=i;i=11;}
   }
   if (ee>=0) {dx[ee]=px+30;dy[ee]=py;pdan=pdan-1;}
  }
 }

 void newtdan()
 {
  ee=-1;
  for (i=0;i<=49;i++)
  {
   if (tdx[i]<-999) {ee=i;i=101;}
  }
  if (ee>=0) {tdx[ee]=406;tdy[ee]=Random.Range(0,5);tdj[ee]=1;tdw[ee]=0;}
  tdwait=20*(5-level);
 }

 void titleput()
 {
  alldel();
  z=16;zx=0;zy=0;objput();z=1;zx=-100;zy=-70;objput();
  z=2;zx=0;zy=-70;objput();z=3;zx=100;zy=-70;objput();
  z=17;zx=-90;zy=104;objput();z=18;zx=-146;zy=-68;objput();
  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 scaleset()
 {
  Vector3 scales=obj[z].transform.localScale;
  scales.x=af;
  obj[z].transform.localScale=scales;
 }

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