"use strict";

var GO_GoddessBlock = function(breakChapter, breakEvent){
	this.sprite = new Sprite(GO_GoddessBlock.spriteSheet,"GoddessBlock"); // so all instances have different sprites
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(6,8,25,31);
	this.sprite.game_object = this; // no longer a generic GO

	this.sprite.frame = 0;

	this.frame_timer = 0;

	this.broken = false;

	this._makeCheckable("GD_GoddessBlock");

	this.break_chapter = breakChapter;
	this.break_event = breakEvent;

	// main sprite for inspection/damage;
	// sub sprite for solid obstruction
	this.block = new Sprite(null,"GoddessBlock_block"); // so all instances have different sprites
	this.block.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID;
	this.block.setBoundingBox(7,9,24,31);
	this.block.game_object = this; // no longer a generic GO
};
GO_GoddessBlock.spriteImg = SDS_Load_Image("art/sheets/sprite/goddessblock.png");
GO_GoddessBlock.spriteSheet = new TileSheet(5, 5, 1, 1, GO_GoddessBlock.spriteImg);

GO_GoddessBlock.prototype = new GameObject(
	new Sprite(
		GO_GoddessBlock.spriteSheet,
		"GoddessBlock"
	),
	"GoddessBlock"
	);

	GO_GoddessBlock.prototype.interactions = DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE;

	//GO_GoddessBlock.prototype.frameSeq = [0,0,0,0,0,0,0,0,0,1,1,1,2,1,1,1];
	GO_GoddessBlock.prototype.frameSeq = [
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,

		0,0,1,0,0,1,0,0,
		1,0,1,0,1,0,1,0,

		//1,0,1,0,1,2,1,2,
		//1,2,1,2,1,0,1,0,
		1,0,1,0,1,1,2,1,
		1,2,1,2,1,0,1,0,

		1,0,1,0,1,0,1,0,
		0,1,0,0,1,0,0,0];
	GO_GoddessBlock.prototype.Update = function(){
		if (this.broken){
			this.sprite.frame = this.frame_timer>>3;
			if (++this.frame_timer >= 32){
				this.clearFromRoom(this.sprite.map_room);
				this.manager.remove(this);
			}
		}
		else{
			++this.frame_timer;
			this.frame_timer &= 63;

			//this.sprite.frame = this.frameSeq[this.frame_timer>>2];
			this.sprite.frame = this.frameSeq[this.frame_timer];
		}
	};

	GO_GoddessBlock.prototype.addToRoom = function(mapRoom){
		this.block.x = this.sprite.x;
		this.block.y = this.sprite.y;
		mapRoom.addSprite(this.sprite);
		mapRoom.addSprite(this.block);
	}
	GO_GoddessBlock.prototype.clearFromRoom = function(mapRoom){
		mapRoom.removeSprite(this.sprite);
		mapRoom.removeSprite(this.block);
	}

	// apply damage and return true if destroyed
	GO_GoddessBlock.prototype.damage = function(source,points){
		if (points>0 && GLOBAL_DIARY.hasBlessing){
			if (!this.broken){
				this._heap.poof = new GO_Poof();
				this._heap.poof.spawnOnSprite(this.sprite,this.manager);
				this._heap.poof = null;

				this.frame_timer = 0;
				this.broken = true;

				GLOBAL_DIARY.note(this.break_chapter, this.break_event);
			}

			return true;
		}
		return false;
	};
