快捷导航
查看: 315|回复: 0

[脚本] 对RMMZ官方插件“自动战斗命令”的解读

[复制链接]
会员等级

积分信息
金币:725
元宝:20
活跃:187
发电:0

荣誉勋章
热心会员优秀版主首批会员最佳新人

TA的形象
UID: 6 发表于 2023-2-20 09:12:38 | 显示全部楼层 |阅读模式
先放一整段总的js文件,内容很简单。可以分以下几个部分来阅读:
一、注释段的插件设置(用于RM引擎解析)
二、对插件设置的解析和字符串引用转换
三、在原函数上追加新定义
四、加载数据


[JavaScript] 纯文本查看 复制代码
//=============================================================================
// 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.

插件中常见的函数定义形式:
(() => {  })();
初学者如果不理解的话,可以不写(虽然这么说很不负责任,甚至误导,但的确可以简化插件阅读和制作),待对代码有进一步理解的时候,再加上也不迟。
[JavaScript] 纯文本查看 复制代码
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)的用法,可以看这里:RMMV、MZ插件中常用的call(this)形式解读
这段代码就是在原先基础上,增加了 this._actorCommandWindow.setHandler('auto', this.commandAuto.bind(this));
[JavaScript] 纯文本查看 复制代码
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命令是放在下面还是下面。这个书写方式或许对大家有所启发。
[JavaScript] 纯文本查看 复制代码
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对应的字符串。
如果还是不理解这个选项是如果添加进去的,可以看这里:在MV/MZ的默认选项中追加新的选项
[JavaScript] 纯文本查看 复制代码
Window_ActorCommand.prototype.addAutoCommand = function() {
this.addCommand(commandName, 'auto');
};


这就是自动战斗的真正执行代码。
[JavaScript] 纯文本查看 复制代码
Scene_Battle.prototype.commandAuto = function() {
const actor = BattleManager._currentActor;
if (actor) {
actor.makeAutoBattleActions();//执行自动战斗
}
BattleManager.finishActorInput();//结束角色指令输入
this.changeInputWindow();
BattleManager.selectNextActor();//选择下一个角色(用于非ATB战斗)
};

AddAutoToActorCommand.js

4.98 KB, 下载次数: 0

评分

参与人数 1金币 +15 收起 理由
三姐 + 15 讲这么多辛苦了,很深奥!

查看全部评分

帖子的最近访客

回复 论坛版权

使用道具 举报

ahome_bigavatar:guest
ahome_bigavatar:welcomelogin
您需要登录后才可以回帖 登录 | 加入民族

本版积分规则

论坛用工具

蓝凑云 hello图床 聚合图床 TinyPNG remove 代码测试 颜色代码 颜色代码2

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

实用工具

AI人工智能图片放大 图片无损放大 Deepl翻译器 百宝箱 孟坤工具箱 在线压缩图片 图片切圆角

您一共访问了本站 加载中...

Archiver|小黑屋|RPG民族

GMT+8, 2024-10-23 07:31 , Processed in 0.241294 second(s), 56 queries .

快速回复 返回顶部 返回列表