/* This notice must be untouched at all times.

sprite3d.js    v.1.0

The latest version is available at
http://www.molooga.de

Created 08.06.2006 by H.Matthias (Web: http://www.molooga.de )
Last modified: 08.06.2006

JavaScript cross browser (pseudo)3D sprite library.
needs	sprite.js by H.Matthias (Web: http://www.molooga.de)
and		 tools.js by H.Matthias (Web: http://www.molooga.de)

This program is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License
at http://www.gnu.org/copyleft/gpl.html for more details.
*/

if(!sprite) document.writeLn('<script language="JavaScript" src="sprite.js"></script>');

function resize(){
	xoffset=Math.floor(0.5*getWindowWidth());
	yoffset=Math.floor(0.5*getWindowHeight());
}

window.onresize=resize;
resize();
	

function sprite3d(name,img,href,shadow){
	var t;
	
	
	this.sprite=new sprite(name,img,href,shadow);
	this.size=1;
	this.posX=0;
	this.posY=0;
	this.posZ=0;
	
	this.show = function(){
		this.sprite.show();
	};
	
	this.hide = function(){
		this.sprite.hide();
	};
	
	this.setImage = function(img, shadow){
		this.sprite.setImage(img);
	};
	
	this.getImage = function(){
		return this.sprite.getImage();
	};
	
	this.getX = function(){
		return this.posX;
	};	
	
	this.getY = function(){
		return this.posY;	
	};
	
	this.getZ = function(){
		return this.posZ;	
	};
	
	this.setX = function(xpos){
		this.moveTo(xpos,this.posY,this.posZ);
	};
	
	this.setY = function(ypos){
		this.moveTo(this.posX,ypos,this.posZ);
	};	
	
	this.setZ = function(zpos){
		this.moveTo(this.posX,this.posY,zpos);
	};
	
	this.setSize = function(s){
		this.size=s;
	}	
	
	this.getSize = function(){
		return this.size;
	}
	this.getWidth = function(){
		return this.sprite.getWidth();
	}
	this.getHeight = function(){
		return this.sprite.getHeight();
	}
	
	this.moveTo = function(x,y,z){
		
		//if(z==0) z=1/Number.MAX_VALUE; // a little cheating
		if(z==0) z=1/1000000; // a little cheating
		var x2=xoffset+x*this.size*this.size/z;
		var y2=yoffset-y*this.size*this.size/z;
		this.sprite.zoom(this.size/z);
		this.sprite.setZIndex(Math.floor((10-z)*10)+10);
		this.sprite.moveTo(x2,y2);
		
		this.posX=x;
		this.posY=y;
		this.posZ=z;
	}
	
	this.moveBy =function(x,y,z){
		this.moveTo(this.posX+x,this.posY+y,this.posZ+z);
	}
		
	this.collision = function(sprite){
		var x1=this.getX();
		var y1=this.getY();
		var z1=this.getZ();
		var w1=this.getWidth();
		var h1=this.getHeight();
		var x2=sprite.getX();
		var y2=sprite.getY();
		var z2=sprite.getZ();
		var w2=sprite.getWidth();
		var h2=sprite.getHeight();
		
		if((x1>=x2)&&(x1<(x2+w2))&&(y1>=y2)&&(y1<(y2+h2))&&(z1==z2)) return true;
		if((x2>=x1)&&(x2<(x1+w1))&&(y2>=y1)&&(y2<(y1+h1))&&(z1==z2)) return true;
		if((x2>=x1)&&(x2<(x1+w1))&&((y2+h2)>y1)&&(y2<=y1)&&(z1==z2)) return true;
		if((x1>=x2)&&(x1<(x2+w2))&&((y1+h1)>y2)&&(y1<=y2)&&(z1==z2)) return true;
		
		return false;
	};	
	
}	


