"use strict";

// bnuuy.
var GO_TheGloriousFloof = function(){
	this.sprite = new Sprite(GO_TheGloriousFloof.spriteSheet,"TheGloriousFloof");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(20,10,53,31);
	this.sprite.game_object = this; // no longer a generic GO

	this.floof = new Sprite(GO_TheGloriousFloof.floofSheet,"TheGloriousFloof");
	this.floof.collision_flags = 0;
	this.floof.setBoundingBox(0,0,15,15);
	this.floof.game_object = this; // no longer a generic GO
	this.floof.visible = false;

	this.name = "TheGloriousFloof";

	this.glompage = 0;
	this.glompdown = 0;
	this.max_glomp = 50;
	this.glomped = false;

	this.meli = null;
};
GO_TheGloriousFloof.spriteImg = SDS_Load_Image("art/sheets/sprite/bnyyu.png");
GO_TheGloriousFloof.spriteSheet = new TileSheet(6, 5, 1, 1, GO_TheGloriousFloof.spriteImg);
GO_TheGloriousFloof.floofSheet = new TileSheet(4, 4, 3, 2, GO_TheGloriousFloof.spriteImg);

GO_TheGloriousFloof.prototype = new GameObject(
	new Sprite(
		GO_TheGloriousFloof.spriteSheet,
		"TheGloriousFloof"
	),
	"TheGloriousFloof"
	);

	GO_TheGloriousFloof.prototype.interactions = DEFINE_GAMEOBJECT_INTERACTIONS.CHECK;

	GO_TheGloriousFloof.prototype.Update = function(){
		// no mov. ony gomp.
		if (!this.meli || 
			this.meli.controls.held.left || 
			this.meli.controls.held.right || 
			this.meli.controls.held.A){
			this.glompage = 0;
			this.glompdown = 0;
		}

		if (this.glompdown > 0) --this.glompdown;
		else if (this.glompage < this.max_glomp) this.glompage = 0;

		this.sprite.frame = this.glomped ? 2 :
			this.glompage<10 ? 0 :
			this.glompage<50 ? 1 : 2;
		this.floof.frame = this.glompage<10 ? 0 :
			this.glompage<25 ? 1 :
			this.glompage<40 ? 2 : 3;
		if (this.meli == null){
			this.floof.visible = false;
		}
		else{
			this.floof.frame += (this.meli.direction>0) ? 20 : 28;
			this.floof.visible = this.glompage>=5;
			this.floof.x = this.meli.sprite.x;
			this.floof.y = this.meli.sprite.y;
		}
	};

	GO_TheGloriousFloof.prototype.checkEvent = function(sourceGO){
		if (!this.meli){
			this.meli = sourceGO;
			// place floof later in draw queue
			this.sprite.map_room.removeSprite(this.floof);
			this.sprite.map_room.addSprite(this.floof);
		}

		this.glompdown = 10;
		if ((this.glompage&3) == 3 && this.meli!=null) this.meli.direction *= -1;
		++this.glompage;

		if (!this.glomped && this.glompage >= this.max_glomp){
			this.glomped = true;
			return "grey_challenge_win";
		}
	};

	GO_TheGloriousFloof.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		mapRoom.addSprite(this.floof);
	}
	GO_TheGloriousFloof.prototype.clearFromRoom = function(mapRoom){
		mapRoom.removeSprite(this.sprite);
		mapRoom.removeSprite(this.floof);
	}
