#include "GPMEvigenAttack.h"

#ifdef _DXLIB
#include "GStaticDataDX.h"
#else
#include "GStaticData.h"
#endif

using namespace GEnum;

GPMEvigenAttack::GPMEvigenAttack(){
}
GPMEvigenAttack::~GPMEvigenAttack(){
}

void GPMEvigenAttack::setup( HPLShootingObject* lpObject ) {
	this->m_State = *lpObject->getState();
	this->m_nWeaponType = lpObject->getUsingWeaponTypeIndex();

	bool bIsPlayer = lpObject->getObjectType() == ObjectType::PLAYER;
	this->m_bIsHeavyWeapon = false;

	this->m_lpSequence = getStaticData()->m_CommonWork.getSequenceFromObject( lpObject );

	this->m_SequenceState = this->m_lpSequence->getState();

}


void GPMEvigenAttack::attack( HPLShootingObject* lpObject, HPLShootingObject* lpTarget,
	HPLVector* lpCenter, HPLVector* lpVelocityAngle, 
	std::vector<bool>& vecCurrentKeyState, std::vector<bool>& vecPreviousKeyState )
{
	// セットアップ
	this->setup( lpObject );

	int nFaceDirection = lpObject->getFaceDirection();

	// 
	if( this->m_State.getState() == StateType::ATTACK ||
		this->m_State.getState() == StateType::ATTACK_GUN
		)
	{

		int nShotType = ShotType::BULLET;
		if( this->m_State.getState() == StateType::ATTACK ) {
			// TODO
			nShotType = ShotType::FRAME_THROWER_FIRE;
		}

		// 発射
		HPLVector vecShotCenter = lpObject->getCenter();
		vecShotCenter.add( (lpObject->getFaceDirection() > 0 ? 1 : -1) * 20,
			25 );
		GShot* lpShot = getStaticData()->m_ObjectManager.m_ShotManager.create(
			nShotType, vecShotCenter, lpObject->getID(), lpObject->getFaceDirection() );

		if( this->m_SequenceState.isFinishAnimation ) {
			
			// シーケンスリセット
			this->m_lpSequence->resetCounter();

			if( this->m_State.getState() == StateType::ATTACK &&vecCurrentKeyState[ GEnum::KeyMap::A ] )
				//this->m_nWeaponType == GEnum::WeaponType::Evigen::MINIGUN )
			{
				// 押しっぱなし
				// ステートそのまま
			} else if( this->m_State.getState() == StateType::ATTACK_GUN && vecCurrentKeyState[ GEnum::KeyMap::B ] ) {
				// do nothing
			} else {

				// ステート巻き戻し
				getStaticData()->m_CommonWork.rollBack( lpObject );
			}
		}
	} else if( this->m_State.isAttackState() ) {
		if( this->m_SequenceState.isFinishAnimation ) {
			// ステート巻き戻し
			getStaticData()->m_CommonWork.rollBack( lpObject );
		}
	} else {
		// 通常状態

		if( lpObject->getState()->isReadyToAttackState() ) {
			// 攻撃可能な状態

			if( vecCurrentKeyState[ GEnum::KeyMap::A ] ) {
				// Z/近接→火炎放射器
				getStaticData()->m_CommonWork.changeState( lpObject, StateType::ATTACK ) ;

			} else if( vecCurrentKeyState[ GEnum::KeyMap::B ] ) {
				// X→ミニガン
				getStaticData()->m_CommonWork.changeState( lpObject, StateType::ATTACK_GUN );

			} else if( vecCurrentKeyState[ GEnum::KeyMap::C ] && vecPreviousKeyState[ GEnum::KeyMap::C ] == false ) {

				getStaticData()->m_CommonWork.changeState( lpObject, StateType::ATTACK_2 );

				lpVelocityAngle->setX( 
					-1 * (lpObject->getFaceDirection() > 0 ? 1 : -1) * EVIGEN_MISSILE_BACK_SPEED_X );
				// C→ミサイル
				HPLVector vecShotCenter = lpObject->getCenter();
				vecShotCenter.add( (lpObject->getFaceDirection() > 0 ? 1 : -1) * 20,
					0 );
				GShot* lpShot = getStaticData()->m_ObjectManager.m_ShotManager.create(
					ShotType::MISSILE, vecShotCenter, lpObject->getID(), nFaceDirection );
				lpShot->setVelocityAngle( 
					( lpObject->getFaceDirection() > 0 ? 1 : -1 ) * 
					EVIGEN_MISSILE_SPEED_X , 0);


			}
		}
	}
}
