Advertisement
MkNish

[RMMV Plugin] Advanced Parallax

Nov 29th, 2015
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // μ'ki's Advanced Parallax v1.00
  3. // MK_AdvParallax.js
  4. //=============================================================================
  5.  
  6. var Imported = Imported || {};
  7. var Maki = Maki || {};
  8.  
  9. //=============================================================================
  10. /*:
  11.  * @plugindesc v1.00 Advanced Parallax
  12.  * @author μ'ki
  13.  *
  14.  * @help
  15.  * ============================================================================
  16.  * Introduction
  17.  * ============================================================================
  18.  *
  19.  * This plugin allows you to have more control on map parallaxes by defining
  20.  * them in the map notetag. It also allows you to define multiple layered
  21.  * parallaxes (either behind or in front of the tiles and characters), a bit
  22.  * like in RPG Maker XP games, which have panoramas and fogs. Additionally,
  23.  * you can make animated parallaxes (having more than one frame).
  24.  *
  25.  * Contact me for support/bug report:
  26.  * listra92[at]gmail[dot]com
  27.  *
  28.  * ============================================================================
  29.  * Map Notetag
  30.  * ============================================================================
  31.  *
  32.  * <parallax>
  33.  *   property: value
  34.  *   ...
  35.  * </parallax>
  36.  * Defines a parallax with properties as follows (optionally present, have
  37.  * their default value):
  38.  *  - name: parallax image name (in \img\parallaxes folder)
  39.  *  - loopx: whether to loop the parallax horizontally and let it scroll
  40.  *  - loopy: whether to loop the parallax vertically and let it scroll
  41.  *  - sx: the parallax horizontal scroll speed (a script to eval)
  42.  *  - sy: the parallax vertical scroll speed (a script to eval)
  43.  *  - x: the parallax initial x-position
  44.  *  - y: the parallax initial y-position
  45.  *  - alpha: the parallax opacity (0~255, a script to eval)
  46.  *  - depth: the parallax image depth (1: behind the map tiles,
  47.  *                                     2: just behind the map weather, a 'fog',
  48.  *                                     3: in front of the map weather, a 'fog')
  49.  *  - frames: number of the image animation frames
  50.  *  - framesp: the image animation speed (frames per sec., a script to eval)
  51.  * Note that you can define multiple tags for multiple parallaxes.
  52.  *
  53.  * Example:
  54.  * <parallax>
  55.  * name: StarlitSky
  56.  * loopx: true
  57.  * loopy: true
  58.  * sx: 4
  59.  * sy: $gameVariables.value(1)
  60.  * depth: 1
  61.  * frames: 4
  62.  * framesp: 2
  63.  * </parallax>
  64.  *
  65.  * ============================================================================
  66.  * Animated Parallaxes
  67.  * ============================================================================
  68.  *
  69.  * You can make parallaxes animated with more than one frame. For such that,
  70.  * put several files in \img\parallaxes folder, for example StarlitSky_1.png,
  71.  * StarlitSky_2.png, ... for each frame of the parallax StarlitSky defined in
  72.  * the map note. For non-animated ones, just put the file as usual, i.e.
  73.  * StarlitSky.png.
  74.  *
  75.  * ============================================================================
  76.  * Functions
  77.  * ============================================================================
  78.  *
  79.  * $gameMap.changeParallax(name, loopX, loopY, sx, sy[, idx, opacity, framesp]);
  80.  *
  81.  * Changes parallax properties with designated index idx (by order of the tag
  82.  * definition in the map note). Here sx, sy, opacity, framesp can be either
  83.  * number or eval string.
  84.  *
  85.  * ============================================================================
  86.  * Known Issues
  87.  * ============================================================================
  88.  *
  89.  *  - Somewhat huge memory usage, especially in dealing with animated parallaxes.
  90.  *
  91.  */
  92. //=============================================================================
  93.  
  94. //=============================================================================
  95. // Parameter Variables
  96. //=============================================================================
  97.  
  98. Maki.Parameters = PluginManager.parameters('MK_AdvParallax');
  99. Maki.Params = Maki.Params || {};
  100. Maki.Aliases = Maki.Aliases || {};
  101.  
  102. //-----------------------------------------------------------------------------
  103. // Game_Map
  104. //
  105. // The game object class for a map. It contains scrolling and passage
  106. // determination functions.
  107.  
  108. Maki.Aliases.GameMapInitialize = Game_Map.prototype.initialize;
  109. Game_Map.prototype.initialize = function() {
  110.     Maki.Aliases.GameMapInitialize.call(this);
  111.     this._sparallaxName = [];
  112.     this._sparallaxZero = [];
  113.     this._sparallaxLoopX = [];
  114.     this._sparallaxLoopY = [];
  115.     this._sparallaxSx = [];
  116.     this._sparallaxSy = [];
  117.     this._sparallaxSxf = [];
  118.     this._sparallaxSyf = [];
  119.     this._sparallaxX = [];
  120.     this._sparallaxY = [];
  121.     this._sparallaxOpacity = [];
  122.     this._sparallaxOpacityf = [];
  123.     this._sparallaxDepth = [];
  124.     this._sparallaxFrame = [];
  125.     this._sparallaxFrames = [];
  126.     this._sparallaxFrameSp = [];
  127.     this._sparallaxFrameSpf = [];
  128.     this._sparallaxFrameDel = [];
  129.     this._sparallaxNeedRef = [];
  130. };
  131.  
  132. Game_Map.prototype.parallaxName = function(idx) {
  133.     idx = idx || 0;
  134.     return this._sparallaxName[idx];
  135. };
  136.  
  137. Maki.Aliases.GameMapSetupParallax = Game_Map.prototype.setupParallax;
  138. Game_Map.prototype.setupParallax = function() {
  139.     Maki.Aliases.GameMapSetupParallax.call(this);
  140.     delete this._sparallaxName;
  141.     delete this._sparallaxZero;
  142.     delete this._sparallaxLoopX;
  143.     delete this._sparallaxLoopY;
  144.     delete this._sparallaxSx;
  145.     delete this._sparallaxSy;
  146.     delete this._sparallaxSxf;
  147.     delete this._sparallaxSyf;
  148.     delete this._sparallaxX;
  149.     delete this._sparallaxY;
  150.     delete this._sparallaxOpacity;
  151.     delete this._sparallaxOpacityf;
  152.     delete this._sparallaxDepth;
  153.     delete this._sparallaxFrame;
  154.     delete this._sparallaxFrames;
  155.     delete this._sparallaxFrameSp;
  156.     delete this._sparallaxFrameSpf;
  157.     delete this._sparallaxFrameDel;
  158.     delete this._sparallaxNeedRef;
  159.     this._sparallaxName = [];
  160.     this._sparallaxZero = [];
  161.     this._sparallaxLoopX = [];
  162.     this._sparallaxLoopY = [];
  163.     this._sparallaxSx = [];
  164.     this._sparallaxSy = [];
  165.     this._sparallaxSxf = [];
  166.     this._sparallaxSyf = [];
  167.     this._sparallaxX = [];
  168.     this._sparallaxY = [];
  169.     this._sparallaxOpacity = [];
  170.     this._sparallaxOpacityf = [];
  171.     this._sparallaxDepth = [];
  172.     this._sparallaxFrame = [];
  173.     this._sparallaxFrames = [];
  174.     this._sparallaxFrameSp = [];
  175.     this._sparallaxFrameSpf = [];
  176.     this._sparallaxFrameDel = [];
  177.     this._sparallaxNeedRef = [];
  178.     this.parseData();
  179. };
  180.  
  181. Game_Map.prototype.parseData = function() {
  182.     var note1 = /<(?:parallax)>/i;
  183.     var note2 = /<\/(?:parallax)>/i;
  184.     var notedata = $dataMap.note.split(/[\r\n]+/);
  185.     var parallaxPatternFlag = false;
  186.     $dataMap.parallaxPattern = [];
  187.     var l = notedata.length;
  188.     for (var i = 0; i < l; i++){
  189.         if (notedata[i].match(note1)) {
  190.             parallaxPatternFlag = true;
  191.         } else if (notedata[i].match(note2)) {
  192.             parallaxPatternFlag = false;
  193.             this._sparallaxName.push('');
  194.             this._sparallaxZero.push(ImageManager.isZeroParallax(
  195.                 this._sparallaxName[this._sparallaxName.length-1]));
  196.             this._sparallaxLoopX.push(false);
  197.             this._sparallaxLoopY.push(false);
  198.             this._sparallaxSx.push(0);
  199.             this._sparallaxSy.push(0);
  200.             this._sparallaxSxf.push('0');
  201.             this._sparallaxSyf.push('0');
  202.             this._sparallaxX.push(0);
  203.             this._sparallaxY.push(0);
  204.             this._sparallaxOpacity.push(255);
  205.             this._sparallaxOpacityf.push('255');
  206.             this._sparallaxDepth.push(1);
  207.             this._sparallaxFrame.push(0);
  208.             this._sparallaxFrames.push(1);
  209.             this._sparallaxFrameSp.push(1);
  210.             this._sparallaxFrameSpf.push('1');
  211.             this._sparallaxFrameDel.push(-1);
  212.             this._sparallaxNeedRef.push(true);
  213.             var ll = $dataMap.parallaxPattern.length;
  214.             for (var j = 0; j < ll; j++) {
  215.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:name):[ ](.*)/i)) {
  216.                     this._sparallaxName[this._sparallaxName.length-1] = String(RegExp.$1);
  217.                 } else
  218.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:loopx):[ ](.*)/i)) {
  219.             this._sparallaxLoopX[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  220.                 } else
  221.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:loopy):[ ](.*)/i)) {
  222.             this._sparallaxLoopY[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  223.                 } else
  224.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:sx):[ ](.*)/i)) {
  225.             this._sparallaxSxf[this._sparallaxName.length-1] = String(RegExp.$1);
  226.             this._sparallaxSx[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  227.                 } else
  228.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:sy):[ ](.*)/i)) {
  229.             this._sparallaxSyf[this._sparallaxName.length-1] = String(RegExp.$1);
  230.             this._sparallaxSy[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  231.                 } else
  232.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:x):[ ](.*)/i)) {
  233.             this._sparallaxX[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  234.                 } else
  235.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:y):[ ](.*)/i)) {
  236.             this._sparallaxY[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  237.                 } else
  238.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:alpha):[ ](.*)/i)) {
  239.             this._sparallaxOpacityf[this._sparallaxName.length-1] = String(RegExp.$1);
  240.             this._sparallaxOpacity[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  241.                 } else
  242.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:depth):[ ](.*)/i)) {
  243.             this._sparallaxDepth[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  244.                 } else
  245.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:frames):[ ](.*)/i)) {
  246.             this._sparallaxFrames[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  247.                 } else
  248.                 if ($dataMap.parallaxPattern[j].match(/[ ]*(?:framesp):[ ](.*)/i)) {
  249.             this._sparallaxFrameSpf[this._sparallaxName.length-1] = String(RegExp.$1);
  250.             this._sparallaxFrameSp[this._sparallaxName.length-1] = eval(String(RegExp.$1));
  251.             this._sparallaxFrameDel[this._sparallaxName.length-1] =
  252.                 Math.floor(60/this._sparallaxFrameSp[this._sparallaxName.length-1]);
  253.                 }
  254.             }
  255.             $dataMap.parallaxPattern = [];
  256.         } else if (parallaxPatternFlag) {
  257.             $dataMap.parallaxPattern.push(notedata[i]);
  258.         }
  259.     }
  260. };
  261.  
  262. Maki.Aliases.GameMapSetDisplayPos = Game_Map.prototype.setDisplayPos;
  263. Game_Map.prototype.setDisplayPos = function(x, y) {
  264.     Maki.Aliases.GameMapSetDisplayPos.call(this, x, y);
  265.     if (this.isLoopHorizontal()) {
  266.         for(var i = 0; i < this._sparallaxName.length; i++){
  267.             this._sparallaxX[i] = x;
  268.         };
  269.     } else {
  270.         for(var i = 0; i < this._sparallaxName.length; i++){
  271.             this._sparallaxX[i] = this._displayX;
  272.         };
  273.     }
  274.     if (this.isLoopVertical()) {
  275.         for(var i = 0; i < this._sparallaxName.length; i++){
  276.             this._sparallaxY[i] = y;
  277.         };
  278.     } else {
  279.         for(var i = 0; i < this._sparallaxName.length; i++){
  280.             this._sparallaxY[i] = this._displayY;
  281.         };
  282.     }
  283. };
  284.  
  285. Maki.Aliases.GameMapScrollDown = Game_Map.prototype.scrollDown;
  286. Game_Map.prototype.scrollDown = function(distance) {
  287.     Maki.Aliases.GameMapScrollDown.call(this, distance);
  288.     if (this.isLoopVertical()) {
  289.         var l = this._sparallaxName.length;
  290.         for (var i = 0; i < l; i++){
  291.             if (this._sparallaxLoopY[i]) this._sparallaxY[i] += distance;
  292.         };
  293.     } else if (this.height() >= this.screenTileY()) {
  294.         var l = this._sparallaxName.length;
  295.         for (var i = 0; i < l; i++){
  296.             this._sparallaxY[i] += 0;
  297.         };
  298.     }
  299. };
  300.  
  301. Maki.Aliases.GameMapScrollLeft = Game_Map.prototype.scrollLeft;
  302. Game_Map.prototype.scrollLeft = function(distance) {
  303.     Maki.Aliases.GameMapScrollLeft.call(this, distance);
  304.     if (this.isLoopHorizontal()) {
  305.         var l = this._sparallaxName.length;
  306.         for (var i = 0; i < l; i++){
  307.             if (this._sparallaxLoopX[i]) this._sparallaxX[i] -= distance;
  308.         };
  309.     } else if (this.width() >= this.screenTileX()) {
  310.         var l = this._sparallaxName.length;
  311.         for (var i = 0; i < l; i++){
  312.             this._sparallaxX[i] += 0;
  313.         };
  314.     }
  315. };
  316.  
  317. Maki.Aliases.GameMapScrollRight = Game_Map.prototype.scrollRight;
  318. Game_Map.prototype.scrollRight = function(distance) {
  319.     Maki.Aliases.GameMapScrollRight.call(this, distance);
  320.     if (this.isLoopHorizontal()) {
  321.         var l = this._sparallaxName.length;
  322.         for (var i = 0; i < l; i++){
  323.             if (this._sparallaxLoopX[i]) this._sparallaxX[i] += distance;
  324.         };
  325.     } else if (this.width() >= this.screenTileX()) {
  326.         var l = this._sparallaxName.length;
  327.         for (var i = 0; i < l; i++){
  328.             this._sparallaxX[i] += 0;
  329.         };
  330.     }
  331. };
  332.  
  333. Maki.Aliases.GameMapScrollUp = Game_Map.prototype.scrollUp;
  334. Game_Map.prototype.scrollUp = function(distance) {
  335.     Maki.Aliases.GameMapScrollUp.call(this, distance);
  336.     if (this.isLoopVertical()) {
  337.         var l = this._sparallaxName.length;
  338.         for (var i = 0; i < l; i++){
  339.             if (this._sparallaxLoopY[i]) this._sparallaxY[i] -= distance;
  340.         };
  341.     } else if (this.height() >= this.screenTileY()) {
  342.         var l = this._sparallaxName.length;
  343.         for (var i = 0; i < l; i++){
  344.             this._sparallaxY[i] += 0;
  345.         };
  346.     }
  347. };
  348.  
  349. Maki.Aliases.GameMapUpdateParallax = Game_Map.prototype.updateParallax;
  350. Game_Map.prototype.updateParallax = function() {
  351.     Maki.Aliases.GameMapUpdateParallax.call(this);
  352.     var l = this._sparallaxName.length;
  353.     for (var i = 0; i < l; i++){
  354.         if (this._sparallaxLoopX[i]){
  355.             this._sparallaxSx[i] = eval(this._sparallaxSxf[i]);
  356.             this._sparallaxX[i] += this._sparallaxSx[i] / this.tileWidth() / 2;
  357.         }
  358.         if (this._sparallaxLoopY[i]){
  359.             this._sparallaxSy[i] = eval(this._sparallaxSyf[i]);
  360.             this._sparallaxY[i] += this._sparallaxSy[i] / this.tileHeight() / 2;
  361.         }
  362.         this._sparallaxOpacity[i] = eval(this._sparallaxOpacityf[i]);
  363.         if (1 < this._sparallaxFrames[i]){
  364.             this._sparallaxFrameSp[i] = eval(this._sparallaxFrameSpf[i]);
  365.             this._sparallaxFrameDel[i] -= 1;
  366.             if (this._sparallaxFrameDel[i] === 0) {
  367.                 this._sparallaxFrameDel[i] = Math.floor(60/this._sparallaxFrameSp[i]);
  368.                 this._sparallaxFrame[i] = (this._sparallaxFrame[i]+1) %
  369.                     this._sparallaxFrames[i];
  370.                 this._sparallaxNeedRef[i] = true;
  371.             }
  372.         }
  373.     };
  374. };
  375.  
  376. Game_Map.prototype.parallaxOx = function(idx) {
  377.     idx = idx || 0;
  378.     if (this._sparallaxZero[idx]) {
  379.         return this._sparallaxX[idx] * this.tileWidth();
  380.     } else if (this._sparallaxLoopX[idx]) {
  381.         return this._sparallaxX[idx] * this.tileWidth() / 2;
  382.     } else {
  383.         return 0;
  384.     }
  385. };
  386.  
  387. Game_Map.prototype.parallaxOy = function(idx) {
  388.     idx = idx || 0;
  389.     if (this._sparallaxZero[idx]) {
  390.         return this._sparallaxY[idx] * this.tileHeight();
  391.     } else if (this._sparallaxLoopY[idx]) {
  392.         return this._sparallaxY[idx] * this.tileHeight() / 2;
  393.     } else {
  394.         return 0;
  395.     }
  396. };
  397.  
  398. Maki.Aliases.GameMapChangeParallax = Game_Map.prototype.changeParallax;
  399. Game_Map.prototype.changeParallax = function(name, loopX, loopY, sx, sy,
  400.     idx, opacity, framesp) {
  401.     Maki.Aliases.GameMapChangeParallax.call(this, name, loopX, loopY, sx, sy);
  402.     idx = idx || 0;
  403.     opacity = opacity || 255;
  404.     framesp = framesp || 1;
  405.     this._sparallaxName[idx] = name;
  406.     this._sparallaxZero[idx] = ImageManager.isZeroParallax(this._sparallaxName[idx]);
  407.     if (this._sparallaxLoopX[idx] && !loopX) {
  408.         this._sparallaxX[idx] = 0;
  409.     }
  410.     if (this._sparallaxLoopY[idx] && !loopY) {
  411.         this._sparallaxY[idx] = 0;
  412.     }
  413.     this._sparallaxLoopX[idx] = loopX;
  414.     this._sparallaxLoopY[idx] = loopY;
  415.     this._sparallaxSxf[idx] = String(sx);
  416.     this._sparallaxSyf[idx] = String(sy);
  417.     this._sparallaxSx[idx] = eval(this._sparallaxSxf[idx]);
  418.     this._sparallaxSy[idx] = eval(this._sparallaxSyf[idx]);
  419.     this._sparallaxOpacityf[i] = String(opacity);
  420.     this._sparallaxOpacity[i] = eval(this._sparallaxOpacityf[idx]);
  421. };
  422.  
  423. Spriteset_Map.prototype.createLowerLayer = function() {
  424.     Spriteset_Base.prototype.createLowerLayer.call(this);
  425.     this._sparallaxName = [];
  426.     this._sparallaxIdx = [];
  427.     this._sparallaxx = [];
  428.     this._parallax = new TilingSprite();
  429.     this.createParallax(1);
  430.     this.createTilemap();
  431.     this.createCharacters();
  432.     this.createShadow();
  433.     this.createDestination();
  434.     this.createParallax(2);
  435.     this.createWeather();
  436.     this.createParallax(3);
  437. };
  438.  
  439. Spriteset_Map.prototype.createParallax = function(depth) {
  440.     depth = depth || 1;
  441.     var l = $gameMap._sparallaxName.length;
  442.     for(var i = 0; i < l; i++){
  443.         if ($gameMap._sparallaxDepth[i] === depth) {
  444.             this._sparallaxName.push('');
  445.             this._sparallaxIdx.push(i);
  446.             this._sparallaxx.push([]);
  447.             var f = this._sparallaxx.length-1;
  448.             for(var j = 0; j < $gameMap._sparallaxFrames[i]; j++){
  449.                 this._sparallaxx[f].push(new TilingSprite());
  450.                 this._sparallaxx[f][j].move(
  451.                     0, 0, Graphics.width, Graphics.height);
  452.                 this._baseSprite.addChild(this._sparallaxx[f][j]);
  453.             };
  454.         }
  455.     };
  456. };
  457.  
  458. Spriteset_Map.prototype.updateParallax = function() {
  459.     var l = this._sparallaxName.length;
  460.     for(var i = 0; i < l; i++){
  461.     if (this._sparallaxName[i] !== $gameMap.parallaxName(this._sparallaxIdx[i])) {
  462.         this._sparallaxName[i] = $gameMap.parallaxName(this._sparallaxIdx[i]);
  463.         if (1 < $gameMap._sparallaxFrames[i]){
  464.             for(var j = 0; j < $gameMap._sparallaxFrames[i]; j++){
  465.                 this._sparallaxx[i][j].bitmap = ImageManager.loadParallax(
  466.                     $gameMap._sparallaxName[this._sparallaxIdx[i]]+'_'+j);
  467.             };
  468.         }
  469.         if ($gameMap._sparallaxFrames[i] === 1)
  470.         this._sparallaxx[i][0].bitmap = ImageManager.loadParallax(
  471.             $gameMap._sparallaxName[this._sparallaxIdx[i]]);
  472.     }
  473.     var f = $gameMap._sparallaxFrame[i];
  474.     if (this._sparallaxx[i][f].bitmap) {
  475.         if (1 < $gameMap._sparallaxFrames[i] &&
  476.             $gameMap._sparallaxNeedRef[this._sparallaxIdx[i]]){
  477.             for(var j = 0; j < $gameMap._sparallaxFrames[i]; j++){
  478.                 this._sparallaxx[i][j].visible = (f === j);
  479.             };
  480.             $gameMap._sparallaxNeedRef[this._sparallaxIdx[i]] = false;
  481.         }
  482.         this._sparallaxx[i][f].origin.x = $gameMap.parallaxOx(this._sparallaxIdx[i]);
  483.         this._sparallaxx[i][f].origin.y = $gameMap.parallaxOy(this._sparallaxIdx[i]);
  484.         this._sparallaxx[i][f].opacity =
  485.             $gameMap._sparallaxOpacity[this._sparallaxIdx[i]];
  486.     }
  487.     };
  488. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement