Zalerinian

ZE - Free Jump

Nov 29th, 2015
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Plugin: ZE - Free Jump
  3. // Version: 1.0.0
  4. // Date: November 29th, 2015.
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows the player to jump at anytime on the map, and can be used to allow the player to jump over certain tiles. <pluginID ZE - Free Jump>
  9.  * @author Zalerinian
  10.  *
  11.  * @param Jump Key
  12.  * @desc Press this key to jump. Default: ok
  13.  * @default ok
  14.  *
  15.  * @param Check Terrain Tags
  16.  * @desc While jumping, should Terrain Tags be checked? Default: false
  17.  * @default false
  18.  *
  19.  * @param Default Terrain Tag
  20.  * @desc If the above is false, this number will be used as the default terrain tag. Default: 0
  21.  * @default 0
  22.  *
  23.  * @param Check Region IDs
  24.  * @desc While jumping, should Region IDs be checked? Default: false
  25.  * @default false
  26.  *
  27.  * @param Default Region ID
  28.  * @desc If the above is false, this will be used as the default region. Default: 0
  29.  * @default 0
  30.  *
  31.  * @param Passable Terrain Tag
  32.  * @desc If the terrain tag below the player is this value, then the player can jump over this tile. See help. Default: -1
  33.  * @default -1
  34.  *
  35.  * @param Passable Region ID
  36.  * @desc If the region ID below the player is this value, then the player can jump over this tile. See help. Default: -1
  37.  * @default -1
  38.  *
  39.  * @param Dash Jump Threshold
  40.  * @desc If we were dashing for this many frames or more, then consider the jump a dash jump. Default: 6
  41.  * @default 6
  42.  *
  43.  * @param Regular Peak Max
  44.  * @desc The peak of the jump should be less than this value. Default: 8
  45.  * @default 8
  46.  *
  47.  * @param Dash Peak Max
  48.  * @desc The peak max when dashing. Default: 10
  49.  * @default 10
  50.  *
  51.  * @param Min Peak
  52.  * @desc The minimum jump peak. Jumps will be this height at the least. Default: 3
  53.  * @default 3
  54.  *
  55.  * @param Count Decrease
  56.  * @desc How much should be subtracted from the amount of time left to jump? Default: 0.8
  57.  * @default 0.8
  58.  *
  59.  * @param Count Increase
  60.  * @desc The amount of time in the jump to add per increase to the peak. Default: 2
  61.  * @default 2
  62.  *
  63.  * @param X Increment
  64.  * @desc How much should we increase the player's X value per update? Default: 0.125
  65.  * @default 0.125
  66.  *
  67.  * @param Y Increment
  68.  * @desc How much should we increase the player's Y value per update? Default 0.125
  69.  * @default 0.125
  70.  *
  71.  * @param Grid Snap
  72.  * @desc See help text. If true, the player is snapped back into the grid at the end of a jump. Default: true
  73.  * @default true
  74.  *
  75.  * @param Tile Width
  76.  * @desc The width of a tile. See help text.
  77.  * @default 1
  78.  *
  79.  * @param Tile Height
  80.  * @desc The height of a tile. See help text.
  81.  * @default 1
  82.  *
  83.  * @param Enable Diagonal
  84.  * @desc Enable diagonal movement during a jump. Default: true
  85.  * @default true
  86.  *
  87.  * @help
  88.  * Movement Count
  89.  * ------------------------------------------------------------------------------
  90.  * This is added to an internal counter every time the jump is updated. Once the
  91.  * counter is at least 1, the player will be moved. Movement will work such that
  92.  * the player is moved as many times as the counter is whole number.
  93.  *
  94.  * For example, if the counter becomes 2.25 at one point, the player will be
  95.  * moved 2 times that frame, and may appear to move really quickly at once.
  96.  *
  97.  *
  98.  * Tile Width/Height
  99.  * ------------------------------------------------------------------------------
  100.  * When jumping using decimal values, it's possible that the player may get stuck
  101.  * oddly on the grid. These values are used to figure out how we should force the
  102.  * player back onto the grid. Smaller decimals mean that tiles are split into
  103.  * pieces. For example, 0.25 implies that when we move, we're moving 25% between
  104.  * one tile and the next (meaning, internally, out X/Y position may be decimals
  105.  * that are multiples of .25, such as 1.25 or 7.75).
  106.  *
  107.  * These values only have an affect if Grid Snap is enabled.
  108.  *
  109.  *
  110.  * Grid Snap
  111.  * ------------------------------------------------------------------------------
  112.  * If this setting is enabled, once a jump has ended, we'll snap the player to
  113.  * the tile-grid, since we don't necessarily limit jump movement by tile sizes,
  114.  * and instead by pieces of tiles.
  115.  *
  116.  *
  117.  * Diagonal Movement
  118.  * ------------------------------------------------------------------------------
  119.  * If diagonal movement is turned off, the player can only jump in the basic four
  120.  * directions. The direction is determined by which key was last pressed, not
  121.  * necessarily by the direction the character is facing.
  122.  *
  123.  *
  124.  * Passable Terrain Tag / Region ID
  125.  * ------------------------------------------------------------------------------
  126.  * If these values are out of range of what the editor will allow you to input,
  127.  * then this feature will be disabled. As neither terrain tags, nor region IDs
  128.  * can be negative, the default value is -1, which disables this feature.
  129.  *
  130.  * In order to properly read terrain tags/region IDs on the maps, the player's
  131.  * position is rounded using Math.round.
  132.  *
  133.  * In order to define multiple terrain tags or region IDs, simply separate each
  134.  * number with a comma.
  135.  * For example: -1, 2, 90, 4 for Region IDs will allow the player to jump over
  136.  * any tile with the region ID -1, 2, 90, or 4. Please note that -1 is not a
  137.  * valid region ID. Having -1 in an ID list will not disable the feature.
  138.  * Disabling the feature is done by supplying a list of IDs that can never occur,
  139.  * like -1.
  140.  */
  141.  
  142. var Input = Input || {};
  143.  
  144. var Zale = Zale || {};
  145. Zale.FreeJump = {};
  146.  
  147. (function() {
  148.   if(Imported["MVCommons"] && PluginManager.version("MVCommons", ">=", "1.4.0")) {
  149.     var author = [{
  150.       email: "[email protected]",
  151.       name: "Zalerinian",
  152.       website: "http://www.razelon.com"
  153.       }];
  154.     var v = PluginManager.register("ZE - Free Jump", "1.0.0", PluginManager.getBasicPlugin("ZE - Free Jump").description, author, "2015-11-18");
  155.     if (v === false){
  156.       PluginManager.printPlugin("ZE - Free Jump")
  157.       throw new Error("Unable to load ZE - Free Jump due to registration failure! Is there another version running?");
  158.     }
  159.   } else {
  160.     throw new Error("Error: ZE - Free Jump requires MVCommons 1.4.0 or higher!")
  161.   }
  162. })();
  163.  
  164. (function(){
  165.   var params                  = PluginManager.parameters("ZE - Free Jump");
  166.   Zale.FreeJump.PARAMS        = params;
  167.   Zale.FreeJump.JUMPKEY       = params["Jump Key"];
  168.   Zale.FreeJump.CHECKTAG      = MVC.Boolean(params["Check Terrain Tags"]);
  169.   Zale.FreeJump.CHECKREGION   = MVC.Boolean(params["Check Region IDs"]);
  170.   Zale.FreeJump.DEFAULTTAG    = Number(params["Default Terrain Tag"]);
  171.   Zale.FreeJump.DEFAULTREGION = Number(params["Default Region ID"]);
  172.   Zale.FreeJump.IGNORETAG     = params["Passable Terrain Tag"].split(",").map(function(v) { return Number(v); } );
  173.   Zale.FreeJump.IGNOREREGION  = params["Passable Region ID"  ].split(",").map(function(v) { return Number(v); } );
  174.   Zale.FreeJump.DASHTHRESHOLD = Number(params["Dash Jump Threshold"]);
  175.   Zale.FreeJump.PEAKMAX       = Number(params["Regular Peak Max"]);
  176.   Zale.FreeJump.DASHPEAKMAX   = Number(params["Dash Peak Max"]);
  177.   Zale.FreeJump.PEAKMIN       = Number(params["Min Peak"]);
  178.   Zale.FreeJump.COUNTDECREASE = Number(params["Count Decrease"]);
  179.   Zale.FreeJump.COUNTINCREASE = Number(params["Count Increase"]);
  180.   Zale.FreeJump.XINC          = Number(params["X Increment"]);
  181.   Zale.FreeJump.YINC          = Number(params["Y Increment"]);
  182.   Zale.FreeJump.GRIDSNAP      = MVC.Boolean(params["Grid Snap"]);
  183.   Zale.FreeJump.TILEWIDTH     = Number(params["Tile Width"]);
  184.   Zale.FreeJump.TILEHEIGHT    = Number(params["Tile Height"]);
  185.   Zale.FreeJump.DIAGONAL      = MVC.Boolean(params["Enable Diagonal"]);
  186.  
  187.   /*
  188.    * Game_Player.prototype.initialize()
  189.    * @note
  190.    * Alias the initialize function to add in variable defaults for
  191.    * free jumping.
  192.    */
  193.   Zale.FreeJump.GP_init_IOB43bqzog = Game_CharacterBase.prototype.initialize;
  194.   Game_Player.prototype.initialize = function() {
  195.     Zale.FreeJump.GP_init_IOB43bqzog.call(this);
  196.     this._endJumpInput = false;
  197.     this._freeJump     = false;
  198.     this._dashJump     = false;
  199.     this._dashCount    = 0;
  200.     this._jumpDir      = 0;
  201.   }
  202.  
  203.   /*
  204.    * Game_Player.prototype.canMove()
  205.    * @note
  206.    * Make sure we're not jumping before we try to move.
  207.    */
  208.   Zale.FreeJump.GP_canmove_BVio32h8oh = Game_Player.prototype.canMove;
  209.   Game_Player.prototype.canMove = function() {
  210.     return Zale.FreeJump.GP_canmove_BVio32h8oh.call(this) && !this.isJumping();
  211.   }
  212.  
  213.   /*
  214.    * Game_Player.prototype.updateDashing()
  215.    * @note
  216.    * Alias the updateDashing function to also increment the dash count
  217.    * if we're dashing, or set it to 9 if we aren't.
  218.    */
  219.   Zale.FreeJump.GP_udash_XZ9pohv2n = Game_Player.prototype.updateDashing;
  220.   Game_Player.prototype.updateDashing = function() {
  221.     Zale.FreeJump.GP_udash_XZ9pohv2n.call(this);
  222.     if(this._dashing) {
  223.       this._dashCount += 1;
  224.     } else {
  225.       this._dashCount = 0;
  226.     }
  227.   }
  228.  
  229.   /*
  230.    * Game_Player.prototype.moveByInput()
  231.    * @note
  232.    * Alias the moveByInput function so that we can check for
  233.    * the input required for jumping.
  234.    */
  235.   Zale.FreeJump.GP_mbi_IO98Cnkwa = Game_Player.prototype.moveByInput;
  236.   Game_Player.prototype.moveByInput = function() {
  237.     Zale.FreeJump.GP_mbi_IO98Cnkwa.call(this);
  238.     if(Input.isTriggered(Zale.FreeJump.JUMPKEY) && this.canMove() && !this.triggerAction()) {
  239.       this.freeJump();
  240.     }
  241.   }
  242.  
  243.   /*
  244.    * Game_Player.prototype.isJumping()
  245.    * @note
  246.    * Aliases the isJumping function to include whether or not we are
  247.    * doing free jumping.
  248.    */
  249.   Zale.FreeJump.GP_isjump_BCOlwnc9 = Game_Player.prototype.isJumping;
  250.   Game_Player.prototype.isJumping = function() {
  251.     return this.isFreeJumping() || Zale.FreeJump.GP_isjump_BCOlwnc9.call(this);
  252.   }
  253.  
  254.   /*
  255.    * Game_Player.prototype.freeJump()
  256.    * @note
  257.    * Sets initial values for the jump onto the player so that
  258.    * the update function has all the information it needs.
  259.    *
  260.    * If Character Poses is included in the game project, the
  261.    * jumping pose is set here so that our character changes
  262.    */
  263.   Game_Player.prototype.freeJump = function() {
  264.     this._freeJump = true;
  265.     this._endJumpInput = false;
  266.     this._jumpPeak  = Zale.FreeJump.PEAKMIN;
  267.     this._jumpCount = this._jumpPeak * Zale.FreeJump.COUNTINCREASE;
  268.     this._dashJump = this.isDashJump();
  269.     this._jumpDir = Zale.FreeJump.DIAGONAL ? Input.dir8 : Input.dir4;
  270.     if(Imported["ZE - Character Poses"]) {
  271.       this.setPose(Zale.CharPose.JUMPPOSE);
  272.     }
  273.   }
  274.  
  275.   /*
  276.    * Game_Player.prototype.isFreeJumping()
  277.    * @return {Boolean} Whether or not we are free jumping.
  278.    */
  279.   Game_Player.prototype.isFreeJumping = function() {
  280.     return this._freeJump;
  281.   }
  282.  
  283.   /*
  284.    * Game_Player.prototype.isDashJump()
  285.    * @return {Boolean} Whether or not the current free jump is a dash jump.
  286.    */
  287.   Game_Player.prototype.isDashJump = function() {
  288.     return this._dashCount >= Zale.FreeJump.DASHTHRESHOLD;
  289.   }
  290.  
  291.   /*
  292.    * Game_Player.prototype.updateJump()
  293.    * @note
  294.    * Alias updateJump to call our update separate function if the jump is a
  295.    * freejump.
  296.    */
  297.   Zale.FreeJump.GP_ujump_UICiuoa91 = Game_Player.prototype.updateJump;
  298.   Game_Player.prototype.updateJump = function() {
  299.     if(this.isFreeJumping()) {
  300.       this.updateFreeJump();
  301.     } else {
  302.       Zale.FreeJump.GP_ujump_UICiuoa91.call(this);
  303.     }
  304.   }
  305.  
  306.   /*
  307.    * Game_Player.prototype._freeJumpMove(d)
  308.    * @param {Number} d The direction we're moving in.
  309.    * @note
  310.    * A specific function to increment the player's position during the
  311.    * freejump.
  312.    */
  313.   Game_Player.prototype._freeJumpMove = function(d) {
  314.     if(this.canPass(this._x, this._y, d)){
  315.       switch(d) {
  316.         case 1:
  317.           this._x -= Zale.FreeJump.XINC;
  318.           this._y += Zale.FreeJump.YINC;
  319.           break;
  320.         case 2:
  321.           this._y += Zale.FreeJump.YINC;
  322.           break;
  323.         case 3:
  324.           this._x += Zale.FreeJump.XINC;
  325.           this._y += Zale.FreeJump.YINC;
  326.           break;
  327.         case 4:
  328.           this._x -= Zale.FreeJump.XINC;
  329.           break;
  330.         case 6:
  331.           this._x += Zale.FreeJump.XINC;
  332.           break;
  333.         case 7:
  334.           this._x -= Zale.FreeJump.XINC;
  335.           this._y -= Zale.FreeJump.YINC;
  336.           break;
  337.         case 8:
  338.           this._y -= Zale.FreeJump.YINC;
  339.           break;
  340.         case 9:
  341.           this._x += Zale.FreeJump.XINC;
  342.           this._y -= Zale.FreeJump.YINC;
  343.           break;
  344.         default:
  345.           break;
  346.       }
  347.     }
  348.   }
  349.  
  350.   /*
  351.    * Game_Player.prototype.updateFreeJump
  352.    * @note
  353.    * Update the player's free jump. This function does pretty much all the
  354.    * heavy-lifting of the plugin. It will allow for the variable jump height,
  355.    * move the player, and also check if the jump is finished.
  356.    * If Character Poses is imported, this will clear the jump pose when the
  357.    * jump ends.
  358.    */
  359.   Game_Player.prototype.updateFreeJump = function() {
  360.     if(!Input.isPressed(Zale.FreeJump.JUMPKEY)) {
  361.       this._endJumpInput = true;
  362.     }
  363.     if(Input.isPressed(Zale.FreeJump.JUMPKEY) && !this._endJumpInput) {
  364.       if(!this._dashJump && this._jumpPeak < Zale.FreeJump.PEAKMAX || this._dashJump && this._jumpPeak < Zale.FreeJump.DASHPEAKMAX) {
  365.         this._jumpCount += Zale.FreeJump.COUNTINCREASE;
  366.         this._jumpPeak  += 1;
  367.       }
  368.     }
  369.     this._jumpCount = [this._jumpCount - Zale.FreeJump.COUNTDECREASE, 0].max().fix();
  370.     this._freeJumpMove(this._jumpDir);
  371.     this._realX = this._x;
  372.     this._realY = this._y;
  373.     if(this._jumpCount === 0) {
  374.       this._jumpCount = 0;
  375.       this._freeJump  = false;
  376.       this._dashJump  = false;
  377.       if(Zale.FreeJump.GRIDSNAP) {
  378.         this.snapX();
  379.         this.snapY();
  380.       }
  381.       this._jumpDir   = 0;
  382.       if(Imported["ZE - Character Poses"]) {
  383.         this.removePose(Zale.CharPose.JUMPPOSE);
  384.       }
  385.     }
  386.   }
  387.  
  388.   /*
  389.    * Game_Player.prototype.snapX()
  390.    * @note
  391.    * Snaps the player to the grid, based on the Tile Width parameter
  392.    * set by the game dev. This does not check for passability. The
  393.    * grid snap typically has to happen because when jumping and
  394.    * hitting a wall, this.canPass returns false for the last small
  395.    * bit, so the player gets stuck between the grid. Since we have
  396.    * the game dev input the tile width, we really shouldn't get stuck
  397.    * in any walls.
  398.    *
  399.    * This takes into account the player's direction.
  400.    */
  401.   Game_Player.prototype.snapX = function() {
  402.     var dx = this._x % Zale.FreeJump.TILEWIDTH;
  403.     if(dx !== 0) {
  404.       if(dx >= Zale.FreeJump.TILEWIDTH / 2) {
  405.         this._x += (Zale.FreeJump.TILEWIDTH - dx) * ([1, 4, 7].contains(this._jumpDir) ? -1 : 1);
  406.       } else {
  407.         this._x += dx * ([1, 4, 7].contains(this._jumpDir) ? -1 : 1);
  408.       }
  409.     }
  410.   }
  411.  
  412.   /*
  413.    * Game_Player.prototype.snapY()
  414.    * @note
  415.    * Snaps the player to the grid, based on the Tile Height parameter
  416.    * set by the game dev. This does not check for passability. The
  417.    * grid snap typically has to happen because when jumping and
  418.    * hitting a wall, this.canPass returns false for the last small
  419.    * bit, so the player gets stuck between the grid. Since we have
  420.    * the game dev input the tile height, we really shouldn't get stuck
  421.    * in any walls.
  422.    */
  423.   Game_Player.prototype.snapY = function() {
  424.     var dy = this._y % Zale.FreeJump.TILEHEIGHT;
  425.     if(dy !== 0) {
  426.       if(dy >= Zale.FreeJump.TILEHEIGHT / 2) {
  427.         //Round up
  428.         this._y += (Zale.FreeJump.TILEHEIGHT - dy) * ([1, 2, 3].contains(this._jumpDir) ? 1 : -1);
  429.       } else {
  430.         this._y += dy * ([1, 2, 3].contains(this._jumpDir) ? 1 : -1);
  431.       }
  432.     }
  433.   }
  434.  
  435.   /*
  436.    * Game_Player.prototype.terrainTag()
  437.    * @note
  438.    * Adds a check to see if we're free jumping, and whether or not
  439.    * terrain tags were disabled while doing so. If the conditions
  440.    * are not true, the aliased function is called.
  441.    *
  442.    * If they are met, however, the predefined default terrain tag is
  443.    * returned instead of checking the map.
  444.    *
  445.    * @returns {Number} A terrain tag.
  446.    */
  447.   Zale.FreeJump.GP_ttag_UIBw0nv83c0 = Game_Player.prototype.terrainTag;
  448.   Game_Player.prototype.terrainTag = function() {
  449.     if(this.isFreeJumping() && !Zale.FreeJump.CHECKTAG) {
  450.       return Zale.FreeJump.DEFAULTTAG;
  451.     } else {
  452.       return Zale.FreeJump.GP_ttag_UIBw0nv83c0.call(this);
  453.     }
  454.   }
  455.  
  456.   /*
  457.    * Game_Player.prototype.isMapPassable(x, y, d)
  458.    * @param {Number} x The x coordinate to check.
  459.    * @param {Number} y The y coordinate to check.
  460.    * @param {Number} d The direction to check.
  461.    * @note
  462.    * Adds on a check to see if we should ignore a tile while free
  463.    * jumping. The ignore is based on the passable region id and
  464.    * terrain tag defined by parameters.
  465.    */
  466.   Zale.FreeJump.GP_imp = Game_Player.prototype.isMapPassable;
  467.   Game_Player.prototype.isMapPassable = function(x, y, d) {
  468.     return Zale.FreeJump.GP_imp.call(this, x, y, d) || this.canIgnoreTile(x, y, d);
  469.   }
  470.  
  471.   /*
  472.    * Game_Player.prototype.canIgnoreTile(x, y, d)
  473.    * @param {Number} x The x coordinate to check.
  474.    * @param {Number} y The y coordinate to check.
  475.    * @param {Number} d The direction to check.
  476.    * @note
  477.    * This function will check if the tile at the given position can
  478.    * be passed over while jumping. This checks the ignored terrain
  479.    * tags and region IDs.
  480.    */
  481.   Game_Player.prototype.canIgnoreTile = function(x, y, d) {
  482.     x = $gameMap.roundXWithDirection(x, d);
  483.     y = $gameMap.roundYWithDirection(y, d);
  484.     if(this.isFreeJumping()) {
  485.       if(Zale.FreeJump.IGNORETAG.contains($gameMap.terrainTag(Math.round(x), Math.round(y)))) {
  486.         return true;
  487.       }
  488.       if(Zale.FreeJump.IGNOREREGION.contains($gameMap.regionId(Math.round(x), Math.round(y)))) {
  489.         return true;
  490.       }
  491.     }
  492.     return false;
  493.   }
  494.  
  495.   /*
  496.    * Game_Player.prototype.regionId()
  497.    * @note
  498.    * Adds a check to see if we're free jumping, and whether or not
  499.    * region IDs were disabled while doing so. If the conditions
  500.    * are not true, the aliased function is called.
  501.    *
  502.    * If they are met, however, the predefined default region id is
  503.    * returned instead of checking the map.
  504.    *
  505.    * @returns {Number} A region id.
  506.    */
  507.   Zale.FreeJump.GP_rid_XBDFj92nq = Game_Player.prototype.regionId;
  508.   Game_Player.prototype.regionId = function() {
  509.     if(this.isFreeJumping() && !Zale.FreeJump.CHECKREGION) {
  510.       return Zale.FreeJump.DEFAULTREGION;
  511.     } else {
  512.       return Zale.FreeJump.GP_rid_XBDFj92nq.call(this);
  513.     }
  514.   }
  515.  
  516.   /*
  517.    * Game_Player.prototype.isOnDamageFloor
  518.    * @return {Boolean} Whether or not the player is on a adamaging tile.
  519.    * @note
  520.    * This function has been aliased to return false if the player is jumping.
  521.    */
  522.   Zale.FreeJump.GP_idf_ISbcyr6vAF = Game_Player.prototype.isOnDamageFloor;
  523.   Game_Player.prototype.isOnDamageFloor = function() {
  524.     return Zale.FreeJump.GP_idf_ISbcyr6vAF.call(this) && !this.isJumping();
  525.   }
  526. })();
Advertisement
Add Comment
Please, Sign In to add comment