#include "StdAfx.h"
#include "Character.h"

#include "OGLrendrer.h"
#include "Level.h"
#include "Block.h"
#include "Game.h"
#include "Bomb.h"

extern Level *myLvl;

Character::Character(int x_, int y_)
{
	x = x_;
	y = y_;
	associatedGame = NULL;
	carriedBomb = NULL;
}


Character::~Character(void)
{
}

void 
Character::draw(OGLrendrer *ogl)
{
	void(__stdcall* glMultiTexCoord2dARB)(GLenum, double,double)=(void(__stdcall*)(GLenum,double,double))ogl->getGlProcAddress("glMultiTexCoord2dARB");

	float x_ = x+0.5;
	float y_ = y+0.5;


	glBindTexture(GL_TEXTURE_2D, 2);

	//glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, );

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	//glMatrixMode(GL_MODELVIEW);
	//glPushMatrix();
	//glLoadIdentity();
	//glRotatef(orientation, 0, 0, 1);
	glBegin(GL_QUADS);
		glMultiTexCoord2dARB(GL_TEXTURE0_ARB,0.00,1.0); glVertex3f(x_-0.33, y_-0.33, 1.25);
		glMultiTexCoord2dARB(GL_TEXTURE0_ARB,0.00,0.0); glVertex3f(x_-0.33, y_+0.33, 1.25);
		glMultiTexCoord2dARB(GL_TEXTURE0_ARB,1.00,0.0); glVertex3f(x_+0.33, y_+0.33, 1.25);
		glMultiTexCoord2dARB(GL_TEXTURE0_ARB,1.00,1.0); glVertex3f(x_+0.33, y_-0.33, 1.25);
	glEnd();
	// Undo what we just did to the modelview matrix.
	//glRotatef(-orientation, 0, 0, 1);

	//glPopMatrix();

	glDisable(GL_BLEND);
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}

void 
Character::move(unsigned char direction)
{
	while(true)
	{
		int destX = x;
		int destY = y;

		switch(direction)
		{
		case 1:
			destX += 1;
		break;
		case 2:
			destY += 1;
		break;
		case 3:
			destX -= 1;
		break;
		case 4:
			destY -= 1;
		break;
		}

		int oldX = x;
		int oldY = y;

		if(isCarrying())
		{
			Block *newBlock = myLvl->getBlock(destX, destY);
			if(newBlock)
			{
				if((!newBlock->canMove()) || (newBlock->getBomb() != NULL))
				{
					// Drop bomb in existing square.
					putDown();
				}
			}
		}

		x = destX;
		y = destY;

		Block *curBlock = myLvl->getBlock(oldX, oldY);
		if(curBlock)
			curBlock->leaveSquare();

		curBlock = myLvl->getBlock(x, y);
		if(!curBlock)
		{
			// We just fell off the world and died. 
			if(associatedGame)
				associatedGame->die();
			return;
		}
		else if(curBlock->canStop())
		{
			return;
		}
	}
}

void 
Character::getPosition(float *x_, float *y_)
{
	*x_ = x;
	*y_ = y;
}

void 
Character::triggerSquare()
{
	Block *curBlock = myLvl->getBlock(x, y);
	if(curBlock)
		curBlock->trigger();
}

void 
Character::pickUp()
{
	if(isCarrying())
		return;

	Block *curBlock = myLvl->getBlock(x, y);
	if(curBlock)
	{
		if(curBlock->canMove())
		{
			carriedBomb = curBlock->getBomb();
			if(carriedBomb)
			{
				curBlock->removeBomb();
			}
		}
	}
}

void 
Character::putDown()
{
	if(carriedBomb)
	{
		Block *curBlock = myLvl->getBlock(x, y);
		if(curBlock->canMove())
		{
			carriedBomb->setNewCoords(x, y);
			curBlock->addBomb(carriedBomb);
			carriedBomb = NULL;
		}
	}
}

void 
Character::carryImpulse()
{
	if(isCarrying())
		putDown();
	else
		pickUp();
}

bool 
Character::isCarrying()
{
	if(carriedBomb)
		return true;
	return false;
}

void 
Character::setGame(Game *a)
{
	associatedGame = a;
}

void 
Character::exploded()
{
	associatedGame->die();
}