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
{
	//ReNXg
	private Context _context;
	//ʕ
	public int _width;
	//ʍ
	public int _height;
	//ʂ^b`H
	public boolean _touch;
	//^b`XW
	private float _xPos;
	//^b`YW
	private float _yPos;
	//2XvCg
    private Sprite2D _title = new Sprite2D();
    private Sprite2D _earth = new Sprite2D();
	//@G@̐
	private static final int ENEMY_NUM = 8;
    //AG@
    private Sprite2D[] _enemy = new Sprite2D[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();
    //@̈ړx
    public Vector2D _shipDelta = new Vector2D(0,0);
    //摜̕⍂
	private static final int XY_SIZE = 64;

	//@Override
	//RXgN^
	public HyperMotion2D(Context context)
	{
	    _context = context;
	}

	//@Override
	//`ł悤ɖt[Ăяo֐
	public void onDrawFrame(GL10 gl)
	{
	    // `pobt@NA
	    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());
	    	enemyDraw(gl);//B
	    	shipMove();
			_ship.draw(gl);
	    	break;
	    case GS_GAMEOVER:
	    	_earth.draw(gl,getRatio());
	    	enemyDraw(gl);//C
			_ship.draw(gl);//D
	    	break;
	    }
	}

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

	//E΂̓G`
	private void enemyDraw(GL10 gl)
	{
		//Ffor[v
		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;
	   	}
	}
	
	//@Override
	//T[tFCX̃TCYύXɌĂ΂
	public void onSurfaceChanged(
			GL10 gl, int width, int height)
	{
	}

	//@Override
	//T[tFCXہE܂͍ĐۂɌĂ΂
	public void onSurfaceCreated(
			 GL10 gl, EGLConfig config)
	{
		//wiF
	   	gl.glClearColor(0.6f,0.8f,1.0f,1.0f);
	    // fBU𖳌
	    gl.glDisable(GL10.GL_DITHER);
	    // [xeXgL
	    gl.glEnable(GL10.GL_DEPTH_TEST);
	 	//eNX`@\ON
	  	gl.glEnable(GL10.GL_TEXTURE_2D);
	    //\
	  	gl.glEnable(GL10.GL_ALPHA_TEST);
	   	//uh\
	   	gl.glEnable(GL10.GL_BLEND);
	  	//F̃uh@
	  	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);
		//G
		for ( i = 0; i < _enemy.length; i++ )
		{
			//H
			_enemy[i] = new Sprite2D();
			//I
			_enemy[i].setTexture(gl,_context.getResources(),R.drawable.enemy);
		}
	}
	
	public void init()
	{
		int i;
		//J
		for ( i = 0; i < _enemy.length; i++ )
		{
			//K
			_enemy[i]._pos._x = (float)Math.random()*(_width-XY_SIZE);
			//L
			_enemy[i]._pos._y = 500+i*300+(float)Math.random()*100;
		}
		_shipDelta = new Vector2D(0,0);
		_ship._pos._x = (_width-XY_SIZE)/2;
		_ship._pos._y = 0;
	}
	
	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:
			break;
		}
 	}
}
