/*:
* @plugindesc Allows automatic linking of maps, using map note tags. (See "Help...")
* @help
* Tags take the form of:
* <MapLink.DIR : MAP_ID X Y>
*
* DIR indicates which edge of the map should be linked, and must be one of:
* Top
* Bottom
* Left
* Right
*
* MAP_ID indicates which map the player should be taken to, when touching the edge.
*
* X and Y have different meanings, depending on the DIR:
* where DIR is either Top or Bottom
* X = adjustment to be made to player's x (typically 0). for example "-3" means "shift the player left 3 tiles"
* Y = new y value to be set. for example "20" means "set the player's y to 20".
* where DIR is either Left or Right
* X = new x value to be set. for example "0" means "set the player's x to 0".
* Y = adjustment to be made to player's y (typically 0). for example "10" means "shift the player down 10 tiles"
*
* Some complete examples:
*
* <MapLink.Bottom:9 0 1>
* Link the bottom of the map to map #9. The player's X will not be adjusted; the player's new Y will be 1
* <MapLink.Top:6 0 11>
* Link the top of the map to map #6. The player's X will not be adjusted; the player's new Y will be 11
*
* Compatibility:
* * ALIASES Game_Map..setup
* * ALIASES Game_Player..checkEventTriggerHere
* @author Ben Hendel-Doying
*/

(function() {
var MapLink_Game_Map_setup = Game_Map.prototype.setup;

Game_Map.prototype.setup = function (mapId) {
MapLink_Game_Map_setup.call(this, mapId);

this.mapLink = {};

for (var key in $dataMap.meta) {
if (!$dataMap.meta.hasOwnProperty(key)) continue;

var lowerKey = key.trim().toLowerCase();

if (lowerKey == 'maplink.top') this.mapLink.top = $dataMap.meta.trim().split(' ');
else if (lowerKey == 'maplink.left') this.mapLink.left = $dataMap.meta.trim().split(' ');
else if (lowerKey == 'maplink.right') this.mapLink.right = $dataMap.meta.trim().split(' ');
else if (lowerKey == 'maplink.bottom') this.mapLink.bottom = $dataMap.meta.trim().split(' ');
}
};

var MapLink_Game_Player_checkEventTriggerHere = Game_Player.prototype.checkEventTriggerHere;

Game_Player.prototype.checkEventTriggerHere = function (triggers) {
MapLink_Game_Player_checkEventTriggerHere.call(this, triggers);

if (!this.isTransferring() && !$gameMap.isEventRunning()) {
if (this.y == 0 && $gameMap.mapLink.hasOwnProperty('top')) {
this.reserveTransfer($gameMap.mapLink.top, this.x + parseInt($gameMap.mapLink.top), parseInt($gameMap.mapLink.top), this.direction(), 0);
}
else if (this.x == 0 && $gameMap.mapLink.hasOwnProperty('left')) {
this.reserveTransfer($dataMap.mapLink.left, parseInt($gameMap.mapLink.left), this.y + parseInt($gameMap.mapLink.left), this.direction(), 0);
}
else if (this.y == $dataMap.height - 1 && $gameMap.mapLink.hasOwnProperty('bottom')) {
this.reserveTransfer($gameMap.mapLink.bottom, this.x + parseInt($gameMap.mapLink.bottom), parseInt($gameMap.mapLink.bottom), this.direction(), 0);
}
else if (this.x == $dataMap.width - 1 && $gameMap.mapLink.hasOwnProperty('right')) {
this.reserveTransfer($gameMap.mapLink.right, parseInt($gameMap.mapLink.right), this.y + parseInt($gameMap.mapLink.right), this.direction(), 0);
}
}
};
})();