Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // Battle Voice Manager
- // Jay_BattleVAManager.js
- // Version 0.03
- //=============================================================================
- var Imported = Imported || {};
- Imported.Jay_BattleVAManager = true;
- var Jay = Jay || {};
- Jay.BattleVAManager = Jay.BattleVAManager || {};
- //=============================================================================
- /*:
- * @plugindesc Controls voice acting in battle
- *
- * @author Jason R. Godding
- *
- * @param Subtitles On
- * @type boolean
- * @desc Turn on and subtitles will be available by default when starting a new game.
- * @default false
- *
- * @help Allows battle voiceovers. Currently only works with battlestart.
- *
- * This plugin is free for non-commercial and commercial use, but please credit
- * Jason R. Godding if you use it. Thank you.
- *
- */
- Jay.Parameters = Jay.Parameters || {};
- Jay.Parameters.BattleVAManager = PluginManager.parameters('Jay_BattleVAManager');
- Jay.Param = Jay.Param || {};
- Jay.Param.SubtitlesOn = eval(String(Jay.Parameters.BattleVAManager['Subtitles On']));
- Game_BattlerBase.prototype.getVoiceFiles = function() {
- if (!this._voiceFiles) {
- this._voiceFiles = [];
- var voicesRaw = null;
- if (this.isActor()) {
- voicesRaw = this.actor().meta.voice;
- }
- else {
- voicesRaw = this.enemy().meta.voice;
- }
- if (voicesRaw == null) {
- return [];
- }
- var splitVoicesRaw = voicesRaw.split('\n');
- for (var i = 0; i < splitVoicesRaw.length; i++) {
- if (splitVoicesRaw[i] != '') {
- var voiceRaw = splitVoicesRaw[i].split('|');
- if (voiceRaw.length < 2) {
- throw new Error('Invalid voice file setup: ' + splitVoicesRaw[i]);
- }
- var voice = {};
- voice.voiceName = voiceRaw[0];
- voice.voiceFile = voiceRaw[1];
- voice.speaker = this;
- voice.forcedResponse = false;
- for (var paramIndex = 2; paramIndex < voiceRaw.length; paramIndex++) {
- var param = voiceRaw[paramIndex];
- if (param.match(/motion[ ]?=[ ]?([a-zA-Z]+)/i)) {
- voice.motion = RegExp.$1;
- }
- else if (param.toLowerCase().match(/response[ ]?=[ ]?([a-zA-Z0-9_]+) ([a-zA-Z0-9_]+)[ ]?(optional|required)?/i)) {
- voice.responder = RegExp.$1;
- voice.response = RegExp.$2;
- if (RegExp.$3 == 'optional') {
- voice.forcedResponse = false;
- }
- else if (RegExp.$3 == 'required') {
- voice.forcedResponse = true;
- }
- }
- else if (param.match(/pitch[ ]?=[ ]?([0-9]+)/i)) {
- voice.pitch = parseInt(RegExp.$1);
- }
- else if (param.match(/volume[ ]?=[ ]?([0-9]+)/i)) {
- voice.volume = parseInt(RegExp.$1);
- }
- else if (param.match(/sub[ ]?=[ ]?(.*)/i)) {
- voice.subtitle = RegExp.$1;
- }
- }
- this._voiceFiles.push(voice);
- }
- }
- }
- return this._voiceFiles;
- };
- Game_BattlerBase.prototype.getVoiceFilesFromTag = function(voiceTag) {
- return this.getVoiceFiles().filter(function(voiceFile) {
- return voiceFile.voiceName == voiceTag && this.canSayLine(voiceFile);
- }.bind(this));
- };
- Game_BattlerBase.prototype.canSayLine = function(voiceFile) {
- if (!this.canTalk()) {
- return false;
- }
- if (this.isActor() && !$gameParty.battleMembers().contains(this)) {
- return false;
- }
- if (this.isEnemy() && !$gameTroop._enemies.contains(this)) {
- return false;
- }
- if (voiceFile.response && voiceFile.forcedResponse == true) {
- if (voiceFile.responder.match(/actor([0-9]+)/i)) {
- var responder = $gameActors.actor(parseInt(RegExp.$1));
- if (!$gameParty.battleMembers().contains(responder)) {
- return false;
- }
- if (responder.getVoiceFilesFromTag(voiceFile.response).length == 0) {
- return false;
- }
- }
- else if (voiceFile.responder.match(/enemy([0-9]+)/i)) {
- var responderClass = parseInt(RegExp.$1);
- var possibleResponders = $gameTroop._enemies.filter(function(enemy) {
- return enemy._enemyId == responderClass &&
- enemy.getVoiceFilesFromTag(voiceFile.response).length > 0;
- });
- if (possibleResponders.length == 0) {
- return false;
- }
- }
- else {
- // Format is screwed up
- return false;
- }
- }
- return true;
- };
- Game_BattlerBase.prototype.canTalk = function() {
- var blockingStates = this.states().filter(function(state) {
- return !!state.meta.novoice;
- });
- return blockingStates.length == 0;
- };
- AudioManager.playSeAndReturn = function(se) {
- if (se.name) {
- this._seBuffers = this._seBuffers.filter(function(audio) {
- return audio.isPlaying();
- });
- var buffer = this.createBuffer('se', se.name);
- this.updateSeParameters(buffer, se);
- buffer.play(false);
- this._seBuffers.push(buffer);
- return buffer;
- }
- return null;
- };
- Jay.BattleVAManager.startBattle = BattleManager.startBattle;
- BattleManager.startBattle = function() {
- var battleStartClipsToPlay = [];
- $gameTroop._enemies.forEach(function(enemy) {
- if (enemy.canTalk()) {
- var voiceFiles = enemy.getVoiceFilesFromTag("battlestart");
- voiceFiles.forEach(function(voiceFile) {
- battleStartClipsToPlay.push(voiceFile);
- });
- }
- });
- if (battleStartClipsToPlay.length == 0) {
- $gameParty._actors.forEach(function(actorId) {
- var actor = $gameActors.actor(actorId);
- if (actor.canTalk()) {
- var voiceFiles = actor.getVoiceFilesFromTag("battlestart");
- voiceFiles.forEach(function(voiceFile) {
- battleStartClipsToPlay.push(voiceFile);
- });
- }
- });
- }
- if (battleStartClipsToPlay.length > 0) {
- var clip = battleStartClipsToPlay[Math.randomInt(battleStartClipsToPlay.length)];
- this.playClip(clip, function() {
- Jay.BattleVAManager.startBattle.call(this)
- }.bind(this));
- }
- else {
- Jay.BattleVAManager.startBattle.call(this);
- }
- };
- BattleManager.lastActor = function() {
- if (this._action && this._action._subjectActorId > 0) {
- return $gameActors.actor(this._action._subjectActorId);
- }
- return null;
- }
- Jay.BattleVAManager.BattleManager_initMembers = BattleManager.initMembers;
- BattleManager.initMembers = function() {
- Jay.BattleVAManager.BattleManager_initMembers.call(this);
- this._startVictoryPhase = false;
- }
- Jay.BattleVAManager.BattleManager_playVictoryMe = BattleManager.playVictoryMe;
- BattleManager.playVictoryMe = function() {
- };
- Jay.BattleVAManager.processVictory = BattleManager.processVictory;
- BattleManager.processVictory = function() {
- if (this._startVictoryPhase) {
- return;
- }
- $gameParty.performVictory();
- Jay.BattleVAManager.BattleManager_playVictoryMe.call(this);
- this._startVictoryPhase = true;
- var victoryClipsToPlay = [];
- var winningActor = this.lastActor();
- if (winningActor != null && winningActor.canTalk()) {
- var voiceFiles = winningActor.getVoiceFilesFromTag("battleend");
- voiceFiles.forEach(function(voiceFile) {
- victoryClipsToPlay.push(voiceFile);
- });
- }
- if (victoryClipsToPlay.length > 0) {
- var clip = victoryClipsToPlay[Math.randomInt(victoryClipsToPlay.length)];
- this.playClip(clip, function() {
- Jay.BattleVAManager.processVictory.call(this)
- }.bind(this));
- }
- else {
- Jay.BattleVAManager.processVictory.call(this);
- }
- }
- BattleManager.playClip = function(clip, callback) {
- if (clip.motion && clip.speaker.forceMotion) {
- clip.speaker.forceMotion(clip.motion);
- }
- var buffer = null;
- if (clip.voiceFile.toLowerCase() != 'none') {
- var volume = 100;
- var pitch = 100;
- if (clip.volume) {
- volume = clip.volume;
- }
- if (clip.pitch) {
- pitch = clip.pitch;
- }
- // TODO: See if I can set "pan" to something relating to unit location
- var se = {
- name: clip.voiceFile,
- volume: volume,
- pitch: pitch,
- pan: 0
- };
- var buffer = AudioManager.playSeAndReturn(se);
- }
- if(clip.subtitle) {
- BattleManager.ShowSubtitleWindow(clip.subtitle);
- }
- if (clip.subtitle) {
- if(buffer) {
- buffer.addStopListener(function() {
- BattleManager.HideSubtitleWindow();
- }.bind(this));
- }
- else {
- BattleManager.HideSubtitleWindow();
- }
- }
- var callbackNow = !!callback;
- if (clip.response) {
- if (clip.responder.match(/actor([0-9]+)/i)) {
- var responder = parseInt(RegExp.$1);
- if (responder > 0 && $gameParty.battleMembers().contains($gameActors.actor(responder))) {
- var clips = $gameActors.actor(responder).getVoiceFilesFromTag(clip.response);
- if (clips.length > 0) {
- var response = clips[Math.randomInt(clips.length)];
- if (buffer) {
- buffer.addStopListener(function() {
- this.playClip(response, callback);
- }.bind(this));
- }
- else {
- this.playClip(response, callback);
- }
- callbackNow = false;
- }
- }
- }
- else if (clip.responder.match(/enemy([0-9]+)/i)) {
- var responder = parseInt(RegExp.$1);
- var possibleResponders = $gameTroop._enemies.filter(function(enemy) {
- return enemy._enemyId == responder;
- });
- for (var i = 0; i < possibleResponders.length; i++) {
- var clips = possibleResponders[i].getVoiceFilesFromTag(clip.response);
- if (clips.length > 0) {
- var response = clips[Math.randomInt(clips.length)];
- if (buffer) {
- buffer.addStopListener(function() {
- this.playClip(response, callback);
- }.bind(this));
- }
- else {
- this.playClip(response, callback);
- }
- callbackNow = false;
- break;
- }
- }
- }
- }
- if (callbackNow) {
- if (buffer) {
- buffer.addStopListener(callback);
- }
- else {
- callback();
- }
- }
- };
- //////////////////////////////////////////////////
- //
- // Subtitle window
- //
- //////////////////////////////////////////////////
- function Window_Subtitle() {
- this.initialize.apply(this, arguments);
- }
- Window_Subtitle.prototype = Object.create(Window_Base.prototype);
- Window_Subtitle.prototype.constructor = Window_Subtitle;
- Window_Subtitle.prototype.initialize = function(offset) {
- var width = this.windowWidth();
- var height = this.windowHeight();
- Window_Base.prototype.initialize.call(this, 0, SceneManager._screenHeight - offset - height, width, height);
- this.opacity = 0;
- this._showCount = 0;
- this._text = '';
- this._isDisplayed = false;
- };
- Window_Subtitle.prototype.windowWidth = function() {
- return Graphics.boxWidth;
- };
- Window_Subtitle.prototype.windowHeight = function() {
- return this.fittingHeight(this.maxLines());
- };
- Window_Subtitle.prototype.maxLines = function() {
- return 1;
- };
- Window_Subtitle.prototype.refresh = function() {
- this.contents.clear()
- var width = this.contentsWidth();
- var tw = this.textWidthEx(this._text);
- var wx = (width - tw) / 2;
- this.resetFontSettings();
- this.drawTextEx(this._text, wx, 0);
- };
- Scene_Battle.prototype.createSubtitleWindow = function(offset) {
- this._subtitleWindow = new Window_Subtitle(offset);
- this.addWindow(this._subtitleWindow);
- };
- Jay.BattleVAManager.createAllWindows = Scene_Battle.prototype.createAllWindows;
- Scene_Battle.prototype.createAllWindows = function() {
- Jay.BattleVAManager.createAllWindows.call(this);
- this.createSubtitleWindow(this._statusWindow.height)
- };
- Jay.BattleVAManager.createDisplayObjects = Scene_Battle.prototype.createDisplayObjects;
- Scene_Battle.prototype.createDisplayObjects = function() {
- Jay.BattleVAManager.createDisplayObjects.call(this);
- BattleManager.setSubtitleWindow(this._subtitleWindow);
- }
- BattleManager.setSubtitleWindow = function(subtitleWindow) {
- this._subtitleWindow = subtitleWindow;
- };
- BattleManager.ShowSubtitleWindow = function(subtitle) {
- if (!$gameSystem.getSubtitlesOn()) {
- return;
- }
- this._subtitleWindow._text = subtitle;
- this._subtitleWindow._isDisplayed = true;
- this._subtitleWindow.refresh();
- };
- BattleManager.HideSubtitleWindow = function() {
- this._subtitleWindow._text = '';
- this._subtitleWindow._isDisplayed = false;
- this._subtitleWindow.refresh();
- };
- Jay.BattleVAManager.Game_System_initialize = Game_System.prototype.initialize;
- Game_System.prototype.initialize = function() {
- Jay.BattleVAManager.Game_System_initialize.call(this);
- this.setSubtitlesOn();
- };
- Game_System.prototype.setSubtitlesOn = function() {
- this._subtitlesOn = Jay.Param.SubtitlesOn;
- };
- Game_System.prototype.getSubtitlesOn = function() {
- if (this._subtitlesOn == undefined) {
- this.setSubtitlesOn();
- }
- return this._subtitlesOn;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement