/*
* A solid fixed platform-block with an aura that 0-damages enemies
*/
"use strict";

var GO_HolyBlock = function(width, height){
	this.width = width ? width : 1;
	this.height = height ? height : 1;
	this.sprite = new Sprite(null,"HolyBlock");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_ACTIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(-1,-1,(this.width<<4),(this.height<<4)); // negative bounding box.  Engine should handle it...
	//sprite.constraints=constraints;
	this.sprite.max_step = 0;
	this.sprite.game_object = this; // no longer a generic GO

	// a secondary sprite forming the solid core of the block
	this.core = new Sprite(null,"HolyBlock_core");
	this.core.setBoundingBox(0,0,(this.width<<4)-1,(this.height<<4)-1); // negative bounding box.  Engine should handle it...
	this.core.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE |
		DEFINE_SPRITE_COLLISION_FLAGS.SOLID;;
	this.core.game_object = this;

	// steal construction theory from roller to minimize total objects.
	// But to minimize sprite draws, let's keep to just the perimeter
	this.blocks = [];
	this._heap.b = 0;
	for (this._heap.j=0; this._heap.j<this.height; ++this._heap.j){
		for (this._heap.i=0; this._heap.i<this.width; ++this._heap.i){
			if (this._heap.i===0 || this._heap.i===this.width-1 || this._heap.j===0 || this._heap.j===this.height-1){
				this.blocks.push(new Sprite(GO_HolyBlock.spriteSheet,"HolyBlockSegment"));
				this.blocks[this._heap.b].setBoundingBox(0,0,15,15);
				this.blocks[this._heap.b].collision_flags = 0;
				this.blocks[this._heap.b].game_object = this;
				++this._heap.b;
			}
		}
	}

	this._heap.targets = [];

	this.frame_timer = 0;
	this.frame = 0;
};
GO_HolyBlock.spriteImg = SDS_Load_Image("art/sheets/sprite/holyblock.png");
GO_HolyBlock.spriteSheet = new TileSheet(4, 4, 2, 0, GO_HolyBlock.spriteImg);

GO_HolyBlock.prototype = new GameObject(
	new Sprite(
		GO_HolyBlock.spriteSheet,
		"HolyBlock"
	),
	"HolyBlock"
	);

	// INFLICT mechanic to harm Meli for 0/-8
	// (this object mainly occurs after Meli is unsealed and chi damage reverses)
	GO_HolyBlock.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.INFLICT;

	GO_HolyBlock.prototype.inflict = function(){
		return {hp: 0, chi:-16};
	};

	GO_HolyBlock.prototype.addToRoom = function(mapRoom){
		mapRoom.addSprite(this.sprite);
		mapRoom.addSprite(this.core);
		this.core.x = this.sprite.x;
		this.core.y = this.sprite.y;
		
		this._heap.b = 0;
		for (this._heap.j=0; this._heap.j<this.height; ++this._heap.j){
			for (this._heap.i=0; this._heap.i<this.width; ++this._heap.i){
				if (this._heap.i===0 || this._heap.i===this.width-1 || this._heap.j===0 || this._heap.j===this.height-1){
					mapRoom.addSprite(this.blocks[this._heap.b]);
					this.blocks[this._heap.b].x = this.sprite.x + (this._heap.i<<4);
					this.blocks[this._heap.b].y = this.sprite.y + (this._heap.j<<4);
					++this._heap.b;
				}
			}
		}
	}

	GO_HolyBlock.prototype.frames = [0,1,2,3,3,3,2,1];

	GO_HolyBlock.prototype.Update = function(){
		// collision test to DAMAGE enemies for 0
		this.sprite.getCollisions(this._heap.targets);

		for (this._heap.i=0; this._heap.i<this._heap.targets.length; ++this._heap.i){
			if (this._heap.targets[this._heap.i].game_object.interactions & DEFINE_GAMEOBJECT_INTERACTIONS.DAMAGE){
				this._heap.targets[this._heap.i].game_object.damage(this,0); // no HP damage, just "stun"
			}
		}


		++this.frame_timer;
		this.frame_timer = this.frame_timer & 31;
		this.frame = this.frames[this.frame_timer>>2];
		//this.sprite.frame = this.frames[this.frame];
		for (this._heap.i=0; this._heap.i<this.blocks.length; ++this._heap.i){
			this.blocks[this._heap.i].frame = this.frame;
		}
	};



