Advertisement
jerry2810

MapScroll

Nov 4th, 2015
4,955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // MapScroll.js
  3. //=============================================================================
  4. /*:
  5.  *Version 2.0
  6.  *Change log:
  7.  *11/5/2015:Scroll scales with window width/height.
  8.  *          Added plugin commands to disable/enagle scrolling in game.
  9.  *11/5/2015: v2.0 Auto grid snap more commands and notetags.
  10.  *
  11.  *
  12.  * @plugindesc Scrolls the map when you get to the edge.
  13.  * @author Jeremy Cannady
  14.  *
  15.  * @help Scrolls the map when you get to the edge, normal maps are 13 by 17
  16.  *  
  17.  *Plugin commands:
  18.  *  Map_Scroll_Disable ----> This will disable and revert to normal scrolling.
  19.  *  Map_Scroll_Enable  ----> This will enable scrolling.
  20.  *  Grid_Snap_Disable  ---> Disables grid auto snap.
  21.  *  Grid_Snap_Enable   ---> Enables grid auto snap (default)
  22.  *
  23.  *Note Tags:
  24.  *<ScrollDisable> if put in the map note tags then scrolling will be back to normal instead of grid scrolling.
  25.  *
  26. */
  27.  
  28. (function(){
  29. //Define a variable to enable/disable the scrolling
  30. Game_Player.prototype._Map_Scroll_Enabled = true;
  31. //Default grid snap
  32. Game_Player.prototype.gridSnap = true;
  33.  
  34. //Superseding the updateScroll function. Don't scroll until we are on the edge.
  35. Game_Player.prototype.updateScroll = function(lastScrolledX, lastScrolledY) {
  36.     //If we have scroll diabled for this map then set scroll to false.
  37.     if($dataMap.meta.ScrollDisable){
  38.         Game_Player.prototype.Map_Scroll_for_this_Map = false;
  39.     }else{
  40.         Game_Player.prototype.Map_Scroll_for_this_Map = true;
  41.     };
  42.    
  43.     var x1 = lastScrolledX;
  44.     var y1 = lastScrolledY;
  45.     var x2 = this.scrolledX();
  46.     var y2 = this.scrolledY();
  47.     var xScroll = Math.round(SceneManager._boxWidth/48);
  48.     var yScroll = Math.round(SceneManager._boxHeight/48);
  49.  
  50.     if(Game_Player.prototype._Map_Scroll_Enabled && Game_Player.prototype.Map_Scroll_for_this_Map){
  51.         //Checks to see if we move beyond the screen if we do then it scrolls it for us.
  52.         if (x2 > xScroll-1) {$gameMap.startScroll(6,xScroll,6);};//Left
  53.         if (x2 < -0.5) {$gameMap.startScroll(4,xScroll,6);};//Right
  54.         if (y2 > yScroll-1) {$gameMap.startScroll(2,yScroll,6);};//Down
  55.         if (y2 < -0.5) {$gameMap.startScroll(8,yScroll,6);};//Up   
  56.     }else{//If we have it disabled then run the original code
  57.         if (y2 > y1 && y2 > this.centerY()) {$gameMap.scrollDown(y2 - y1);};
  58.         if (x2 < x1 && x2 < this.centerX()) {$gameMap.scrollLeft(x1 - x2);};
  59.         if (x2 > x1 && x2 > this.centerX()) {$gameMap.scrollRight(x2 - x1);};
  60.         if (y2 < y1 && y2 < this.centerY()) {$gameMap.scrollUp(y1 - y2);}; 
  61.     };
  62. };//End of update scroll.
  63.  
  64. //Superseding the canMove function. Check if we are scrolling the screen.
  65. Game_Player.prototype.canMove = function() {
  66.     if ($gameMap.isEventRunning() || $gameMessage.isBusy()) {return false;}
  67.     if (this.isMoveRouteForcing() || this.areFollowersGathering()) {return false;}
  68.     if (this._vehicleGettingOn || this._vehicleGettingOff) {return false;}
  69.     if (this.isInVehicle() && !this.vehicle().canMove()) {return false;}
  70.     if (Game_Player.prototype._Map_Scroll_Enabled && $gameMap.isScrolling()){return false;};   
  71.     return true;
  72. };//End of canMove.
  73.  
  74. var alias_Game_Map_setDisplayPos = Game_Map.prototype.setDisplayPos;
  75. Game_Map.prototype.setDisplayPos = function(x, y) {
  76.     //If the grid snap is disabled then run the original code;
  77.     if(Game_Player.prototype.gridSnap === false){
  78.         alias_Game_Map_setDisplayPos.call(this,x,y);   
  79.     }else{//If grid snap is enabled then snap the camera to the correct position.
  80.         var xScroll = Math.floor(SceneManager._boxWidth / 48);
  81.         var yScroll = Math.floor(SceneManager._boxHeight / 48);
  82.         var newX = $gamePlayer._newX;
  83.         var newY = $gamePlayer._newY;
  84.         var gridX = Math.floor(newX / xScroll)
  85.         var gridY = Math.floor(newY / yScroll)
  86.         this._displayX = gridX * xScroll;
  87.         this._displayY = gridY * yScroll;
  88.         this._parallaxX = this._displayX;
  89.         this._parallaxY = this._displayY;
  90.     };
  91. };//End of set display position.
  92.  
  93. //Define the pugin command for disable.
  94. var Map_Scroll_Disable_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  95. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  96.     Map_Scroll_Disable_pluginCommand.call(this, command, args);
  97.     if (command === "Map_Scroll_Disable") {
  98.         //If we type in Map_Scroll_Disable then disable scrolling.
  99.         Game_Player.prototype._Map_Scroll_Enabled = false;
  100.     };
  101. };
  102.  
  103. //Define the pugin command for enable.
  104. var Map_Scroll_Enable_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  105. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  106.     Map_Scroll_Enable_pluginCommand.call(this, command, args);
  107.     if (command === "Map_Scroll_Enable") {
  108.         //If we type in Map_Scroll_Enable then enable scrolling.
  109.         Game_Player.prototype._Map_Scroll_Enabled = true;
  110.     };
  111. };
  112.  
  113. var Grid_Snap_Disable_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  114. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  115.     Grid_Snap_Disable_pluginCommand.call(this, command, args);
  116.     if (command === "Grid_Snap_Disable") {
  117.         //If we type in Grid_Snap_Disable then disable grid snap.
  118.         Game_Player.prototype.gridSnap = false;
  119.     };
  120. };
  121.  
  122. var Grid_Snap_Enable_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  123. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  124.     Grid_Snap_Enable_pluginCommand.call(this, command, args);
  125.     if (command === "Grid_Snap_Enable") {
  126.         //If we type in Grid_Snap_Enable then enable grip snap.
  127.         Game_Player.prototype.gridSnap = true;
  128.     };
  129. };
  130.  
  131. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement