#include "player.h"

Player::Player(glm::vec2 pos, glm::vec2 size)
	:Position(pos), Size(size)
{
	ResourceManager::LoadTexture("../art/player/playerAnim.png", "idle1");
	this->Sprite = ResourceManager::GetTexture("idle1");
}
Player::~Player(){}
void Player::drawPlayer(SpriteRenderer &renderer) {
	renderer.DrawSprite(this->Sprite, this->Position, this->spriteUVs, this->Size, 0.0f);
}

void Player::calcPos(float dt) {
	//walking
	if (this->hSpeed != 0 && this->hSpeed < maxRunningSpeed && this->hSpeed > -maxRunningSpeed) {
		this->state = WALK;
	}
	//running
	else if (this->hSpeed >= this->maxRunningSpeed || this->hSpeed <= -this->maxRunningSpeed) {
		this->state = RUN;
	}
	else { this->state = IDLE; }
	//vSpeed
	if (this->onGround) {
	this->vSpeed = 0.0f;
	this->onGround = 0;
	}
	else if (!this->onGround && this->vSpeed <= maxGravity) {
		this->vSpeed += gravity*dt; this->state = JUMP;
	}
	else { this->vSpeed = maxGravity; this->state = JUMP; }

	this->Position += glm::vec2(this->hSpeed, this->vSpeed) *dt;

	this->playerBody.min = c2V(this->Position.x + 50.0, this->Position.y + 50.0);
	this->playerBody.max = c2V(this->Position.x - 50.0 + this->Size.x, this->Position.y + this->Size.y);
}



void Player::animSprite(float dt) {
	switch (state) {
	case IDLE:
		this->numTiles = 6;
		this->animSpeed = 4.0f;
		this->index = 50;
		break;
	case WALK:
		this->numTiles = 8;
		this->animSpeed = 6.5f;
		this->index = 40;
		break;
	case RUN:
		this->numTiles = 14;
		this->animSpeed = 12.0f;
		this->index = 80;
		break;
	case JUMP:
		this->numTiles = 1;
		this->index = 100;
		//start jump
		if (this->vSpeed >= this->jumpStrength && this->vSpeed <= this->jumpStrength*7.5f/9.0f) {
			this->index = 110;
		}
		//medium
		else if (this->vSpeed >= this->jumpStrength * 7.5f / 9.0f && this->vSpeed <= this->jumpStrength * 5.0f / 9.0f) {
			this->index = 111;
		}
		//high
		else if (this->vSpeed >= this->jumpStrength * 5.0 / 9.0f && this->vSpeed <= this->jumpStrength * 2.0f / 9.0f) {
			this->index = 112;
		}
		//hover1
		else if (this->vSpeed >= this->jumpStrength * 2.0 / 9.0f && this->vSpeed <= this->jumpStrength * 0.0f / 9.0f) {
			this->index = 113;
		}
		//hover2
		else if (this->vSpeed >= this->jumpStrength * 0.0 / 9.0f && this->vSpeed <= -this->jumpStrength * 2.0f / 9.0f) {
			this->index = 114;
		}
		//falling
		else if (this->vSpeed >= -this->jumpStrength * 2.0 / 9.0f /*&& this->vSpeed <= -this->jumpStrength * 6.0f / 9.0f*/) {
			this->index = 115;
		}
		break;
	}
	this->animTime += animSpeed*dt;
	this->index += (int)this->animTime % this->numTiles;
	this->spriteUVs = this->spriteSheet->getUVs(this->index);
	if (this->direction == -1) {
		float z = this->spriteUVs.z;
		this->spriteUVs.z = this->spriteUVs.x;
		this->spriteUVs.x = z;
	}
}