GameEntity::GameEntity( const char* a_pSpriteSheet, GLFWwindow * window)
{
	GameWindow = window;
	elapsedTime = 0;
	LoadVertShader("./Resources/LoadVertexShader.glsl");
	LoadFragShader("./Resources/LoadFragmentShader.glsl");
	LinkShaders();
	GLint uvAttrib = glGetAttribLocation(m_ShaderProgram,"texcoord");
	glEnableVertexAttribArray(uvAttrib);
	matrix_location = glGetUniformLocation (m_ShaderProgram, "matrix");
	LoadSprites(a_pSpriteSheet);
	LoadAnimations(atlas.sAnimations.c_str());
	LoadTexture(atlas.sSheet.c_str());


	m_dFrames = (1.0/15.0);
	currentAnimation = "Idle";
	currentSprite = "Idle_01";
	currentFrame = 0;
	currentPlayType = SINGLE;
	m_uvScale.m_fX = atlas.v2Size.m_fY;
	m_uvScale.m_fY	= atlas.v2Size.m_fX;
	SetSprite();
	SetUVData();
	iFacing = "Right"; //flag for facing


}
void GameEntity::PlayAnimation()
{
	elapsedTime += getDeltaTime();
	
	if(elapsedTime > m_dFrames){
		elapsedTime = 0;
	switch (currentPlayType){
	case ONCE:
		if(mAnimations.at(currentAnimation)[currentFrame] != mAnimations.at(currentAnimation).back())
		{
			currentFrame++;
			currentSprite = mAnimations.at(currentAnimation)[currentFrame];
		}
		break;
	case LOOPSECTION:
	case LOOP:
		if(mAnimations.at(currentAnimation)[currentFrame] == mAnimations.at(currentAnimation).back())
		{
			currentFrame = loopFrame;
			currentSprite = mAnimations.at(currentAnimation)[currentFrame];
		}
		else
		{
			currentFrame++;
			currentSprite = mAnimations.at(currentAnimation)[currentFrame];
		}
		break;
	case PINGPONG:
		break;
	case REVERSE:
		currentFrame = mAnimations[currentAnimation].size();
		loopFrame = currentFrame;
		break;
	case RANDOMLOOP:
	case RANDOM:
		srand(time(NULL));
		currentFrame =  rand() % mAnimations[currentAnimation].size();
		loopFrame = currentFrame;
		break;
	case SINGLE:
		break;
	default:
		break;
	}
	SetSprite();
	SetUVData();
	}



}
AnimatedSprite::AnimatedSprite(const char* a_pSpriteSheet, GLFWwindow * window) {
	GameWindow = window;
	m_dElapsedTime = 0;
	LoadVertShader("../resources/VertexShader.glsl");
	LoadFragShader("../resources/FragShader.glsl");
	LinkShaders();

	GLint uvAttrib = glGetAttribLocation(m_ShaderProgram, "texcoord");
	glEnableVertexAttribArray(uvAttrib);
	matrix_location = glGetUniformLocation(m_ShaderProgram, "matrix");
	LoadSprites(a_pSpriteSheet);
	LoadAnimations(atlas.sAnimations.c_str());

	m_dFrames = (1.0 / 15.0);
	sCurrentAnimation = "";
	sCurrentSprite ="idle 0";
	CurrentPlayType = SINGLE;
	m_uvScale.m_fX = atlas.v2Size.m_fY;
	m_uvScale.m_fY = atlas.v2Size.m_fX;
	SetSprite();
	SetUVData();
}
AnimatedSprite::AnimatedSprite( const char* a_pSpriteSheet, GLFWwindow * window)
{
	//Make GameWindow equal window
	GameWindow = window;
	//Make elapsedTime equal 0
	elapsedTime = 0;
	//Load the Shaders
	LoadVertShader("resources/VertexShader.glsl");
	LoadFragShader("resources/FragmentShader.glsl");
	//Link the Shaders
	LinkShaders();
	//Make a GLint and equal it to glGetAttribLocation
	GLint uvAttrib = glGetAttribLocation(m_ShaderProgram,"texcoord");
	//Call glEnableVertexAttribArray with the required data
	glEnableVertexAttribArray(uvAttrib);
	//Make matrix_location equal to glGetUniformLocation
	matrix_location = glGetUniformLocation (m_ShaderProgram, "matrix");
	
	//Load the Sprites,Animations, and Texture
	LoadSprites(a_pSpriteSheet);
	//
	LoadTexture(atlas.sSheet.c_str());
	//Make m_dFrames equal to 1 divided by 2
	m_dFrames = (1.0/2.0);
	//Make currentAnimation equal nothing
	currentAnimation = "";
	//Make the currentSprite equal idle
	//currentSprite = "idle";
	//Make the currentFrame equal 0
	currentFrame = 0;
	//Make uvScale equale the atlas size
	m_uvScale.m_fX = atlas.v2Size.m_fY;
	m_uvScale.m_fY	= atlas.v2Size.m_fX;
	//Call SetSprite
	SetSprite();
	//call SetUVData
	SetUVData();
}
void AnimatedSprite::PlayAnimation() {
	m_dElapsedTime += getDeltaTime();

	if (m_dElapsedTime > m_dFrames) {
		switch (CurrentPlayType) {
		case ONCE:
			if (mAnimations.at(sCurrentAnimation)[iCurrentFrame] != mAnimations.at(sCurrentAnimation).back()) {
				iCurrentFrame++;
				sCurrentSprite = mAnimations.at(sCurrentAnimation)[iCurrentFrame];
			}
			break;
		case LOOPSECTION:
		case LOOP:
			if (mAnimations.at(sCurrentAnimation)[iCurrentFrame] == mAnimations.at(sCurrentAnimation).back()) {
				iCurrentFrame = iLoopFrame;
				sCurrentSprite = mAnimations.at(sCurrentAnimation)[iCurrentFrame];
			} else {
				iCurrentFrame++;
				sCurrentSprite = mAnimations.at(sCurrentAnimation)[iCurrentFrame];
			}
			break;
		case REVERSE:
			iCurrentFrame = mAnimations[sCurrentAnimation].size();
			iLoopFrame = iCurrentFrame;
			break;
		case RANDOMLOOP:
		case RANDOM:
			srand(time(NULL));
			iCurrentFrame = rand() % mAnimations[sCurrentAnimation].size();
			iLoopFrame = iCurrentFrame;
			break;
		default:
			break;
		}
		SetSprite();
		SetUVData();
	}
}
void GameEntity::SetAnimation(std::string animation,PlayType type)
{
	currentAnimation = animation;
	currentFrame = 0;
	loopFrame = 0;
	currentPlayType = type;
	switch (type){
	case ONCE:
		
		break;
	case LOOP:
		loopFrame =0;
		break;
	case PINGPONG:
		break;
	case REVERSE:
		currentFrame = mAnimations[currentAnimation].size();
		loopFrame = currentFrame;
		break;
	case RANDOMLOOP:
	case RANDOM:
		srand(time(NULL));
		currentFrame =  (unsigned int)(rand() % mAnimations[currentAnimation].size());
		loopFrame = currentFrame;
		break;
	case LOOPSECTION:
		SetAnimation(animation,type, 0);
	case SINGLE:
		break;
	default:
		break;
	}
	currentSprite = mAnimations.at(currentAnimation)[currentFrame];
	SetSprite();
	SetUVData();

}
/*void AnimatedSprite::LoadAnimations(const char* a_pAnimationSheet)
{
	tinyxml2::XMLDocument doc;
	tinyxml2::XMLNode *rootNode, *currentNode;
	tinyxml2::XMLElement *childElement, *child;
	std::string str,aniName;
	doc.LoadFile(a_pAnimationSheet);
	rootNode = doc.FirstChildElement("animation");
	currentNode = rootNode;
	for (childElement = currentNode->ToElement(); 
		childElement != NULL; childElement = childElement->NextSiblingElement())
	{
		aniName = childElement->Attribute("name");
		int i = 0;
		for (child = childElement->FirstChildElement(); 
			child != NULL; child = child->NextSiblingElement())
		{

			str = child->Attribute("name");
			mAnimations[aniName].push_back(str);
			i++;
		}
	}
	std:printf("done");
}
void AnimatedSprite::SetAnimation(std::string animation,PlayType type)
{

	//Set currentAnimation to animation
	currentAnimation = animation;
	//Set currentFrame and loopFrame to 0
	currentFrame = 0;
	loopFrame = 0;
	//Set currentPlayType to type
	currentPlayType = type;
	//Make a switch for type
	switch (type)
	{
		//Case ONCE
		case ONCE:
			break;
		//Case LOOP
		case LOOP:
			loopFrame = 0;
			break;
		//Case PINGPONG
		case PINGPONG:
			break;
		//Case REVERSE
		case REVERSE:
			currentFrame = mAnimations[currentAnimation].size();
			loopFrame = currentFrame;
			break;
		//Case RANDOMLOOP
		case RANDOMLOOP:
		//Case RANDOM
		case RANDOM:
			srand(time(NULL));
			currentFrame =  (unsigned int)(rand() % mAnimations[currentAnimation].size());
			loopFrame = currentFrame;
			break;
		//Case LOOPSECTION
		case LOOPSECTION:
			SetAnimation(animation,type, 0);
		//Case SINGLE
		case SINGLE:
			break;
		//Default
		default:
			break;
	}
	//Set currentSprite to currentAnimation's currentFrame
	currentSprite = mAnimations.at(currentAnimation)[currentFrame];
	//Call SetSprite
	SetSprite();
	//Call SetUVData
	SetUVData();
}
void AnimatedSprite::SetAnimation(std::string animation,PlayType type, int frame)
{
	//Make a switch for type
	switch(type)
	{
		//Case LOOPSECTION
		case LOOPSECTION:
			currentAnimation = animation;
			currentFrame = 0;
			currentPlayType = type;
			loopFrame = frame;
			break;
		//Default
		default:
			SetAnimation(animation,type);
			break;
	}
}*/
void AnimatedSprite::PlayAnimation()
{
	//Add getDeltaTime to elapsedTime
	elapsedTime += getDeltaTime();
	//If elapsedTime is greater than m_dFrames
	if(elapsedTime > m_dFrames)
	{
		// Reset elapsed time
		elapsedTime = 0;
		//Make currentFrame equal 1
		if(currentFrame == 1)
		{
			currentFrame = 0;
		}
		else
		{
			currentFrame = 1;
		}
		//Call SetSprite
		SetSprite();
		//Call SetUVData
		SetUVData();
	}
}
void AnimatedSprite::SetAnimation(std::string a_sAnimation, PlayType a_Type) {
	sCurrentAnimation = a_sAnimation;
	iCurrentFrame = 0;
	iLoopFrame = 0;
	CurrentPlayType = a_Type;

	switch (a_Type) {
	case ONCE:
		break;
	case LOOP:
		iLoopFrame = 0;
		break;
	case PINGPONG:
		break;
	case REVERSE:
		iCurrentFrame = mAnimations[sCurrentAnimation].size();
		iLoopFrame = iCurrentFrame;
		break;
	case RANDOMLOOP:
	case RANDOM:
		srand(time(NULL));
		iCurrentFrame = (unsigned int)(rand() % mAnimations[sCurrentAnimation].size());
		iLoopFrame = iCurrentFrame;
		break;
	case LOOPSECTION:
		SetAnimation(a_sAnimation, a_Type, 0);
	case SINGLE:
		break;
	default:
		break;
	}
	sCurrentSprite = mAnimations.at(sCurrentAnimation)[iCurrentFrame];
	SetSprite();
	SetUVData();

}