//
//  MainCharacter.cpp
//  GlobalGameJam15
//
//  Created by Cesar Parent on 24/01/2015.
//  Copyright (c) 2015 Cesar Parent. All rights reserved.
//

#include "MainCharacter.h"
#include "TimedLabel.h"
#include "DungeonGame.h"


MainCharacter::MainCharacter() : Entity("assets/characters/player.png")
{
	setAnchorPoint(0.5, 0.7);
	animate("assets/anim/player/idle_down.spt");
	_setEmitPoint(kMotionDown);
	lives = 10;
	addCollisionBody(0, 6, kTileSize, kTileSize);
	collisionBody.categoryBitmask = 0b10;
	collisionBody.collisionBitmask = 0b01;
}

MainCharacter::~MainCharacter()
{
	
}

Particle* MainCharacter::emit(int timeInterval)
{
	if(rand() > RAND_MAX*0.4) return nullptr;
	
	Particle* part = new Particle("assets/particles/fire.png", {0,0,1,1}, 40 + rand() % 20);
	
	part->shouldFade(true);
	part->setPosition(_position.x + _emitPoint.x + rand()%4-2, _position.y + _emitPoint.y);
	part->setYScale(2);
	part->setXScale(1);
	part->setAnchorPoint(.5, .5);
	part->setAccelerator(0, -50);
	part->setSpeed(0, -5);
	part->setRotation(rand()%360);
	part->setRotationSpeed(rand()%1440 - 720);
	
	return part;
}

void MainCharacter::didStartMotion(EntityMotion direction)
{
	_setEmitPoint(direction);
	switch(direction)
	{
		case kMotionRight:
			animate("assets/anim/player/jump_right.spt");
			break;
		case kMotionLeft:
			animate("assets/anim/player/jump_left.spt");
			break;
		case kMotionUp:
			animate("assets/anim/player/jump_up.spt");
			break;
		case kMotionDown:
			animate("assets/anim/player/jump_down.spt");
			break;
		default:
			break;
	}
	if(_moveController == nullptr) return;
	_moveController->nextTurn();
}

void MainCharacter::takeTurn()
{
	_move = true;
}

void MainCharacter::didFinishMotion(EntityMotion direction)
{
	_setEmitPoint(direction);
	switch(direction)
	{
		case kMotionRight:
			animate("assets/anim/player/idle_right.spt");
			break;
		case kMotionLeft:
			animate("assets/anim/player/idle_left.spt");
			break;
		case kMotionUp:
			animate("assets/anim/player/idle_up.spt");
			break;
		case kMotionDown:
			animate("assets/anim/player/idle_down.spt");
			break;
		default:
			break;
	}
	_move = false;
}

void MainCharacter::move(EntityMotion direction)
{
	if(!_move) return;
	Entity::move(direction);
}

void MainCharacter::_setEmitPoint(EntityMotion direction)
{
	switch(direction)
	{
		case kMotionRight:
			_emitPoint = {16, -14};
			break;
		case kMotionLeft:
			_emitPoint = {-12, -14};
			break;
		case kMotionUp:
			_emitPoint = {-6, -16};
			break;
		case kMotionDown:
			_emitPoint = {10, -16};
			break;
		default:
			break;
	}
}

