/*
GO straight to the void.  Do not pass Go, do not collect $200
*/
"use strict";

var GO_Deathwarp = function(meli){
	this.sprite = new Sprite(null,"Deathwarp");
	this.sprite.collision_flags = 
		DEFINE_SPRITE_COLLISION_FLAGS.SPRITES_PASSIVE;
	this.sprite.setBoundingBox(0,0,15,15);
	this.sprite.game_object = this; // no longer a generic GO

	this.sprite.x = meli.sprite.x;
	this.sprite.y = meli.sprite.y;

	this.relocate_pos = {x:meli.sprite.x, y:meli.sprite.y};

	this.calculateRelocateCoords(meli.sprite);
};

GO_Deathwarp.prototype = new GameObject(
	new Sprite(
		null,
		"Deathwarp"
	),
	"Deathwarp"
	);

	GO_Deathwarp.prototype.interactions = 
		DEFINE_GAMEOBJECT_INTERACTIONS.AUTOPORT;// |
		//DEFINE_GAMEOBJECT_INTERACTIONS.TRIGGER; // port seems to now resolve completely before trigger, so this won't work

	// custom crunchy relocator to try to ensure that our screen position doesn't change
	GO_Deathwarp.prototype.calculateRelocateCoords = function(sprite){
		var spriteCollisionCenterX = Math.floor((sprite.box_left+sprite.box_right)/2);
		var spriteCollisionCenterY = Math.floor((sprite.box_top+sprite.box_bottom)/2);
		var spriteMidX = sprite.x + spriteCollisionCenterX;
		var spriteMidY = sprite.y + spriteCollisionCenterY;

		// how close are we to the edge of the visible room?
		var fromLeft = spriteMidX - sprite.map_room.pixel_margin;
		var fromRight = sprite.map_room.pixel_width - spriteMidX - sprite.map_room.pixel_margin;
		var fromTop = spriteMidY - sprite.map_room.pixel_margin;
		var fromBottom = sprite.map_room.pixel_height - spriteMidY - sprite.map_room.pixel_margin;

		var voidWidth = 24<<4;
		var voidHeight = 15<<4;
		var voidMargin = 32;

		// too narrow a room; pending better computations, go to the center of the void
		if (sprite.map_room.width - sprite.map_room.margin*2 < 24){
			this.relocate_pos.x = (voidWidth>>1) - spriteCollisionCenterX;
		}
		// we're at the left edge of the current room; go to the left edge of the void
		else if (fromLeft < voidWidth>>1){
			this.relocate_pos.x = fromLeft + voidMargin - spriteCollisionCenterX;
		}
		// we're at the right edge of the current room; go to the right edge of the void
		else if (fromRight < voidWidth>>1){
			this.relocate_pos.x = voidWidth - fromRight + voidMargin - spriteCollisionCenterX;
		}
		// we're at screen center; go to the center of the void
		else{
			this.relocate_pos.x = (voidWidth>>1) - spriteCollisionCenterX;
		}

		// too narrow a room; pending better computations, go to the center of the void
		if (sprite.map_room.height - sprite.map_room.margin*2 < 15){
			this.relocate_pos.y = (voidHeight>>1) - spriteCollisionCenterY;
		}
		// we're at the top edge of the current room; go to the top edge of the void
		else if (fromTop < voidHeight>>1){
			this.relocate_pos.y = fromTop + voidMargin - spriteCollisionCenterY;
		}
		// we're at the right edge of the current room; go to the right edge of the void
		else if (fromBottom < voidHeight>>1){
			this.relocate_pos.y = voidHeight - fromBottom + voidMargin - spriteCollisionCenterY;
		}
		// we're at screen center; go to the center of the void
		else{
			this.relocate_pos.y = (voidHeight>>1) - spriteCollisionCenterY;
		}

	};

	GO_Deathwarp.prototype.getDestName = function(){return "the_void"};

	// custom crunchy relocator to try to ensure that our screen position doesn't change
	GO_Deathwarp.prototype.relocate = function(sprite){
		sprite.x = this.relocate_pos.x;
		sprite.y = this.relocate_pos.y;
	};

	GO_Deathwarp.prototype.triggerEvent = function(){
		return GLOBAL_DIARY.check(0,"MET_DEATH") ? "" : "GD_Death's_domain";
	};


