urami-demo/www/js/plugins/SelfSwitchController_MV.js
2026-04-24 20:01:10 -05:00

101 lines
No EOL
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//=============================================================================
// SelfSwitchController_Private_MV.js
//=============================================================================
// RPG Maker MV Plugin
// Control Self Switches (ON / OFF)
//=============================================================================
/*:
* @plugindesc (MV) Turn ON / OFF self switches (AD) of any event via plugin commands.
*
* @help
* -----------------------------------------------------------------------------
* Plugin Commands (RPG Maker MV)
* -----------------------------------------------------------------------------
*
* ■ OFF (delete / reset) self switches
* SS_OFF mapId eventId [switchId]
*
* mapId : 0 = all maps, -1 = current map, 1+ = specific map ID
* eventId : 0 = all events, 1+ = specific event ID
* switchId: A / B / C / D (optional; omit = all)
*
* ■ ON (set) a self switch
* SS_ON mapId eventId switchId
*
* mapId : -1 = current map, 1+ = specific map ID
* eventId : 1+ = specific event ID
* switchId: A / B / C / D
*
* -----------------------------------------------------------------------------
* Notes:
* - Map refresh is requested after each operation.
* - OFF removes the key from $gameSelfSwitches.
*/
(function() {
'use strict';
var CMD_SS_OFF = 'SS_OFF';
var CMD_SS_ON = 'SS_ON';
function toNumber(value, defaultValue) {
var n = parseInt(value, 10);
return isNaN(n) ? defaultValue : n;
}
function normalizeSwitchLetter(letter) {
letter = (letter || '').toString().trim().toUpperCase();
return ['A', 'B', 'C', 'D'].includes(letter) ? letter : '';
}
function turnOffSelfSwitches(targetMapId, targetEventId, targetSwitch) {
if (targetMapId < 0) targetMapId = $gameMap.mapId();
var data = $gameSelfSwitches._data;
for (var key in data) {
if (!data.hasOwnProperty(key)) continue;
var parts = key.split(',');
var mapId = toNumber(parts[0], 0);
var eventId = toNumber(parts[1], 0);
var letter = parts[2];
var mapMatch = (targetMapId === 0) || (mapId === targetMapId);
var eventMatch = (targetEventId === 0) || (eventId === targetEventId);
var switchMatch = (targetSwitch === '') || (letter === targetSwitch);
if (mapMatch && eventMatch && switchMatch) {
delete data[key];
}
}
$gameMap.requestRefresh();
}
function turnOnSelfSwitch(targetMapId, targetEventId, switchLetter) {
if (targetMapId < 0) targetMapId = $gameMap.mapId();
if (targetMapId > 0 && targetEventId > 0 && switchLetter) {
$gameSelfSwitches.setValue([targetMapId, targetEventId, switchLetter], true);
$gameMap.requestRefresh();
}
}
var _pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
_pluginCommand.call(this, command, args);
var cmd = (command || '').toUpperCase();
if (cmd === CMD_SS_OFF) {
turnOffSelfSwitches(
toNumber(args[0], 0),
toNumber(args[1], 0),
normalizeSwitchLetter(args[2])
);
} else if (cmd === CMD_SS_ON) {
turnOnSelfSwitch(
toNumber(args[0], -1),
toNumber(args[1], 0),
normalizeSwitchLetter(args[2])
);
}
};
})();