"use strict";

// vertical beam wall from floor emitter
var GO_Sealwall = function(length, key){
	this.sprite = new Sprite(null,"SealwallSeal");
	this.sprite.setBoundingBox(0,0,15,(length<<4)-1);
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID | DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.game_object = this; // no longer a generic GO

	// separate display beam from collision box
	this.seglength = length;
	this.segments = [];
	for (this._heap.i=0; this._heap.i<this.seglength; ++this._heap.i){
		this.segments.push(new Sprite(GO_Sealwall.spriteSheet,"SealwallSegment"));
		this.segments[this._heap.i].setBoundingBox(0,0,15,15);
		this.segments[this._heap.i].collision_flags = 0;
		this.segments[this._heap.i].game_object = this;
		this.segments[this._heap.i].frame = 0;
	}

	this.ends = [];
	this.ends.push(new Sprite(GO_Sealwall.spriteSheet2,"SealwallTop"));
	this.ends[0].setBoundingBox(0,0,15,7);
	this.ends[0].collision_flags = 0;
	this.ends[0].game_object = this;
	this.ends[0].frame = 12;
	this.ends.push(new Sprite(GO_Sealwall.spriteSheet2,"SealwallBottom"));
	this.ends[1].setBoundingBox(0,0,15,7);
	this.ends[1].collision_flags = 0;
	this.ends[1].game_object = this;
	this.ends[1].frame = 8;

	this.lock = new Sprite(GO_Sealwall.spriteSheet,"Sealwall");
	this.lock.collision_flags = 0;
	this.lock.setBoundingBox(0,0,15,15);
	this.lock.game_object = this;
	this.lock.frame = 3;

	this.seal = new Sprite(GO_Sealwall.spriteSheet,"Sealwall");
	this.seal.collision_flags = 0;
	this.seal.setBoundingBox(0,0,15,15);
	this.seal.game_object = this;
	this.seal.frame = 7;
	

	this.frame = 0;
	this.length = length;
	this.key = key;
	if (this.key) this.name += "_"+this.key;

	this.destructing = false;
};
GO_Sealwall.spriteImg = SDS_Load_Image("art/sheets/sprite/sealwall.png");
GO_Sealwall.spriteSheet = new TileSheet(4, 4, 2, 1, GO_Sealwall.spriteImg);
GO_Sealwall.spriteSheet2 = new TileSheet(4, 3, 2, 2, GO_Sealwall.spriteImg);

GO_Sealwall.prototype = new GameObject(
	new Sprite(
		GO_Sealwall.spriteSheet,
		"Sealwall"
	),
	"Sealwall"
	);

	GO_Sealwall.prototype.interactions = 0;

	GO_Sealwall.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);

		for (this._heap.i=0; this._heap.i<this.seglength; ++this._heap.i){
			mapRoom.addSprite(this.segments[this._heap.i]);
			this.segments[this._heap.i].x = this.sprite.x;
			this.segments[this._heap.i].y = this.sprite.y + (this._heap.i<<4);
		}
		mapRoom.addSprite(this.ends[0]);
		this.ends[0].x = this.sprite.x;
		this.ends[0].y = this.sprite.y - 8;
		mapRoom.addSprite(this.ends[1]);
		this.ends[1].x = this.sprite.x;
		this.ends[1].y = this.sprite.y + (this.length<<4);

		mapRoom.addSprite(this.lock);
		this.lock.x = this.sprite.x;
		this.lock.y = this.sprite.y + ((this.length-1)<<3);

		mapRoom.addSprite(this.seal);
		this.seal.x = this.lock.x;
		this.seal.y = this.lock.y;
	}
	GO_Sealwall.prototype.clearFromRoom = function(mapRoom){
		mapRoom.removeSprite(this.sprite);
		
		mapRoom.removeSprite(this.lock);
		mapRoom.removeSprite(this.seal);

		for (this._heap.i=0; this._heap.i<this.seglength; ++this._heap.i){
			mapRoom.removeSprite(this.segments[this._heap.i]);
		}
		mapRoom.removeSprite(this.ends[0]);
		mapRoom.removeSprite(this.ends[1]);
	}

	GO_Sealwall.prototype.Destruct = function(){
		this._heap.poof = new GO_Poof("FX_splat");
		this._heap.poof.spawnOnSprite(this.lock,this.manager);
		this._heap.poof = null;
		this._heap.sparkle = new GO_Sparkle(10,-5,-5);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);
		this._heap.sparkle = new GO_Sparkle(10,5,-5);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);
		this._heap.sparkle = new GO_Sparkle(10,5,5);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);
		this._heap.sparkle = new GO_Sparkle(10,-5,5);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);

		this._heap.sparkle = new GO_Sparkle(10,-7,0);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);
		this._heap.sparkle = new GO_Sparkle(10,0,-7);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);
		this._heap.sparkle = new GO_Sparkle(10,7,0);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);
		this._heap.sparkle = new GO_Sparkle(10,0,7);
		this._heap.sparkle.spawnOnSprite(this.lock,this.manager);

		this._heap.sparkle = null;
		this.lock.visible = false;
		this.seal.visible = false;

		this.destructing = true;
	}

	GO_Sealwall.prototype.Update = function(){
		if (!this.destructing){
			++this.frame;
			this.frame = this.frame%6;
			
			for (this._heap.i=0; this._heap.i<this.seglength; ++this._heap.i){
				this.segments[this._heap.i].frame = (this.frame>>1);
			}
			this.ends[0].frame = this.segments[0].frame + 12;
			this.ends[1].frame = this.segments[0].frame + 8;

			this.seal.visible = !(this.frame&1);
		}
		else{
			++this.frame;
			if (!(this.frame&3) && this.frame>=8){
				this._heap.i = (this.frame>>2)-2;
				if (this._heap.i == (this.seglength+1)>>1){
					// endcaps & cleanup
					this._heap.poof = new GO_Poof("FX_shatter");
					this._heap.poof.spawnOnSprite(this.ends[0],this.manager);
					this._heap.poof = null;
					this._heap.sparkle = new GO_Sparkle(6,-8,-4);
					this._heap.sparkle.spawnOnSprite(this.ends[0],this.manager,-4,0);
					this._heap.sparkle = new GO_Sparkle(6,8,-4);
					this._heap.sparkle.spawnOnSprite(this.ends[0],this.manager,4,0);
					this.ends[0].visible = false;

					this._heap.poof = new GO_Poof();
					this._heap.poof.spawnOnSprite(this.ends[1],this.manager);
					this._heap.sparkle = new GO_Sparkle(6,8,4);
					this._heap.sparkle.spawnOnSprite(this.ends[1],this.manager,4,0);
					this._heap.sparkle = new GO_Sparkle(6,-8,4);
					this._heap.sparkle.spawnOnSprite(this.ends[1],this.manager,-4,0);
					this.ends[1].visible = false;

					this._heap.poof = null;
					this._heap.sparkle = null;
					
					this.clearFromRoom(this.sprite.map_room);
					this.manager.remove(this);
				}
				else{
					// symmetrical about middle

					// 1: 0/0
					// 2: 0/1
					// 3: 1/1, 0/2
					// 4: 1/2, 0/3
					// 5: 2/2, 1/3, 0/4
					// 6: 2/3, 1/4, 0/5
					this._heap.i = ((this.seglength-1)>>1) - this._heap.i;
					this._heap.poof = new GO_Poof();
					this._heap.poof.spawnOnSprite(this.segments[this._heap.i],this.manager);
					this._heap.poof = null;
					this._heap.sparkle = new GO_Sparkle(6,-8,-4);
					this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,-4,-4);
					this._heap.sparkle = new GO_Sparkle(6,8,-4);
					this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,4,-4);
					this._heap.sparkle = new GO_Sparkle(6,8,4);
					this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,4,4);
					this._heap.sparkle = new GO_Sparkle(6,-8,4);
					this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,-4,4);
					this._heap.sparkle = null;
					this.segments[this._heap.i].visible = false;

					this._heap.i = this.seglength - 1 - this._heap.i;
					if (this.segments[this._heap.i].visible){
						this._heap.poof = new GO_Poof("FX_shatter");
						this._heap.poof.spawnOnSprite(this.segments[this._heap.i],this.manager);
						this._heap.poof = null;
						this._heap.sparkle = new GO_Sparkle(6,-8,-4);
						this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,-4,-4);
						this._heap.sparkle = new GO_Sparkle(6,8,-4);
						this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,4,-4);
						this._heap.sparkle = new GO_Sparkle(6,8,4);
						this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,4,4);
						this._heap.sparkle = new GO_Sparkle(6,-8,4);
						this._heap.sparkle.spawnOnSprite(this.segments[this._heap.i],this.manager,-4,4);
						this._heap.sparkle = null;
						this.segments[this._heap.i].visible = false;
					}
				}
			}
		}
	};
