#include ".\maincharacter.h"

MainCharacter::MainCharacter( CharacterManager* characterManager, CollisionManager* collisionManager, TextManager* newText, DialogueManager* newDialogue, int tileX, int tileY ) : Character( characterManager, collisionManager, newText, newDialogue, 3, tileX, tileY ){
	// Fill in states
	RECT* boundingBox = new RECT();
	boundingBox->left =	18;
	boundingBox->top = 51;
	boundingBox->right = 34;
	boundingBox->bottom = 59;
	setSpriteState( spriteStates, 0, 0, 6, boundingBox, 50, 68 );
	boundingBox = new RECT();
	boundingBox->left =	33;
	boundingBox->top = 71;
	boundingBox->right = 49;
	boundingBox->bottom = 79;
	setSpriteState( spriteStates + 1, 200, 0, 4, boundingBox, 80, 104 );
	setSpriteState( spriteStates + 2, 200, 0, 1, boundingBox, 80, 104 );
	currentState = spriteStates;

	setHeadTextureId( 0 );
	setClip( GameCore::DIRECTION ::DOWN );
	setTextureId( 5, tileX, tileY, 1024, 512, currentState->boundingBox );

	HP = 5000;
	HP_MAX = HP;

	setPosition( tileX, tileY, currentState->boundingBox );
}

MainCharacter::~MainCharacter(void){
}

HRESULT MainCharacter::tick() {
	if ( isWalking || getState() == Character::ATTACK ) {
		int y = currentState->y;
		int numFrames = currentState->numFrames;
		int clipHeight = currentState->height;
		yOffset = ( yOffset + 1 ) % numFrames;
		clippingRect->top = y + ( yOffset * clipHeight );
		clippingRect->bottom = clippingRect->top + clipHeight - 1;

		if ( ( getState() == Character::ATTACK ) && ( yOffset == numFrames - 1 ) ) {
			setState( Character::ALIVE );
		}
	}

	return S_OK;
}

void MainCharacter::collide( Character* moveChar ) {
	if( moveChar->getType() == Character::ENEMY ) {
		attacked( moveChar->getFacing() );
	}
}

Character::TYPE MainCharacter::getType() {
	return Character::TYPE::MAIN;
}

void MainCharacter::attacked(GameCore::DIRECTION aDirection) {
	setHP( getHP() - 10 );
	/*characterManager->moveCharacter( this, aDirection );
	characterManager->moveCharacter( this, aDirection );
	characterManager->moveCharacter( this, aDirection );
	characterManager->moveCharacter( this, aDirection );
	characterManager->moveCharacter( this, aDirection );
	characterManager->moveCharacter( this, aDirection );*/
	if( getHP() <= 0 ) {
	    setState( Character::DEAD );
	}
}

void MainCharacter::setCurrentSpriteState( Character::STATE aState ) {
	SpriteState* oldState = currentState;
	yOffset = 0;
	switch ( aState ) {
		case Character::STATE::ALIVE:
			currentState = spriteStates;
			break;
		case Character::STATE::ATTACK_READY:
			currentState = spriteStates + 2;
			break;
		case Character::STATE::ATTACK:
			currentState = spriteStates + 1;
			break;
		case Character::STATE::DEAD:
			currentState = spriteStates;
			break;
	}
	setClip( facing );

	// We've changed yoffset stuff....update it
	int y = currentState->y;
	int numFrames = currentState->numFrames;
	int clipHeight = currentState->height;
	clippingRect->top = y + ( ( yOffset % numFrames ) * clipHeight );
	clippingRect->bottom = clippingRect->top + clipHeight - 1;

	// Set position appropriately
	int halfWidthOld = oldState->width / 2;
	int halfWidthNew = currentState->width / 2;
	int halfHeightOld = oldState->height / 2;
	int halfHeightNew = currentState->height / 2;

	setX( getX() - ( halfWidthNew - halfWidthOld ) );
	setY( getY() - ( halfHeightNew - halfHeightOld ) );
}
