Iavra

Iavra Text Sound

Nov 4th, 2015 (edited)
4,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Dynamically plays sound effects, while text messages are printed.
  3.  * <Iavra Text Sound>
  4.  * @author Iavra
  5.  *
  6.  * @param Enable Text Sounds
  7.  * @desc Controls, if text sounds should be played at all. Default: true
  8.  * @default true
  9.  *
  10.  * @param Sound Name
  11.  * @desc Sound effect to be played, when text is displayed. Can be changed for each text individually.
  12.  * @default Cursor1
  13.  *
  14.  * @param Sound Pan
  15.  * @desc Value to be used as sound pan. Default: 0
  16.  * @default 0
  17.  *
  18.  * @param Sound Pitch
  19.  * @desc Value to be used as sound pitch. Default: 100
  20.  * @default 100
  21.  *
  22.  * @param Sound Volume
  23.  * @desc Volume of the played sound. Default: 100
  24.  * @default 100
  25.  *
  26.  * @param Interval
  27.  * @desc How often the sound effect should be played. Only specify a value of 1 or higher. Default: 1
  28.  * @default 1
  29.  *
  30.  * @param Limit
  31.  * @desc Only process the first X characters. A value less than 0 removes the limitation. Default: -1
  32.  * @default -1
  33.  *
  34.  * @help
  35.  * Plays sound effects when messages are displayed. The sound effect can be configured in the following ways:
  36.  *
  37.  * sound    The name of the sound effect to be played (must match with a file in audio/se).
  38.  * pan      Pan of the sound effect.
  39.  * pitch    Pitch of the sound effect.
  40.  * volume   Volume of the sound effect.
  41.  * interval How often the sound effect should be played. "1" means every character.
  42.  * limit    Only play sound effects for the first X characters. A value less than 0 removes the limitation.
  43.  *
  44.  * All options can be specified via plugin parameters or changed during the game by executing the following
  45.  * script call.
  46.  *
  47.  * IAVRA.TEXTSOUND.setOptions(options);
  48.  *
  49.  * "options" is an object containing one or more of the options specified above and the new value to be set.
  50.  * If you want to reset all options the their default values, you can do so by executing the following script:
  51.  *
  52.  * IAVRA.TEXTSOUND.resetOptions();
  53.  *
  54.  * If you want to (temporarily) enable or disable text sounds, you can do so by calling one of the following:
  55.  *
  56.  * IAVRA.TEXTSOUND.enable();
  57.  * IAVRA.TEXTSOUND.disable();
  58.  *
  59.  * If you prefer plugin commands over script calls, you can use one of the following ones:
  60.  *
  61.  * TextSound set <option> <value>
  62.  * TextSound reset
  63.  * TextSound enable
  64.  * TextSound disable
  65.  *
  66.  * For the "set" command, you need to specify one of the options specified above and the new value to be used.
  67.  * If you want to set multiple options at the same time, you'll still need to use the script call, instead.
  68.  */
  69.  
  70. var Imported = Imported || {};
  71. Imported.iavra_text_sound = true;
  72.  
  73. //=============================================================================
  74. // namespace IAVRA
  75. //=============================================================================
  76.  
  77. var IAVRA = IAVRA || {};
  78.  
  79. (function() {
  80.     "use strict";
  81.    
  82.     /**
  83.      * Since PluginManager.parameters() breaks when the plugin file is renamed, we are using our own solution.
  84.      */
  85.     var _params = (function($) {
  86.         return {
  87.             enabled: $['Enable Text Sounds'].toLowerCase() === 'true',
  88.             sound: $['Sound Name'],
  89.             pan: parseInt($['Sound Pan']) || 0,
  90.             pitch: parseInt($['Sound Pitch']) || 0,
  91.             volume: parseInt($['Sound Volume']) || 0,
  92.             interval: parseInt($['Interval']) || 1,
  93.             limit: parseInt($['Limit']) || 0
  94.         };
  95.     })($plugins.filter(function(p) { return p.description.contains('<Iavra Text Sound>'); })[0].parameters);
  96.    
  97.     /**
  98.      * The current options to be used when displaying a message. Can be changed at any time via script/plugin
  99.      * commands or reset to their default values.
  100.      */
  101.     var _options = {};
  102.    
  103.     /**
  104.      * Can be used to disable text sounds entirely.
  105.      */
  106.     var _enabled = _params.enabled;
  107.    
  108.     //=============================================================================
  109.     // module IAVRA.TEXTSOUND
  110.     //=============================================================================
  111.    
  112.     IAVRA.TEXTSOUND = {
  113.        
  114.         /**
  115.          * Sets one or more options for sound effects.
  116.          */
  117.         setOptions: function(options) {
  118.             console.log(options);
  119.             options || (options = {});
  120.             for(var key in _options) { (options[key] === undefined) || (_options[key] = options[key]); }
  121.         },
  122.        
  123.         /**
  124.          * Resets all options to their default values.
  125.          */
  126.         resetOptions: function() {
  127.             _options = ['sound', 'pan', 'pitch', 'volume', 'interval', 'limit'].reduce(function(map, key) {
  128.                 map[key] = _params[key]; return map;
  129.             }, {});
  130.         },
  131.        
  132.         /**
  133.          * Enables sound effects.
  134.          */
  135.         enable: function() { _enabled = true; },
  136.        
  137.         /**
  138.          * Disables sound effects.
  139.          */
  140.         disable: function() { _enabled = false; }
  141.        
  142.     };
  143.    
  144.     /**
  145.      * Initialize all options to their default values.
  146.      */
  147.     IAVRA.TEXTSOUND.resetOptions();
  148.    
  149.     //=============================================================================
  150.     // class Window_Message
  151.     //=============================================================================
  152.    
  153.     (function($) {
  154.        
  155.         /**
  156.          * When starting a new page, we reset the character counter and set all options accordingly.
  157.          */
  158.         var _alias_newPage = $.prototype.newPage;
  159.         $.prototype.newPage = function(textState) {
  160.             _alias_newPage.apply(this, arguments);
  161.             this._iavra_text_sound = {
  162.                 sound: {name: _options.sound, pan: _options.pan, pitch: _options.pitch, volume: _options.volume },
  163.                 interval: _options.interval, limit: _options.limit, counter: 0
  164.             };
  165.         };
  166.        
  167.         /**
  168.          * When printing a normal character, sound effects are enabled and the configured limit and interval both
  169.          * pass, we play our sound effect.
  170.          */
  171.         var _alias_processNormalCharacter = $.prototype.processNormalCharacter;
  172.         $.prototype.processNormalCharacter = function(textState) {
  173.             _alias_processNormalCharacter.apply(this, arguments);
  174.             if(!_enabled) { return; }
  175.             var counter = this._iavra_text_sound.counter++;
  176.             var limit = this._iavra_text_sound.limit;
  177.             var interval = this._iavra_text_sound.interval;
  178.             if((limit < 0 || counter < limit) && (counter % interval) == 0) {
  179.                 AudioManager.playStaticSe(this._iavra_text_sound.sound);
  180.             }
  181.         };
  182.        
  183.     })(Window_Message);
  184.    
  185.     //=============================================================================
  186.     // class Game_Interpreter
  187.     //=============================================================================
  188.    
  189.     (function($) {
  190.        
  191.         /**
  192.          * Calls setOptions() according to the given arguments.
  193.          */
  194.         var setOption = function(option, value) {
  195.             var _options = {}, _option;
  196.             switch(_option = option.toLowerCase()) {
  197.                 case 'sound':
  198.                     _options[_option] = value || ''; break;
  199.                 case 'pan': case 'pitch': case 'volume':case 'interval': case 'limit':
  200.                     _options[_option] = parseInt(value) || 0; break;
  201.             }
  202.             IAVRA.TEXTSOUND.setOptions(_options);
  203.         };
  204.        
  205.         /**
  206.          * We provide multiple plugin commands:
  207.          * TextSound set <option> <value>   Set a single option for future messages.
  208.          * TextSound reset                  Reset all options to their default values.
  209.          * TextSound enable                 Enable text sounds.
  210.          * TextSound diable                 Disable text sounds.
  211.          */
  212.         var _alias_pluginCommand = $.prototype.pluginCommand;
  213.         $.prototype.pluginCommand = function(command, args) {
  214.             _alias_pluginCommand.apply(this, arguments);
  215.             if(command === 'TextSound') {
  216.                 console.log(args);
  217.                 switch(args.shift().toLowerCase()) {
  218.                     case 'set': setOption(args[0], args[1]); break;
  219.                     case 'reset': IAVRA.TEXTSOUND.resetOptions(); break;
  220.                     case 'enable': IAVRA.TEXTSOUND.enable(); break;
  221.                     case 'disable': IAVRA.TEXTSOUND.disable(); break;
  222.                 }
  223.             }
  224.         }
  225.        
  226.     })(Game_Interpreter);
  227.    
  228. })();
Add Comment
Please, Sign In to add comment