"use strict";

// block-dropping chutes for the Boss Room.  Do not obstruct!
var GO_BossRoomChute = function(walltype){
	walltype = walltype ? 1 : 0;
	this.sprite = new Sprite(GO_BossRoomChute.spriteSheet,"BossRoomChute");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(0,0,15,15);
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	this.sprite.frame = walltype<<2;

	this.walltype = walltype;
	this.tampered = false;
	this.jammed = false;

	this.state = GO_BossRoomChute.prototype.ANIM_STATES.idle;
	this.state_timer = 0;

};
GO_BossRoomChute.spriteImg = SDS_Load_Image("art/sheets/sprite/bossroom.png");
GO_BossRoomChute.spriteSheet = new TileSheet(4, 4, 2, 2, GO_BossRoomChute.spriteImg);

GO_BossRoomChute.prototype = new GameObject(
	new Sprite(
		GO_BossRoomChute.spriteSheet,
		"BossRoomChute"
	),
	"BossRoomChute"
	);

	GO_BossRoomChute.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.CHECK |
		DEFINE_GAMEOBJECT_INTERACTIONS.SIGNALABLE;

	GO_BossRoomChute.prototype.ANIM_STATES = {
		"idle" : 0,
		"open" : 1,
		"idleopen" : 2,
		"dump" : 3,
		"idleclose" : 4,
		"close" : 5,
		"jammed" : 6, // pseudostate with exceptional frame
		"tampered" : 7, // pseudostate with exceptional case animation
	};

	GO_BossRoomChute.prototype.anim_frames = [
		[0,0,0,0], //"idle" : 
		[0,1,2,3], //"open" : 
		[8,8,8,8], //"idleopen" : 
		[8,9,10,11], //"dump" : 
		[8,8,8,8], //"idleclose" : 
		[3,2,1,0], //"close" :
		[12], //"jammed" :
		[14], //"tampered" :
	];

	GO_BossRoomChute.prototype.signal = function(sourceGO){
		if (this.state===GO_BossRoomChute.prototype.ANIM_STATES.idle && !this.jammed){
			this.state = GO_BossRoomChute.prototype.ANIM_STATES.open;
			this.state_timer = 0;
		}
	};

	GO_BossRoomChute.prototype.checkEvent = function(sourceGO){
		if (!this.tampered){
			this.tampered = true;
			this.sfx = new Chiptune("FX_switchon",SDS.sfxchip);
			this.sfx.play(false);

			//this._heap.poof = new GO_Poof();
			//this._heap.poof.spawnOnSprite(this.sprite,this.manager);
			//this._heap.poof = null;

			this.interactions &= ~DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;
			//return "GD_BossRoomChute_Sensor";

			SDS.dlgMgr.open("BossRoomChute_Sensor",function(index,retval){
				if (retval!=null){
					
				}
			},true);
			return "";
		}
		return null;
	};

	GO_BossRoomChute.prototype.Update = function(){
		++this.state_timer;
		this.state_timer = this.state_timer&7;
		if (this.state!==GO_BossRoomChute.prototype.ANIM_STATES.idle && !this.jammed){
			if (!this.state_timer){
				if (this.state===GO_BossRoomChute.prototype.ANIM_STATES.dump){

					this._heap.collisions = this._heap.collisions || [];
					this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
					this.sprite.getCollisions(this._heap.collisions);
					this.sprite.collision_flags &= ~DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
					for(this._heap.i=0; this._heap.i<this._heap.collisions.length; ++this._heap.i){
						if(this._heap.collisions[this._heap.i].collision_flags & DEFINE_SPRITE_COLLISION_FLAGS.SOLID){
							this.jammed = true;

							if (!(this._heap.collisions[this._heap.i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.GRAB) || 
								 ((this._heap.collisions[this._heap.i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.GRAB) && !this._heap.collisions[this._heap.i].game_object.held)){
								this._heap.poof = new GO_Poof();
								this._heap.poof.spawnOnSprite(this.sprite,this.manager);
								this._heap.poof = null;

								//this._heap.collisions[this._heap.i].map_room.removeSprite(this._heap.collisions[this._heap.i]); // redundant w/ GameObject.cleanup
								this._heap.collisions[this._heap.i].game_object.manager.remove(this._heap.collisions[this._heap.i].game_object);

								SDS.dlgMgr.open("BossRoomChute_Jam",function(index,retval){
									if (retval!=null){
										
									}
								},true);
							}

							break;
						}
					}
					this._heap.collisions.length = 0;

					if (this.jammed){
						this._heap.poof = new GO_Poof();
						this._heap.poof.spawnOnSprite(this.sprite,this.manager);
						this._heap.poof = null;
						this.state = GO_BossRoomChute.prototype.ANIM_STATES.idle;
					}
					else{
						this._heap.block = new GO_Boulder(GO_Boulder.TYPES.SKYBLOCK);
						this._heap.block.spawnOnSprite(this.sprite,this.manager);
						this._heap.block = null;
						++this.state;
					}
					
				}
				else{
					++this.state;
				}

				// if we're going to dump and there already isn't room,
				// skip dumnp state.
				// Unless, of course, we've been tampered with...
				if (this.state===GO_BossRoomChute.prototype.ANIM_STATES.dump && !this.tampered){
					this._heap.collisions = this._heap.collisions || [];
					this.sprite.collision_flags |= DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
					this.sprite.getCollisions(this._heap.collisions);
					this.sprite.collision_flags &= ~DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
					for(this._heap.i=0; this._heap.i<this._heap.collisions.length; ++this._heap.i){
						if(this._heap.collisions[this._heap.i].collision_flags & DEFINE_SPRITE_COLLISION_FLAGS.SOLID){
							++this.state;
							break;
						}
					}
					this._heap.collisions.length = 0;
				}
				this.state %= 6;
			}
		}

		this.sprite.frame = GO_BossRoomChute.prototype.anim_frames[this.state][this.state_timer>>1];
		if (this.jammed){
			this.sprite.frame = GO_BossRoomChute.prototype.anim_frames[GO_BossRoomChute.prototype.ANIM_STATES.jammed][0] + this.walltype;
		}
		else if (!this.tampered && this.state===GO_BossRoomChute.prototype.ANIM_STATES.idle && (this.state_timer&1)){
			this.sprite.frame = GO_BossRoomChute.prototype.anim_frames[GO_BossRoomChute.prototype.ANIM_STATES.tampered][0] + this.walltype;
		}
		else if (this.state===GO_BossRoomChute.prototype.ANIM_STATES.idle || this.state===GO_BossRoomChute.prototype.ANIM_STATES.open || this.state===GO_BossRoomChute.prototype.ANIM_STATES.close){
			this.sprite.frame += this.walltype<<2;
		}
	};

