SHARE
TWEET

Jay_BattleVAManager.js

a guest Nov 15th, 2017 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Battle Voice Manager
  3. // Jay_BattleVAManager.js
  4. // Version 0.03
  5. //=============================================================================
  6.  
  7. var Imported = Imported || {};
  8. Imported.Jay_BattleVAManager = true;
  9.  
  10. var Jay = Jay || {};
  11. Jay.BattleVAManager = Jay.BattleVAManager || {};
  12.  
  13. //=============================================================================
  14.  /*:
  15.  * @plugindesc Controls voice acting in battle
  16.  *
  17.  * @author Jason R. Godding
  18.  *
  19.  * @param Subtitles On
  20.  * @type boolean
  21.  * @desc Turn on and subtitles will be available by default when starting a new game.
  22.  * @default false
  23.  *
  24.  * @help Allows battle voiceovers. Currently only works with battlestart.
  25.  *
  26.  * This plugin is free for non-commercial and commercial use, but please credit
  27.  * Jason R. Godding if you use it. Thank you.
  28.  *
  29.  */
  30.  
  31. Jay.Parameters = Jay.Parameters || {};
  32. Jay.Parameters.BattleVAManager = PluginManager.parameters('Jay_BattleVAManager');
  33.  
  34. Jay.Param = Jay.Param || {};
  35. Jay.Param.SubtitlesOn = eval(String(Jay.Parameters.BattleVAManager['Subtitles On']));
  36.  
  37. Game_BattlerBase.prototype.getVoiceFiles = function() {
  38.     if (!this._voiceFiles) {
  39.         this._voiceFiles = [];
  40.         var voicesRaw = null;  
  41.  
  42.         if (this.isActor()) {
  43.             voicesRaw = this.actor().meta.voice;
  44.         }
  45.         else {
  46.             voicesRaw = this.enemy().meta.voice;
  47.         }
  48.  
  49.         if (voicesRaw == null) {
  50.             return [];
  51.         }
  52.  
  53.         var splitVoicesRaw = voicesRaw.split('\n');
  54.  
  55.         for (var i = 0; i < splitVoicesRaw.length; i++) {
  56.             if (splitVoicesRaw[i] != '') {
  57.                 var voiceRaw = splitVoicesRaw[i].split('|');
  58.                 if (voiceRaw.length < 2) {
  59.                     throw new Error('Invalid voice file setup: ' + splitVoicesRaw[i]);
  60.                 }
  61.                 var voice = {};
  62.                 voice.voiceName = voiceRaw[0];
  63.                 voice.voiceFile = voiceRaw[1];
  64.                 voice.speaker = this;
  65.                 voice.forcedResponse = false;
  66.                
  67.                 for (var paramIndex = 2; paramIndex < voiceRaw.length; paramIndex++) {
  68.                     var param = voiceRaw[paramIndex];
  69.                     if (param.match(/motion[ ]?=[ ]?([a-zA-Z]+)/i)) {
  70.                         voice.motion = RegExp.$1;
  71.                     }
  72.                     else if (param.toLowerCase().match(/response[ ]?=[ ]?([a-zA-Z0-9_]+) ([a-zA-Z0-9_]+)[ ]?(optional|required)?/i)) {
  73.                         voice.responder = RegExp.$1;
  74.                         voice.response = RegExp.$2;
  75.                         if (RegExp.$3 == 'optional') {
  76.                             voice.forcedResponse = false;
  77.                         }
  78.                         else if (RegExp.$3 == 'required') {
  79.                             voice.forcedResponse = true;
  80.                         }
  81.                     }
  82.                     else if (param.match(/pitch[ ]?=[ ]?([0-9]+)/i)) {
  83.                         voice.pitch = parseInt(RegExp.$1);
  84.                     }
  85.                     else if (param.match(/volume[ ]?=[ ]?([0-9]+)/i)) {
  86.                         voice.volume = parseInt(RegExp.$1);
  87.                     }
  88.                     else if (param.match(/sub[ ]?=[ ]?(.*)/i)) {
  89.                         voice.subtitle = RegExp.$1;
  90.                     }
  91.                 }
  92.                 this._voiceFiles.push(voice);
  93.             }
  94.         }
  95.     }
  96.    
  97.     return this._voiceFiles;
  98. };
  99.  
  100. Game_BattlerBase.prototype.getVoiceFilesFromTag = function(voiceTag) {
  101.     return this.getVoiceFiles().filter(function(voiceFile) {
  102.         return voiceFile.voiceName == voiceTag && this.canSayLine(voiceFile);
  103.     }.bind(this));
  104. };
  105.  
  106. Game_BattlerBase.prototype.canSayLine = function(voiceFile) {
  107.     if (!this.canTalk()) {
  108.         return false;
  109.     }
  110.  
  111.     if (this.isActor() && !$gameParty.battleMembers().contains(this)) {
  112.         return false;
  113.     }
  114.  
  115.     if (this.isEnemy() && !$gameTroop._enemies.contains(this)) {
  116.         return false;
  117.     }
  118.  
  119.     if (voiceFile.response && voiceFile.forcedResponse == true) {
  120.         if (voiceFile.responder.match(/actor([0-9]+)/i)) {
  121.             var responder = $gameActors.actor(parseInt(RegExp.$1));
  122.             if (!$gameParty.battleMembers().contains(responder)) {
  123.                 return false;
  124.             }
  125.  
  126.             if (responder.getVoiceFilesFromTag(voiceFile.response).length == 0) {
  127.                 return false;
  128.             }
  129.         }
  130.         else if (voiceFile.responder.match(/enemy([0-9]+)/i)) {
  131.             var responderClass = parseInt(RegExp.$1);
  132.            
  133.             var possibleResponders = $gameTroop._enemies.filter(function(enemy) {
  134.                     return enemy._enemyId == responderClass &&
  135.                         enemy.getVoiceFilesFromTag(voiceFile.response).length > 0;
  136.                 });
  137.  
  138.             if (possibleResponders.length == 0) {
  139.                 return false;
  140.             }
  141.         }
  142.         else {
  143.             // Format is screwed up
  144.             return false;
  145.         }
  146.     }
  147.  
  148.     return true;
  149. };
  150.  
  151. Game_BattlerBase.prototype.canTalk = function() {
  152.     var blockingStates = this.states().filter(function(state) {
  153.         return !!state.meta.novoice;
  154.     });
  155.  
  156.     return blockingStates.length == 0;
  157. };
  158.  
  159. AudioManager.playSeAndReturn = function(se) {
  160.     if (se.name) {
  161.         this._seBuffers = this._seBuffers.filter(function(audio) {
  162.             return audio.isPlaying();
  163.         });
  164.         var buffer = this.createBuffer('se', se.name);
  165.         this.updateSeParameters(buffer, se);
  166.         buffer.play(false);
  167.         this._seBuffers.push(buffer);
  168.         return buffer;
  169.     }
  170.     return null;
  171. };
  172.  
  173. Jay.BattleVAManager.startBattle = BattleManager.startBattle;
  174. BattleManager.startBattle = function() {
  175.     var battleStartClipsToPlay = [];
  176.    
  177.     $gameTroop._enemies.forEach(function(enemy) {
  178.         if (enemy.canTalk()) {
  179.             var voiceFiles = enemy.getVoiceFilesFromTag("battlestart");
  180.             voiceFiles.forEach(function(voiceFile) {
  181.                 battleStartClipsToPlay.push(voiceFile);
  182.             });
  183.         }
  184.     });
  185.  
  186.     if (battleStartClipsToPlay.length == 0) {
  187.         $gameParty._actors.forEach(function(actorId) {
  188.             var actor = $gameActors.actor(actorId);
  189.             if (actor.canTalk()) {
  190.                 var voiceFiles = actor.getVoiceFilesFromTag("battlestart");
  191.                 voiceFiles.forEach(function(voiceFile) {
  192.                     battleStartClipsToPlay.push(voiceFile);
  193.                 });
  194.             }
  195.         });
  196.     }
  197.  
  198.     if (battleStartClipsToPlay.length > 0) {
  199.         var clip = battleStartClipsToPlay[Math.randomInt(battleStartClipsToPlay.length)];
  200.  
  201.         this.playClip(clip, function() {
  202.                 Jay.BattleVAManager.startBattle.call(this)
  203.             }.bind(this));
  204.     }
  205.     else {
  206.         Jay.BattleVAManager.startBattle.call(this);
  207.     }
  208. };
  209.  
  210. BattleManager.lastActor = function() {
  211.     if (this._action && this._action._subjectActorId > 0) {
  212.         return $gameActors.actor(this._action._subjectActorId);
  213.     }
  214.     return null;
  215. }
  216.  
  217. Jay.BattleVAManager.BattleManager_initMembers = BattleManager.initMembers;
  218. BattleManager.initMembers = function() {    
  219.     Jay.BattleVAManager.BattleManager_initMembers.call(this);
  220.     this._startVictoryPhase = false;
  221. }
  222.  
  223. Jay.BattleVAManager.BattleManager_playVictoryMe = BattleManager.playVictoryMe;
  224. BattleManager.playVictoryMe = function() {
  225. };
  226.  
  227. Jay.BattleVAManager.processVictory = BattleManager.processVictory;
  228. BattleManager.processVictory = function() {
  229.     if (this._startVictoryPhase) {
  230.         return;
  231.     }
  232.  
  233.     $gameParty.performVictory();
  234.     Jay.BattleVAManager.BattleManager_playVictoryMe.call(this);
  235.     this._startVictoryPhase = true;
  236.  
  237.     var victoryClipsToPlay = [];
  238.  
  239.     var winningActor = this.lastActor();
  240.     if (winningActor != null && winningActor.canTalk()) {
  241.         var voiceFiles = winningActor.getVoiceFilesFromTag("battleend");
  242.         voiceFiles.forEach(function(voiceFile) {
  243.             victoryClipsToPlay.push(voiceFile);
  244.         });
  245.     }
  246.  
  247.     if (victoryClipsToPlay.length > 0) {
  248.         var clip = victoryClipsToPlay[Math.randomInt(victoryClipsToPlay.length)];
  249.  
  250.         this.playClip(clip, function() {
  251.                 Jay.BattleVAManager.processVictory.call(this)
  252.             }.bind(this));
  253.     }
  254.     else {
  255.         Jay.BattleVAManager.processVictory.call(this);
  256.     }
  257. }
  258.  
  259. BattleManager.playClip = function(clip, callback) {
  260.     if (clip.motion && clip.speaker.forceMotion) {
  261.         clip.speaker.forceMotion(clip.motion);
  262.     }
  263.  
  264.     var buffer = null;
  265.  
  266.     if (clip.voiceFile.toLowerCase() != 'none') {
  267.         var volume = 100;
  268.         var pitch = 100;
  269.  
  270.         if (clip.volume) {
  271.             volume = clip.volume;
  272.         }
  273.  
  274.         if (clip.pitch) {
  275.             pitch = clip.pitch;
  276.         }
  277.  
  278.         // TODO: See if I can set "pan" to something relating to unit location
  279.         var se = {
  280.             name: clip.voiceFile,
  281.             volume: volume,
  282.             pitch: pitch,
  283.             pan: 0
  284.         };
  285.        
  286.         var buffer = AudioManager.playSeAndReturn(se);
  287.     }
  288.  
  289.     if(clip.subtitle) {
  290.         BattleManager.ShowSubtitleWindow(clip.subtitle);
  291.     }
  292.    
  293.     if (clip.subtitle) {
  294.         if(buffer) {
  295.             buffer.addStopListener(function() {
  296.                     BattleManager.HideSubtitleWindow();
  297.                 }.bind(this));
  298.         }
  299.         else {
  300.             BattleManager.HideSubtitleWindow();
  301.         }
  302.     }
  303.  
  304.     var callbackNow = !!callback;
  305.  
  306.     if (clip.response) {
  307.         if (clip.responder.match(/actor([0-9]+)/i)) {
  308.             var responder = parseInt(RegExp.$1);
  309.  
  310.             if (responder > 0 && $gameParty.battleMembers().contains($gameActors.actor(responder))) {
  311.                 var clips = $gameActors.actor(responder).getVoiceFilesFromTag(clip.response);
  312.                 if (clips.length > 0) {
  313.                     var response = clips[Math.randomInt(clips.length)];
  314.  
  315.                     if (buffer) {
  316.                         buffer.addStopListener(function() {
  317.                                 this.playClip(response, callback);
  318.                             }.bind(this));
  319.                     }
  320.                     else {
  321.                         this.playClip(response, callback);
  322.                     }
  323.  
  324.                     callbackNow = false;
  325.                 }
  326.             }
  327.         }
  328.         else if (clip.responder.match(/enemy([0-9]+)/i)) {
  329.             var responder = parseInt(RegExp.$1);
  330.                
  331.             var possibleResponders = $gameTroop._enemies.filter(function(enemy) {
  332.                 return enemy._enemyId == responder;
  333.             });
  334.  
  335.             for (var i = 0; i < possibleResponders.length; i++) {
  336.                 var clips = possibleResponders[i].getVoiceFilesFromTag(clip.response);
  337.                 if (clips.length > 0) {
  338.                     var response = clips[Math.randomInt(clips.length)];
  339.  
  340.                     if (buffer) {
  341.                         buffer.addStopListener(function() {
  342.                                 this.playClip(response, callback);
  343.                             }.bind(this));
  344.                     }
  345.                     else {
  346.                         this.playClip(response, callback);
  347.                     }
  348.  
  349.                     callbackNow = false;
  350.                     break;
  351.                 }
  352.             }
  353.         }
  354.     }
  355.        
  356.     if (callbackNow) {
  357.         if (buffer) {
  358.             buffer.addStopListener(callback);
  359.         }
  360.         else {
  361.             callback();
  362.         }
  363.     }
  364. };
  365.  
  366. //////////////////////////////////////////////////
  367. //
  368. // Subtitle window
  369. //
  370. //////////////////////////////////////////////////
  371.  
  372. function Window_Subtitle() {
  373.     this.initialize.apply(this, arguments);
  374. }
  375.  
  376. Window_Subtitle.prototype = Object.create(Window_Base.prototype);
  377. Window_Subtitle.prototype.constructor = Window_Subtitle;
  378.  
  379. Window_Subtitle.prototype.initialize = function(offset) {
  380.     var width = this.windowWidth();
  381.     var height = this.windowHeight();
  382.     Window_Base.prototype.initialize.call(this, 0, SceneManager._screenHeight - offset - height, width, height);
  383.     this.opacity = 0;
  384.     this._showCount = 0;
  385.     this._text = '';
  386.     this._isDisplayed = false;
  387. };
  388.  
  389. Window_Subtitle.prototype.windowWidth = function() {
  390.     return Graphics.boxWidth;
  391. };
  392.  
  393. Window_Subtitle.prototype.windowHeight = function() {
  394.     return this.fittingHeight(this.maxLines());
  395. };
  396.  
  397. Window_Subtitle.prototype.maxLines = function() {
  398.     return 1;
  399. };
  400.  
  401. Window_Subtitle.prototype.refresh = function() {
  402.     this.contents.clear()
  403.     var width = this.contentsWidth();
  404.     var tw = this.textWidthEx(this._text);
  405.     var wx = (width - tw) / 2;
  406.     this.resetFontSettings();
  407.     this.drawTextEx(this._text, wx, 0);
  408. };
  409.  
  410. Scene_Battle.prototype.createSubtitleWindow = function(offset) {
  411.     this._subtitleWindow = new Window_Subtitle(offset);
  412.     this.addWindow(this._subtitleWindow);
  413. };
  414.  
  415. Jay.BattleVAManager.createAllWindows = Scene_Battle.prototype.createAllWindows;
  416. Scene_Battle.prototype.createAllWindows = function() {
  417.     Jay.BattleVAManager.createAllWindows.call(this);
  418.     this.createSubtitleWindow(this._statusWindow.height)
  419. };
  420.  
  421. Jay.BattleVAManager.createDisplayObjects = Scene_Battle.prototype.createDisplayObjects;
  422. Scene_Battle.prototype.createDisplayObjects = function() {
  423.     Jay.BattleVAManager.createDisplayObjects.call(this);
  424.     BattleManager.setSubtitleWindow(this._subtitleWindow);
  425. }
  426.  
  427. BattleManager.setSubtitleWindow = function(subtitleWindow) {
  428.     this._subtitleWindow = subtitleWindow;
  429. };
  430.  
  431. BattleManager.ShowSubtitleWindow = function(subtitle) {
  432.     if (!$gameSystem.getSubtitlesOn()) {
  433.         return;
  434.     }
  435.  
  436.     this._subtitleWindow._text = subtitle;
  437.     this._subtitleWindow._isDisplayed = true;
  438.     this._subtitleWindow.refresh();
  439. };
  440.  
  441. BattleManager.HideSubtitleWindow = function() {
  442.     this._subtitleWindow._text = '';
  443.     this._subtitleWindow._isDisplayed = false;
  444.     this._subtitleWindow.refresh();
  445. };
  446.  
  447. Jay.BattleVAManager.Game_System_initialize = Game_System.prototype.initialize;
  448. Game_System.prototype.initialize = function() {
  449.     Jay.BattleVAManager.Game_System_initialize.call(this);
  450.     this.setSubtitlesOn();
  451. };
  452.  
  453. Game_System.prototype.setSubtitlesOn = function() {
  454.     this._subtitlesOn = Jay.Param.SubtitlesOn;
  455. };
  456.  
  457. Game_System.prototype.getSubtitlesOn = function() {
  458.     if (this._subtitlesOn == undefined) {
  459.         this.setSubtitlesOn();
  460.     }
  461.     return this._subtitlesOn;
  462. };
RAW Paste Data
Top