"use strict";
/* an overlay to obscure the screen, or a rectangular region thereof.
Slower than Flash, and bidirectional.
Triggered by setting "fade" on the scratchpad */
var Deco_Fade = function(tilesw,tilesh){
	this.layer = 3; // in front of all
	this.tilesw = tilesw ? tilesw : 4;
	this.tilesh = tilesh ? tilesh : 4;
	// usage 2: if fade is set upon deco load, stage a fade-in
	// (and consume the flag)
	this.direction = true;
	this.frame = GLOBAL_SCRATCHPAD.check("fade") ? 0 : 7;
	GLOBAL_SCRATCHPAD.unpersist("fade");
	GLOBAL_SCRATCHPAD.note("fade",false);

	this.timer = 0;
}

Deco_Fade.prototype = new Decoration(
	new TileSheet(
		3, 3, 3, 0, 
		SDS_Load_Image("art/sheets/decoration/fade.png")
	),
	"Fade");

Deco_Fade.prototype.animate = function(){
		// usage 1: set fade at runtime to stage a fade-out
		if (GLOBAL_SCRATCHPAD.check("fade") && this.direction){
			this.direction = false;
			this.timer=0;
		}
		// usage 3: set unfade at runtime to fade back in same room
		if (GLOBAL_SCRATCHPAD.check("unfade") && !this.direction){
			this.direction = true;
			this.timer=0;
		}
		++this.timer;
		if (!(this.timer & 1)){
			if (this.direction && this.frame<7){
				++this.frame;
			}
			else if (!this.direction && this.frame>0){
				--this.frame;
			}
		}
	}

Deco_Fade.prototype.drawIntoCanvas = function(canvas){
		if (this.tile_sheet){
			for (this._heap.j=0; this.frame!=7 && this._heap.j<this.tilesh; this._heap.j++){
				for (this._heap.i=0; this._heap.i<this.tilesw; this._heap.i++){
					this.tile_sheet.drawIntoCanvas(
						canvas,
						this.frame,
						this.x + this._heap.i*this.tile_sheet.tile_pixel_width,
						this.y + this._heap.j*this.tile_sheet.tile_pixel_height);
				}
			}
		}
	};
