/*
 * UndergroundPathFinder
 * 
 * Find underground paths for the MotionManager
 */

/**
 * Constructor.
 */
function UndergroundPathFinder(motionManager, mapPaths, mapTeleporters)
{
	this.PathFinder(motionManager, mapPaths, mapTeleporters);
}
copyPrototype(UndergroundPathFinder, PathFinder);

/**
 * Build a path between 2 teleporters.
 *
 * @param teleporterPosition grid position of the first teleporter
 * @return true if success, false if an error occurs
 */
UndergroundPathFinder.prototype.buildPathBetweenTeleporters = function(teleporterPosition)
{
	this.clearComputedPath();
	
	//Get the teleporter ID
	var teleporterID = this.mapTeleporters[teleporterPosition.y][teleporterPosition.x];
	if(teleporterID <= 1) return false; //0 = no teleporter, 1 = player's initial position
	
	//Look for the exit
	var teleporterExitPosition = this.getExitTeleporterLocation(teleporterPosition);
	
	//The underground path shape is like a "L", first horizontal then vertical
	
	//Compute the horizontal part
	var xstart = Math.min(teleporterPosition.x, teleporterExitPosition.x);
	var xend = Math.max(teleporterPosition.x, teleporterExitPosition.x);
	var direction = (teleporterPosition.x == xstart ? this.motionManager.KEY_RIGHT : this.motionManager.KEY_LEFT);
	for(var x = xstart; x <= xend; x++)
		this.computedPath[teleporterPosition.y][x] = direction;
	
	//Compute the vertical part
	var ystart = Math.min(teleporterPosition.y, teleporterExitPosition.y);
	var yend = Math.max(teleporterPosition.y, teleporterExitPosition.y);
	direction = (teleporterPosition.y == ystart ? this.motionManager.KEY_DOWN : this.motionManager.KEY_UP);
	for(var y = ystart; y <= yend; y++)
		this.computedPath[y][teleporterExitPosition.x] = direction;
	
	//The latest tile direction must be KEY_DOWN
	this.computedPath[teleporterExitPosition.y][teleporterExitPosition.x] = this.motionManager.KEY_DOWN;
	this.lastComputedPathTilePosition = teleporterExitPosition;
	
	return true;
}

