用户:
𝙲ℴ𝗌𝔦𝒹ₑ𝑟查看:0 回复:0 评论:0 创建时间:2023-07-08T15:59:14
const bagmodule = {
init: function () {
arguments.forEach((i) => {
Object.assign(i, { bag: [] });
});
},
};
class ItemAction {
/**
* @param {string} name - 动作名称
* @param {string | undefined} type - 动作类型(可选)
* @param {string[] | undefined} tags - 动作标签数组(可选)
* @param {number} spendTime - 消耗时间
* @param {{name:string,numbers:number}[]} costList - 动作消耗列表
* @param {function} callback - 动作触发时执行的回调函数
*/
constructor(name, type, tags, spendTime, costList, callback) {
this.name = name;
this.type = type;
this.tags = tags;
this.spendTime = spendTime;
this.costList = costList;
this.callback = callback;
}
/**
* 触发动作
* @param {...any} args - 参数
*/
trigAction(...args) {
this.callback(...args);
}
/**
* 根据名称选择动作
* @param {ItemAction[]} actions - 动作数组
* @param {string} actionName - 动作名称
* @returns {ItemAction} - 找到的动作对象,如果未找到则返回空对象
*/
static selectActionsByName(actions, actionName) {
for (let act of actions) {
if (act.name == actionName) return act;
}
return {};
}
}
class Item {
/**
* @param {string} name - 物品名称
* @param {string | undefined} type - 物品类型(可选)
* @param {string[] | undefined} tags - 物品标签数组(可选)
* @param {boolean} useable - 是否可使用
* @param {ItemAction[]} actions - 物品拥有的动作数组
*/
constructor(name, type, tags, useable, actions) {
this.name = name;
this.type = type;
this.tags = tags;
this.useable = useable;
this.actions = actions;
}
/**
* 使用物品
* @param {string} actionName - 动作名称
* @param {...any} args - 参数
*/
useItem(actionName, ...args) {
if (!this.useable) return;
const selectedAction = ItemAction.selectActionsByName(this.actions, actionName);
selectedAction.callback(...args);
}
}
module.exports = { bagmodule, Item, ItemAction };