#include "GEnemy.h"

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

GEnemy::GEnemy(int t): HPLShootingObject(t){

	/// 敵設定から読み込み
	//HPLObjectSetting::Object* staticData->getSetting() = NULL;

	int sp = GEnum::SpriteType::Kikai;

	if(t == EnemyType::Kikai){
		sp = GEnum::SpriteType::Kikai;
	}else if(t == EnemyType::Ramia){
		sp = GEnum::SpriteType::Ramia;
	}else if(t == EnemyType::Shokushu){
		sp = GEnum::SpriteType::Shokushu;

	}
	this->setSprite(staticData->getImageSetContainer()->getSprite(sp)->clone());
	//staticData->getSetting() = staticData->getEnemySpriteList()->getSetting(t);


	setHitRect(32,30,33,60);
	this->setSize(204,204);

	//setHitToPlayer();

	setTarget(staticData->getObjectManager()->getPlayer());

	float baseHP = 100;

	float hpRate = 1;

	if(t == EnemyType::Normal){
	}else if(t == EnemyType::Heavy){
	}else if(t == EnemyType::Body){
	}else if(t == EnemyType::Kikai){
		setHitRect(20,18,171,184);
	}else if(t == EnemyType::Ramia){
		setHitRect(48,82,118,120);
	}else if(t == EnemyType::Shokushu){
		hpRate = 3;
		setHitRect(16,84,254,219);
	}else{
	}

	setHPMax(baseHP * hpRate);
	setHP(getHPMax());
	
}

GEnemy::~GEnemy(){
	int t = getType();

	/////
	// stepCounterのDeleteとか
}

void GEnemy::collision(HPLMatrix<int>* matrix,
			   std::map<int, HPLBlock*>* blockMap){
		// TODO


/*				   const float RESIST = staticData->getSetting()->resist;
	const float GRAVITY = staticData->getSetting()->gravity;
	// 自由落下最大速度（空気抵抗とのつりあい）
	const float FALL_MAX_SPEED = staticData->getSetting()->fallMaxSpeed;

	
	int tileSize = staticData->getSetting()->tileSize;
	

	HPLRect hitArea = getHitArea();
	HPLVector velocityAngle = getVelocityAngle();
	HPLVector center = getCenter();

	{
		CollisionState stateX = HPLCollisionTool::adjustCollisionX2(
			hitArea.getWidth(),
			hitArea.getHeight(),
			hitArea.getX(), hitArea.getY(),
			velocityAngle.getX(), velocityAngle.getY(),
			matrix, tileSize, 
			*blockMap);

		bool isTouch = stateX.touchTileType != 0;
		
		this->getTrigger()->setTrigger(TriggerType::HIT_TO_THE_LEFT,
			isTouch && stateX.isHitX == HitDirectionX::LEFT);
		this->getTrigger()->setTrigger(TriggerType::HIT_TO_THE_RIGHT,
			isTouch && stateX.isHitX == HitDirectionX::RIGHT);
		///////
		// 横方向にぶつかった
		if(isTouch){
			center.setX(center.getX() + stateX.fixDeltaX);
			hitArea.setX(hitArea.getX() + stateX.fixDeltaX);

			//velocityAngle.setX(- velocityAngle.getX());
		}else{
		}
		//
		///////
	}

	{
		
		CollisionState stateY = HPLCollisionTool::adjustCollisionY2(
			hitArea.getWidth(), hitArea.getHeight(),
			hitArea.getX(), hitArea.getY(),
			velocityAngle.getX(), velocityAngle.getY(),
			matrix, tileSize, 
			*blockMap);
		// 修正量
		bool isTouch = stateY.touchTileType != 0;
		this->getTrigger()->setTrigger(TriggerType::ON_THE_GROUND,
			isTouch && stateY.isHitY == HitDirectionY::BOTTOM);
		this->getTrigger()->setTrigger(TriggerType::HIT_TO_THE_CEILING,
			isTouch && stateY.isHitY == HitDirectionY::TOP);

		if(isTouch){
			// 触れている
			center.setY(center.getY() + stateY.fixDeltaY);
			hitArea.setY(hitArea.getY() + stateY.fixDeltaY);

			velocityAngle.setY(0);

			if(stateY.isHitY == HitDirectionY::BOTTOM){
				//velocityAngle.setY(0);
				////
				// 減速
				velocityAngle.setX(HPLActionMoveTool::reduceSpeed(
					RESIST,
					velocityAngle.getX()));

			}else{
			}


		}else{
			velocityAngle.setY(velocityAngle.getY() + GRAVITY);
		}
	}

	center.add(velocityAngle);

	setCenter(center);
	setVelocityAngle(velocityAngle);


	HPLActionMoveTool::checkGroundAtTheFaceDirection((HPLShootingObject*)this,
		staticData->getBlockManager(), tileSize);
	*/
}

void GEnemy::die(HPLShootingObject* damageSource){
	setAlive(false);
}

void GEnemy::damage(float dmg, HPLShootingObject* damageSource){
	float oldHP = getHP();

	setHP(getHP() - dmg);

	
	if(getHP() <= 0){
		die(damageSource);
	}
}

void GEnemy::draw(HPLSDLImageManager* imageManager){

	// スクロール
	HPLScroller* scroller = staticData->getScrollManager()->getScroller(0);

	float left = this->getArea().getX() + scroller->getVirtualRect().getX();
	float top = this->getArea().getY() + scroller->getVirtualRect().getY();

	// hit rectの表示
	/*staticData->fillRect(
		left + this->getHitRect().getX(),
		top + this->getHitRect().getY(),
		this->getHitRect().getWidth(),
		this->getHitRect().getHeight(),
		100,0,0);*/

	/////
	// 種類の表示
	HPLSDLKanjiManager* sdlkanji = staticData->getSDLKanjiManager();
	char labels[][20] ={
		"通常",
		"強",
		"腹",
		"機械",
		"ラミア",
		"触手",
		"裸通常",
	};
	sdlkanji->drawString(staticData->getGraphics()->getScreen(),
		left, top, 0, 255,255,255, labels[getType()]);


	/*if(isHoldEnemy() == false || (isHoldEnemy() && getState()->getState() == StateType::STAND)){
		return ;
	}*/

	// シーケンスに変換
	HPLSequence* seq = this->getSequence();
	
	///////
	// えびげんの場合
	// ゲームの状況によりアニメーション速度が変化
	/*if(getType() == EnemyType::Evigen && getState()->getState() == StateType::ATTACK_PINEAPPLE){
		HPLImageInformation* info = seq->getImageInformation(seq->getFrameIndex());
		info->waitFrame = staticData->getGame()->getThrowData()->interval;
	}*/




	//this->setHitRect(seq->getHitRect());



	float rotateAngle = 0;
	int alpha = 255;

	HPLActionMoveTool::drawCommon(scroller, this->getSequence(), this->getArea(),
		imageManager, staticData->getGraphics()->getScreen(), rotateAngle, alpha);

	// アニメーションステップ
	// 
	if(staticData->getGame()->isTheWorld() == false){
		seq->step();
	}
	

}

/**
	アルゴリズム
*/
void GEnemy::algorithm(){
}


HPLSequence* GEnemy::getSequence(){
	int seqID = getSequenceID();

	HPLSequence* seq = getSprite()->getPair(seqID)->left;
	if(getFaceDirection() == FaceDirection::RIGHT){
		seq = getSprite()->getPair(seqID)->right;
	}

	return seq;

}
int GEnemy::getSequenceID(){
	int id = 0;

	int st = getState()->getState();
	if(st == StateType::ATTACK){
		id = GEnum::Simple::ATTACK;
	}else if(st == StateType::ATTACK_2){
		id = GEnum::Simple::ATTACK2;
	}

	return id;
}
void GEnemy::changeState(int st){
	int oldSt = getState()->getState();
	//////
	// 死亡時は異なるシーケンスにする
	if(this->isAlive() ){
	}else if(st != StateType::DISAPPEAR && 
		st != StateType::BROKEN){
		/////
		// 死亡
		if(oldSt == StateType::STAND || oldSt == StateType::WALK){
			st = StateType::DEAD;
		}
	}

	if(oldSt != st){
		this->getSequence()->resetCounter();
	}

	setState(st);
}

bool GEnemy::isHoldEnemy(){
	int t = getType();
	if(t == EnemyType::Kikai ||
		t == EnemyType::Ramia ||
		t == EnemyType::Shokushu){
			return true;
	}

	return false;
}

