"use strict";

// vertical beam trigger
var GO_BeamswitchV = function(targetObj,length){
	this.sprite = new Sprite(null,"BeamswitchV");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(1,0,6,length<<4);
	this.sprite.game_object = this; // no longer a generic GO

	this.target_objs_name = targetObj ? targetObj : null; // name of GameObject with interaction SIGNALABLE
	this.target_objs = [];
	this.sticky = false; // will stay pressed once triggered
	this.pressed = false;
	this.pressed_pending = false;
	// if pressed by trigger (i.e. Meli touch), disable DAMAGE interaction
	// so kickwaves and thrown projectiles don't bounce
	this.pressed_by_trigger = false; 

	this.triggered_height = 0;

	this.emitter1 = new Sprite(GO_BeamswitchV.spriteSheet,"BeamswitchVEmitterBottom");
	this.emitter1.setBoundingBox(0,0,7,15);
	this.emitter1.collision_flags = 0;
	this.emitter1.game_object = this;
	this.emitter1.frame = 0;

	this.emitter2 = new Sprite(GO_BeamswitchV.spriteSheet,"BeamswitchVEmitterTop");
	this.emitter2.setBoundingBox(0,0,7,15);
	this.emitter2.collision_flags = 0;
	this.emitter2.game_object = this;
	this.emitter2.frame = 1;

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

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

GO_BeamswitchV.prototype = new GameObject(
	new Sprite(
		GO_BeamswitchV.spriteSheet,
		"BeamswitchV"
	),
	"BeamswitchV"
	);

	GO_BeamswitchV.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.TRIGGER | 
		DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE;

	GO_BeamswitchV.prototype.triggerEvent = function(sourceGO){
		this.pressed_pending=true;
		this.pressed_by_trigger=true;

		this.triggered_height = sourceGO.sprite.y + sourceGO.sprite.top_left.y;
		return null;
	};
	GO_BeamswitchV.prototype.damage = function(sourceGO){
		this.pressed_pending=true;

		this.triggered_height = sourceGO.sprite.y + sourceGO.sprite.top_left.y;

		return true; // always "die" for purposes of not destroying blocks
	};

	GO_BeamswitchV.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		
		mapRoom.addSprite(this.emitter1);
		this.emitter1.x = this.sprite.x;
		this.emitter1.y = this.sprite.y + (this.length<<4) - 3;

		mapRoom.addSprite(this.emitter2);
		this.emitter2.x = this.sprite.x;
		this.emitter2.y = this.sprite.y - 13;

		for (this._heap.i=0; this._heap.i<this.length; ++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);
		}
	}

	GO_BeamswitchV.prototype.Update = function(){
		this._heap.wasPressed = false;
		this._heap.wasReleased = false;
		
		this._heap.newpressed = (this.pressed && this.sticky) || this.pressed_pending;
		this._heap.wasPressed = this._heap.newpressed && !this.pressed;
		this._heap.wasReleased = this.pressed && !this._heap.newpressed;
		this.pressed = this._heap.newpressed;
		this.pressed_pending=false;

		if (this.pressed_by_trigger){
			this.interactions &= ~DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE;
		}
		else{
			this.interactions |= DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE;
		}
		this.pressed_by_trigger=false;

		if ((this._heap.wasPressed || this._heap.wasReleased) && this.target_objs_name && !this.target_objs.length){
			//this.target_objs = this.manager.getNamedObject(this.target_objs_name);
			this.manager.getAllNamedObjects(this.target_objs_name,this.target_objs);
			this.target_objs_name = null; // prevent re-search even if no targets found
		}
		if ((this._heap.wasPressed || this._heap.wasReleased) && this.target_objs.length){
			//if (this._heap.wasPressed) this.target_objs.signal(this,1);
			//if (this._heap.wasReleased) this.target_objs.signal(this,-1);
			if (this._heap.wasPressed) {
				for (this._heap.i=0; this._heap.i<this.target_objs.length; ++this._heap.i){
					this.target_objs[this._heap.i].signal(this,1);
				}
			}
			if (this._heap.wasReleased){
				for (this._heap.i=0; this._heap.i<this.target_objs.length; ++this._heap.i){
					this.target_objs[this._heap.i].signal(this,-1);
				}
			}
		}

		this.frame = (this.frame+1)&1;

		for (this._heap.i=0; this._heap.i<this.length; ++this._heap.i){
			this.segments[this._heap.i].frame = this.frame ? 3 : 2;
			this.segments[this._heap.i].visible = !(this.pressed && this.segments[this._heap.i].y<this.triggered_height);
		}
	};
