Advertisement
Guest User

Jay_BattleVAManager.js

a guest
Jan 22nd, 2018
90
0
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.041
  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. Documentation will be included with version 1.0, but is now available on rpgmakerweb's forums.
  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.mandatoryResponse = false;
  66.                 voice.forcedResponse = false;
  67.                 voice.forced = false;
  68.                
  69.                 for (var paramIndex = 2; paramIndex < voiceRaw.length; paramIndex++) {
  70.                     var param = voiceRaw[paramIndex];
  71.                     if (param.match(/motion[ ]?=[ ]?([a-zA-Z]+)/i)) {
  72.                         voice.motion = RegExp.$1;
  73.                     }
  74.                     else if (param.toLowerCase().match(/response[ ]?=[ ]?([a-zA-Z0-9_]+) ([a-zA-Z0-9_]+)[ ]?(optional|required|forced)?/i)) {
  75.                         voice.responder = RegExp.$1;
  76.                         voice.response = RegExp.$2;
  77.                         if (RegExp.$3 == 'optional') {
  78.                             voice.mandatoryResponse = false;
  79.                         }
  80.                         else if (RegExp.$3 == 'required') {
  81.                             voice.mandatoryResponse = true;
  82.                         }
  83.                         else if (RegExp.$3 == 'forced') {
  84.                             voice.mandatoryResponse = true;
  85.                             voice.forcedResponse = true;
  86.                         }
  87.                     }
  88.                     else if (param.match(/pitch[ ]?=[ ]?([0-9]+)/i)) {
  89.                         voice.pitch = parseInt(RegExp.$1);
  90.                     }
  91.                     else if (param.match(/volume[ ]?=[ ]?([0-9]+)/i)) {
  92.                         voice.volume = parseInt(RegExp.$1);
  93.                     }
  94.                     else if (param.match(/sub[ ]?=[ ]?(.*)/i)) {
  95.                         voice.subtitle = RegExp.$1;
  96.                     }
  97.                     else if (param.match(/forced.*/i)) {
  98.                         forced = true;
  99.                     }
  100.                 }
  101.                 this._voiceFiles.push(voice);
  102.             }
  103.         }
  104.     }
  105.    
  106.     return this._voiceFiles;
  107. };
  108.  
  109. Game_BattlerBase.prototype.getVoiceFilesFromTag = function(voiceTag) {
  110.     return this.getVoiceFilesFromTag(voiceTag, false);
  111. }
  112.  
  113. Game_BattlerBase.prototype.getVoiceFilesFromTag = function(voiceTag, ignoreState) {
  114.     return this.getVoiceFiles().filter(function(voiceFile) {
  115.         return voiceFile.voiceName == voiceTag && (this.canSayLine(voiceFile) || ignoreState);
  116.     }.bind(this));
  117. };
  118.  
  119. Game_BattlerBase.prototype.canSayLine = function(voiceFile) {
  120.     if (!this.canTalk() || voiceFile.forced) {
  121.         return false;
  122.     }
  123.  
  124.     if (this.isActor() && !$gameParty.battleMembers().contains(this)) {
  125.         return false;
  126.     }
  127.  
  128.     if (this.isEnemy() && !$gameTroop._enemies.contains(this)) {
  129.         return false;
  130.     }
  131.  
  132.     if (voiceFile.response && voiceFile.mandatoryResponse == true) {
  133.         if (voiceFile.responder.match(/actor([0-9]+)/i)) {
  134.             var responder = $gameActors.actor(parseInt(RegExp.$1));
  135.             var forcedResponse = voiceFile.forcedResponse;
  136.            
  137.             if (!$gameParty.battleMembers().contains(responder)) {
  138.                 return false;
  139.             }
  140.            
  141.             if (responder.getVoiceFilesFromTag(voiceFile.response, forcedResponse).length == 0) {
  142.                 return false;
  143.             }
  144.         }
  145.         else if (voiceFile.responder.match(/enemy([0-9]+)/i)) {
  146.             var responderClass = parseInt(RegExp.$1);
  147.             var forcedResponse = voiceFile.forcedResponse;
  148.            
  149.             var possibleResponders = $gameTroop._enemies.filter(function(enemy) {
  150.                     return enemy._enemyId == responderClass &&
  151.                         enemy.getVoiceFilesFromTag(voiceFile.response, forcedResponse).length > 0;
  152.                 });
  153.  
  154.             if (possibleResponders.length == 0) {
  155.                 return false;
  156.             }
  157.         }
  158.         else {
  159.             // Format is screwed up
  160.             return false;
  161.         }
  162.     }
  163.  
  164.     return true;
  165. };
  166.  
  167. Game_BattlerBase.prototype.canTalk = function() {
  168.     var blockingStates = this.states().filter(function(state) {
  169.         return !!state.meta.novoice;
  170.     });
  171.  
  172.     return blockingStates.length == 0;
  173. };
  174.  
  175. Jay.BattleVAManager.Game_Actor_performCollapse = Game_Actor.prototype.performCollapse;
  176. Game_Actor.prototype.performCollapse = function() {
  177.     var koClips = this.getVoiceFilesFromTag("ko", true);
  178.    
  179.     if (koClips.length > 0) {
  180.         var clipToPlay = koClips[Math.randomInt(koClips.length)];
  181.         BattleManager._unitIsDying = true;
  182.         BattleManager.playClip(clipToPlay, function() {
  183.             Jay.BattleVAManager.Game_Actor_performCollapse.call(this);
  184.             BattleManager._unitIsDying = false;
  185.         }.bind(this));
  186.     }
  187.     else {
  188.         Jay.BattleVAManager.Game_Actor_performCollapse.call(this);
  189.     }
  190. }
  191.  
  192. Jay.BattleVAManager.Game_Enemy_performCollapse = Game_Enemy.prototype.performCollapse;
  193. Game_Enemy.prototype.performCollapse = function() {
  194.     var koClips = this.getVoiceFilesFromTag("ko", true);
  195.    
  196.     if (koClips.length > 0) {
  197.         var clipToPlay = koClips[Math.randomInt(koClips.length)];
  198.         BattleManager._unitIsDying = true;
  199.         BattleManager.playClip(clipToPlay, function() {
  200.             Jay.BattleVAManager.Game_Enemy_performCollapse.call(this);
  201.             BattleManager._unitIsDying = false;
  202.         }.bind(this));
  203.     }
  204.     else {
  205.         Jay.BattleVAManager.Game_Enemy_performCollapse.call(this);
  206.     }
  207. }
  208.  
  209. AudioManager.playSeAndReturn = function(se) {
  210.     if (se.name) {
  211.         this._seBuffers = this._seBuffers.filter(function(audio) {
  212.             return audio.isPlaying();
  213.         });
  214.         var buffer = this.createBuffer('se', se.name);
  215.         this.updateSeParameters(buffer, se);
  216.         buffer.play(false);
  217.         this._seBuffers.push(buffer);
  218.         return buffer;
  219.     }
  220.     return null;
  221. };
  222.  
  223. Jay.BattleVAManager.startBattle = BattleManager.startBattle;
  224. BattleManager.startBattle = function() {
  225.     var battleStartClipsToPlay = [];
  226.    
  227.     $gameTroop._enemies.forEach(function(enemy) {
  228.         if (enemy.canTalk()) {
  229.             var voiceFiles = enemy.getVoiceFilesFromTag("battlestart");
  230.             voiceFiles.forEach(function(voiceFile) {
  231.                 battleStartClipsToPlay.push(voiceFile);
  232.             });
  233.         }
  234.     });
  235.  
  236.     if (battleStartClipsToPlay.length == 0) {
  237.         $gameParty._actors.forEach(function(actorId) {
  238.             var actor = $gameActors.actor(actorId);
  239.             if (actor.canTalk()) {
  240.                 var voiceFiles = actor.getVoiceFilesFromTag("battlestart");
  241.                 voiceFiles.forEach(function(voiceFile) {
  242.                     battleStartClipsToPlay.push(voiceFile);
  243.                 });
  244.             }
  245.         });
  246.     }
  247.  
  248.     if (battleStartClipsToPlay.length > 0) {
  249.         var clip = battleStartClipsToPlay[Math.randomInt(battleStartClipsToPlay.length)];
  250.  
  251.         this.playClip(clip, function() {
  252.                 Jay.BattleVAManager.startBattle.call(this)
  253.             }.bind(this));
  254.     }
  255.     else {
  256.         Jay.BattleVAManager.startBattle.call(this);
  257.     }
  258. };
  259.  
  260. BattleManager.isBusy = function() {
  261.     return ($gameMessage.isBusy() || this._spriteset.isBusy() ||
  262.             this._logWindow.isBusy() || this._unitIsDying);
  263. };
  264.  
  265. BattleManager.lastActor = function() {
  266.     if (this._action && this._action._subjectActorId > 0) {
  267.         return $gameActors.actor(this._action._subjectActorId);
  268.     }
  269.     return null;
  270. }
  271.  
  272. Jay.BattleVAManager.BattleManager_initMembers = BattleManager.initMembers;
  273. BattleManager.initMembers = function() {    
  274.     Jay.BattleVAManager.BattleManager_initMembers.call(this);
  275.     this._startVictoryPhase = false;
  276. }
  277.  
  278. Jay.BattleVAManager.BattleManager_playVictoryMe = BattleManager.playVictoryMe;
  279. BattleManager.playVictoryMe = function() {
  280. };
  281.  
  282. Jay.BattleVAManager.processVictory = BattleManager.processVictory;
  283. BattleManager.processVictory = function() {
  284.     if (this._startVictoryPhase) {
  285.         return;
  286.     }
  287.  
  288.     $gameParty.performVictory();
  289.     if (!$gameSystem.skipVictoryMusic || !$gameSystem.skipVictoryMusic()) {
  290.         Jay.BattleVAManager.BattleManager_playVictoryMe.call(this);
  291.     }
  292.     this._startVictoryPhase = true;
  293.  
  294.     var victoryClipsToPlay = [];
  295.  
  296.     var winningActor = this.lastActor();
  297.     if (winningActor != null && winningActor.canTalk()) {
  298.         var voiceFiles = winningActor.getVoiceFilesFromTag("battlevictory");
  299.         voiceFiles.forEach(function(voiceFile) {
  300.             victoryClipsToPlay.push(voiceFile);
  301.         });
  302.     }
  303.  
  304.     $gameParty._actors.forEach(function(actorId) {
  305.         var actor = $gameActors.actor(actorId);
  306.         if (actor.canTalk()) {
  307.             var voiceFiles = actor.getVoiceFilesFromTag("battleend");
  308.             voiceFiles.forEach(function(voiceFile) {
  309.                 victoryClipsToPlay.push(voiceFile);
  310.             });
  311.         }
  312.     });
  313.  
  314.     if (victoryClipsToPlay.length > 0) {
  315.         var clip = victoryClipsToPlay[Math.randomInt(victoryClipsToPlay.length)];
  316.  
  317.         this.playClip(clip, function() {
  318.                 Jay.BattleVAManager.processVictory.call(this)
  319.             }.bind(this));
  320.     }
  321.     else {
  322.         Jay.BattleVAManager.processVictory.call(this);
  323.     }
  324. }
  325.  
  326. BattleManager.playClip = function(clip, callback) {
  327.     if (clip.motion && clip.speaker.forceMotion) {
  328.         clip.speaker.forceMotion(clip.motion);
  329.     }
  330.  
  331.     var buffer = null;
  332.  
  333.     if (clip.voiceFile.toLowerCase() != 'none') {
  334.         var volume = 100;
  335.         var pitch = 100;
  336.  
  337.         if (clip.volume) {
  338.             volume = clip.volume;
  339.         }
  340.  
  341.         if (clip.pitch) {
  342.             pitch = clip.pitch;
  343.         }
  344.  
  345.         // TODO: See if I can set "pan" to something relating to unit location
  346.         var se = {
  347.             name: clip.voiceFile,
  348.             volume: volume,
  349.             pitch: pitch,
  350.             pan: 0
  351.         };
  352.        
  353.         var buffer = AudioManager.playSeAndReturn(se);
  354.     }
  355.  
  356.     if(clip.subtitle) {
  357.         BattleManager.ShowSubtitleWindow(clip.subtitle);
  358.     }
  359.    
  360.     if (clip.subtitle) {
  361.         if(buffer) {
  362.             buffer.addStopListener(function() {
  363.                     BattleManager.HideSubtitleWindow();
  364.                 }.bind(this));
  365.         }
  366.         else {
  367.             BattleManager.HideSubtitleWindow();
  368.         }
  369.     }
  370.  
  371.     var callbackNow = !!callback;
  372.  
  373.     if (clip.response) {
  374.         if (clip.responder.match(/actor([0-9]+)/i)) {
  375.             var responder = parseInt(RegExp.$1);
  376.             var forcedResponse = clip.forcedResponse;
  377.  
  378.             if (responder > 0 && $gameParty.battleMembers().contains($gameActors.actor(responder))) {
  379.                 var clips = $gameActors.actor(responder).getVoiceFilesFromTag(clip.response, forcedResponse);
  380.                 if (clips.length > 0) {
  381.                     var response = clips[Math.randomInt(clips.length)];
  382.  
  383.                     if (buffer) {
  384.                         buffer.addStopListener(function() {
  385.                                 this.playClip(response, callback);
  386.                             }.bind(this));
  387.                     }
  388.                     else {
  389.                         this.playClip(response, callback);
  390.                     }
  391.  
  392.                     callbackNow = false;
  393.                 }
  394.             }
  395.         }
  396.         else if (clip.responder.match(/enemy([0-9]+)/i)) {
  397.             var responder = parseInt(RegExp.$1);
  398.             var forcedResponse = clip.forcedResponse;
  399.                
  400.             var possibleResponders = $gameTroop._enemies.filter(function(enemy) {
  401.                 return enemy._enemyId == responder;
  402.             });
  403.  
  404.             for (var i = 0; i < possibleResponders.length; i++) {
  405.                 var clips = possibleResponders[i].getVoiceFilesFromTag(clip.response, forcedResponse);
  406.                 if (clips.length > 0) {
  407.                     var response = clips[Math.randomInt(clips.length)];
  408.  
  409.                     if (buffer) {
  410.                         buffer.addStopListener(function() {
  411.                                 this.playClip(response, callback);
  412.                             }.bind(this));
  413.                     }
  414.                     else {
  415.                         this.playClip(response, callback);
  416.                     }
  417.  
  418.                     callbackNow = false;
  419.                     break;
  420.                 }
  421.             }
  422.         }
  423.     }
  424.        
  425.     if (callbackNow) {
  426.         if (buffer) {
  427.             buffer.addStopListener(callback);
  428.         }
  429.         else {
  430.             callback();
  431.         }
  432.     }
  433. };
  434.  
  435. //////////////////////////////////////////////////
  436. //
  437. // Subtitle window
  438. //
  439. //////////////////////////////////////////////////
  440.  
  441. function Window_Subtitle() {
  442.     this.initialize.apply(this, arguments);
  443. }
  444.  
  445. Window_Subtitle.prototype = Object.create(Window_Base.prototype);
  446. Window_Subtitle.prototype.constructor = Window_Subtitle;
  447.  
  448. Window_Subtitle.prototype.initialize = function(offset) {
  449.     var width = this.windowWidth();
  450.     var height = this.windowHeight();
  451.     Window_Base.prototype.initialize.call(this, 0, SceneManager._screenHeight - offset - height, width, height);
  452.     this.opacity = 0;
  453.     this._showCount = 0;
  454.     this._text = '';
  455.     this._isDisplayed = false;
  456. };
  457.  
  458. Window_Subtitle.prototype.windowWidth = function() {
  459.     return Graphics.boxWidth;
  460. };
  461.  
  462. Window_Subtitle.prototype.windowHeight = function() {
  463.     return this.fittingHeight(this.maxLines());
  464. };
  465.  
  466. Window_Subtitle.prototype.maxLines = function() {
  467.     return 1;
  468. };
  469.  
  470. Window_Subtitle.prototype.refresh = function() {
  471.     this.contents.clear()
  472.     if (this.textWidthEx != undefined) {
  473.         var width = this.contentsWidth();
  474.         var tw = this.textWidthEx(this._text);
  475.         var wx = (width - tw) / 2;
  476.         this.resetFontSettings();
  477.         this.drawTextEx(this._text, wx, 0);
  478.     }
  479.     else {
  480.         this.drawTextEx(this._text, 0, 0);
  481.     }
  482. };
  483.  
  484. Scene_Battle.prototype.createSubtitleWindow = function(offset) {
  485.     this._subtitleWindow = new Window_Subtitle(offset);
  486.     this.addWindow(this._subtitleWindow);
  487. };
  488.  
  489. Jay.BattleVAManager.createAllWindows = Scene_Battle.prototype.createAllWindows;
  490. Scene_Battle.prototype.createAllWindows = function() {
  491.     Jay.BattleVAManager.createAllWindows.call(this);
  492.     this.createSubtitleWindow(this._statusWindow.height)
  493. };
  494.  
  495. Jay.BattleVAManager.createDisplayObjects = Scene_Battle.prototype.createDisplayObjects;
  496. Scene_Battle.prototype.createDisplayObjects = function() {
  497.     Jay.BattleVAManager.createDisplayObjects.call(this);
  498.     BattleManager.setSubtitleWindow(this._subtitleWindow);
  499. }
  500.  
  501. BattleManager.setSubtitleWindow = function(subtitleWindow) {
  502.     this._subtitleWindow = subtitleWindow;
  503. };
  504.  
  505. BattleManager.ShowSubtitleWindow = function(subtitle) {
  506.     if (!$gameSystem.getSubtitlesOn()) {
  507.         return;
  508.     }
  509.  
  510.     this._subtitleWindow._text = subtitle;
  511.     this._subtitleWindow._isDisplayed = true;
  512.     this._subtitleWindow.refresh();
  513. };
  514.  
  515. BattleManager.HideSubtitleWindow = function() {
  516.     this._subtitleWindow._text = '';
  517.     this._subtitleWindow._isDisplayed = false;
  518.     this._subtitleWindow.refresh();
  519. };
  520.  
  521. Jay.BattleVAManager.Game_System_initialize = Game_System.prototype.initialize;
  522. Game_System.prototype.initialize = function() {
  523.     Jay.BattleVAManager.Game_System_initialize.call(this);
  524.     this.setSubtitlesOn();
  525. };
  526.  
  527. Game_System.prototype.setSubtitlesOn = function() {
  528.     this._subtitlesOn = Jay.Param.SubtitlesOn;
  529. };
  530.  
  531. Game_System.prototype.getSubtitlesOn = function() {
  532.     if (this._subtitlesOn == undefined) {
  533.         this.setSubtitlesOn();
  534.     }
  535.     return this._subtitlesOn;
  536. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement