#include "enemy.h"

Enemy::Enemy(glm::vec2 pos, glm::vec2 size)
	:Position(pos), Size(size)
{
	ResourceManager::LoadTexture("art/hostiles/enemyAnim1.png", "idleAnim");
	this->Sprite = ResourceManager::GetTexture("idleAnim");
}
Enemy::~Enemy() {}
void Enemy::drawEnemy(SpriteRenderer& renderer) {
	renderer.DrawSprite(this->Sprite, this->Position, this->spriteUVs, this->Size, 0.0f);
}

void Enemy::calcPos(float dt) {
	//walking
	if (this->hSpeed && 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;
		this->vSpeed = 0.0f;
	}*/
	//else { this->vSpeed = maxGravity; this->state = JUMP; }

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

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



void Enemy::animSprite(float dt) {
	switch (state) {
	case IDLE:
		this->numTiles = 8;
		this->animSpeed = 12.0f;
		this->index = 16;
		break;
	case WALK:
		this->numTiles = 4;
		this->animSpeed = 12.0f;
		this->index = 0;
		break;
	case RUN:
		this->numTiles = 16;
		this->animSpeed = 12.0f;
		this->index = 4;
		break;
	case JUMP:
		this->numTiles = 4;
		this->animSpeed = 12.0f;
		this->index = 8;
		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;
	}
}