#include "GPlayerManager.h"

#include "../game/GStaticData.h"


void movePlayer(GPlayer* player, HPLVector* velocityAngle,
				int direction){
	float RESIST = staticData->getSetting()->resist;

	{
		float SPEED = staticData->getSetting()->player.walkMaxSpeed + RESIST;
		float ACCEL = staticData->getSetting()->player.walkAccel;

	/*	if(player->getType() == PlayerType::Robogen){
			ACCEL /= 2.f;
			SPEED *= 2.f;
		}*/

		if(player->getTrigger()->getTrigger(TriggerType::ON_THE_GROUND)){
			ACCEL += RESIST;

			
		}else{
			SPEED = staticData->getSetting()->player.airMaxSpeed;
			ACCEL = staticData->getSetting()->player.airAccel;
			ACCEL += RESIST/2;
		}

		velocityAngle->setX(velocityAngle->getX() + direction * ACCEL);
	//	velocityAngle->setX(direction * SPEED);

		if(fabs(velocityAngle->getX()) > SPEED){
			velocityAngle->setX(direction * SPEED);
		}

	}

		/////
		// 上下キー押した
	if(player->getTrigger()->getTrigger(TriggerType::ON_THE_GROUND)){
		bool down = staticData->getInputManager()->isPressed(GEnum::KeyMap::DOWN);
		bool up = staticData->getInputManager()->isPressed(GEnum::KeyMap::UP);
			float RESIST = staticData->getSetting()->resist;
			float SPEED = staticData->getSetting()->player.walkMaxSpeed / 2 + RESIST;
			float ACCEL = staticData->getSetting()->player.walkAccel / 2;

		/*	if(player->getType() == PlayerType::Robogen){
				ACCEL /= 2.f;
				SPEED *= 2.f;
			}*/

			if(player->getTrigger()->getTrigger(TriggerType::ON_THE_GROUND)){
				ACCEL += RESIST;
				
				
			}else{
			/*	SPEED = staticData->getSetting()->player.airMaxSpeed / 2;
				ACCEL = staticData->getSetting()->player.airAccel / 2;
				ACCEL += RESIST/2;*/
			}

			float delta = 0;
			if(up){
				delta = -1;
			}else if(down ){
				delta = 1;
			}else{
				
			}
			player->groundY += delta * SPEED;

			//velocityAngle->setY(velocityAngle->getY() + delta * ACCEL);
		//	velocityAngle->setX(direction * SPEED);

			//if(fabs(velocityAngle->getY()) > SPEED){
			//	velocityAngle->setY(delta * SPEED);
			//}
		}
}


void GPlayerManager::standToWalk(GPlayer* player, HPLVector* velocityAngle){
	// 状態
//	GPlayerState* state = player->getState();
	int st = player->getState()->getState();


	HPLSDLInputManager* inputManager = staticData->getInputManager();


	//player->getBackShotCounter()->step();


/*	if(st == StateType::ATTACK ||
		player->isAttackGunState() ||
		st == StateType::ATTACK_PINEAPPLE || 
		st == StateType::ATTACK_PUNCH ||
		st == StateType::STAND ||
		st == StateType::WALK ||
		st == StateType::FLYDOWN ||
		st == StateType::FLYUP ||
		st == StateType::CROUCH){
		//////
		// ダニもす面ではランダムにしびれる
		if(staticData->getMapManager()->getSelectedMap() == 44){
			//if(HPLMath::nextDouble(0,100) < 0.1){
			player->elecCounter->step();
			if(player->elecCounter->isAlive() == false){
				player->elecCounter->reset();
				///////
				//
				///
				// se
				staticData->getMixerManager()->playSE(
					GEnum::SE::Elec,
					-1,
					1);
				player->changeState(StateType::DAMAGE_ELEC);
				player->playDamageSE(false);

				/////
				// 無敵になる
				player->startInvinsible();

			}
		}
	}*/
	
	bool right = inputManager->isPressed(GEnum::KeyMap::RIGHT);
	bool left = inputManager->isPressed(GEnum::KeyMap::LEFT);

	//////////////
	// ベルトアクション
	bool down = inputManager->isPressed(GEnum::KeyMap::DOWN);
	bool up = inputManager->isPressed(GEnum::KeyMap::UP);


	/*if(st == StateType::BORN){
		if(!right && !left){
			return;
		}
	}*/

	/*
		軽い：びんた、モヒ、バール
		中　：チャッカ、ショットガン、エネ、パイナップル
		重い：ミニガン、ミサイル

	*/

	int faceDirection = 0;
/*	if(player->isAttackGunState() && player->isUsingHeavyWeapon()){
		/////
		// 動けない
		int a = 0;

	}else*/
	{
		if(right){// && player->getTrigger()->getTrigger(TriggerType::HIT_TO_THE_RIGHT) == false){
			faceDirection = FaceDirection::RIGHT;
		}
		if(left){// && player->getTrigger()->getTrigger(TriggerType::HIT_TO_THE_LEFT) == false){
			faceDirection = FaceDirection::LEFT;
		}
	}

//	bool isBackShot =
//		player->getUsingWeaponTypeIndex() == WeaponType::SemiAutoColt &&
//		player->getBackShotCounter()->isAlive();

	// 方向設定
/*	if(isBackShot){
			////////////////
			// ハンドガン所持、かつ発射後しばらくは後ずさり可能
			int a = 0;
	}else*/
	{
		if(faceDirection != 0 && faceDirection != player->getFaceDirection()){
			/////
			// リセット
			if(player->isAttackGunState()){
				player->changeState(StateType::STAND);
				st = player->getState()->getState();
			}
		}
		if(faceDirection != 0){
			player->setFaceDirection(faceDirection);
		}
	}

	/*if(!right && !left){
		faceDirection = 0;
	}*/

	//HPLSequence* seq = player->getSequence();
	HPLSequenceState seqState = player->getSequence()->getState();

	bool isOnTheGround = player->getTrigger()->getTrigger(TriggerType::ON_THE_GROUND);


	if(st == StateType::STAND || st == StateType::WALK || st == StateType::BORING){
		//////
		// 左右キー押した
		movePlayer(player, velocityAngle, faceDirection);


		if(isOnTheGround){
			/////
			// 動く


			if((st == StateType::STAND || st == StateType::BORING) && (up || down || left || right)){
				// 歩きになる
				player->changeState(StateType::WALK_READY);
			}
		}else{
			////
			// 空中
			// 空中動き
			//flyMovePlayer(player, velocityAngle, faceDirection);

			// 状態は変えない
		}
	}else if(st == StateType::BREAK){
		if(seqState.isFinishAnimation){
			player->changeState(StateType::STAND);
		}

	}else if(st == StateType::WALK_READY){
		/////
		// 動く
		movePlayer(player, velocityAngle, faceDirection);

		if(isOnTheGround){
			if(seqState.isFinishAnimation){
				// アニメーション終了
				//		→WALKに変更
				player->changeState(StateType::WALK);
			}
		}else{
			// 空中動き
			//flyMovePlayer(player, velocityAngle, faceDirection);
		}



	}else if(st == StateType::CROUCH || st == StateType::BORING_CROUCH){

		if(inputManager->isPressed(GEnum::KeyMap::DOWN) == false &&
			player->isAutoCrouch()){
				///////////
				// ↓キー以外の座り状態
				//		→歩ける！
				movePlayer(player, velocityAngle, faceDirection);

				// 歩きになる
				player->changeState(StateType::CROUCH_2);
		}else{
			///////
			// アニメーションが終わったら立つ
			if(isOnTheGround && seqState.isFinishAnimation && !down){
				player->changeState(StateType::CROUCH_2);
			}
		}
	}else if(st == StateType::CROUCH_2){
		if(seqState.isFinishAnimation && !down){
			player->changeState(StateType::STAND);
		}

	}else if(st == StateType::FLYDOWN || st == StateType::FLYUP){
		/////
		// 飛んでる！
		movePlayer(player, velocityAngle, faceDirection);
		

	}else if(st == StateType::ATTACK_PUNCH){

		movePlayer(player, velocityAngle, faceDirection);
/*		if(isOnTheGround){
			/////
			// 動く

			// 歩く?
		}else{
			/////
			// 空中移動可能(というか撃てないだけ)
			flyMovePlayer(player, velocityAngle, faceDirection);
		}*/

	}else if(st == StateType::ATTACK_PINEAPPLE){
		if(isOnTheGround){
			/////
			// 地上移動不可
		}else{
			/////
			// 空中移動可能
			movePlayer(player, velocityAngle, faceDirection);
		}


	}else if(player->isAttackGunState()){
		int wtype = player->getUsingWeaponTypeIndex();

/*		if(wtype == WeaponType::SemiAutoColt){
			if(isOnTheGround){
				//////
				// 動く
				//movePlayer(player, velocityAngle, faceDirection);
				// 歩く?
				if(faceDirection != 0){
					player->changeState(StateType::WALK);
				}
			}else {
				flyMovePlayer(player, velocityAngle, faceDirection);
			}
		}else if(player->isUsingHeavyWeapon()){
			if(isOnTheGround){
				/////
				// 地上移動不可
			}else{
				/////
				// 空中移動可能(というか撃てないだけ)
				flyMovePlayer(player, velocityAngle, faceDirection);
			}
		}else{
			if(isOnTheGround){
				/////
				// 地上移動不可
			}else{
				/////
				// 空中移動可能
				flyMovePlayer(player, velocityAngle, faceDirection);
			}
		}*/
	}else if(st == StateType::LYING || st == StateType::BORN){

		////
		// 倒れてた
		if(player->isBaby()){
			player->setBaby(false);
		}else{
			//  →CROUCH状態（で、その後歩く、と。）
			player->changeState(StateType::CROUCH);
		}

	}else if(st == StateType::READY_TO_JUMP){
		if(seqState.isFinishAnimation){
			/////
			// ジャンプする
			float JUMP_VELOCITY = staticData->getSetting()->player.jumpSpeed;
	/*		if(player->getType() == PlayerType::Robogen){
				JUMP_VELOCITY /= 2.f;
			};*/

			velocityAngle->setY(-JUMP_VELOCITY);

			player->getJumpManager()->jump();

			player->changeState(StateType::FLYUP);
		}
	}else if(st == StateType::SET_ON_THE_GROUND){

		if(seqState.isFinishAnimation){
			////
			// アニメーション終了
			// 立ち状態にする
			player->changeState(StateType::STAND);
		}
	}


}

void GPlayerManager::walkToStand(GPlayer* player, HPLVector* velocityAngle){
	//////////////////
	// 歩行中移動キーを話すと止まる

	HPLSDLInputManager* inputManager = staticData->getInputManager();

	float THRESHOLD = 0.01f;

	int st = player->getState()->getState();
	if((st == StateType::WALK ||
		st == StateType::WALK_READY ||
		st == StateType::STAND)&&


		inputManager->isPressed(GEnum::KeyMap::RIGHT) == false &&
		inputManager->isPressed(GEnum::KeyMap::DOWN) == false &&
		inputManager->isPressed(GEnum::KeyMap::UP) == false &&
		inputManager->isPressed(GEnum::KeyMap::LEFT) == false ){

		if(player->isAutoCrouch()){
			/////
			// ダメージ状態だとしゃがむ
			player->changeState(StateType::CROUCH);
		}else{
			if(fabs(velocityAngle->getX()) >= 
				staticData->getSetting()->player.walkMaxSpeed / 2
				//+ staticData->getSetting()->resist
				){
				
				// UDON-グレーキング
				player->changeState(StateType::BREAK);
			}else{
				player->changeState(StateType::STAND);
			}
		}
	}

	//
	////////////

}

void GPlayerManager::crouch(GPlayer* player, HPLVector* velocityAngle){
	int st = player->getState()->getState();

	///////////
	// 立ち中、ライフが残り30%以下なら座り込む
	if((st == StateType::STAND || st == StateType::BORING)&& player->isAutoCrouch()){
			player->changeState(StateType::CROUCH);
	}
	///
	///////////

	////////
	// 下キーでしゃがむ
	if((st == StateType::STAND ||
		st == StateType::BORING ||
		st == StateType::WALK) &&

		staticData->getInputManager()->isPressed(GEnum::KeyMap::DOWN)){

		//player->changeState(StateType::CROUCH);

	}else{
		HPLSequence* seq = player->getSequence();

		if(/*!player->isErogen() &&*/
			(
			(st == StateType::CROUCH || st == StateType::BORING_CROUCH) &&
			seq->getState().isFinishAnimation) &&
		staticData->getInputManager()->isPressed(GEnum::KeyMap::DOWN) == false
		/*&&
		!player->isAutoCrouch()*/
		){
			//////
			// しゃがみ状態で下キーを離した
			// 　→立ち上がる
			player->changeState(StateType::STAND);
		}
	}
}


void GPlayerManager::jump(GPlayer* player, HPLVector* velocityAngle){

/*	if(player->getType() == PlayerType::Undelsen &&
		player->getSave()->getItemNum(ItemType::MaidWareUpgrade) > 0){
			player->getJumpManager()->setJumpNumMax(3);
	}else{*/
		player->getJumpManager()->setJumpNumMax(2);
//	}

	int st = player->getState()->getState();

	///////
	// Xキーでジャンプ
	bool sp = false;

/*	if(player->getType() == PlayerType::Tanya &&
		st == StateType::ATTACK_PUNCH){
			sp = true;
	}*/

	/*////
	// パンチ、パイナップル、あるいは、重火器以外の火器使用時はジャンプ可能
	*/
	//if(player->getType() == PlayerType::Undelsen){

		if(st == StateType::ATTACK_PUNCH || st == StateType::ATTACK_PINEAPPLE){
		
			sp = true;
//		}else if(player->isAttackGunState() && player->isUsingHeavyWeapon() == false){
//			sp = true;
		}
	//}

	int wtype = player->getUsingWeaponTypeIndex();

	bool isStateTypeOK = (sp ||
		st == StateType::STAND || 
		st == StateType::BORING ||
		st == StateType::WALK ||
		st == StateType::WALK_READY ||

		st == StateType::FLYDOWN ||
		st == StateType::FLYUP ||
		st == StateType::CROUCH ||
		st == StateType::BORING_CROUCH);
	bool isJumpableOK = (player->getJumpManager()->isJumpable() || staticData->getGame()->isCheat());

	bool isPressedJumpKey = staticData->getInputManager()->isPressedNow(GEnum::KeyMap::B);

	//////
	// never jump when tank
	if(//player->getType() != PlayerType::Tank
		//&&
		isStateTypeOK
		&& 
		isJumpableOK
		&& 
		isPressedJumpKey
		){

/*		if(player->getType() == PlayerType::Tanya){
			player->getJumpManager()->jump();
		}*/
//		staticData->getMixerManager()->playSE(GEnum::SE::Puyo00);

//		if(st == StateType::ATTACK_PUNCH || (st == StateType::ATTACK_GUN && wtype == WeaponType::SemiAutoColt)){
//		}else
	{
		player->changeState(StateType::READY_TO_JUMP);
		}
	}

}

