package com.hypermotion2d;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import com.roxiga.hypermotion2d.*;

public class HyperMotion2D implements GLSurfaceView.Renderer
{
	//コンテクスト
	private Context _context;
	//画面幅
	public int _width;
	//画面高さ
	public int _height;
	//画面をタッチしたか？
	public boolean _touch;
	//タッチしたX座標
	private float _xPos;
	//タッチしたY座標
	private float _yPos;
	//2次元スプライト
    private Sprite2D _title = new Sprite2D();
    private Sprite2D _earth = new Sprite2D();
	//敵機の数
	private static final int ENEMY_NUM = 8;
    //敵機
    private Sprite2D[] _enemy = new Sprite2D[ENEMY_NUM];
    //敵機の増分
    private Vector2D[] _enemyDelta = new Vector2D[ENEMY_NUM];
	public static final int GS_TITLE = 0;
	public static final int GS_MAIN  = 1;
	public static final int GS_GAMEOVER = 2;
	public int _gameState = GS_TITLE;
    private Sprite2D _ship = new Sprite2D();
    //自機の移動速度
    public Vector2D _shipDelta = new Vector2D(0,0);
    //画像の幅や高さ
	private static final int XY_SIZE = 64;
    //爆発音
	private static SoundPool _spExplode;
	//サウンド番号
	private int _soundID;
	//①爆発画像
    public Sprite2D _explode = new Sprite2D();

	//@Override
	//コンストラクタ
	public HyperMotion2D(Context context)
	{
	    _context = context;
		//サウンドプール
	    _spExplode = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		//爆発音
	    _soundID = _spExplode.load(context, R.raw.explode, 1);
	}

	//@Override
	//描画できるように毎フレーム呼び出される関数
	public void onDrawFrame(GL10 gl)
	{
	    // 描画用バッファをクリア
	    gl.glClear(GL10.GL_COLOR_BUFFER_BIT
	    		| GL10.GL_DEPTH_BUFFER_BIT);
	    switch ( _gameState )
	    {
	    case GS_TITLE:
	    	_title.draw(gl,getRatio());
	    	break;
	    case GS_MAIN:
	    	_earth.draw(gl,getRatio());
	    	enemyMove();
	    	enemyDraw(gl);
	    	shipMove();
			_ship.draw(gl);
	    	break;
	    case GS_GAMEOVER:
	    	_earth.draw(gl,getRatio());
	    	enemyDraw(gl);
			_ship.draw(gl);
	    	explodeDraw(gl);//②
			//③爆発終了したら
   			if ( _explode._width > 350 )
			{
				_gameState = GS_TITLE;
			}
	    	break;
	    }
	}

	private float getRatio()
	{
		return (float)_width/600.0f;
	}

	//緑の敵
	private void enemyMove()
	{
		int i;
		for (i = 0; i < _enemy.length; i++)
		{
			//進む
			_enemy[i]._pos._x += 8*(Math.random()-Math.random()) + _enemyDelta[i]._x;
			_enemy[i]._pos._y += Math.random()-Math.random()*2 + _enemyDelta[i]._y;
			//増分除算
			_enemyDelta[i]._x *= 0.9;
			_enemyDelta[i]._y *= 0.9;
			//左右の画面外に出ないように
			if ( _enemy[i]._pos._x < 0 )
			{
				_enemy[i]._pos._x = 0;
				_enemyDelta[i]._x *= -1;
			}
			if ( _enemy[i]._pos._x > _width - XY_SIZE )
			{
				_enemy[i]._pos._x = _width - XY_SIZE;
				_enemyDelta[i]._x *= -1;
			}
			//④地球に到達
			if ( _enemy[i]._pos._y < 0 )
			{
				//⑤
				_explode._pos._x = _enemy[i]._pos._x;
				//⑥
				_explode._pos._y = _enemy[i]._pos._y;
				//⑦
				_explode._visible = true;
				//⑧
				_gameState = GS_GAMEOVER;
				//⑨爆発音再生
			    _spExplode.play(_soundID, 1.0F, 1.0F, 0, 0, 1.0F);
			}
			//自機と敵機の当たり判定
			float x = _ship._pos._x - _enemy[i]._pos._x;
			float y = _ship._pos._y - _enemy[i]._pos._y;
			float x2 = _shipDelta._x;
			float y2 = _shipDelta._y;
			if ( x*x + y*y < 50*50 )
			{
				//
				if ( x2*x2 + y2*y2 > 1*1 )
				{
					_enemyDelta[i]._x = _shipDelta._x; 
					_enemyDelta[i]._y = -_shipDelta._y;
					_shipDelta._x *= -1;
					_shipDelta._y *= -1;
					_spExplode.play(_soundID, 1.0F, 1.0F, 0, 0, 1.0F);
				}
				else
				{
					_enemy[i]._pos._x += x/2;
					_enemy[i]._pos._y -= y/2;
				}
			}
		}
	}
	
	//緑の敵描画
	private void enemyDraw(GL10 gl)
	{
		for (int i = 0; i < _enemy.length; i++)
		{
			_enemy[i].draw(gl);
		}
	}

	//自機
	private void shipMove()
	{
	    _ship._pos._x += _shipDelta._x;
	    _ship._pos._y -= _shipDelta._y;
	    if ( _ship._pos._x < 0 )
	    {
	    	_ship._pos._x = 0;
	    	_shipDelta._x *= -1;
	    }
	    else if (_ship._pos._x > _width-XY_SIZE )
	    {
	    	_ship._pos._x = _width-XY_SIZE;
	    	_shipDelta._x *= -1;
	    }
	    else if ( _ship._pos._y < 0 )
	    {
	    	_ship._pos._y = 0;
	    	_shipDelta._y *= -1;
	    }
	    if ( _ship._pos._y > _height-XY_SIZE )
	    {
	    	_ship._pos._y = _height-XY_SIZE;
	    	_shipDelta._y *= -1;
	    }
	    else
	   	{
	    	_shipDelta._x *= 0.9f;
	    	_shipDelta._y *= 0.9f;
	   	}
	}

	//⑩爆発描画
	private void explodeDraw(GL10 gl)
	{
		//⑪
		if ( _explode._visible )
		{
			//⑫
			_explode._pos._x--;
			//⑬
			_explode._pos._y--;
			//⑭
			_explode._width += 2;
			//⑮
			_explode._height += 2;
		}
		//⑯
	    _explode.draw(gl);
	}
	
	//@Override
	//サーフェイスのサイズ変更時に呼ばれる
	public void onSurfaceChanged(
			GL10 gl, int width, int height)
	{
	}

	//@Override
	//サーフェイスが生成される際・または再生成される際に呼ばれる
	public void onSurfaceCreated(
			 GL10 gl, EGLConfig config)
	{
		//背景色
	   	gl.glClearColor(0.6f,0.8f,1.0f,1.0f);
	    // ディザを無効化
	    gl.glDisable(GL10.GL_DITHER);
	    // 深度テストを有効に
	    gl.glEnable(GL10.GL_DEPTH_TEST);
	 	//テクスチャ機能ON
	  	gl.glEnable(GL10.GL_TEXTURE_2D);
	    //透明可能に
	  	gl.glEnable(GL10.GL_ALPHA_TEST);
	   	//ブレンド可能に
	   	gl.glEnable(GL10.GL_BLEND);
	  	//色のブレンド方法
	  	gl.glBlendFunc(GL10.GL_SRC_ALPHA,
			GL10.GL_ONE_MINUS_SRC_ALPHA);
	  	
	  	int i;
		_title.setTexture(gl,_context.getResources(),R.drawable.title);
		_title._texWidth = 600;
		_title._width = 600;
		_earth.setTexture(gl,_context.getResources(),R.drawable.earth);
		_earth._texWidth = 600;
		_earth._width = 600;
		_ship.setTexture(gl,_context.getResources(),R.drawable.ship);

		for ( i = 0; i < _enemy.length; i++ )
		{
			_enemy[i] = new Sprite2D();
			_enemy[i].setTexture(gl,_context.getResources(),R.drawable.enemy);
		}
		//⑰
		_explode.setTexture(gl,_context.getResources(),R.drawable.explode);
	}
	
	public void init()
	{
		int i;
		for ( i = 0; i < _enemy.length; i++ )
		{
			_enemy[i]._pos._x = (float)Math.random()*(_width-XY_SIZE);
			_enemy[i]._pos._y = 500+i*300+(float)Math.random()*100;
			_enemyDelta[i] = new Vector2D(0,0);
		}
		_shipDelta = new Vector2D(0,0);
		_ship._pos._x = (_width-XY_SIZE)/2;
		_ship._pos._y = 0;
		//⑱
		_explode._width = 32;
		_explode._height = 32;//⑱
		_explode._visible = false;//⑱
	}
	
	public void actionDown(float x,float y)
	{
	   	_xPos = x;
    	_yPos = y;
	}
	
	public void actionMove(float x,float y)
	{
	}

	public void actionUp(float x,float y)
	{
		switch ( _gameState )
		{
		case GS_TITLE:
			init();
			_gameState = GS_MAIN;
			break;
		case GS_MAIN:
			_shipDelta._x = (x-_xPos)/8;
			_shipDelta._y = (y-_yPos)/8;
			break;
		case GS_GAMEOVER:
			//⑲
			if ( _explode._width > 200 )
			{
				_gameState = GS_TITLE;
			}
			break;
		}
 	}
}
