Advertisement
nio_kasgami

Rhyme_MapSmoothScrolling

Mar 14th, 2016
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2. * @plugindesc this plugin permit to smooth scroll in map
  3. * @author rhyme (converted by Nio kasgami and Quasi)
  4. * @param SpeedDivider
  5. * @desc Will setup how much the map smooth when scrolling more it's high more it's smooth
  6. * @default 8
  7. * @help
  8. * ==============================================================================
  9. *  * for divider
  10. * ------------------------------------------------------------------------------
  11. * $gameMap.setDivider(number); // will set the divider speed in game
  12. * $gameMap.resetDivider(); // will reset the divider to his default value (8)
  13. * ==============================================================================
  14. *
  15.  * =============================================================================
  16.  * ** How to use Quasi Scroll
  17.  * =============================================================================
  18.  * Quasi scrolling will let the screen scroll at any angle and can do the
  19.  * math for you can find the scroll distance to a certain event or player.
  20.  *
  21.  *   Diagonal Scrolls <Script Call>
  22.  *       $gameMap.startQuasiScroll(distanceX, distanceY, speed);
  23.  *     Set distanceX and distanceY to the value you want to scroll.
  24.  *      - This values are in grid terms.
  25.  *     Set speed to the scrolling speed.
  26.  *      - 1 - 6 same as from the Scroll Map Event command.
  27.  *
  28.  *   Scroll Towards Characters <Script Call>
  29.  *       $gameMap.scrollTowards(ID, speed);
  30.  *     Set ID to the ID of the event you want to scroll towards.
  31.  *      - If ID is set to 0, then it will scroll to the player.
  32.  *     Set speed to the scroll speed.
  33.  * =============================================================================
  34.  * ** How to use Quasi Scroll with frames instead of default speed settings
  35.  * =============================================================================
  36.  * Using the scroll with a frame duration instead of speed can be helpful with
  37.  * timing / making cutscenes since the screen can scroll at any speed you want
  38.  * it.
  39.  *
  40.  *   Diagonal Scrolls <Script Call>
  41.  *       $gameMap.startQuasiScroll(distanceX, distanceY, duration, true);
  42.  *     Set distanceX and distanceY to the value you want to scroll.
  43.  *      - This values are in grid terms.
  44.  *     Set duration to the amount of frames this scroll will take
  45.  *     !IMPORTANT! Leave the last value as true! This sets it appart from
  46.  *     the other .startQuasiScroll()!
  47.  *
  48.  *   Scroll Towards Characters <Script Call>
  49.  *       $gameMap.scrollTowards(ID, duration, true);
  50.  *     Set ID to the ID of the event you want to scroll towards.
  51.  *      - If ID is set to 0, then it will scroll to the player.
  52.  *     Set duration to the amount of frames this scroll will take
  53.  *     !IMPORTANT! Leave the last value as true! This sets it appart from
  54.  *     the other .scrollTowards()!
  55.  * =============================================================================
  56. *
  57. */
  58.  
  59. var Alone = Alone || {};
  60.  Alone.Alias = Alone.Alias || {};
  61.  
  62.  Alone.Parameters = PluginManager.parameters('Rhyme_MapSmoothScrolling');
  63.  Alone.Param = Alone.Param || {};
  64.  
  65.  Alone.Param.Divider = Number(Alone.Parameters['SpeedDivider']);
  66.  
  67. //==============================================================================
  68. // ■ Game_Map
  69. //------------------------------------------------------------------------------
  70. // The game object class for a map. It contains scrolling and passage
  71. // determination functions.
  72. //==============================================================================
  73.  
  74.   Game_Map.prototype._Divider = Alone.Param.Divider;
  75.  
  76.   Alone.Alias.A01 = Game_Map.prototype.initialize;
  77.   Game_Map.prototype.initialize = function(){
  78.     Alone.Alias.A01.call(this);
  79.     this._tDisplayX = this._displayX;
  80.     this._tDisplayY = this._displayY;
  81.   };
  82.  
  83.   Game_Map.prototype.setDivider = function(speed){
  84.     this._Divider = speed;
  85.   };
  86.  
  87.   Game_Map.prototype.resetDivider = function(){
  88.     this._Divider = Alone.Param.Divider;
  89.   };
  90.  
  91.   Game_Map.prototype.setDisplayPos = function(x, y){
  92.     if(!this.isLoopHorizontal()){
  93.         x = x.clamp(0,this.width() - this.screenTileX());
  94.     }
  95.     if(!this.isLoopVertical()){
  96.         y = y.clamp(0, this.width() - this.screenTileY());
  97.     }
  98.     this._displayX = (x + this.width()) % this.width();
  99.     this._displayY = (y + this.height()) % this.height();
  100.     this._tDisplayX = this._displayX;
  101.     this._tDisplayY = this._displayY;
  102.     this._parallaxX = x;
  103.     this._parallaxY = y;
  104.   };
  105.  
  106.   Game_Map.prototype.scrollDown = function(distance){
  107.     if(this.isLoopVertical()){
  108.         this._tDisplayY += distance;
  109.         this._tDisplayY %= $dataMap.height;
  110.         if(this._parallaxLoopY){
  111.             this._parallaxY += distance;
  112.         }
  113.     } else {
  114.        var lastY = this._tDisplayY;
  115.        this._tDisplayY = Math.min(this._tDisplayY + distance, this.height() - this.screenTileY());
  116.        this._parallaxY += this._tDisplayY - lastY;
  117.     }
  118.   };
  119.  
  120.   Game_Map.prototype.scrollLeft = function(distance){
  121.     if(this.isLoopHorizontal()){
  122.         this._tDisplayX += $dataMap.width - distance;
  123.         this._tDisplayX %= $dataMap.width;
  124.         if(this._parallaxLoopX){
  125.             this._parallaxX -= distance;
  126.         }
  127.     } else if (this.width() >= this.screenTileX()) {
  128.         var lastX = this._tDisplayX;
  129.         this._tDisplayX = Math.max(this._tDisplayX - distance,  0);
  130.     }
  131.   };
  132.  
  133.   Game_Map.prototype.scrollRight = function(distance){
  134.     if(this.isLoopHorizontal()){
  135.         this._tDisplayX += distance;
  136.         this._tDisplayX %= $dataMap.width;
  137.         if(this._parallaxLoopX){
  138.             this._parallaxX += distance;
  139.         }
  140.     } else {
  141.         var lastX = this._tDisplayX;
  142.         this._tDisplayX = Math.min(this._tDisplayX + distance, (this.width() - this.screenTileX()));
  143.         this._parallaxX += this._tDisplayX - lastX;
  144.     }
  145.   };
  146.  
  147.   Game_Map.prototype.scrollUp = function(distance){
  148.     if(this.isLoopVertical()){
  149.         this._tDisplayY += $dataMap.height - distance;
  150.         this._tDisplayY %= $dataMap.height;
  151.         if(this._parallaxLoopY){
  152.             this._parallaxY  -= distance;
  153.         }
  154.     } else {
  155.         var lastY = this._tDisplayY;
  156.         this._tDisplayY = Math.max(this._tDisplayY - distance, 0);
  157.         this._parallaxY += this._tDisplayY - lastY;
  158.     }
  159.   };
  160.  
  161.   Game_Map.prototype.updateScroll = function(){
  162.     if(this._displayX != this._tDisplayX) {
  163.         var xSpeed = Math.abs(this._displayX - this._tDisplayX) / this._Divider;
  164.         if(this._displayX > this._tDisplayX) {
  165.             this._displayX = Math.max(this._displayX - xSpeed, this._tDisplayX);
  166.         } else if (this._displayX < this._tDisplayX){
  167.             this._displayX = Math.min(this._displayX + xSpeed, this._tDisplayX);
  168.         }
  169.     }
  170.     if(this._displayY != this._tDisplayY){
  171.         var ySpeed = Math.abs(this._displayY - this._tDisplayY) / this._Divider;
  172.         if(this._displayY > this._tDisplayY) {
  173.             this._displayY = Math.max(this._displayY - ySpeed, this._tDisplayY);
  174.         } else if (this._displayY < this._tDisplayY){
  175.             this._displayY = Math.min(this._displayY + ySpeed, this._tDisplayY);
  176.         }
  177.     }
  178.     if(this.isScrolling()){
  179.         var lastX = this._tDisplayX;
  180.         var lastY = this._tDisplayY;
  181.       // code from Quasi coding
  182.       if (this._scrollDirection.constructor === Array) {
  183.         this.doQuasiScroll(this._scrollDirection[0], this._scrollDirection[1], this.scrollDistanceX(), this.scrollDistanceY());
  184.       } else {
  185.         this.doScroll(this._scrollDirection, this.scrollDistance());
  186.       }
  187.  
  188.         if(this._tDisplayX === lastX && this._tDisplayY === lastY){
  189.             this._scrollRest = 0;
  190.         } else {
  191.             this._scrollRest -= this.scrollDistance();
  192.         }
  193.     }
  194.   };
  195.  
  196.   Alone.Alias.A02 = Game_Map.prototype.scrollDistance;
  197.   Game_Map.prototype.scrollDistance = function() {
  198.     if (this._scrollFrames) {
  199.       return Math.abs(this._scrollDistance / this._scrollSpeed);
  200.     }
  201.     return Alone.Alias.A02.call(this);
  202.   }
  203.  
  204.   Game_Map.prototype.scrollDistanceX = function() {
  205.     if (this._scrollFrames) {
  206.       return Math.abs((this._scrollDistance * Math.cos(this._scrollRad)) / this._scrollSpeed);
  207.     }
  208.     return Math.abs(this.scrollDistance() * Math.cos(this._scrollRad));
  209.   };
  210.  
  211.   Game_Map.prototype.scrollDistanceY = function() {
  212.     if (this._scrollFrames) {
  213.       return Math.abs((this._scrollDistance * Math.sin(this._scrollRad)) / this._scrollSpeed);
  214.     }
  215.     return Math.abs(this.scrollDistance() * Math.sin(this._scrollRad));
  216.   };
  217.  
  218.   Game_Map.prototype.doQuasiScroll = function(directionX, directionY, distanceX, distanceY) {
  219.     if (directionX === 4) {
  220.       this.scrollLeft(distanceX);
  221.     } else if (directionX === 6) {
  222.       this.scrollRight(distanceX);
  223.     }
  224.     if (directionY === 2) {
  225.       this.scrollDown(distanceY);
  226.     } else if (directionY === 8) {
  227.       this.scrollUp(distanceY);
  228.     }
  229.   };
  230.  
  231.   Game_Map.prototype.startQuasiScroll = function(distanceX, distanceY, speed, frames) {
  232.     if (!this.isLoopHorizontal()) {
  233.       if (this._tDisplayX + distanceX < 0) {
  234.         distanceX = -this._tDisplayX;
  235.       }
  236.       if (this._tDisplayX + distanceX > this.width() - this.screenTileX()) {
  237.         distanceX = this.width() - this.screenTileX() - this._tDisplayX;
  238.       }
  239.     }
  240.     if (!this.isLoopVertical()) {
  241.       if (this._tDisplayY + distanceY < 0) {
  242.         distanceY = -this._tDisplayY;
  243.       }
  244.       if (this._tDisplayY + distanceY > this.height() - this.screenTileY()) {
  245.         distanceY = this.height() - this.screenTileY() - this._tDisplayY;
  246.       }
  247.     }
  248.     var directionX = distanceX > 0 ? 6 : distanceX < 0 ? 4 : 0;
  249.     var directionY = distanceY > 0 ? 2 : distanceY < 0 ? 8 : 0;
  250.     this._scrollDirection = [directionX, directionY];
  251.     this._scrollRest      = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
  252.     this._scrollDistance  = this._scrollRest;
  253.     this._scrollSpeed     = speed;
  254.     this._scrollFrames    = frames;
  255.     this._scrollRad       = Math.atan2(-distanceY, distanceX);
  256.   };
  257.  
  258.   Game_Map.prototype.scrollTowards = function(chara, speed, frames) {
  259.     var chara = chara === 0 ? $gamePlayer : $gameMap.event(chara);
  260.     var centerX = this._tDisplayX + this.screenTileX() / 2;
  261.     var centerY = this._tDisplayY + this.screenTileY() / 2;
  262.     if (!this.isLoopHorizontal()) {
  263.       if (centerX < this.screenTileX() / 2) {
  264.         centerX = this.screenTileX() / 2;
  265.       }
  266.       if (centerX > this.width() - this.screenTileX() / 2) {
  267.         centerX = this.width() - this.screenTileX() / 2;
  268.       }
  269.     }
  270.     if (!this.isLoopVertical()) {
  271.       if (centerY < this.screenTileY() / 2) {
  272.         centerY = this.screenTileY() / 2;
  273.       }
  274.       if (centerY > this.height() - this.screenTileY() / 2) {
  275.         centerY = this.height() - this.screenTileY() / 2;
  276.       }
  277.     }
  278.     var distanceX = (chara.x + 0.5) - centerX;
  279.     var distanceY = (chara.y + 0.5) - centerY;
  280.     this.startQuasiScroll(distanceX, distanceY, speed || 4, frames);
  281.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement