modern_algebra

ExtraMovementFrames

Nov 1st, 2015
2,503
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. //  ExtraMovementFrames.js
  3. //=============================================================================
  4. //  Version: 1.0.3
  5. //  Date: 10 November 2015
  6. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. /*:
  8.  * @author Modern Algebra (rmrk.net)
  9.  * @plugindesc Set sprites with more than 3 frames of animation
  10.  *
  11.  * @param Cycle Time
  12.  * @desc The normal number of frames to complete animation cycle for custom sprites
  13.  * @default 60
  14.  *
  15.  * @param Default Idle Frame
  16.  * @desc The idle frame for custom sprites unless changed in the filename
  17.  * @default 0
  18.  *
  19.  * @param Default Pattern
  20.  * @desc Set patterns for custom sprites unless changed in the filename.
  21.  * @default []
  22.  *
  23.  * @help INSTRUCTIONS:
  24.  *
  25.  * To create sprites that have more than 3 frames of animation, you need
  26.  * to rename the character graphic to something of the form:
  27.  *
  28.  *      RegularName%(x)
  29.  *          x : the number of frames in each character sprite
  30.  *
  31.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32.  * EXAMPLES:
  33.  *
  34.  *     $001-Fighter01%(4)
  35.  *         // This graphic is a single character with four frames of animation.
  36.  *
  37.  *     022-Actors12%(6)
  38.  *         // This graphic would be interpreted as a character sheet of 8
  39.  *         // characters each having six frames of animation.
  40.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41.  *
  42.  * Additionally, this script also allows you to specify the "idle" frame (the
  43.  * frame where the sprite is not moving), and also the pattern if you wish to so
  44.  * specify. In essence, all you need to do is add those integers after the
  45.  * number of frames:
  46.  *
  47.  *     Regular_Name%(x y z1 z2 ... zn)
  48.  *         x : the number of frames in each character sprite
  49.  *         y : the idle frame (the frame shown when sprite is not moving)
  50.  *         z1 ... zn : the pattern.
  51.  *
  52.  * If you choose to specify a pattern, then the idle frame is not automatically
  53.  * included in the pattern and should be repeated if you want it to appear
  54.  *
  55.  * When naming your files, be aware that the first frame in a sprite is index 0,
  56.  * the second frame is index 1, etc.
  57.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58.  * EXAMPLES:
  59.  *
  60.  *     $003-Fighter03%(4 2)
  61.  *         // This graphic is a single character with four frames of animation.
  62.  *         // The idle frame is 2 (the third one over). The pattern when moving
  63.  *         // would be 2 3 0 1, 2 3 0 1, etc. (unless default patterns set -
  64.  *         // see below)
  65.  *
  66.  *     032-People05%(4 0 1 0 3 2)
  67.  *         // This graphic would be interpreted as a character sheet of 8
  68.  *         // characters, each having four frames of animation. The idle frame is
  69.  *         // 0 (the first in the sheet), and the pattern is 1 0 3 2,
  70.  *         // 1 0 3 2, etc.
  71.  *
  72.  *     $003-Fighter03%(6 0 1 2 3 4 5)
  73.  *         // This graphic is a single character with six frames of animation.
  74.  *         // The idle frame is 0 (the first frame). The pattern when moving
  75.  *         // is 1 2 3 4 5, 1 2 3 4 5, etc.
  76.  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  77.  * PLUGIN SETTINGS:
  78.  *
  79.  *     Cycle Time = 60
  80.  *
  81.  * Cycle Time is the number of frames it will take to complete a full
  82.  * animation cycle for custom sprites at normal speed. It must be set to an
  83.  * integer.
  84.  *
  85.  *
  86.  *     Default Idle Frame = 0
  87.  *
  88.  * If you do not specify an idle frame for custom sprites in the file name, then it
  89.  * will be this frame. You must set this to an integer.
  90.  *
  91.  *     Default Pattern = []
  92.  *
  93.  * If you do not specify a pattern, then what happens depends on what you write
  94.  * in the plugin setting for "Default Pattern". For this setting, you have the
  95.  * option of writing in arrays of numbers in the following format:
  96.  *
  97.  *     [x y z1 z2 ... zn]
  98.  *         x : number of frames in the sprites for which this pattern is default
  99.  *         y : idle frame
  100.  *         z1 z2 ... zn : the pattern
  101.  *
  102.  * If you have setup one of those arrays for the number of frames which this
  103.  * custom sprite has, then it will use that pattern and idle frame.
  104.  *
  105.  * If you have not set up a default pattern for this number of frames, then the
  106.  * animation will simply cycle through the number of frames, starting with the
  107.  * idle frame and moving right. The idle frame will be included in the animation.
  108.  *
  109.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  110.  * EXAMPLES
  111.  *
  112.  *     Default Pattern = [5 1 2 3 4 3 2]
  113.  *         // Whenever you set up a custom sprite that has 5 frames of animation
  114.  *         // but do not specify a pattern, the idle frame will be 1 and the
  115.  *         // pattern will be 2 3 4 3 2, 2 3 4 3 2, etc.
  116.  *
  117.  *     Default Pattern = [5 1 2 3 4 3 2], [6 0 1 2 5 4 3 0]
  118.  *         // Whenever you set up a custom sprite that has 5 frames of animation
  119.  *         // but do not specify a pattern, the idle frame will be 1 and the
  120.  *         // pattern will be 2 3 4 3 2, 2 3 4 3 2, etc.
  121.  *         // Whenever you set up a custom sprite that has 6 frames of animation
  122.  *         // but do not specify a pattern, the idle frame will be 0 and the
  123.  *         // pattern will be 1 2 5 4 3 0, 1 2 5 4 3 0, etc.
  124.  */
  125. //=============================================================================
  126.  
  127. var Imported = Imported || {};
  128. Imported.MA_ExtraMovementFrames = true;
  129.  
  130. var ModernAlgebra = ModernAlgebra || {};
  131. ModernAlgebra.EMF = {};
  132.  
  133. (function() {
  134.    
  135.     // Get Script Name, in case user unexpectedly altered it
  136.     var path = document.currentScript.src;
  137.     var scriptName = path.substring(path.lastIndexOf('/')+1).match(/^(.+?)(\.[^.]*$|$)/)[1];
  138.    
  139.     // Set Parameters
  140.     ModernAlgebra.EMF.parameters = PluginManager.parameters(scriptName);
  141.     ModernAlgebra.EMF.cycleTime = (+ModernAlgebra.EMF.parameters['Cycle Time']) || 60; // Default = 60
  142.     ModernAlgebra.EMF.idleFrame = (+ModernAlgebra.EMF.parameters['Default Idle Frame']) || 0; // Default = 0
  143.     ModernAlgebra.EMF.defaultPattern = [];
  144.    
  145.     var emfPattMatch = ModernAlgebra.EMF.parameters['Default Pattern'].match(/\[.+?\]/g); // Default []
  146.     if (emfPattMatch) {
  147.         // Get all arrays of numbers
  148.         for (var i = 0; i < emfPattMatch.length; i++) {
  149.             digitMatch = emfPattMatch[i].match(/\d+/g);
  150.             if (digitMatch) { ModernAlgebra.EMF.defaultPattern.push(digitMatch.map(Number)); }
  151.         }
  152.     }
  153.    
  154.     //=========================================================================
  155.     // ImageManager
  156.     //=========================================================================
  157.     // isEmfCharacter - Checks if filename is a customly animated sprite
  158.     ImageManager.isEmfCharacter = function(filename) {
  159.         return !!filename.match(/\%[\(\[][\d\s]+[\)\]]/); // check filename for %() or %[]
  160.     };
  161.    
  162.     //=========================================================================
  163.     // Game_CharacterBase
  164.     //=========================================================================
  165.     // initMembers
  166.     ModernAlgebra.EMF.GameCharacterBase_initMembers =
  167.             Game_CharacterBase.prototype.initMembers;
  168.     Game_CharacterBase.prototype.initMembers = function() {
  169.         this.maClearEmfCharacterState();
  170.         ModernAlgebra.EMF.GameCharacterBase_initMembers.apply(this, arguments); // original method
  171.     };
  172.    
  173.     // maClearEmfCharacterState
  174.     Game_CharacterBase.prototype.maClearEmfCharacterState = function() {
  175.         this._isEmfCharacter = false;
  176.         this._emfCharacterState = { frameNum: 3, idleFrame: ModernAlgebra.EMF.idleFrame, pattern: [2, 1, 0, 1] };
  177.     };
  178.    
  179.     // isEmfCharacter - Check whether a customly animated sprites
  180.     Game_CharacterBase.prototype.isEmfCharacter = function() {
  181.         return this._isEmfCharacter;
  182.     };
  183.    
  184.     // emfCharacterState - makes this._emfCharacterState public
  185.     Game_CharacterBase.prototype.emfCharacterState = function() {
  186.         return this._emfCharacterState;
  187.     };
  188.    
  189.     // setImage - adjusts to call EMF setup method
  190.     ModernAlgebra.EMF.GameCharacterBase_setImage =
  191.             Game_CharacterBase.prototype.setImage;
  192.     Game_CharacterBase.prototype.setImage = function() {
  193.         ModernAlgebra.EMF.GameCharacterBase_setImage.apply(this, arguments); // original method
  194.         this.maemfSetupEmfCharacter();
  195.         this.resetPattern();
  196.     };
  197.    
  198.     // maSetupEmfCharacter - setup custom animation sprite
  199.     Game_CharacterBase.prototype.maemfSetupEmfCharacter = function() {
  200.         this.maClearEmfCharacterState();
  201.         var charName = this.characterName();
  202.         if (ImageManager.isEmfCharacter(charName)) {
  203.             var sign = charName.match(/(?:\%[\(\[])[\d\s]+(?:[\)\]])/);
  204.             var signArgs = sign[0].match(/\d+/g); // array of digit strings
  205.             if (signArgs) {
  206.                 this._isEmfCharacter = true;
  207.                 // Map arguments in file name to an array of numbers
  208.                 signArgs = signArgs.map(Number);
  209.                 signArgsLength = signArgs.length;
  210.                 this.emfCharacterState().frameNum = signArgs.shift();
  211.                 this.emfCharacterState().idleFrame = (signArgsLength > 1) ? signArgs.shift() : ModernAlgebra.EMF.idleFrame;
  212.                 if (signArgsLength > 2) {
  213.                     this.emfCharacterState().pattern = signArgs;
  214.                 } else {
  215.                     var success = false;
  216.                     // Check for a default match for this number of frames
  217.                     for (var i = 0; i < ModernAlgebra.EMF.defaultPattern.length; i++) {
  218.                         if (ModernAlgebra.EMF.defaultPattern[i][0] === this.emfCharacterState().frameNum) {
  219.                             this.emfCharacterState().idleFrame = ModernAlgebra.EMF.defaultPattern[i][1];
  220.                             this.emfCharacterState().pattern = ModernAlgebra.EMF.defaultPattern[i].slice(2, (ModernAlgebra.EMF.defaultPattern[i].length));
  221.                             success = true;
  222.                             break;
  223.                         }
  224.                     }
  225.                     // If still no pattern specified
  226.                     if (!success) {
  227.                         // Populate pattern with a simple cycle starting after idle
  228.                         this.emfCharacterState().pattern = [];
  229.                         var idleFramePlus = this.emfCharacterState().idleFrame + 1;
  230.                         for (var i = 0; i < this.emfCharacterState().frameNum; i++) {
  231.                             this.emfCharacterState().pattern.push((i + idleFramePlus) % this.emfCharacterState().frameNum);
  232.                         }
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.     };
  238.    
  239.     // animationWait
  240.     ModernAlgebra.EMF.GameCharacterBase_animationWait =
  241.             Game_CharacterBase.prototype.animationWait;
  242.     Game_CharacterBase.prototype.animationWait = function() {
  243.         // If EMF Character
  244.         if (this.isEmfCharacter()) {
  245.             var realSpeed = this.realMoveSpeed();
  246.             var frameNum = this.maxPattern();
  247.             return Math.floor((8 - realSpeed)*(ModernAlgebra.EMF.cycleTime / (4*frameNum))); // CycleTime divided by number of frames in animation
  248.         } else {
  249.             // Run Default Method - approx. 60 frames at normal speed
  250.             return ModernAlgebra.EMF.GameCharacterBase_animationWait.apply(this, arguments) // original method
  251.         }
  252.     };
  253.    
  254.     // maxPattern
  255.     ModernAlgebra.EMF.GameCharacterBase_maxPattern =
  256.                 Game_CharacterBase.prototype.maxPattern;
  257.     Game_CharacterBase.prototype.maxPattern = function() {
  258.         if (this.isEmfCharacter()) {
  259.             return this.emfCharacterState().pattern.length; // Length of pattern array
  260.         } else {
  261.             return ModernAlgebra.EMF.GameCharacterBase_maxPattern.apply(this, arguments); // original method
  262.         }
  263.     };
  264.    
  265.     // pattern
  266.     ModernAlgebra.EMF.GameCharacterBase_pattern =
  267.             Game_CharacterBase.prototype.pattern;
  268.     Game_CharacterBase.prototype.pattern = function() {
  269.         if (this.isEmfCharacter()) {
  270.             if (this._pattern < 0) {
  271.                 return this.emfCharacterState().idleFrame; // Idle Frame if _pattern < 0
  272.             } else {
  273.                 var patternIndex = (this._pattern % this.emfCharacterState().pattern.length);
  274.                 return this.emfCharacterState().pattern[patternIndex]; // index of pattern array
  275.             }
  276.         } else {
  277.             return ModernAlgebra.EMF.GameCharacterBase_pattern.apply(this, arguments); // original method
  278.         }
  279.     };
  280.    
  281.     // isOriginalPattern - Original pattern is -1 for custom sprites
  282.     ModernAlgebra.EMF.GameCharacterBase_isOriginalpattern =
  283.             Game_CharacterBase.prototype.isOriginalPattern;
  284.     Game_CharacterBase.prototype.isOriginalPattern = function() {
  285.         if (this.isEmfCharacter()) {
  286.             return this.pattern() === -1;
  287.         } else {
  288.             return ModernAlgebra.EMF.GameCharacterBase_isOriginalpattern.apply(this, arguments); // original method
  289.         }
  290.     };
  291.    
  292.     // straighten - Straighten to original pattern
  293.     ModernAlgebra.EMF.GameCharacterBase_straighten =
  294.             Game_CharacterBase.prototype.straighten;
  295.     Game_CharacterBase.prototype.straighten = function() {
  296.         if (this.isEmfCharacter()) {
  297.             if (this.hasWalkAnime() || this.hasStepAnime()) {
  298.                 this._pattern = -1;
  299.             }
  300.             this._animationCount = 0;
  301.         } else {
  302.             ModernAlgebra.EMF.GameCharacterBase_straighten.apply(this, arguments)
  303.         }
  304.     };
  305.        
  306.     // resetPattern - Idle is -1 for custom sprites
  307.     ModernAlgebra.EMF.GameCharacterBase_resetPattern =
  308.             Game_CharacterBase.prototype.resetPattern;
  309.     Game_CharacterBase.prototype.resetPattern = function() {
  310.         if (this.isEmfCharacter()) {
  311.             this.setPattern(-1);
  312.         } else {
  313.             ModernAlgebra.EMF.GameCharacterBase_resetPattern.apply(this, arguments); // original method
  314.         }
  315.     };
  316.    
  317.     //=========================================================================
  318.     // Game_Event
  319.     //=========================================================================
  320.     // setupPageSettings - adjust original pattern
  321.     ModernAlgebra.EMF.GameEvent_setupPageSettings =
  322.             Game_Event.prototype.setupPageSettings;
  323.     Game_Event.prototype.setupPageSettings = function() {
  324.         ModernAlgebra.EMF.GameEvent_setupPageSettings.apply(this, arguments);
  325.         // Original pattern is always idle for custom sprites
  326.         if (this.isEmfCharacter()) { this._originalPattern = -1; }
  327.         this.resetPattern();
  328.     };
  329.        
  330.     //=========================================================================
  331.     // Sprite_Character
  332.     //=========================================================================
  333.     // patternWidth - afjust based on number of frames
  334.     ModernAlgebra.EMF.SpriteCharacter_patternWidth =
  335.             Sprite_Character.prototype.patternWidth;
  336.     Sprite_Character.prototype.patternWidth = function() {
  337.         var pw = ModernAlgebra.EMF.SpriteCharacter_patternWidth.apply(this, arguments)
  338.         if (this._character.isEmfCharacter()) {
  339.             var frameNum = this._character.emfCharacterState().frameNum;
  340.             return ((pw*3) / frameNum);
  341.         } else {
  342.             return pw;
  343.         }
  344.     };
  345.    
  346. })();
RAW Paste Data