function Rocket(width, height, color, engineColor) {
	this.right = false;
	this.left = false;
	this.up = false;
	this.px = 900;
	this.py = 400;
	this.vx = 0;
	this.vy = 0;
	this.direction = 0;
	this.maxSpeed = 5;
	this.radius = 50;
	this.fireball_radius = 5;
	this.fireball_offset = this.radius + this.fireball_radius;
	this.color = color;
	this.engineColor = engineColor;
	this.shootcount = 0;
	
	
	this.move = function(width, height) {
		if (this.right) {
			this.direction = this.direction + (Math.PI/64);
		}
		if (this.left) {
			this.direction = this.direction -  (Math.PI/64);
		}
		this.direction = this.direction % (2 * Math.PI);
		if (this.up) {
			this.vx = this.vx + Math.cos(this.direction);
			this.vy = this.vy + Math.sin(this.direction);
		}
		
		
		this.vx = Math.min(this.vx, this.maxSpeed);
		this.vx = Math.max(this.vx, -this.maxSpeed);
		this.vy = Math.min(this.vy, this.maxSpeed);
		this.vy = Math.max(this.vy, -this.maxSpeed);
		
		this.px = (this.px + this.vx + width) % width;
		this.py = (this.py + this.vy + height) % height;
	}

	this.draw = function(ctx) {
		ctx.save();
		ctx.translate(this.px, this.py);
		ctx.rotate(this.direction);
		ctx.fillStyle = color;
		drawCircle(ctx, 0, 0, this.radius);
		ctx.fillStyle = engineColor;
		ctx.beginPath();
		ctx.moveTo(-this.radius, 0);
		ctx.lineTo(-(this.radius + 15), -10);
		ctx.lineTo(-(this.radius + 15), 10);
		ctx.fill();
		ctx.fillStyle = "red";
		if (this.up) {
			drawCircle(ctx, -(this.fireball_offset + 15), 0, this.fireball_radius);
		}
		ctx.restore();
	}
	
	this.shoot = function() {
			var laser = new Laser(this.px + (this.radius * Math.cos(this.direction)),
				this.py + (this.radius * Math.sin(this.direction)),
				20 * Math.cos(this.direction),
				20 * Math.sin(this.direction),
				this);
			lasers.push(laser);
//			this.shootcount = this.shootcount + 1;
//			timenow = Date.now();
//			if (timenow )
//			if (this.shootcount > 6) {
//				alert("Your cannons are worn out, please wait!")
//			}
	}
}

function Alien(width, height, color) {
	this.px = 600;
	this.py = 100;
	this.radius = 100;
	this.vx = 0;
	this.vy = 0;
	this.color = color;
	this.lastbomb = null;
	

	this.draw = function(ctx) {
		ctx.save();
		ctx.fillStyle = color;
		drawCircle(ctx, this.px, this.py, this.radius)		
		ctx.beginPath();
		ctx.moveTo(-this.radius, 0);
		ctx.lineTo(-(this.radius + 15), -10);
		ctx.lineTo(-(this.radius + 15), 10);
		ctx.fill();
		ctx.fillStyle = "red";
		ctx.restore();
	}
	
	this.dropbomb = function() {
		var now = Date.now();
		var launchspeed = (Math.random() * 10000) + 2000
		if (!this.lastbomb) {
			this.lastbomb = now - 8000;
		}
		if ((now - this.lastbomb) > (Math.random() * 1000000)) {
			var bomb = new Bomb(this.px, this.py, bomb);
			bombs.push(bomb);
			this.lastbomb = now;
		}
	}
	
}

var lasers = [];
var bombs = [];
var rocket1 = null;
var alien = null;

function draw(ctx, width, height) {
	ctx.save();
	ctx.fillStyle = "black";
	ctx.fillRect(0, 0, width, height);
	if (!rocket1) {
		rocket1 = new Rocket(width + 400, height - 300, "blue", "silver");
	}
	if (!alien){
		alien = new Alien(80, 80, "red")
	}
	rocket1.move(width, height);
	alien.dropbomb();
	if (collision(rocket1, alien)) {
		rocket1.vx = -rocket1.vx
		rocket1.vy = -rocket1.vy
		rocket1.radius = rocket1.radius - 3
	}
	if (collision(rocket1, bombs[i])) {
		rocket1.vx = -rocket1.vx
		rocket1.vy = -rocket1.vy
		rocket1.radius = rocket1.radius - 10
	}
//	if (collision(rocket1, bomb)) {
//		var rocketsavex = rocket1.vx
//		var rocketsavey = rocket1.vy
//		rocket1.vx = bomb.vx
//		rocket1.vy = bomb.vy
//		bomb.vx = -rocketsavex
//		bomb.vy = -rocketsavey
//		rocket1.radius = rocket1.radius - bomb.radius
//	}

	rocket1.draw(ctx);
	alien.draw(ctx);
	for (var i = 0; i < lasers.length; i++) {
		lasers[i].move(width, height);
		lasers[i].draw(ctx);
		lasers[i].life--;
	}
	var old_lasers = lasers;
	lasers = [];
	for (var i = 0; i < old_lasers.length; i++) {
		if (old_lasers[i].life > 0) {
			lasers.push(old_lasers[i]);
		}
	}
	
	for (var i = 0; i < lasers.length; i++) {
	    if (lasers[i].rocket != alien && collision(lasers[i], alien)) {
				alien.radius = alien.radius - 1;
				lasers[i].life = 0
				if (alien.radius <= 0) {
					alert("You destroyed the alien ship, congratulations!")
					//start over
					stop();
					lasers = [];
					rocket1 = null;
					alien = null;
					animate();
				}
		}
		
		if (lasers[i].rocket != rocket1 && collision(lasers[i], rocket1)) {
				rocket1.radius = rocket1.radius - 10;
				lasers[i].life = 0
			}
			if (rocket1.radius <= 0) {
				alert("You are dead");
				// start over
				stop();
				lasers = [];
				rocket1 = null;
				alien = null;
				animate();
			}

	}
	
	for (var i = 0; i < bombs.length; i++) {
		bombs[i].move(width, height);
		bombs[i].draw(ctx);
		bombs[i].life--;
	}
	var old_bombs = bombs;
	bombs = [];
	for (var i = 0; i < old_bombs.length; i++) {
		if (old_bombs[i].life > 0) {
			bombs.push(old_bombs[i]);
		}
	}
	
	
	
	ctx.restore();
}

function collision(obj1, obj2) {
	return ((Math.abs(obj1.px - obj2.px) < (obj1.radius + obj2.radius)) &&
		(Math.abs(obj1.py - obj2.py) < (obj1.radius + obj2.radius)));
}




function Laser(x, y, vx, vy, rocket) {	
	this.px = x;
	this.py = y;
	this.radius = 4;
	this.vx = vx;
	this.vy = vy;
	this.life = 20;
	this.rocket = rocket;
	
	this.draw = function(ctx) {
		ctx.save();
		ctx.translate(this.px, this.py);
		ctx.fillStyle = "red";
		drawCircle(ctx, 0, 0, this.radius);
		ctx.restore();
	}
	
	
	this.move = function(width, height) {	
		this.px = (this.px + this.vx + width) % width;
		this.py = (this.py + this.vy + height) % height;
	}
}

function Bomb(x, y) {
	this.px = x;
	this.py = y;
	this.radius = 15;
	this.vx = 1;
	this.vy = 1;
	this.life = 400;
	this.explode = 0;
	
	this.draw = function(ctx) {
		ctx.save();
		ctx.translate(this.px, this.py);
		ctx.fillStyle = "green";
		drawCircle(ctx, 0, 0, this.radius);
		ctx.fill();
		if (this.life <= 0) {
			this.vx = 0;
			this.vy = 0;
			if (this.explode > 3) {
				bombs.push(old_bombs[i]);
			}
			this.explode = this.explode + 1;
			this.radius = Math.ceil(this.radius * 1.5)
		}
		ctx.restore();
	}	
	
	this.move = function(width, height) {	
		this.px = (this.px + this.vx + width) % width;
		this.py = (this.py + this.vy + height) % height;
		this.vx = this.vx + Math.random()
		this.vy = this.vy + Math.random()
		this.vx = this.vx - Math.random()
		this.vy = this.vy - Math.random()
	}
}