package com.hypermotion2d;

import android.content.Context;
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;
	//A^b`YW
	private float _yPos;
	//2XvCg
    private Sprite2D _title = new Sprite2D();
    private Sprite2D _earth = new Sprite2D();
	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;
	//BFD
    private Sprite2D _ship = new Sprite2D();
    //CFD̈ړ
    public Vector2D _shipDelta = new Vector2D(0,0);
    //DL̕⍂
	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());
			//EFDړ
	    	shipMove();
			//FFD`
			_ship.draw(gl);
	    	break;
	    case GS_GAMEOVER:
	    	_earth.draw(gl,getRatio());
	    	break;
	    }
	}

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

	//GFD
	private void shipMove()
	{
		//H
	    _ship._pos._x += _shipDelta._x;
	    _ship._pos._y -= _shipDelta._y;
		//I
	    if ( _ship._pos._x < 0 )
	    {
	    	_ship._pos._x = 0;
	    	_shipDelta._x *= -1;
	    }
	    else if (_ship._pos._x > _width-XY_SIZE )//J
	    {
	    	_ship._pos._x = _width-XY_SIZE;
	    	_shipDelta._x *= -1;
	    }
	    else if ( _ship._pos._y < 0 )//K
	    {
	    	_ship._pos._y = 0;
	    	_shipDelta._y *= -1;
	    }
	    if ( _ship._pos._y > _height-XY_SIZE )//L
	    {
	    	_ship._pos._y = _height-XY_SIZE;
	    	_shipDelta._y *= -1;
	    }
	    else//M
	   	{
	    	_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);
	  	
		_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;
		//N
		_ship.setTexture(gl,_context.getResources(),R.drawable.ship);
	}
	
	//O
	public void init()
	{
		_shipDelta = new Vector2D(0,0);
		_ship._pos._x = (_width-XY_SIZE)/2;
		_ship._pos._y = 0;
	}
	
	public void actionDown(float x,float y)
	{
		//P
	   	_xPos = x;
    	_yPos = y;
	}
	
	public void actionMove(float x,float y)
	{
	}

	public void actionUp(float x,float y)
	{
		switch ( _gameState )
		{
		case GS_TITLE:
			//Q
			init();
			_gameState = GS_MAIN;
			break;
		case GS_MAIN:
			//R
			_shipDelta._x = (x-_xPos)/8;
			_shipDelta._y = (y-_yPos)/8;
			break;
		case GS_GAMEOVER:
			break;
		}
 	}
}
