先放一整段总的js文件,内容很简单。可以分以下几个部分来阅读:
一、注释段的插件设置(用于RM引擎解析)
二、对插件设置的解析和字符串引用转换
三、在原函数上追加新定义
四、加载数据
//=============================================================================
// Plugin for RPG Maker MZ
// AddAutoToActorCommand.js
//=============================================================================
/*:
* @target MZ
* @plugindesc Add 'Auto' command on the top or bottom of Actor Command
* @author Sasuke KANNAZUKI
*
* @param commandName
* @text Command Name
* @desc Displaying command name that 'Auto' command.
* @type string
* @default Auto Select
*
* @param autoCommandPos
* @text Auto Command Position
* @desc Adding position of 'Auto' command in the window
* (0:Top 1:Bottom)
* @type select
* @option Top
* @value 0
* @option Bottom
* @value 1
* @default 0
*
* @help This plugin does not provide plugin commands.
* This plugin runs under RPG Maker MZ.
*
* This plugin adds 'Auto' command on the top or bottom of Actor Command.
*
* [Summary]
* When player select 'Auto' command, the actor performs appropriate action.
* - The action is the same as one when the actor has the trait 'Auto Battle'.
* - The 'Auto' commands works only current turn. The next turn, actor command
* window appears again.
*
* [Note]
* When the actor can more than 1 actions and once select 'Auto',
* all actions become ones that auto battle routine decide,
* and previous inputted actions are ignored.
*
* [Note Setting]
* Write actor's note <NoAutoCommand> not to display 'Auto'
* to the actor's command.
*
* [License]
* this plugin is released under MIT license.
* [url]http://opensource.org/licenses/mit-license.php[/url]
*/
(() => {
const pluginName = 'AddAutoToActorCommand';
//
// process parameters
//
const parameters = PluginManager.parameters(pluginName);
const commandName = parameters['commandName'] || 'Auto Select';
const yPosType = Number(parameters['autoCommandPos'] || 0);
//
// add command to actor command window
//
const _Scene_Battle_createActorCommandWindow =
Scene_Battle.prototype.createActorCommandWindow;
Scene_Battle.prototype.createActorCommandWindow = function() {
_Scene_Battle_createActorCommandWindow.call(this);
this._actorCommandWindow.setHandler('auto', this.commandAuto.bind(this));
};
const doesDisplayAuto = actor => actor && !actor.actor().meta.NoAutoCommand;
const _Window_ActorCommand_makeCommandList =
Window_ActorCommand.prototype.makeCommandList;
Window_ActorCommand.prototype.makeCommandList = function() {
if (doesDisplayAuto(this._actor) && yPosType === 0) {
this.addAutoCommand();
}
_Window_ActorCommand_makeCommandList.call(this);
if (doesDisplayAuto(this._actor) && yPosType === 1) {
this.addAutoCommand();
}
};
Window_ActorCommand.prototype.addAutoCommand = function() {
this.addCommand(commandName, 'auto');
};
Scene_Battle.prototype.commandAuto = function() {
const actor = BattleManager._currentActor;
if (actor) {
actor.makeAutoBattleActions();
}
BattleManager.finishActorInput();
this.changeInputWindow();
BattleManager.selectNextActor();
};
})();
以下这段文字,各位可以自行创建来实际体验。
* @target MZ(说明插件的适用对象定义为MZ)
* @plugindesc Add 'Auto' command on the top or bottom of Actor Command (插件在清单列表中的文字介绍)
* @author Sasuke KANNAZUKI (插件的作者)
*
* @param commandName (定义一个变量)
* @text Command Name (对这个变量的描述)
* @desc Displaying command name that 'Auto' command. (对这个变量的详细描述)
* @type string (这个变量的类型指定为字符串)
* @default Auto Select (设定默认值)
*
* @param autoCommandPos (定义一个变量)
* @text Auto Command Position (对这个变量的描述)
* @desc Adding position of 'Auto' command in the window (对这个变量的详细描述)
* (0:Top 1:Bottom)
* @type select (设置下拉菜单的形式来供RM制作者选择)
* @option Top (第一个选项,名字叫做 Top)
* @value 0 (第一个选项对应的值为0)
* @option Bottom (第一个选项,名字叫做 Bottom)
* @value 1 (第一个选项对应的值为1)
* @default 0 (默认值为0,即选择Top)
*
* @help This plugin does not provide plugin commands. (插件的长篇文字说明)
* This plugin runs under RPG Maker MZ.
*
* This plugin adds 'Auto' command on the top or bottom of Actor Command.
插件中常见的函数定义形式:
(() => { })();
初学者如果不理解的话,可以不写(虽然这么说很不负责任,甚至误导,但的确可以简化插件阅读和制作),待对代码有进一步理解的时候,再加上也不迟。
const pluginName = 'AddAutoToActorCommand';//可以理解为插件的名字规定为AddAutoToActorCommand,如果这个js文件名字不是这个,则不进行加载
const parameters = PluginManager.parameters(pluginName);//加载插件
const commandName = parameters['commandName'] || 'Auto Select';//commandName的值为commandName所含的字符串,如果commandName值为0,则commandName的值为'Auto Select'
const yPosType = Number(parameters['autoCommandPos'] || 0);
//同上,||的意思是“或者”,如果这个计算方式不理解,建议还是需要打牢基础知识再修改插件。
这里就是之前提到过的call(this)的用法
这段代码就是在原先基础上,增加了 this._actorCommandWindow.setHandler('auto', this.commandAuto.bind(this));
const _Scene_Battle_createActorCommandWindow =
Scene_Battle.prototype.createActorCommandWindow;
Scene_Battle.prototype.createActorCommandWindow = function() {
_Scene_Battle_createActorCommandWindow.call(this);
this._actorCommandWindow.setHandler('auto', this.commandAuto.bind(this));
};
这一步是通过对yPosType的数值进行判断,来设置新增加的auto命令是放在下面还是下面。这个书写方式或许对大家有所启发。
const _Window_ActorCommand_makeCommandList =
Window_ActorCommand.prototype.makeCommandList;
Window_ActorCommand.prototype.makeCommandList = function() {
if (doesDisplayAuto(this._actor) && yPosType === 0) {
this.addAutoCommand();
}
_Window_ActorCommand_makeCommandList.call(this);
if (doesDisplayAuto(this._actor) && yPosType === 1) {
this.addAutoCommand();
}
};
这一步,就是将‘auto’命令加入到列表中,而这个命令的显示文字是commandName对应的字符串。
Window_ActorCommand.prototype.addAutoCommand = function() {
this.addCommand(commandName, 'auto');
};
这就是自动战斗的真正执行代码。
Scene_Battle.prototype.commandAuto = function() {
const actor = BattleManager._currentActor;
if (actor) {
actor.makeAutoBattleActions();//执行自动战斗
}
BattleManager.finishActorInput();//结束角色指令输入
this.changeInputWindow();
BattleManager.selectNextActor();//选择下一个角色(用于非ATB战斗)
};