
class Astroid {
    Initialize(engine,x,y) {
		this.x = x;
		this.y = y;
		this.rotate = Math.random();
		this.xv = Math.random()*10 -5;
		this.yv = Math.random()*10 -5;
		this.rv = Math.random()*0.05 -0.025;
		this.life = 500;
		this.size = Math.random()*200 +20;
    	this.missle_image = engine.LoadImage('asteroid.png');
	
		engine.draw_entities.Add(this);
		engine.calculate_entities.Add(this);
		engine.collide_entities.Add(this);


    }
    
    Destroy(engine) {
		engine.draw_entities.Remove(this);
		engine.calculate_entities.Remove(this);
		engine.collide_entities.Remove(this);
	}
	
    Draw(engine) {
		//console.log('drawmissle');
	    engine.ctx.save();
	    engine.ctx.translate(this.x,this.y);
	    engine.ctx.rotate(this.rotate*2*Math.PI);
        var h= this.size;
    	var w=this.missle_image.width*h/this.missle_image.height;
        engine.ctx.drawImage(this.missle_image, -w/2, -h/2, w, h);
            //this.ctx.scale(-1,1);
            //this.ctx.drawImage(image, -x+width/2, ground-y-height, -width, height);
        engine.ctx.restore();
	}
	
	Collide(engine, other) {
		if (other instanceof Ship) {
			this.Explode(engine);
			
		}
	}
	Calculate(engine) {
		this.x += this.xv;
    	this.y += this.yv;
    	this.rotate += this.rv;

    	this.xv *= 0.99;
    	this.yv *= 0.99;
		this.life -= 1;
		if (this.life<=0) {
				this.Destroy(engine);
		}
    }
    Explode(engine) {

		engine.PlayAudio('small_explosion.mp3');
		
			 
		var explosion = new Explosion();
		explosion.Initialize(engine, this.x, this.y, 150, 0);
		//explosion.xv = this.xv;
		//explosion.yv = this.yv;
		this.Destroy(engine);
	}
    
} // class Astroid



 
 
