// args[0] : 画像 "xxx.png"
// args[1] : コールバック
// args[2] : これをtrueにすると接触状態で決定キーまたは下キーを押したときのみ反応(false, 省略だと接触した瞬間反応)

var FreeItemClass = function(stage, args){

	FreeItemClass.base.apply(this, arguments);
	if(args[0]){
		var img = document.createElement('img');
		img.src = "images/" + args[0];	//画像は仮
		this.image = img;
	} else{
		this.image = null;
	}
	
	//アイテムを取得できる回数。-1で無限
	this.times = -1;
	
	this.size = new Point2D(32, 32);
	
	//アイテムを取得したときに呼ばれるコールバック。引数にアタックしてきたオブジェクトが指定される
	//nullで何もしないアイテムになる
	this.callback = args[1];
	
	//最後にキャラに乗られている状態でなくなってから何tickで再び取得できるか。
	this.touchInterval = 0;
	this.lastTouchedTickCount = -9999;
	
	//選択キー押下時のみコールバックを発行
	this.mustSelectKey = args[2] != undefined ? args[2] : false;
	
	this.isPhantom = true;
	this.debugMode = false;
	this.size.x = 32;
	this.size.y = 32;
	
	this.isDisabled = false;
	this.isTouching = false;
	
	this.selectKeyPushed = true;

}.extend(ItemClass, {
	draw: function(x, y){
		if(this.image){
			this.ownerStage.mainContext.globalAlpha = this.opacity;
			this.ownerStage.mainContext.drawImage(this.image, x, y, this.size.x, this.size.y);
			this.ownerStage.mainContext.globalAlpha = 1.0;
		}
		/*
		if(this.debugMode && this.times > 0){
			//残り取得回数を表示
			this.ownerStage.mainContext.save();
			this.ownerStage.mainContext.fillStyle = "rgba(255,255,255,0.5)";
			this.ownerStage.mainContext.strokeStyle = "rgba(0, 0, 0, 1)";
			this.ownerStage.mainContext.font = "normal 12px sans-serif";
			drawText(this.ownerStage.mainContext, this.times, x, y - 20);
			this.ownerStage.mainContext.restore();
		}
		*/
		//var tickCount = this.ownerStage.manager.tickCount;
		/*
		if(this.debugMode && ((tickCount - this.lastTouchedTickCount)< this.touchInterval)){
			//取得できるようになるまでの残り時間を表示
			this.ownerStage.mainContext.save();
			this.ownerStage.mainContext.fillStyle = "rgba(255,255,255,0.5)";
			this.ownerStage.mainContext.strokeStyle = "rgba(0, 0, 0, 1)";
			this.ownerStage.mainContext.font = "normal 12px sans-serif";
			drawText(this.ownerStage.mainContext, this.touchInterval - (tickCount - this.lastTouchedTickCount), x, y - 32);
			this.ownerStage.mainContext.restore();
		}
		*/
	},
	debugDraw: function(x, y){
		//衝突マップに描画
		this.ownerStage.collisionMapContext.save();
		this.ownerStage.collisionMapContext.strokeStyle='rgba(255, 255, 0, 0.5)';
		this.ownerStage.collisionMapContext.strokeRect(x, y, this.size.x, this.size.y);
		this.ownerStage.collisionMapContext.restore();
	},
	tick: function(){
		var retv = false;
		if(this.ownerStage.userControlledCharacter)
		{
			var main = this.ownerStage.userControlledCharacter;
			if(this.isOverlappedWithLocatedObject(main, main.origin.x, main.origin.y))
			{
				var tickCount = this.ownerStage.manager.tickCount;
				if(!this.isDisabled && tickCount - this.lastTouchedTickCount >= this.touchInterval)
				{
					if((!this.mustSelectKey && !this.isTouching) || (this.mustSelectKey && !this.selectKeyPushed && (this.ownerStage.manager.UIManager.keyState.select || this.ownerStage.manager.UIManager.keyState.cursorDown)))
					{
						if(this.callback)
						{
							retv = this.callback(main,0,0);
						}
						
						if(this.times > 0)
						{
							this.times--;
							if(this.times == 0)
							{
								this.ownerStage.removeStageObject(this);
								this.isDisabled = true;
							}
						}
						//最後に触られた時間を記録
						this.lastTouchedTickCount = tickCount;
					}
				}
				if(this.ownerStage.manager){
					this.selectKeyPushed = this.ownerStage.manager.UIManager.keyState.select || this.ownerStage.manager.UIManager.keyState.cursorDown;
				}
				this.isTouching = true;
			} else{
				this.isTouching = false;
			}
		}
		return retv;
	},

});
FreeItemClass.tick = function(){return false;};
