#include "GGameScene.h"

#include "GStaticData.h"
#include "GPlayerManager.h"

using namespace GEnum;

static int DAMAGE_TEXT_LEFT;
static int DAMAGE_TEXT_TOP;
static int DAMAGE_TEXT_INTERVAL_X;
GGameScene::GGameScene(){

	// 表示位置の計算

	HPLSequence* lpSequence = staticData->getSequence( SpriteType::COMMON, Common::NUMBERS );

	HPLImageInformation* lpInfo = lpSequence->getImageInformation(0);

	// 一人あたりの幅
	int WIDTH_PER_PLAYER = staticData->getSetting()->windowWidth / PLAYER_MAX_NUM;
	DAMAGE_TEXT_LEFT = WIDTH_PER_PLAYER / 2 - lpInfo->srcRect.getWidth() / 2;
	DAMAGE_TEXT_TOP = staticData->getSetting()->windowHeight - lpInfo->srcRect.getHeight() - DAMAGE_TEXT_MERGINE;
	DAMAGE_TEXT_INTERVAL_X = WIDTH_PER_PLAYER;
}
GGameScene::~GGameScene(){
}
void GGameScene::init(){
	/////
	// プレイヤー初期化
	for( int i = 0; i < GEnum::PLAYER_MAX_NUM; i ++ ) {
		GPlayer* player = &staticData->m_ObjectManager.m_aryPlayerList[i];

		player->setHP(player->getHPMax());

		//状態異常補修
//		player->getRemoveFearCounter()->end();
//		player->getRemoveNennEkiCounter()->end();
//		player->getRemovePoisonCounter()->end();
	}



	//staticData->setupMap( HPLMath::nextInt(0,staticData->getMapManager()->getMapNum() ) );
	
	staticData->m_ObjectManager.m_EnemyManager.clear();
}
void GGameScene::process(){
	staticData->clearScreen(0,0,0);

	////////
	// adjust scroll
	{
		
		HPLScroller* scroller = staticData->getScrollManager()->getScroller(0);

		HPLVector shake = staticData->m_ShakeManager.m_vShake;

		staticData->m_ShakeManager.stepShake();

		HPLVector scrPoint(
			staticData->getSetting()->windowWidth / 2.f + shake.getX(),
			staticData->getSetting()->windowHeight / 2.f + shake.getY());

		bool isAdjust = true;

		// tag
		scroller->matchPoint(
			scrPoint, staticData->m_ObjectManager.m_aryPlayerList[0].getCenter(),
			isAdjust);
	}
	/////
	// タイル表示
	{
		// draw blocks
		staticData->getBlockManager()->draw(staticData->getImageManager(),
			staticData->getGraphics()->getScreen(),
			staticData->getScrollManager()->getScroller(0));
	}

	// 移動する？
	bool isMove = getStaticData()->m_CommonWork.m_Game.isTheWorld() == false &&
		getStaticData()->m_CommonWork.m_Game.isMenu() == false &&
		getStaticData()->m_CommonWork.m_Game.isScenario() == false;

	for( int i = 0; i < GEnum::PLAYER_MAX_NUM; i ++ ) {
		GPlayer* player = &staticData->m_ObjectManager.m_aryPlayerList[i];

		// drawplayer
		player->draw(staticData->getImageManager());

		if( player->isAlive() == false ) {
			// 震度
			// スタート地点に戻す
			player->setHP( player->getHPMax() );
			player->setAlive( true );

			// 速度を0に戻す
			player->setVelocityAngle(0,0);

			// 体力復活
			player->setStoredDamage( 0 );
			player->setCenter( getStaticData()->m_CommonWork.m_ObjectPlacer.getRandomPlayerPoint() );
		}
		if(isMove){
			getStaticData()->m_CommonWork.defaultPlayerProcess( player , false);

		}

		// それぞれのダメージ蓄積量を表示
		staticData->m_CommonWork.drawNumbers( 
			DAMAGE_TEXT_LEFT + i * DAMAGE_TEXT_INTERVAL_X,
			DAMAGE_TEXT_TOP,
			static_cast<int>( player->getStoredDamage() ) );
	}

	staticData->m_ObjectManager.m_EnemyManager.process( isMove );

	staticData->m_ObjectManager.m_ShotManager.process( isMove );
	staticData->m_ObjectManager.m_EffectManager.process( isMove );
	{
		// draw front blocks
		staticData->getBlockManager()->draw(staticData->getImageManager(),
			staticData->getGraphics()->getScreen(),
			staticData->getScrollManager()->getScroller(0),
			true);
	}

	//////
	// イベントの実行と表示
	staticData->checkEvent();


	if( staticData->getInputManager()->isPressedNow( GEnum::KeyMap::D ) ) {
		// 次のキャラに変更
		GPlayer* player = &staticData->m_ObjectManager.m_aryPlayerList[
		staticData->m_ObjectManager.m_nPlayingIndex];

		int nType = player->getType();

		nType ++;
		if( nType >= GEnum::PlayerType::MAX_NUM ) {
			nType = 0;
		}

		player->changeType( nType );

	}
}
void GGameScene::quit(){
}
