function Rocket(width, height, color, engineColor) {
	this.right = false;
	this.left = false;
	this.up = false;
	this.px = Math.floor(width/2);
	this.py = Math.floor(height/2);
	this.vx = 0;
	this.vy = 0;
	this.direction = 0;
	this.maxSpeed = 4;
	this.radius = 50;
	this.fireball_radius = 5;
	this.color = color;
	this.engineColor = engineColor;
	this.lastShot = Date.now();
	
	
	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);
		if (this.canShoot()) {
			ctx.fillStyle = "black";
			drawCircle(ctx, 0, 0, 2);
		}
		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 = "orange";
		if (this.up) {
			drawCircle(ctx, -(this.radius + this.fireball_radius + 15), 0, this.fireball_radius);
		}
		ctx.restore();
	}
	
	this.shoot = function() {
		if (this.canShoot()) {
			var laser = new Laser(this.px + (this.radius * Math.cos(this.direction)),
				this.py + (this.radius * Math.sin(this.direction)),
				8 * Math.cos(this.direction),
				8 * Math.sin(this.direction),
				this);
			lasers.push(laser);
			this.lastShot = Date.now();
		}
	}
	
	this.canShoot = function() {
		return (Date.now() - this.lastShot) > 1000
	}
}

var lasers = [];
var rocket1 = null;
var rocket2 = null;

function draw(ctx, width, height) {
	ctx.save();
	ctx.fillStyle = "black";
	ctx.fillRect(0, 0, width, height);
	if (!rocket1 && !rocket2) {
		rocket1 = new Rocket(width + 600, height, "blue", "silver");
		rocket2 = new Rocket(width - 600, height, "red", "silver");
	}
	rocket1.move(width, height);
	rocket2.move(width, height);
	if (collision(rocket1, rocket2)) {
		var tempvx = rocket1.vx;
		var tempvy = rocket1.vy;
		rocket1.vx = rocket2.vx;
		rocket1.vy = rocket2.vy;
		rocket2.vx = tempvx;
		rocket2.vy = tempvy;
	}

	rocket1.draw(ctx);
	rocket2.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 != rocket2 && collision(lasers[i], rocket2)) {
				rocket2.radius = rocket2.radius - 10;
				lasers[i].life = 0
		}
		if (lasers[i].rocket != rocket1 && collision(lasers[i], rocket1)) {
				rocket1.radius = rocket1.radius - 10;
				lasers[i].life = 0
			}
			if (rocket1.radius <= 0 || rocket2.radius <= 0) {
				if (rocket1.radius <= 0) {
					alert("The " + rocket1.color + " ship is dead");
					redScore.value = parseInt(redScore.value) + 1;
				} else if (rocket2.radius <= 0) {
					alert("The " + rocket2.color + " ship is dead");
					blueScore.value = parseInt(blueScore.value) + 1;
				}
				// start over
				stop();
				lasers = [];
				rocket1 = null;
				rocket2 = null;
				animate();
			}

	}
	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 = 200;
	this.rocket = rocket;
	
	this.draw = function(ctx) {
		ctx.save();
		ctx.translate(this.px, this.py);
		ctx.fillStyle = rocket.color;
		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;
	}
}

