document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //=============================================================================
  2. // EdgeHandler.js
  3. //=============================================================================
  4.  
  5. /*:
  6.  * @plugindesc Adds automatic handling when the party touches the edge of a map.
  7.  * @author whitesphere
  8.  *
  9.  * @param Common Event ID
  10.  * @desc Which common event is called when the player reaches an edge, by default.
  11.  * @default 1
  12.  *
  13.  * @param Direction Variable
  14.  * @desc Contain the party\'s current direction: "top", "left", "right" or "bottom"
  15.  * @default 10
  16.  *
  17.  * @param Map ID Variable
  18.  * @desc Variable which will contain the current Map ID
  19.  * @default 11
  20.  *
  21.  * @param Fade Type
  22.  * @desc How does the screen fade during the transfer.  0=Black, 1=White, 2=None
  23.  * @default 0
  24.  *
  25.  * @param Transfer Sound
  26.  * @desc The Sound Effect to play when starting a map transfer
  27.  * @default Move1
  28.  *
  29.  * @help With this active, by default, when the player touches an edge of the screen,
  30.  * this plugin will first set the party\'s direction and current Map ID in the variables
  31.  * specified in the plug-in parameters.  Then, it will call the common event ID specified
  32.  * in the "Common Event ID" parameter.  The common event can then check the current map ID,
  33.  * direction, etc, anything else to transfer the player to a different map.  This is highly
  34.  * useful for fairly open maps where the player has many different ways to
  35.  * exit the map.
  36.  * The direction will be set to "bottom" for the bottom edge, "left" for the left edge,
  37.  * "right" for the right edge and "top" for the top edge.
  38.  *
  39.  * By default, when the player reaches an edge of the current map. the plug-in
  40.  * sets the current Map ID into the "Map ID Variable" variable, and the current
  41.  * player direction into the "Direction Variable" variable.  It then calls the
  42.  * Common event specified by "Common Event ID"
  43.  *
  44.  * However, if you add in a per-map notetag, you can ask this plug-in to automatically
  45.  * transport the player from, say, the left-most edge of one map to the right-most
  46.  * edge of another.  When this plug-in wraps the player this way, it maintains the
  47.  * other coordinate.  For example, moving left from (0,33) onto a map which is 50 tiles
  48.  * wide will end up on (49, 33).  
  49.  * Moving off the top or bottom preserves the X coordinate, and moving off the left or
  50.  * right preserves the Y coordinate.
  51.  *
  52.  * ============================================================================
  53.  * Plug-in Commands
  54.  * ============================================================================
  55.  *
  56.  * EdgeHandler EnterMap <map id> <edge name> - Enters the map specified as if the
  57.  * player had walked in from the appropriate edge (bottom, top, left, right) from his
  58.  * or her current location.
  59.  * For example, if Map ID 3 were 50 tiles wide and 30 high, and the player were
  60.  * currently at X=10, Y=20, if you call EnterMap 3 top, the player  
  61.  * would be transferred to X=10, Y=1 on Map ID 3
  62.  * If you called EnterMap 3 right instead, the player would be transferred to
  63.  * X=49, Y=20 on Map ID 3.
  64.  *
  65.  * EdgeHandler SetEnabled (true or false) - If true, enables the current map\'s edge
  66.  * handling.  If false, disables the current map\'s edge handling. Transferring to a
  67.  * new map re-enables edge handling.
  68.  *
  69.  * ============================================================================
  70.  * Notetags
  71.  * ============================================================================
  72.  *
  73.  * You can use this notetag inside of your maps.  
  74.  *
  75.  * Map Notetags:
  76.  *   <edge: (direction)=handling_type(param) [fade=(0, 1, 2)] [sound=Move1]>
  77.  *   The direction can be one of these strings: top, bottom, left or right
  78.  *   Handling_type is either "common_event" to call a particular common event, "none" to
  79.  *   disable edge handling for this side, or "map" to automatically wrap to the opposite
  80.  *   edge of the map ID specified by "param"
  81.  *   Param is either a Common Event ID for the "common_event" handling type, or
  82.  *   a Map ID for the "map" handling type.
  83.  *   If specified, fade describes how the screen fades during the map transfer.
  84.  *   0=Fade to Black, 1=Fade to White, 2=No Fade
  85.  *   Finally, if sound is set, the Sound Effect named will play before a map transfer.
  86.  *   If it is not, the Transfer Sound is played.  
  87.  *   For example:
  88.  *   <edge: top=common_event(3) bottom=map(2) left=map(3) right=map(4)>
  89.  *   When the player reaches the top, this calls Common Event 3, setting the Direction
  90.  *   and Map ID Variables before it does so.
  91.  *   When the player reaches the left, this finds the size of Map ID 3, and puts the
  92.  *   player at the rightmost side of Map ID 3, with the same Y coordinate.
  93.  *   When the player reaches the bottom, this puts the player at the topmost side of
  94.  *   Map ID 2, with the same X coordinate.
  95.  *   When the player reaches the right, this puts the player at the leftmost side of
  96.  *   Map ID 4, with the same Y coordinate.
  97.  *
  98.  *   If a side is not set in the notetag, this plug-in reverts to the default behavior for
  99.  *   that side:  Set Map ID and Direction variables, then call the Common Event ID
  100.  *   specified in the plug-in parameters.
  101.  */
  102.  
  103.  (function() {
  104.  
  105. WS_EdgeHandler = {};
  106.  
  107. WS_EdgeHandler.Parameters = PluginManager.parameters(\'EdgeHandler\');
  108. WS_EdgeHandler.Param = {};
  109.  
  110. WS_EdgeHandler.Param.CommonEventID = parseInt(WS_EdgeHandler.Parameters[\'Common Event ID\']);
  111. WS_EdgeHandler.Param.DirectionVariable = parseInt(WS_EdgeHandler.Parameters[\'Direction Variable\']);
  112. WS_EdgeHandler.Param.MapIDVariable = parseInt(WS_EdgeHandler.Parameters[\'Map ID Variable\']);
  113. WS_EdgeHandler.Param.FadeType = parseInt(WS_EdgeHandler.Parameters[\'Fade Type\']);
  114. WS_EdgeHandler.Param.TransferSound = WS_EdgeHandler.Parameters[\'Transfer Sound\'];
  115.  
  116.  
  117. $edgeHandler = {};
  118.  
  119. //=============================================================================
  120. // Object to do special case handling at map edges
  121. // Expects a note tag in the following format:
  122. // <edge: top=common_event:3 fade=2 bottom=map:2 left=map:3 right=map:4>
  123. //=============================================================================
  124. Edge_Handler = function(meta) {
  125.     if (meta && meta.edge) {
  126.         var handlerString=meta.edge;
  127.         var argSet=handlerString.split(" ");
  128.         this.sides = {};
  129.         this.fade=WS_EdgeHandler.Param.FadeType;
  130.         this.sound=WS_EdgeHandler.Param.TransferSound;
  131.         for (var index=0; index<argSet.length; index++) {
  132.             var current_arg=argSet[index];
  133.             current_arg=current_arg.trim().toLowerCase();
  134.            
  135.             /* Split into direction=param:arg */
  136.             direction_and_stuff=current_arg.split("=");
  137.             if (direction_and_stuff.length != 2) {
  138.                 continue;
  139.             }
  140.            
  141.            
  142.             if (direction_and_stuff[0] == "fade") {
  143.                 if (!!parseInt(direction_and_stuff[1]))
  144.                     this.fade=parseInt(direction_and_stuff[1]);
  145.                 continue;
  146.             }
  147.             if (direction_and_stuff[0] == "sound") {
  148.                 this.sound=direction_and_stuff[1];
  149.                 continue;
  150.             }
  151.            
  152.             /* The direction needs to be valid */
  153.             direction_int=this.directionToInt(direction_and_stuff[0]);
  154.             if (direction_int === undefined) {
  155.                 continue;
  156.             }
  157.  
  158.             /* Split the command and parameter from cmd(param) to [cmd param]*/
  159.             param_match=direction_and_stuff[1].match(/\\([0-9]*\\)/);
  160.             cmd_and_param=[];
  161.             if (param_match) {
  162.                 /* Pull the value out of the parenthesis */
  163.                 cmd_and_param[1]=parseInt(param_match[0].substring(1, param_match[0].length-1));
  164.                
  165.                 /* The command comes before the value */
  166.                 cmd_and_param[0]=direction_and_stuff[1].substring(0, direction_and_stuff[1].indexOf(param_match[0]));
  167.             }
  168.             if (cmd_and_param.length != 2 && direction_and_stuff[1] != "none") {
  169.                 continue;
  170.             }
  171.             if (direction_and_stuff[1] == "none")
  172.                 cmd_and_param[0]="none";
  173.             if (cmd_and_param[0] != "common_event" && cmd_and_param[0] != "map" &&
  174.                 cmd_and_param[0] != "none") {
  175.                 continue;
  176.             }
  177.             param_int=cmd_and_param[1];
  178.             this.sides[direction_int]=[cmd_and_param[0], param_int];
  179.         }
  180.     }
  181.     else
  182.     {
  183.         this.sides={};
  184.     }
  185. }
  186.  
  187. //=============================================================================
  188. // Converts a direction (2, 4, 6, 8) to a string
  189. //=============================================================================
  190. Edge_Handler.prototype.directionToString = function(direction) {
  191.     if (Edge_Handler.prototype._directions === undefined) {
  192.         Edge_Handler.prototype._directions = [];
  193.         /* DIRECTION VALUES: 2 = down, 4 = left, 6 = right, 8 = up */
  194.         Edge_Handler.prototype._directions[2]="bottom";
  195.         Edge_Handler.prototype._directions[4]="left";
  196.         Edge_Handler.prototype._directions[6]="right";
  197.         Edge_Handler.prototype._directions[8]="top";
  198.     }
  199.     return Edge_Handler.prototype._directions[direction];
  200. }
  201.  
  202. //=============================================================================
  203. // Converts a direction string to an integer.  Returns undefined if there
  204. // is no match
  205. //=============================================================================
  206. Edge_Handler.prototype.directionToInt = function(direction) {
  207.     if (Edge_Handler.prototype._directions === undefined) {
  208.         Edge_Handler.prototype._directions = [];
  209.         /* DIRECTION VALUES: 2 = down, 4 = left, 6 = right, 8 = up */
  210.         Edge_Handler.prototype._directions[2]="bottom";
  211.         Edge_Handler.prototype._directions[4]="left";
  212.         Edge_Handler.prototype._directions[6]="right";
  213.         Edge_Handler.prototype._directions[8]="top";
  214.     }
  215.     for (var check=2; check<10; check += 2) {
  216.         if (direction == Edge_Handler.prototype._directions[check])
  217.             return check;
  218.     }
  219.     return undefined;
  220. }
  221.  
  222. //=============================================================================
  223. // Play the transfer sound
  224. //=============================================================================
  225. Edge_Handler.prototype.playTransferSound = function(sound_effect) {
  226.     if (!sound_effect)
  227.         sound_effect=WS_EdgeHandler.Param.TransferSound;
  228.     if (sound_effect)
  229.         sound_effect=sound_effect.trim();
  230.    
  231.     if (!sound_effect || !sound_effect.length)
  232.         return;
  233.     sound_obj={};
  234.     sound_obj.name=sound_effect;
  235.     sound_obj.volume=100;
  236.     sound_obj.pitch=100;
  237.     sound_obj.pan=0;
  238.     AudioManager.playSe(sound_obj);
  239. }
  240.  
  241. //=============================================================================
  242. // Loads the map and returns it.
  243. //=============================================================================
  244. Edge_Handler.prototype.moveToDifferentMap = function(x, y, map_id, direction, fade_type, sound) {
  245.     $edgeHandler.edgeLoadDirection=direction;
  246.     $edgeHandler.edgeHandler=this;
  247.     $edgeHandler.edgeMapId=map_id;
  248.     $edgeHandler.edgeX=x;
  249.     $edgeHandler.edgeY=y;
  250.     $edgeHandler.edgeHandler.playTransferSound(sound);
  251.    
  252.     $gamePlayer.reserveTransfer(map_id,9999, 9999, direction, fade_type);
  253. }
  254.  
  255. //=============================================================================
  256. // Handles moving off the top or left edges of the previous map
  257. //=============================================================================
  258. Edge_Handler.prototype.finishSpecialCaseEdges = function(x, y, map, direction, map_id) {
  259.     /* Off left edge.  Here, we must find the width of the map we want to reach,
  260.     then subtract 2 from it
  261.     */
  262.     new_x=x;
  263.     new_y=y;
  264.     if (direction == 4) {
  265.         new_x=$dataMap.width-2;
  266.         if (new_x < 0)
  267.             new_x=0;
  268.     }
  269.  
  270.     /* Off top edge.  Get the height of the new map and subtract 2 from it */
  271.     if (direction == 8) {
  272.         new_y=$dataMap.height-2;
  273.         if (new_y < 0)
  274.             new_y=0;
  275.     }
  276.    
  277.     console.log("Finish: Moving player to ("+new_x+","+new_y+") on map "+map_id);
  278.     $edgeHandler={};
  279.    
  280.     /* And send the player to the new location */
  281.     $gamePlayer.reserveTransfer(map_id, new_x, new_y, direction);
  282. }
  283.  
  284. //=============================================================================
  285. // Does the handling for this particular direction (2=bottom, 4=left, 6=right, 8=top)
  286. //=============================================================================
  287. Edge_Handler.prototype.processEdge = function(direction, cmd_map_id) {
  288.     var current_type="common_event";
  289.    
  290.     var param_id=WS_EdgeHandler.Param.CommonEventID;
  291.     if (this.sides[direction] !== undefined) {
  292.         current_side=this.sides[direction];
  293.         current_type=current_side[0];
  294.         param_id=current_side[1];
  295.     }
  296.    
  297.     /* Set the map ID and direction variables */
  298.     if (!!WS_EdgeHandler.Param.DirectionVariable)
  299.     {
  300.         $gameVariables.setValue(WS_EdgeHandler.Param.DirectionVariable, this.directionToString(direction));
  301.     }
  302.     if (!!WS_EdgeHandler.Param.MapIDVariable)
  303.     {
  304.         $gameVariables.setValue(WS_EdgeHandler.Param.MapIDVariable, $gameMap.mapId()[0]);
  305.     }
  306.    
  307.     /* If this is disabled, we do nothing */
  308.     if (current_type == "none") {
  309.         return;
  310.     }
  311.    
  312.     /* We\'ve turned off edge handling for this map */
  313.     if ($gameMap._edgesDisabled) {
  314.         return;
  315.     }
  316.    
  317.     /* Easy case: We are a common event.  Just call it and return */
  318.     if (current_type == "common_event") {
  319.         if (!!param_id)
  320.         {
  321.             this.playTransferSound(this.sound);
  322.             $gameTemp.reserveCommonEvent(param_id);
  323.         }
  324.         return;
  325.     }
  326.    
  327.     /* The more interesting case.  If we are moving from the bottom or right, we know
  328.     where we are going.  Bottom means Y=1 on the new map and right means X=1 on the new
  329.     map. We want to stay off the edge so we don\'t end up transporting back to this map
  330.     and creating an endless loop.
  331.     */
  332.     var new_y=$gamePlayer.y;
  333.     var new_x=$gamePlayer.x;
  334.     var map_id=param_id;
  335.     if (cmd_map_id)
  336.     {
  337.         map_id=cmd_map_id;
  338.     }
  339.    
  340.     /* Off right edge */
  341.     if (direction == 6) {
  342.         new_x=1;
  343.     }
  344.    
  345.     /* Off bottom edge */
  346.     if (direction == 2) {
  347.         new_y=1;
  348.     }
  349.    
  350.     /* Off left edge.  Here, we must find the width of the map we want to reach,
  351.     then subtract 2 from it
  352.     */
  353.     if (direction == 4) {
  354.         map=this.moveToDifferentMap(new_x, new_y, map_id, direction, this.fade, this.sound);
  355.         return;
  356.     }
  357.  
  358.     /* Off top edge.  Get the height of the new map and subtract 2 from it */
  359.     if (direction == 8) {
  360.         this.moveToDifferentMap(new_x, new_y, map_id, direction, this.fade, this.sound);
  361.         return;
  362.     }
  363.    
  364.     this.playTransferSound(this.sound);
  365.     console.log("Moving player to ("+new_x+","+new_y+") on map "+map_id);
  366.     $edgeHandler={};
  367.     /* And send the player to the new location */
  368.     $gamePlayer.reserveTransfer(map_id, new_x, new_y, direction);
  369. }
  370.  
  371.  
  372. //=============================================================================
  373. // Plug-in commands
  374. //=============================================================================
  375. var _WS_EH_plugInCommand =
  376.         Game_Interpreter.prototype.pluginCommand;
  377. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  378.     if (command === \'EdgeHandler\') {
  379.         args[0]=args[0].toLowerCase();
  380.         switch (args[0]) {
  381.         // * EdgeHandler entermap <map id> <edge>
  382.             case \'entermap\':
  383.             if (args.length < 3)
  384.             {
  385.                 console.log("Usage: EdgeHandler entermap (map id) (edge: top, bottom, left, right)");
  386.                 return;
  387.             }
  388.             else
  389.             {
  390.                 edgeHandler=new Edge_Handler($dataMap.meta);
  391.                 reversedDirection=10-edgeHandler.directionToInt(args[2]);
  392.                 console.log("Setting the player to enter map ="+args[2]);
  393.                 edgeHandler.processEdge(reversedDirection, parseInt(args[1]));
  394.             }
  395.             break;
  396.             case \'setenabled\':
  397.             if (args.length < 2)
  398.             {
  399.                 console.log("Usage: EdgeHandler SetEnabled (true or false)");
  400.                 return;
  401.             }
  402.             else
  403.             {
  404.                 if (args[1].trim().toLowerCase() == "true")
  405.                 {
  406.                     $gameMap._edgesDisabled=false;
  407.                     console.log("Edge handling enabled");
  408.                 }
  409.                 else
  410.                 {
  411.                     $gameMap._edgesDisabled=true;
  412.                     console.log("Edge handling disabled");
  413.                 }
  414.             }
  415.             break;
  416.         }
  417.     }
  418.     _WS_EH_plugInCommand.call(this, command, args);
  419. };
  420.  
  421.  
  422. //=============================================================================
  423. // Game_Map
  424. //=============================================================================
  425.  
  426.  
  427.  
  428. var WS_EW_GM_move=Game_Map.prototype.moveAfterCommonEvent;
  429.  
  430. //=============================================================================
  431. // Make sure we don\'t keep trying to handle the edge once we\'ve done it
  432. //=============================================================================
  433. Game_Map.prototype.moveAfterCommonEvent = function() {
  434.     if ($gameTemp.destinationX() === $gamePlayer.x &&
  435.       $gameTemp.destinationY() === $gamePlayer.y) {
  436.         $gameTemp.clearDestination();
  437.     }
  438.     return WS_EW_GM_move.call(this);
  439. };
  440.  
  441. //=============================================================================
  442. // Game_Player
  443. //=============================================================================
  444.  
  445. //=============================================================================
  446. // See if are at an edge of the current map
  447. //=============================================================================
  448. Game_Player.prototype.processMapEdge = function() {
  449.    
  450.     if (this.x > 0 && this.y > 0 && this.x < $dataMap.width-1 &&
  451.     this.y < $dataMap.height-1)
  452.     {
  453.         return;
  454.     }
  455.     if ($gameMap._edgesDisabled)
  456.     {
  457.         return;
  458.     }
  459.    
  460.     if (!$edgeHandler.edgeHandler) {
  461.         $edgeHandler.edgeHandler=new Edge_Handler($dataMap.meta);
  462.     }
  463.    
  464.     if (this.x == 0)
  465.         direction=4;
  466.     if (this.y == 0)
  467.         direction=8;
  468.     if (this.x == $dataMap.width-1)
  469.         direction=6;
  470.     if (this.y == $dataMap.height-1)
  471.         direction=2;
  472.    
  473.     /* Don\'t do a thing if we are already handling it */
  474.     if ($edgeHandler.edgeLoadDirection) {
  475.         return;
  476.     }
  477.     $edgeHandler.edgeHandler.processEdge(direction, 0);
  478. };
  479.  
  480. WS_EdgeHandler.Game_Player_moveStraight =
  481.     Game_Player.prototype.moveStraight;
  482. Game_Player.prototype.moveStraight = function(d) {
  483.     WS_EdgeHandler.Game_Player_moveStraight.call(this, d);
  484.     this.processMapEdge();
  485. };
  486.  
  487. WS_EdgeHandler.Game_Player_moveDiagonally =
  488.     Game_Player.prototype.moveDiagonally;
  489. Game_Player.prototype.moveDiagonally = function(horz, vert) {
  490.     WS_EdgeHandler.Game_Player_moveDiagonally.call(this, horz, vert);
  491.     this.processMapEdge();
  492. };
  493.  
  494.  
  495. //=============================================================================
  496. // Game_Map
  497. //=============================================================================
  498. WS_EH_GM_setup=Game_Map.prototype.setup;
  499.  
  500. Game_Map.prototype.setup = function(map_id) {
  501.     $gameMap._edgesDisabled=false;
  502.     WS_EH_GM_setup.call(this, map_id);
  503.     if ($edgeHandler.edgeHandler) {
  504.         $edgeHandler.edgeHandler.finishSpecialCaseEdges(
  505.         $edgeHandler.edgeX, $edgeHandler.edgeY, $gameMap, $edgeHandler.edgeLoadDirection, $edgeHandler.edgeMapId);
  506.     }
  507. }
  508. })();
  509. //=============================================================================
  510. // End of File
  511. //=============================================================================
');