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;
    //G@
    private Sprite2D[] _enemy = new Sprite2D[ENEMY_NUM];
    //@G@̑
    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();
    //@̈ړx
    public Vector2D _shipDelta = new Vector2D(0,0);
    //摜̕⍂
	private static final int XY_SIZE = 64;
    //A
	private static SoundPool _spExplode;
	//BTEhԍ
	private int _soundID;

	//@Override
	//RXgN^
	public HyperMotion2D(Context context)
	{
	    _context = context;
		//CTEhv[
	    _spExplode = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		//D
	    _soundID = _spExplode.load(context, R.raw.explode, 1);
	}

	//@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());
	    	enemyMove();//E
	    	enemyDraw(gl);
	    	shipMove();
			_ship.draw(gl);
	    	break;
	    case GS_GAMEOVER:
	    	_earth.draw(gl,getRatio());
	    	enemyDraw(gl);
			_ship.draw(gl);
	    	break;
	    }
	}

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

	//F΂̓G
	private void enemyMove()
	{
		int i;
		//G
		for (i = 0; i < _enemy.length; i++)
		{
			//Hi
			_enemy[i]._pos._x += 8*(Math.random()-Math.random()) + _enemyDelta[i]._x;
			_enemy[i]._pos._y += Math.random()-Math.random()*2 + _enemyDelta[i]._y;
			//IZ
			_enemyDelta[i]._x *= 0.9;
			_enemyDelta[i]._y *= 0.9;
			//J̉ʊOɏoȂ悤
			if ( _enemy[i]._pos._x < 0 )
			{
				_enemy[i]._pos._x = 0;
				_enemyDelta[i]._x *= -1;
			}
			//KẺʊOɏoȂ悤
			if ( _enemy[i]._pos._x > _width - XY_SIZE )
			{
				_enemy[i]._pos._x = _width - XY_SIZE;
				_enemyDelta[i]._x *= -1;
			}
			//L@ƓG@̓蔻
			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 )
			{
				//M
				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//N
				{
					_enemy[i]._pos._x += x/2;
					_enemy[i]._pos._y -= y/2;
				}
			}
		}
	}
	
	//΂̓G`
	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;
	   	}
	}
	
	//@Override
	//T[tFCX̃TCYύXɌĂ΂
	public void onSurfaceChanged(
			GL10 gl, int width, int height)
	{
	}

	//@Override
	//T[tFCX邩܂͍ĐۂɌĂ΂
	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);
		//
		for ( i = 0; i < _enemy.length; i++ )
		{
			_enemy[i] = new Sprite2D();
			_enemy[i].setTexture(gl,_context.getResources(),R.drawable.enemy);
		}
	}
	
	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;
			//O
			_enemyDelta[i] = new Vector2D(0,0);
		}
		_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;
		}
 	}
}
