"use strict";

// vertical beam wall from floor emitter
var GO_Beamwall = function(length, active){
	this.sprite = new Sprite(GO_Beamwall.spriteSheet,"BeamwallEmitter");
	this.sprite.setBoundingBox(0,0,15,3);
	this.sprite.collision_flags = 0;
	this.sprite.game_object = this; // no longer a generic GO
	this.sprite.frame = 0;

	this.wall = new Sprite(null,"Beamwall");
	this.wall.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
	if (active) {
		this.wall.collision_flags = this.wall.collision_flags | DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	}
	this.wall.setBoundingBox(0,-(length<<4)-1,15,length<<4);
	this.wall.game_object = this;

	this.active = !!active;

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

	this.frame = 0;
	this.length = length;
};
GO_Beamwall.spriteImg = SDS_Load_Image("art/sheets/sprite/beamwall.png");
GO_Beamwall.spriteSheet = new TileSheet(4, 2, 0, 3, GO_Beamwall.spriteImg);

GO_Beamwall.prototype = new GameObject(
	new Sprite(
		GO_Beamwall.spriteSheet,
		"Beamwall"
	),
	"Beamwall"
	);

	GO_Beamwall.prototype.interactions = 0;

	GO_Beamwall.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		
		mapRoom.addSprite(this.wall);
		this.wall.x = this.sprite.x;
		this.wall.y = this.sprite.y;

		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 - 4 - (this._heap.i<<2);
		}
	}

	GO_Beamwall.prototype.Update = function(){
		
		if (this.active){
			// if fully open, update beam
			if (this.sprite.frame==3){
				this.frame = (this.frame+1)&3;
				this._heap.frame = this.frame+4;

				for (this._heap.i=0; this._heap.i<this.seglength; ++this._heap.i){
					this.segments[this._heap.i].frame = this._heap.frame;
				}
			}
			// else, open, and if ready, turn on beam
			else{
				++this.frame;
				if (!(this.frame&1)) ++this.sprite.frame;

				if (this.sprite.frame==3){
					this._heap.frame = this.frame+4;
					for (this._heap.i=0; this._heap.i<this.seglength; ++this._heap.i){
						this.segments[this._heap.i].frame = this._heap.frame;
						this.segments[this._heap.i].visible = this.active;
					}
					this.wall.collision_flags = this.wall.collision_flags | DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
				}
			}
		}
		else{
			// if just disabled from open, be sure beam is off
			if (this.sprite.frame==3){
				for (this._heap.i=0; this._heap.i<this.seglength; ++this._heap.i){
					this.segments[this._heap.i].visible = this.active;
				}
				this.wall.collision_flags = this.wall.collision_flags & ~DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
			}
			// else, be sure we close fully
			if (this.sprite.frame>0){
				if (!(this.frame&3)) --this.sprite.frame;
				this.frame = (this.frame+1)&3;
			}
		}
		
	};

	GO_Beamwall.prototype.Activate = function(active){
		this.frame=0;
		this.active = active;
	};
