"use strict";

var GO_Fallblock = function(falltime,drop){
	this.sprite = new Sprite(GO_Fallblock.spriteSheet,"Fallblock"); // so all instances have different sprites
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
	this.sprite.setBoundingBox(0,1,15,15);
	this.sprite.game_object = this; // no longer a generic GO

	this.ridebox = new Sprite(null,"Fallblock_ridebox");
	this.ridebox.setBoundingBox(0,0,15,0); // sliver of clearance on top, and keep position pegged to main sprite
	this.ridebox.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE;
	this.ridebox.game_object = this;

	this.sprite.frame = 0;

	this._heap.riders = [];

	this.fall_counter = falltime ? falltime : 30;
	this.drop_block = !!drop || false;
};
GO_Fallblock.spriteImg = SDS_Load_Image("art/sheets/sprite/fallblock.png");
GO_Fallblock.spriteSheet = new TileSheet(4, 5, 3, 0, GO_Fallblock.spriteImg);

GO_Fallblock.prototype = new GameObject(
	new Sprite(
		GO_Fallblock.spriteSheet,
		"Fallblock"
	),
	"Fallblock"
	);

	GO_Fallblock.prototype.interactions = 0;

	GO_Fallblock.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		mapRoom.addSprite(this.ridebox);
		this.ridebox.x = this.sprite.x;
		this.ridebox.y = this.sprite.y;
	}

	GO_Fallblock.prototype.Update = function(){

		if (this.fall_counter<=0){
			if (this.drop_block){
				this._heap.block = new GO_Boulder(GO_Boulder.TYPES.FALLBLOCK);
				this._heap.block.sprite.x = this.sprite.x;
				this._heap.block.sprite.y = this.sprite.y+1; // +1 gives just enough time for block to fall, land, and become non-damagey before meli falls onto it

				this.sprite.map_room.addSprite(this._heap.block.sprite);
				this.manager.add(this._heap.block);

				this._heap.block = null;

				this.sprite.frame = 7;
				this.drop_block = false;
			}

			if (this.sprite.frame<7){
				++this.sprite.frame;
				if (this.sprite.frame>3) this.sprite.collision_flags = 0;
			}
			else{
				// disappear
				this.sprite.map_room.removeSprite(this.sprite);
				if (this.ridebox.map_room!=null){
					this.ridebox.map_room.removeSprite(this.ridebox);
				}
				this.manager.remove(this);
			}
		}
		else{
			this.ridebox.getCollisions(this._heap.riders);
			if (this._heap.riders.length){
				--this.fall_counter;
				if (this.fall_counter%15===0){
					this.sprite.move(0,1); // warning drop at .5 and 1 sec
					//this.ridebox.x = this.sprite.x;
					this.ridebox.y = this.sprite.y;

					// if footing gets too bad with the sudden drops, pull riders down with
					/*for (this._heap.i=0; this._heap.i<this._heap.riders.length; ++this._heap.i){
						// don't move ourselves, other blocks, doors, or triggers.
						// could probably use a more explicit flag for mobs, but the need is not critical just yet
						if (!(this._heap.riders[this._heap.i].game_object instanceof GO_Fallblock) && 
							!(this._heap.riders[this._heap.i].game_object.interactions & 
								(
									DEFINE_GAMEOBJECT_INTERACTIONS.TRIGGER |
									DEFINE_GAMEOBJECT_INTERACTIONS.PORT |
									DEFINE_GAMEOBJECT_INTERACTIONS.AUTOPORT
								)
							)){
							this._heap.riders[this._heap.i].move(0, 1);
						}
					}*/
				}
			}

			this._heap.riders.length=0; // don't retain references to riders
		}

		
	};