//--------------------------------------------------------------------------------
//
//　ウインドウ表示プログラム
//	Author : Xu Wenjie
//	Date   : 2016-04-26
//--------------------------------------------------------------------------------
//  Update : 
//	
//--------------------------------------------------------------------------------
#include "input.h"
#include "player.h"
#include "enemy.h"
#include "explosion.h"
//#include "effect.h"
#include "KF_Precision.h"
#include "KF_Body.h"
#include "KF_BodyContact.h"

#include "sound.h"
#include "frame.h"

//--------------------------------------------------------------------------------
//  定数定義
//--------------------------------------------------------------------------------
#define NUM_POLYGON ( 2 )
#define NUM_VERTEX  ( 4 )

#define PLAYER_WIDTH (50.0f)//ポリゴンの幅
#define PLAYER_HEIGHT (50.0f)//ポリゴンの高さ
#define PLAYER_TEXTURENAME "data/TEXTURE/frame.png"//ファイル名
#define PLAYER_POS_X (640.0f)//ポリゴンの表示位置X
#define PLAYER_POS_Y (320.0f)//ポリゴンの表示位置Y
#define MOVE_SPEED_X (D3DXVECTOR3(10.0f,0.0f,0.0f))//四方向移動速度
#define MOVE_SPEED_Y (D3DXVECTOR3(0.0f,10.0f,0.0f))//四方向移動速度
#define ROTATION_SPEED (D3DX_PI * 0.5f)//回転速度

//--------------------------------------------------------------------------------
//  構造体定義
//--------------------------------------------------------------------------------
typedef struct {
	LPDIRECT3DTEXTURE9 pTexture;//textureインターフェース
	LPDIRECT3DVERTEXBUFFER9 pVtxBuffer;//頂点バッファ管理インターフェースポインタ
	CBodyBox *pPlayerBody;
	D3DXVECTOR3 vPosCenter;
	int nframe;
	bool bCheck;
	float fScrollU;//u方向
	float fScrollV;//v方向
}PLAYER;

//--------------------------------------------------------------------------------
//  プロトタイプ宣言
//--------------------------------------------------------------------------------
HRESULT MakeVerTexPlayer(LPDIRECT3DDEVICE9 pDevice);

//--------------------------------------------------------------------------------
//  グローバル変数
//--------------------------------------------------------------------------------
PLAYER g_player;

//--------------------------------------------------------------------------------
//  初期化処理
//--------------------------------------------------------------------------------
void InitPlayer(void)
{
	LPDIRECT3DDEVICE9 pDevice = GetDevice();//デバイス取得

	g_player.fScrollU = 0.0f;
	g_player.fScrollV = 0.0f;
	g_player.nframe = 0;
	g_player.vPosCenter = D3DXVECTOR3(PLAYER_POS_X, PLAYER_POS_Y, 0.0f);
	g_player.pTexture = NULL;
	g_player.pVtxBuffer = NULL;
	g_player.bCheck = true;

	g_player.pPlayerBody = new CBodyBox;
	g_player.pPlayerBody->Init();
	g_player.pPlayerBody->cBody.SetPosCenter(D3DXVECTOR3(PLAYER_POS_X, PLAYER_POS_Y, 0.0f));
	g_player.pPlayerBody->cBody.SetGravityCenter(D3DXVECTOR3(PLAYER_POS_X, PLAYER_POS_Y, 0.0f));
	g_player.pPlayerBody->cBody.SetMass(100000000000000.0f);
	g_player.pPlayerBody->cBody.SetGravity(GRAVITY);
	g_player.pPlayerBody->cBody.SetRotation(D3DX_PI * -0.75f);
	g_player.pPlayerBody->SetHalfSize(D3DXVECTOR2( 5.0f, 25.0f));
	g_player.pPlayerBody->SetAngle(atan2(PLAYER_HEIGHT, PLAYER_WIDTH));

	if (FAILED(MakeVerTexPlayer(pDevice)))//textureポインタへのポインタ
	{
		MessageBox(NULL, "MakeVerTexPlayer ERROR!!", "エラー", MB_OK | MB_ICONWARNING);
	}

	//ハードディスクからTextureの読み込み
	//※エラーチェック必須
	if (FAILED(D3DXCreateTextureFromFile(pDevice,
		PLAYER_TEXTURENAME,
		&g_player.pTexture)))//textureポインタへのポインタ
	{
		MessageBox(NULL, "D3DXCreateTextureFromFile ERROR!!", "エラー", MB_OK | MB_ICONWARNING);
	}
}

//--------------------------------------------------------------------------------
//  終了処理
//--------------------------------------------------------------------------------
void UninitPlayer(void)
{
	if (g_player.pPlayerBody != NULL)
	{
		g_player.pPlayerBody->Uninit();//終了処理
		delete g_player.pPlayerBody;//中身を削除
		g_player.pPlayerBody = NULL;//アドレスを空にする
	}
	
	if (g_player.pVtxBuffer != NULL)
	{
		g_player.pVtxBuffer->Release();
		g_player.pVtxBuffer = NULL;
	}

	if (g_player.pTexture != NULL)
	{
		g_player.pTexture->Release();
		g_player.pTexture = NULL;
	}
}

//--------------------------------------------------------------------------------
//  更新処理
//--------------------------------------------------------------------------------
void UpdatePlayer(void)
{
	VERTEX_2D *pVtx = NULL;

	D3DXVECTOR3 vPlayerPos = g_player.pPlayerBody->cBody.GetPosCenter();
	float fRotation = g_player.pPlayerBody->cBody.GetRotation();
	float fLength = g_player.pPlayerBody->GetLength();
	float fAngle = g_player.pPlayerBody->GetAngle();

	g_player.pVtxBuffer->Lock(0, 0, (void**)&pVtx, 0);

	//texture
	pVtx[0].tex = D3DXVECTOR2(g_player.nframe * 0.2f, 0.0f);
	pVtx[1].tex = D3DXVECTOR2(g_player.nframe * 0.2f + 0.2f, 0.0f);
	pVtx[2].tex = D3DXVECTOR2(g_player.nframe * 0.2f, 1.0f);
	pVtx[3].tex = D3DXVECTOR2(g_player.nframe * 0.2f + 0.2f, 1.0f);

	g_player.pVtxBuffer->Unlock();

	//ランニング
	//g_player.nCntScrollU = (g_player.nCntScrollU + 1) % (NUM_MOTION_X * (int)g_player.fSpeed);
	//g_player.nCntScrollV = (g_player.nCntScrollV + 1) % (NUM_MOTION_X * NUM_MOTION_Y * (int)g_player.fSpeed);
	//g_player.fScrollU = (float)(g_player.nCntScrollU / (int)g_player.fSpeed) / (float)NUM_MOTION_X;
	//g_player.fScrollV = (float)(g_player.nCntScrollV / (int)g_player.fSpeed / NUM_MOTION_X) / (float)NUM_MOTION_Y;

	//key入力
	//if (GetKeyboardPress(DIK_W))//Wキーが押されてる
	//{
	//	g_player.pPlayerBody->cBody.SetPosCenter(g_player.pPlayerBody->cBody.GetPosCenter() - MOVE_SPEED_Y);
	//}

	//if (GetKeyboardPress(DIK_S))//Sキーが押されてる
	//{
	//	g_player.pPlayerBody->cBody.SetPosCenter(g_player.pPlayerBody->cBody.GetPosCenter() + MOVE_SPEED_Y);
	//}

	//if (GetKeyboardPress(DIK_A))//Aキーが押されてる
	//{
	//	g_player.pPlayerBody->cBody.SetPosCenter(g_player.pPlayerBody->cBody.GetPosCenter() - MOVE_SPEED_X);
	//}

	//if (GetKeyboardPress(DIK_D))//Dキーが押されてる
	//{
	//	g_player.pPlayerBody->cBody.SetPosCenter(g_player.pPlayerBody->cBody.GetPosCenter() + MOVE_SPEED_X);
	//}

	if (GetKeyboardTrigger(DIK_LEFT))//左回転
	{
		g_player.nframe--;
		g_player.nframe = g_player.nframe == -1 ? 4 : g_player.nframe;
	}

	if (GetKeyboardTrigger(DIK_RIGHT))//右回転
	{
		g_player.nframe = (g_player.nframe + 1) % 5;
	}

	g_player.bCheck = g_player.nframe == 4 ? false : true;

	//position
	if (g_player.bCheck)
	{
		//rotation
		if (g_player.nframe == 0)
		{
			g_player.pPlayerBody->cBody.SetRotation(D3DX_PI * -0.75f);
		}
		if (g_player.nframe == 3)
		{
			g_player.pPlayerBody->cBody.SetRotation(D3DX_PI * -0.25f);
		}
		if (g_player.nframe == 2)
		{
			g_player.pPlayerBody->cBody.SetRotation(D3DX_PI * 0.25f);
		}
		if (g_player.nframe == 1)
		{
			g_player.pPlayerBody->cBody.SetRotation(D3DX_PI * 0.75f);
		}

		switch (g_player.nframe)
		{
		case 0:
			g_player.pPlayerBody->cBody.SetPosCenter(D3DXVECTOR3(g_player.vPosCenter.x + PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y + PLAYER_HEIGHT * 0.5f, 0.0f));
			break;
		case 3:
			g_player.pPlayerBody->cBody.SetPosCenter(D3DXVECTOR3(g_player.vPosCenter.x - PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y + PLAYER_HEIGHT * 0.5f, 0.0f));
			break;
		case 2:
			g_player.pPlayerBody->cBody.SetPosCenter(D3DXVECTOR3(g_player.vPosCenter.x - PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y - PLAYER_HEIGHT * 0.5f, 0.0f));
			break;
		case 1:
			g_player.pPlayerBody->cBody.SetPosCenter(D3DXVECTOR3(g_player.vPosCenter.x + PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y - PLAYER_HEIGHT * 0.5f, 0.0f));
			break;
		}
	}

	g_player.pPlayerBody->Update();

	//check contact
	if (g_player.bCheck)
	{
		int nCnt = 0;
		ENEMY *pEnemy = NULL;

		for (nCnt = 0; nCnt < NUM_ENEMY; nCnt++)
		{
			pEnemy = GetEnemy(nCnt);
			if (pEnemy->bUsed)
			{
				pEnemy->bUsed = CheckContactCTB(pEnemy->pEnemyBody, g_player.pPlayerBody);
				if (!pEnemy->bUsed)
				{
					SetVerTexExplosion(pEnemy);
					PlayOneShotSoundEffect( 1 );
				}
				pEnemy = NULL;
			}
		}
	}

	g_player.nframe = (int)GetFrame();
	
}

//--------------------------------------------------------------------------------
//  描画処理
//--------------------------------------------------------------------------------
void DrawPlayer(void)
{
	LPDIRECT3DDEVICE9 pDevice = GetDevice();//デバイス取得

	//player
	pDevice->SetStreamSource(
		0,//ストリーム番号
		g_player.pVtxBuffer,
		0,//オフセット（開始位置）
		sizeof(VERTEX_2D));//ストライド量

	//頂点フォーマットの設定
	pDevice->SetFVF(FVF_VERTEX_2D);

	//Textureの設定
	pDevice->SetTexture(0, g_player.pTexture);

	//プリミティブ描画
	pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,
		0,//オフセット（頂点数）
		NUM_POLYGON);


	// 加算合成の設定
	pDevice -> SetRenderState( D3DRS_BLENDOP , D3DBLENDOP_ADD );
	pDevice -> SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );
	pDevice -> SetRenderState( D3DRS_DESTBLEND , D3DBLEND_ONE );

	// ポリゴンの描画
	pDevice ->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, NUM_POLYGON );

	// ポリゴンの描画
	pDevice ->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, NUM_POLYGON );

	// ポリゴンの描画
	pDevice ->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, NUM_POLYGON );


	// 元の状態に戻す
	pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,TRUE );				//半透明処理
	pDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );		//元のα
	pDevice->SetRenderState( D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA );		//先のα



}

//--------------------------------------------------------------------------------
//  頂点の作成
//--------------------------------------------------------------------------------
HRESULT MakeVerTexPlayer(LPDIRECT3DDEVICE9 pDevice)
{
	//player
	if (FAILED(pDevice->CreateVertexBuffer(
		sizeof(VERTEX_2D) * NUM_VERTEX,//作成したい頂点バッファのサイズ
		D3DUSAGE_WRITEONLY,//頂点バッファの使用方法
		FVF_VERTEX_2D,//書かなくても大丈夫
		D3DPOOL_MANAGED,//メモリ管理方法(managed：デバイスにお任せ)
		&g_player.pVtxBuffer,//
		NULL//
	)))
	{
		return E_FAIL;
	}

	//仮想アドレスを取得するためのポインタ
	VERTEX_2D *pVtx;

	//頂点バッファをロックして、仮想アドレスを取得する
	g_player.pVtxBuffer->Lock(
		0,//範囲
		0,//範囲
		(void**)&pVtx,//アドレスが書かれたメモ帳のアドレス
		0);

	//頂点座標の設定（2D座標、右回り）
	//pVtx[0].pos = D3DXVECTOR3(vPlayerPos.x + cosf(fRotation + D3DX_PI + fAngle) * fLength, vPlayerPos.y + sin(fRotation + D3DX_PI + fAngle) * fLength, 0.0f);
	//pVtx[1].pos = D3DXVECTOR3(vPlayerPos.x + cosf(fRotation - fAngle) * fLength, vPlayerPos.y + sinf(fRotation - fAngle) * fLength, 0.0f);
	//pVtx[2].pos = D3DXVECTOR3(vPlayerPos.x + cosf(fRotation - D3DX_PI - fAngle) * fLength, vPlayerPos.y + sinf(fRotation - D3DX_PI - fAngle) * fLength, 0.0f);
	//pVtx[3].pos = D3DXVECTOR3(vPlayerPos.x + cosf(fRotation + fAngle) * fLength, vPlayerPos.y + sinf(fRotation + fAngle) * fLength, 0.0f);

	pVtx[0].pos = D3DXVECTOR3(g_player.vPosCenter.x - PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y - PLAYER_HEIGHT * 0.5f, 0.0f);
	pVtx[1].pos = D3DXVECTOR3(g_player.vPosCenter.x + PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y - PLAYER_HEIGHT * 0.5f, 0.0f);
	pVtx[2].pos = D3DXVECTOR3(g_player.vPosCenter.x - PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y + PLAYER_HEIGHT * 0.5f, 0.0f);
	pVtx[3].pos = D3DXVECTOR3(g_player.vPosCenter.x + PLAYER_WIDTH * 0.5f, g_player.vPosCenter.y + PLAYER_HEIGHT * 0.5f, 0.0f);
	
	//rhwの設定(必ず1.0f)
	pVtx[0].rhw = 1.0f;
	pVtx[1].rhw = 1.0f;
	pVtx[2].rhw = 1.0f;
	pVtx[3].rhw = 1.0f;

	//頂点カラーの設定(0〜255の整数値)
	pVtx[0].color = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f);
	pVtx[1].color = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f);
	pVtx[2].color = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f);
	pVtx[3].color = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f);

	//texture頂点
	pVtx[0].tex = D3DXVECTOR2(0.0f, 0.0f);
	pVtx[1].tex = D3DXVECTOR2(0.2f, 0.0f);
	pVtx[2].tex = D3DXVECTOR2(0.0f, 1.0f);
	pVtx[3].tex = D3DXVECTOR2(0.2f, 1.0f);

	//仮想アドレス解放
	g_player.pVtxBuffer->Unlock();

	return S_OK;
}