137 lines
5.1 KiB
JavaScript
137 lines
5.1 KiB
JavaScript
/*:
|
||
* @plugindesc メッセージ中の制御文字でピクチャを表示するプラグイン
|
||
* @target MV MZ
|
||
* @author ChatGPT
|
||
*
|
||
* @help AutoExecuteScriptWithMessage.js
|
||
*
|
||
* メッセージ中に制御文字 \F[...] を記述することで、指定した値を
|
||
* 変数に代入し、それを基にピクチャを表示します。
|
||
*
|
||
* 表示するピクチャは、以下のフォルダから読み取られます:
|
||
* img/pictures/chara/
|
||
*
|
||
* ファイル名形式:
|
||
* T0[変数10]_a[変数11]_NN.png
|
||
* (NNはスイッチ12と13の状態によるサフィックス)
|
||
*
|
||
* 使用例:
|
||
* メッセージに \F[1,2,3,4,5,6] と記述すると、
|
||
* 以下の変数に値が代入されます:
|
||
* 変数10 = 1, 変数11 = 2, 変数12 = 3, 変数13 = 4, 変数14 = 5, 変数15 = 6
|
||
* その後、対応するファイルが表示されます。
|
||
*
|
||
* 注意:
|
||
* - ファイルが存在しない場合、コンソールに警告が表示されます。
|
||
* - ピクチャIDは 30 ~ 34を使用します。
|
||
*/
|
||
|
||
(() => {
|
||
"use strict";
|
||
|
||
const pluginName = "AutoExecuteScriptWithMessage";
|
||
const parameters = PluginManager.parameters(pluginName);
|
||
|
||
const _Window_Message_processEscapeCharacter = Window_Message.prototype.processEscapeCharacter;
|
||
Window_Message.prototype.processEscapeCharacter = function(code, textState) {
|
||
if (code === "F") {
|
||
const param = this.obtainEscapeArrayParam(textState);
|
||
if (param) {
|
||
executeScriptForArray(param);
|
||
}
|
||
}
|
||
_Window_Message_processEscapeCharacter.call(this, code, textState);
|
||
};
|
||
|
||
Window_Message.prototype.obtainEscapeArrayParam = function(textState) {
|
||
const regExp = /^\[([^\]]+)\]/;
|
||
const match = regExp.exec(textState.text.slice(textState.index));
|
||
if (match) {
|
||
textState.index += match[0].length;
|
||
try {
|
||
return JSON.parse(`[${match[1]}]`);
|
||
} catch (e) {
|
||
console.error("制御文字の配列パラメータ解析エラー", e);
|
||
return null;
|
||
}
|
||
}
|
||
return null;
|
||
};
|
||
|
||
function executeScriptForArray(values) {
|
||
try {
|
||
if (!Array.isArray(values)) {
|
||
console.warn("渡された値が配列ではありません: ", values);
|
||
return;
|
||
}
|
||
values.forEach((value, index) => {
|
||
$gameVariables.setValue(10 + index, value || 0);
|
||
});
|
||
updatePictures();
|
||
} catch (e) {
|
||
console.error(`スクリプト実行中にエラーが発生しました: ${e.message}`);
|
||
}
|
||
}
|
||
|
||
function updatePictures() {
|
||
function fileExists(filePath) {
|
||
try {
|
||
const fs = require("fs");
|
||
return fs.existsSync(filePath);
|
||
} catch (e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
const basePath = "chara/"; // フォルダ名
|
||
const mainVariable = $gameVariables.value(10); // T0[変数10] に対応
|
||
const suffixes = ['a', 'b', 'c', 'd', 'e']; // サフィックス配列
|
||
const variableIndices = [11, 12, 13, 14, 15]; // 変数番号
|
||
|
||
// スイッチの状態を取得
|
||
const switch12 = $gameSwitches.value(12);
|
||
const switch13 = $gameSwitches.value(13);
|
||
|
||
// サフィックスの決定(aのみ)
|
||
let extraSuffix = "01";
|
||
if (switch12 && switch13) {
|
||
extraSuffix = "03"; // スイッチ13が優先
|
||
} else if (switch12) {
|
||
extraSuffix = "02";
|
||
} else if (switch13) {
|
||
extraSuffix = "03";
|
||
}
|
||
|
||
suffixes.forEach((suffix, i) => {
|
||
const variableValue = $gameVariables.value(variableIndices[i]);
|
||
|
||
// 変数値が 0 の場合、処理をスキップ
|
||
if (variableValue === 0) {
|
||
return;
|
||
}
|
||
|
||
const variableValueString = variableValue.toString().padStart(2, "0");
|
||
let picturePath;
|
||
|
||
if (suffix === 'a') {
|
||
// `a` の場合のみスイッチに応じたサフィックスを追加
|
||
picturePath = `${basePath}T0${mainVariable}_${suffix}${variableValueString}_${extraSuffix}`;
|
||
} else {
|
||
// `b`, `c`, `d`, `e` はスイッチに依存せず固定
|
||
picturePath = `${basePath}T0${mainVariable}_${suffix}${variableValueString}`;
|
||
}
|
||
|
||
const pictureId = 30 + i; // ピクチャIDを30~34に固定
|
||
|
||
if (fileExists(`img/pictures/${picturePath}.png`)) {
|
||
const currentPicture = $gameScreen.picture(pictureId);
|
||
const tone = currentPicture ? currentPicture._tone : [0, 0, 0, 0];
|
||
|
||
$gameScreen.showPicture(pictureId, picturePath, 0, 0, 0, 100, 100, 255, 0);
|
||
$gameScreen.picture(pictureId).tint(tone, 1);
|
||
} else {
|
||
console.warn(`File not found: img/pictures/${picturePath}.png`);
|
||
}
|
||
});
|
||
}
|
||
})();
|