跳转至

脚本

Mindustry 使用 JavaScript 进行模组脚本编写。脚本使用 js 扩展名,并放置在 scripts/ 子目录中。

执行从名为 main.js 的文件开始。任何其他脚本文件可以通过主文件使用 require("script_name") 导入。 一个典型的设置如下所示:

scripts/main.js:

require("blocks");
require("items");

scripts/blocks.js:

const myBlock = extend(Conveyor, "terrible-conveyor", {
  // 各种重写...
  size: 3,
  health: 200
  //...
});

scripts/items.js:

const terribleium = Item("terribleium");
terribleium.color = Color.valueOf("ff0000");
//...

示例

监听事件

// 监听单位被摧毁的事件
Events.on(UnitDestroyEvent, event => {
  // 当单位是玩家时在屏幕顶部显示吐司
  if(event.unit.isPlayer()){
    Vars.ui.hudfrag.showToast("可悲。");
  }
});

找到您可以监听的事件的最简单方法是查看源文件:Mindustry/blob/master/core/src/mindustry/game/EventType.java

显示对话框

const myDialog = new BaseDialog("对话框标题");
// 添加"返回"按钮
myDialog.addCloseButton();
// 向主内容添加文本
myDialog.cont.add("再见。");
// 显示对话框
myDialog.show();

播放自定义声音

播放自定义音频很简单,只要您将声音剪辑存储为 .mp3.ogg 文件在 /sounds 目录中。

在这个例子中,我们将 example.mp3 存储在 /sounds/example.mp3

使用库加载声音

scripts/alib.js:

exports.loadSound = (() => {
    const cache = {};
    return (path) => {
        const c = cache[path];
        if (c === undefined) {
            return cache[path] = loadSound(path);
        }
        return c;
    }
})();

scripts/main.js:

const lib = require("alib");

Events.on(WaveEvent, event => {
    // 加载 example.mp3
    const mySound = lib.loadSound("example");
    // 引擎将在此位置(X,Y)生成此声音
    mySound.at(1, 1);
});

//TODO 测试这些示例并添加更多示例