Skip to content

Instantly share code, notes, and snippets.

@jsdryan
Last active April 16, 2024 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsdryan/c99cca20cd43737f94a87947f3f28c60 to your computer and use it in GitHub Desktop.
Save jsdryan/c99cca20cd43737f94a87947f3f28c60 to your computer and use it in GitHub Desktop.
快樂貓 MS Script 教學 - 第 10 課(製作道具)
// 影片教學網址:https://youtu.be/QkQTn7SUdsQ?si=sUay4PJlyYwWqJ_7
// 使用 NPC 編號:2101018 - 杰薩勒(親衛隊長)
var status = -1; // 狀態
var selected; // 選擇的物品
var count; // 購買的數量
// 裝備列表 - id: 裝備 ID, materials: 材料列表
// 材料列表 - id: 材料 ID, quantity: 數量
// 例如:冰凍帽 = 1003741, 冰塊 x 10, 白魔道之帽 x 1
// equipment[0] = { "id": 1003741, "materials": [{ "id": 4003002, "quantity": 10 }, { "id": 1002155, "quantity": 1 }] }
var equipment = [{
"id": 1003741, // 冰凍帽
"materials": [{
"id": 4003002, // 冰塊
"quantity": 10
}, {
"id": 1002155, // 白魔道之帽
"quantity": 1
}],
}, {
"id": 1102507, // 冰凍斗篷
"materials": [{
"id": 4003002,
"quantity": 10
}, {
"id": 1102053, // 破舊的披風
"quantity": 1
}],
}, {
"id": 1082499, // 冰凍手套
"materials": [{
"id": 4003002,
"quantity": 10
}, {
"id": 1082002, // 工地手套
"quantity": 1
}],
}, {
"id": 1052570, // 冰凍套裝
"materials": [{
"id": 4003002,
"quantity": 10
}, {
"id": 1050018, // 藍色桑那服
"quantity": 1
}],
}, {
"id": 1072769, // 冰凍靴
"materials": [{
"id": 4003002,
"quantity": 10
}, {
"id": 1072169, // 藍雪鞋
"quantity": 1
}],
}];
function start() {
action(1, 0, 0);
}
function action(mode, type, selection) {
// mode 1 表示玩家按下了「確定」按鈕,因為玩家是第一次點擊 NPC,所以 mode 不會是 1
// 因此,status 從原本的 -1 變成 0(加 1),然後第一個 if 判斷 status 是否等於 0 即為 true
status = (mode == 1 ? status + 1 : cm.dispose());
if (status == 0) {
var str = "請選擇所需裝備:\r\n";
// 顯示物品列表
for (var i = 0; i < equipment.length; i++) {
str += "#L" + i + "# #v" + equipment[i].id + "##z" + equipment[i].id + "#\r\n";
}
// cm 是 CustomMessage 的縮寫
cm.sendOk(str)
}
// 選擇物品
if (status == 1) {
selected = selection; // 選擇的物品
var str = "您選擇的是 #v" + equipment[selected].id + "# #z" + equipment[selected].id + "#。\r\n";
str += "該裝備需要以下材料:\r\n";
// 顯示材料列表
for (var i = 0; i < equipment[selected].materials.length; i++) {
str += "#v" + equipment[selected].materials[i].id + "# #z" + equipment[selected].materials[i].id + "# x " + equipment[selected].materials[i].quantity + "\r\n";
}
cm.sendOk(str);
}
if (status == 2) {
// 檢查玩家是否有足夠的材料
var hasMaterials = true;
for (var i = 0; i < equipment[selected].materials.length; i++) {
if (!cm.haveItem(equipment[selected].materials[i].id, equipment[selected].materials[i].quantity)) {
hasMaterials = false;
break;
}
}
if (hasMaterials) {
// 移除材料
for (var i = 0; i < equipment[selected].materials.length; i++) {
cm.gainItem(equipment[selected].materials[i].id, -equipment[selected].materials[i].quantity);
}
// 給予裝備
cm.gainItem(equipment[selected].id, 1);
cm.sendOk("恭喜您成功製作 #v" + equipment[selected].id + "# #z" + equipment[selected].id + "#。");
} else {
var str = "您的材料不足,無法製作該裝備。\r\n";
str += "還缺少以下材料:\r\n";
for (var i = 0; i < equipment[selected].materials.length; i++) {
if (!cm.haveItem(equipment[selected].materials[i].id, equipment[selected].materials[i].quantity)) {
str += "#v" + equipment[selected].materials[i].id + "# #z" + equipment[selected].materials[i].id + "# x " + (equipment[selected].materials[i].quantity - cm.itemQuantity(equipment[selected].materials[i].id)) + "\r\n";
}
}
cm.sendOk(str);
}
cm.dispose();
}
}
@jsdryan
Copy link
Author

jsdryan commented Apr 16, 2024

功能展示

2024-04-16.18-26-13.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment