"use strict";

// vertical beam trigger
var GO_BeamswitchH = function(targetObj,length){
	this.sprite = new Sprite(null,"BeamswitchH");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(0,1,length<<4, 6);
	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_left = 0;

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

	this.emitter2 = new Sprite(GO_BeamswitchH.spriteSheet,"BeamswitchEmitterLeft");
	this.emitter2.setBoundingBox(0,0,15,7);
	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_BeamswitchH.spriteSheet,"BeamswitchHSegment"));
		this.segments[this._heap.i].setBoundingBox(0,1,15,6);
		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_BeamswitchH.spriteImg = SDS_Load_Image("art/sheets/sprite/beamnode_h.png");
GO_BeamswitchH.spriteSheet = new TileSheet(4, 3, 0, 2, GO_BeamswitchH.spriteImg);

GO_BeamswitchH.prototype = new GameObject(
	new Sprite(
		GO_BeamswitchH.spriteSheet,
		"BeamswitchH"
	),
	"BeamswitchH"
	);

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

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

		this.triggered_left = sourceGO.sprite.x + sourceGO.sprite.top_left.x;

		return null;
	};
	GO_BeamswitchH.prototype.damage = function(sourceGO){
		this.pressed_pending=true;

		this.triggered_left = sourceGO.sprite.x + sourceGO.sprite.top_left.x;
		
		return true; // always "die" for purposes of not destroying blocks
	};

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

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

		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._heap.i<<4);
			this.segments[this._heap.i].y = this.sprite.y;
		}
	}

	GO_BeamswitchH.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].x<this.triggered_left);
		}
	};
