#include <allegro.h>

#include "player.h"
#include "collide.h"


Player::Player()
{
    Reset();
    mask=NULL;
    player=NULL;
}

Player::~Player()
{
    if(mask)    destroy_mask(mask);
}

void Player::SetBitmap(BITMAP *bmp)
{
    player = bmp;
    if(mask)    destroy_mask(mask);
    mask = create_mask(bmp);
}

void Player::Draw(BITMAP *bmp)
{
    draw_sprite(bmp, player, (int) x, (int) y);
}

void Player::AddX(float how)
{
    x += how;

    x = (x < 0) ? 0 : x;
    x = (x > (SCREEN_W - player->w)) ? (SCREEN_W - player->w) : x;
}

void Player::Reset()
{
    x = (SCREEN_W / 2) - 32;
    y = 350 - 32 - 10;
    score = 0;
    lives = 3;
    energy = 4500; 
}

void Player::ResetPos()
{
    y = 350 - 32 - 10;
    energy = 4500;
}

void Player::AddScore(int num)  { score += num; }
int Player::GetScore()  { return score; }

void Player::AddLife()  { lives++; lives = (lives>6) ? 6 : lives; }
void Player::RemoveLife()   { lives--; lives = (lives < -1) ? -1 : lives; }
int Player::GetLives()  { return lives; }
bool Player::Alive()    { if(lives!=-1) return true; else return false; }

void Player::SubEnergy(int num)
{
    energy -= num;
    energy = (energy < 0) ? 0 : energy;
}
int Player::GetEnergy()    { return energy; }
void Player::SetEnergy(int num) { energy = num; }

float Player::X()   { return x; }
float Player::Y()   { return y; }

const Player &Player::operator=(const Player &src)
{
    if(this != &src)
    {
	player = src.player;
	mask = src.mask;
	x = src.x;
	y = src.y;
	score = src.score;
	lives = src.lives;
	energy = src.energy; 
    }

    return *this;
}

CMask *Player::GetMask()
{
    return mask;
}
