/*******************************************************************
*
* File    : JSFX_MouseAttack.js
*
* Created : 2003/05/07
*
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com
*
* Purpose : A usless mouse chaser script. Kamikaze space ships
*           dive bomb the mouse cursor.
*
* History
* Date         Version        Description
* 2003-05-07	1.0		Created the initial version
***********************************************************************/
/*
 * Class AttackShip extends Layer
 */
JSFX.AttackShip = function()
{
	window[ this.id=("ship" + (JSFX.layerNo++)) ] = this;
	this.imgId = this.id + "Img";

	//Call the super constructor
	this.superC = JSFX.Layer;
	this.superC('<img src="images/ship/0.gif" name="'+this.imgId+'" width="64" height="64">');

	this.show();
	this.setzIndex(100);
	this.reset();
	this.targetX = 100;
	this.targetY = 100;

	this.animate = this.animateTrack;
}
JSFX.AttackShip.prototype = new JSFX.Layer;

JSFX.AttackShip.prototype.animateTrack = function()
{
	var X = (this.targetX - this.x);
	var Y = (this.targetY - this.y);
	var len = Math.sqrt(X*X+Y*Y);
	var dx = 20 * (X/len);
	var dy = 20 * (Y/len);
	var ddx = (dx - this.dx)/10;
	var ddy = (dy - this.dy)/10;
	this.dx += ddx;
	this.dy += ddy;
	this.x += this.dx;
	this.y += this.dy;
	this.moveTo(this.x, this.y);

	this.targetX = JSFX.Browser.mouseX-32;
	this.targetY = JSFX.Browser.mouseY-32;

	var a=Math.atan2(this.targetX - this.x, this.targetY-this.y);
	a = 16 + Math.floor(16*a/Math.PI);

	this.images[this.imgId].src = "images/ship/"+a+".gif";

	if(Math.abs(this.targetX - this.x) < 32 )
		if(Math.abs(this.targetY - this.y) < 32)
		{
			this.frame = 0;
			this.animate = this.animateExplode;
		}

	setTimeout("window."+this.id+".animate()", 40);
}

JSFX.AttackShip.prototype.reset = function()
{
	var side = Math.floor(Math.random()*4);
	this.x = JSFX.Browser.getMaxX() * Math.random();
	this.y = -100 -100 * Math.random();

	this.dx=1;
	this.dy=1;

	if(side == 0)
	{
		this.x = -64;
		this.y = Math.random()*JSFX.Browser.getMaxY();
		this.dx=20;
	}
	else if(side==2)
	{
		this.x = Math.random()*JSFX.Browser.getMaxX();
		this.y = -64;
		this.dy= 20;
	}
	else if(side==3)
	{
		this.x = JSFX.Browser.getMaxX()-64;
		this.y = Math.random()*(JSFX.Browser.getMaxY()-64);
		this.dx=-20;
	}
	else
	{
		this.x = Math.random()*(JSFX.Browser.getMaxX()-64);
		this.y = JSFX.Browser.getMaxY()-64;
		this.dy= -20;
	}
	this.animate = this.animateTrack;
}
JSFX.AttackShip.prototype.animateExplode = function()
{
	if(this.frame == 13)
	{
		this.reset();
	}
	else
	{
		this.images[this.imgId].src="images/explode/"+this.frame+".gif";
		this.frame++;
		this.x += this.dx;
		this.y += this.dy;
		this.dx = this.dx *.9;
		this.dy = this.dy *.9;
		this.moveTo(this.x, this.y);
	}
	setTimeout("window."+this.id+".animate()", 40);
}

//JSFX.AttackShip.prototype.animate = JSFX.AttackShip.prototype.animateTrack;

JSFX.MakeAttackShips = function(n)
{
	//Create the attack ships
	for(i=0 ; i<n ; i++)
	{
		var ship = new JSFX.AttackShip();
		ship.animate();
	}
}
JSFX.MakeAttackShips.s1 = new Array();
JSFX.MakeAttackShips.s2 = new Array();
JSFX.MakeAttackShips.preLoad = function()
{
	var i;

	//Pre load the explode images
	for(i=0 ; i< 13 ; i++)
	{
		JSFX.MakeAttackShips.s1[i] = new Image();
		JSFX.MakeAttackShips.s1[i].src="images/explode/"+i+".gif";
	}

	//Pre load the ship images
	for(i=0 ; i< 32 ; i++)
	{
		JSFX.MakeAttackShips.s2[i] = new Image();
		JSFX.MakeAttackShips.s2[i].src="images/ship/"+i+".gif";
	}
}
JSFX.MakeAttackShips.preLoad();



/*** If no other script has added it yet, add the ns resize fix ***/
if(navigator.appName.indexOf("Netscape") != -1 && !document.getElementById)
{
	if(!JSFX.ns_resize)
	{
		JSFX.ow = outerWidth;
		JSFX.oh = outerHeight;
		JSFX.ns_resize = function()
		{
			if(outerWidth != JSFX.ow || outerHeight != JSFX.oh )
				location.reload();
		}
	}
	window.onresize=JSFX.ns_resize;
}

