// Sprite オブジェクト

function Sprite(x,y,width,height){
	this.x=x;	// スプライト左上のX座標
	this.y=y;	// スプライト左上のY座標
	this.width=width;	// スプライトの幅
	this.height=height;	// スプライトの高さ
	this.cired=0;	// スプライトの赤成分
	this.cigreen=0;	// スプライトの緑成分
	this.ciblue=0;	// スプライトの青成分
	this.image=null;	// スプライトの画像（オブジェクト）
	this.alpha=1.0;	// スプライトの半透明率（透明：0.0-1.0：不透明）
	this.roll=0.0;	// スプライトの回転率（反時計回り：0.0-2PI）
	this.zindex=0;	// スプライトの重なり（手前：0-INF：奥）
	
	// メソッド
	this.moveX=function(num){
		this.x+=num;
		return this.x;
	}
	this.moveY=function(num){
		this.y+=num;
		return this.y;
	}
	
	this.centerX=function(num){
		return this.x+(this.width/2);
	}
	this.centerY=function(num){
		return this.y+(this.height/2);
	}
	
	this.resizeWidth=function(num){
		this.x+=(this.width-num)/2;
		this.width=num;
		return this.x;
	}
	this.resizeHeight=function(num){
		this.y+=(this.height-num)/2;
		this.height=num;
		return this.y;
	}
	
	this.setColor=function(cistr){
		cinum=new Array(3);
		cstr="";
		
		if(cistr.match(/^#/)){
			// #xxxxxx
			cstr=cistr.match(/^#([0-9a-zA-Z]{2})([0-9a-zA-Z]{2})([0-9a-zA-Z]{2})/g);
			if(cstr==null){
				// #xxx
				cstr=cistr.match(/^#([0-9a-zA-Z])([0-9a-zA-Z])([0-9a-zA-Z])/g);
				if(cstr==null){
					cinum[0]=0;
					cinum[1]=0;
					cinum[2]=0;
				}else{
					cinum[0]="0x"+RegExp.$1+RegExp.$1;
					cinum[1]="0x"+RegExp.$2+RegExp.$2;
					cinum[2]="0x"+RegExp.$3+RegExp.$3;
				}
			}else{
				cinum[0]="0x"+RegExp.$1;
				cinum[1]="0x"+RegExp.$2;
				cinum[2]="0x"+RegExp.$3;
			}
		}else{
			if(cistr.match(/^rgb/)){
				// rgb(r, g, b)
				cstr=cistr.match(/^rgb\((.*)\)/);
				cstr=RegExp.$1.replace(/\ */g,"");
				cinum=cstr.split("\,");
			}else{
				//cname=new Colorname();
				
				cinum[0]=0;
				cinum[1]=0;
				cinum[2]=0;
			}
		}
		
		this.cired=parseInt(cinum[0]);
		this.cigreen=parseInt(cinum[1]);
		this.ciblue=parseInt(cinum[2]);
		if(this.cired<0){
			this.cired=0;
		}else if(this.cired>255){
			this.cired=255;
		}
		if(this.cigreen<0){
			this.cigreen=0;
		}else if(this.cigreen>255){
			this.cigreen=255;
		}
		if(this.ciblue<0){
			this.ciblue=0;
		}else if(this.ciblue>255){
			this.ciblue=255;
		}
		return "rgb("+this.cired+","+this.cigreen+","+this.ciblue+")";
		//return""+cinum[0]+"/"+cinum[1]+"/"+cinum[2];
	}
	this.getColor=function(){
		return "rgb("+this.cired+","+this.cigreen+","+this.ciblue+")";
	}
	
	this.shiftAlpha=function(num){
		this.alpha+=num;
		if(this.alpha<0){
			this.alpha=0;
		}else if(this.alpha>100){
			this.alpha=100;
		}
		return this.alpha;
	}
	this.rollFill=function(num){
		this.roll+=num;
		if(this.roll<0){
			this.roll+=Math.PI;
		}else if(this.roll>Math.PI){
			this.roll-=Math.PI;
		}
		return this.roll;
	}
	
	this.drawFill=function(cvctx){
		if(!cvctx){
			return false;
		}
		if(this.image){
			this.image.onload=function(){
				cvctx.drawImage(this.image,this.x,this.y,this.width,this.height);
			}
			cvctx.drawImage(this.image,this.x,this.y,this.width,this.height);
		}else{
			fscache=cvctx.fillStyle;
			sscache=cvctx.strokeStyle;
			cvctx.fillStyle=this.getColor();
			cvctx.strokeStyle="#000000";
			cvctx.fillRect(this.x,this.y,this.width,this.height);
			cvctx.fillStyle=fscache;
			cvctx.strokeStyle=sscache;
		}
		return true;
	}
}

