function Enemy( x , y , hp , fast )
{
    this.x = x;
    this.y = y;
    this.speed = 35 + Math.random()*21;
    if ( fast ) this.speed = 65 + Math.random()*21;
    this.maxhp = hp;
    this.hp = this.maxhp;
    this.b_alive = true;
    this.width = 40;
    this.height = 40;
    this.touchDamage = 1;
}



Enemy.prototype.update = function( dt )
{
    if ( !this.b_alive ) return;

    var dx = player.x - this.x;
    var dy = player.y - this.y;
    var angle = 0;
    if ( dx >= 0 )
    {
        angle = Math.atan( dy / dx );
    }
    else
    {
        angle = Math.atan( dy / dx ) + Math.PI;
    }

    this.x += Math.cos( angle ) * dt * this.speed;
    this.y += Math.sin( angle ) * dt * this.speed;

    if ( this.isTouchingPlayer() )
    {
        player.damage( this.touchDamage );
    }
}



Enemy.prototype.isInSwordRange = function()
{
    if ( !this.b_alive ) return;

    if ( (player.x-this.x)*(player.x-this.x) + (player.y-this.y)*(player.y-this.y) < (player.swordRange+20)*(player.swordRange+20) )
    {
        return true;
    }
    else return false;
}



Enemy.prototype.isTouchingPlayer = function()
{
    if ( this.x+this.width/2 < player.x-player.width/2 ) return false;
    if ( this.x-this.width/2 > player.x+player.width/2 ) return false;
    if ( this.y+this.height/2 < player.y-player.height/2 ) return false;
    if ( this.y-this.height/2 > player.y+player.height/2 ) return false;
    return true;
}



Enemy.prototype.damage = function( damage )
{
    if ( !this.b_alive ) return;

    this.hp -= damage;
    if ( this.hp <= 0 )
    {
        this.hp = 0;
        this.b_alive = false;
        game.score += 10;
        game.spawnUpgrade( this.x , this.y );
        vanquishedSound.play();
    }
    else
    {
        enemyHurtSound.play();
    }
}



Enemy.prototype.draw = function( ctx )
{
    if ( !this.b_alive ) return;

    ctx.drawImage( enemyImage , this.x-this.width/2 , this.y-this.height/2 , this.width , this.height );
}