/*:----------------------------------------------------------------------------------- * NUUN_GamePadVibration.js * * Copyright (C) 2023 NUUN * ------------------------------------------------------------------------------------- */ /*: * @target MZ * @plugindesc Gamepad Vibration * @author NUUN * @base NUUN_Base * @orderAfter NUUN_Base * @version 1.1.0 * * @help * This is a plugin for vibrating the gamepad on X Input. * * Control character * \VG[id]:Vibrate the gamepad. * [id]:Specify the ID in the "VibrationSetting" list number of the plug-in parameter. * * Specified from script * NuunManager.sprictGamePadVibration(StartDelay, Duration, WeakMagnitude, StrongMagnitude) * * Terms of Use * Credit: Optional * Commercial use: Possible * Adult content: Possible * Modifications: Possible * Redistribution: Possible * Support is not available for modified versions or downloads from sources other than https://github.com/nuun888/MZ, the official forum, or authorized retailers. * * Log * 4/2/2023 Ver.1.1.0 * Added function to vibrate gamepad from text code. * 3/16/2023 Ver.1.0.2 * Fixed not to display in the option if the game pad is not recognized. * 3/12/2023 Ver.1.0.1 * Changed the vibration start setting from milliseconds to frames. * Supported so that it can be easily specified from the script. * 2/26/2023 Ver.1.0.0 * First edition. * * @command OnVibration * @desc Perform gamepad vibration. * @text Gamepad vibration * * @arg VibrationSetting * @type struct * @default * @text Vibration settings * @desc Set vibration. * * * @param OptionGamePadVibrationName * @desc Set the name of the gamepad vibration enable to be displayed in the option. * @text Gamepad vibration enabled * @type string * @default Gamepad vibration enabled * * @param VibrationSetting * @type struct[] * @default [] * @text Vibration settings * @desc Set vibration. * */ /*~struct~VibrationData: * * @param StartDelay * @desc Number of delay frames before vibration starts. * @text Start delay frame * @type number * @default 0 * @min 0 * * @param Duration * @desc Number of vibration frames. * @text Number of vibration frames * @type number * @default 120 * * @param WeakMagnitude * @desc The rumble strength of the high frequency (weak) rumble motor. * @text High frequency rumble intensity * @type string * @default 1.0 * * @param StrongMagnitude * @desc The rumble strength of the low frequency (strong) rumble motor. * @text Low frequency rumble strength * @type string * @default 1.0 * */ /*:ja * @target MZ * @plugindesc ゲームパッド振動 * @author NUUN * @base NUUN_Base * @orderAfter NUUN_Base * @version 1.1.0 * * @help * X Inputでのゲームパッドを振動させるためのプラグインです。 * * 制御文字 * \VG[id]:ゲームパッドを振動させます。 * [id]:プラグインパラメータの振動設定リスト番号内のIDを指定します。 * * スクリプトから指定 * NuunManager.sprictGamePadVibration(StartDelay, Duration, WeakMagnitude, StrongMagnitude) * * 利用規約 * クレジット表記:任意 * 商業利用:可能 * 成人向け:可能 * 改変:可能 * 再配布:可能 * https://github.com/nuun888/MZ、公式フォーラム、正規販売サイト以外からのダウンロード、改変済みの場合はサポートは対象外となります。 * * 更新履歴 * 2023/4/2 Ver.1.1.0 * 制御文字からゲームパッドを振動させる機能を追加。 * 2023/3/16 Ver.1.0.2 * ゲームパッドを認識してない場合はオプションに表示しないように修正。 * 2023/3/12 Ver.1.0.1 * 振動を開始する設定をミリ秒からフレーム数に変更。 * スクリプトから容易に指定できるように対応。 * 2023/2/26 Ver.1.0.0 * 初版。 * * @command OnVibration * @desc ゲームパッドの振動を実行します。 * @text ゲームパッド振動 * * @arg VibrationSetting * @type struct * @default * @text 振動設定 * @desc 振動の設定を行います。 * * * @param OptionGamePadVibrationName * @desc オプションに表示するゲームパッド振動有効の名称を設定します。 * @text ゲームパッド振動有効 * @type string * @default ゲームパッド振動 * * @param VibrationSetting * @type struct[] * @default [] * @text 振動設定 * @desc 振動の設定を行います。 * */ /*~struct~VibrationData:ja * * @param StartDelay * @desc 振動を開始するまでのディレイフレーム数 * @text 開始ディレイフレーム * @type number * @default 0 * @min 0 * * @param Duration * @desc 振動フレーム数 * @text 振動フレーム数 * @type number * @default 120 * * @param WeakMagnitude * @desc 高周波 (弱い) ランブル モーターのランブル強度。 * @text 高周波ランブル強度 * @type string * @default 1.0 * * @param StrongMagnitude * @desc 低周波 (強い) ランブル モーターのランブル強度。 * @text 低周波ランブル強度 * @type string * @default 1.0 * */ var Imported = Imported || {}; Imported.NUUN_GamePadVibration = true; (() => { const parameters = PluginManager.parameters('NUUN_GamePadVibration'); const OptionGamePadVibrationName = String(parameters['OptionGamePadVibrationName'] || "ゲームパッド振動"); const VibrationSetting = NUUN_Base_Ver >= 113 ? (DataManager.nuun_structureData(parameters['VibrationSetting'])) : []; const pluginName = "NUUN_GamePadVibration"; let _onGamepad = false; PluginManager.registerCommand(pluginName, 'OnVibration', args => { if (args.VibrationSetting) { const data = DataManager.nuun_structureData(args.VibrationSetting); NuunManager.setupGamePadVibration(data); } }); const _Scene_Base_initialize = Scene_Base.prototype.initialize; Scene_Base.prototype.initialize = function() { _Scene_Base_initialize.call(this); NuunManager.actuatorDuration = 0; }; const _Scene_Base_update = Scene_Base.prototype.update; Scene_Base.prototype.update = function() { _Scene_Base_update.call(this); NuunManager.updateVibration(); }; // NuunManager.sprictGamePadVibration = function(data1, data2, data3, data4) { const vibration = {}; vibration.StartDelay = Number(data1); vibration.Duration = Number(data2); vibration.WeakMagnitude = Number(data3); vibration.StrongMagnitude = Number(data4); this.setupGamePadVibration(vibration); }; NuunManager.setupGamePadVibration = function(data) { if (navigator.getGamepads && ConfigManager.gamePadVibration) { const gamepad = navigator.getGamepads()[0]; if (gamepad && gamepad.vibrationActuator) { this.setVibration(data); } } }; NuunManager.setVibration = function(data) { if (data.Duration > this.actuatorDuration) { this.actuatorDelay = data.StartDelay; this._actuatorData = data; this.actuatorDuration = data.Duration; } }; NuunManager.updateVibration = function() { if (this.actuatorDelay > 0) { this.actuatorDelay--; } if (this.actuatorDelay === 0 && this.actuatorDuration > 0) { const gamepads = navigator.getGamepads(); for (const gamepad of gamepads) { if (gamepad && gamepad.connected) { const actuator = gamepad.vibrationActuator; if (actuator) { //const agent = window.navigator.userAgent.toLowerCase(); //if (agent.indexOf('firefox') >= 0) {//firefox //actuator.pulse(this._actuatorData.WeakMagnitude, 20); //} else { actuator.playEffect(actuator.type, { startDelay: 0, duration: 20, weakMagnitude: this._actuatorData.WeakMagnitude, strongMagnitude: this._actuatorData.StrongMagnitude, }); //} } } } this.actuatorDuration--; } }; // const _Window_Base_processEscapeCharacter = Window_Base.prototype.processEscapeCharacter; Window_Base.prototype.processEscapeCharacter = function(code, textState) { switch (code) { case "VG": this.processGamepadVibration(this.obtainEscapeParam(textState)); break; default: _Window_Base_processEscapeCharacter.call(this, code, textState); break; } }; Window_Base.prototype.processGamepadVibration = function(state) { const data = VibrationSetting[state -1]; if (data) { NuunManager.setupGamePadVibration(data); } }; const _Scene_Options_initialize = Scene_Options.prototype.initialize; Scene_Options.prototype.initialize = function() { _Scene_Options_initialize.call(this); _onGamepad = !!navigator.getGamepads()[0]; }; const _Scene_Options_maxCommands = Scene_Options.prototype.maxCommands; Scene_Options.prototype.maxCommands = function() { return _Scene_Options_maxCommands.call(this) + (_onGamepad ? 1 : 0); }; const _Window_Options_addGeneralOptions = Window_Options.prototype.addGeneralOptions; Window_Options.prototype.addGeneralOptions = function() { _Window_Options_addGeneralOptions.call(this); if (_onGamepad) { this.addCommand(OptionGamePadVibrationName, "gamePadVibration"); } }; ConfigManager.gamePadVibration = true; const _ConfigManager_makeData = ConfigManager.makeData; ConfigManager.makeData = function() { const config = _ConfigManager_makeData.call(this); config.gamePadVibration = this.gamePadVibration; return config; }; const _ConfigManager_applyData = ConfigManager.applyData; ConfigManager.applyData = function(config) { _ConfigManager_applyData.call(this, config); this.gamePadVibration = this.readFlag(config, "gamePadVibration", true); }; })();