diff --git a/libs/items.js b/libs/items.js index d4288779..811a15e0 100644 --- a/libs/items.js +++ b/libs/items.js @@ -26,37 +26,53 @@ items.prototype.getItems = function () { return items; } +///// 根据道具id,返回属性名:属性值的键值对 +items.prototype.getItemEffectValue = function (itemId, ratio) { + let effectObj = {}; + const itemEffectEvent = core.material.items[itemId].itemEffectEvent; + if (!itemEffectEvent) return effectObj; + const value = itemEffectEvent.value; + if (!ratio && ratio !== 0) { + ratio = core.status.thisMap.ratio; + if (!ratio && ratio !== 0) { + ratio = 1; + } + } + for (let statusName in value) { + let statusValue, needRatio; + const effect = value[statusName]; + if (statusName.endsWith(':o')) { + needRatio = true; + statusName = statusName.slice(0, -2); + } + if (core.status.hero.hasOwnProperty(statusName)) { + for (var i = 0; i < itemNum; ++i) { + try { + statusValue = eval(effect); + } + catch (e) { + console.error(e); + } + if (needRatio) statusValue *= ratio; + effectObj[statusName] = statusValue; + } + } + } + return effectObj; +} + + ////// “即捡即用类”道具的使用效果 ////// items.prototype.getItemEffect = function (itemId, itemNum) { const itemCls = core.material.items[itemId].cls; // 消耗品 if (itemCls === 'items') { const curr_hp = core.status.hero.hp; - const itemEffectEvent = core.material.items[itemId].itemEffectEvent; - if (itemEffectEvent) { - const { value } = itemEffectEvent; - for (let statusName in value) { - let statusValue, ratio, needRatio; - const effect = value[statusName]; - if (statusName.endsWith(':o')) { - needRatio = true; - statusName = statusName.slice(0, -2); - } - ratio = core.status.thisMap.ratio || 1; - if (core.status.hero.hasOwnProperty(statusName)) { - for (var i = 0; i < itemNum; ++i) { - try { - statusValue = eval(effect); - } - catch (e) { - console.error(e); - } - if (needRatio) statusValue *= ratio; - core.addStatus(statusName, statusValue); - } - } - } + const effectObj = this.getItemEffectValue(itemId); + for (let statusName in effectObj) { + if (effectObj.hasOwnProperty(statusName)) core.addStatus(statusName, effectObj[statusName]); } + const itemEffect = core.material.items[itemId].itemEffect; if (itemEffect) { try { diff --git a/libs/ui.js b/libs/ui.js index 98447e29..5ca53c49 100644 --- a/libs/ui.js +++ b/libs/ui.js @@ -3316,27 +3316,12 @@ ui.prototype._drawStatistics_items = function (floorId, floor, id, obj) { core.setFlag("__statistics__", true); var ratio = core.status.thisMap.ratio; core.status.thisMap.ratio = core.clone(core.status.maps[floorId].ratio); - const itemInfo = core.material.items[id]; - if (itemInfo.hasOwnProperty('itemEffectEvent') && itemInfo.itemEffectEvent.hasOwnProperty('value')) { - const values = itemInfo.itemEffectEvent.value; - for (let statusName in values) { - const getStatusValue = values[statusName]; - let needRatio, statusValue; - if (statusName.endsWith(':o')) { - needRatio = true; - statusName = statusName.slice(0, -2); - } - if (core.status.hero.hasOwnProperty(statusName)) { - try { - statusValue = eval(getStatusValue); - } catch (error) { - console.log(error); - } - if (needRatio) statusValue *= ratio; - core.addStatus(statusName, statusValue); - } - } + + const effectObj = core.items.getItemEffectValue(itemId, ratio); + for (let statusName in effectObj) { + if (effectObj.hasOwnProperty(statusName)) core.addStatus(statusName, effectObj[statusName]); } + try { eval(core.material.items[id].itemEffect); } catch (e) { } core.status.thisMap.ratio = ratio; diff --git a/runtime.d.ts b/runtime.d.ts index df1ab19e..f703954a 100644 --- a/runtime.d.ts +++ b/runtime.d.ts @@ -1517,7 +1517,7 @@ interface enemys { getEnemys(): any /** 获得所有特殊属性定义 */ - getSpecials():[number, string | ((enemy: Enemy) => string), string | ((enemy: Enemy) => string), + getSpecials(): [number, string | ((enemy: Enemy) => string), string | ((enemy: Enemy) => string), string | [number, number, number, number?], number?][] /** 获得所有特殊属性的颜色 */ @@ -1570,7 +1570,7 @@ interface maps { [x: string]: any } } - + /** * 根据图块id得到数字(地图矩阵中的值) * @example core.getNumberById('yellowWall'); // 1 @@ -2067,6 +2067,14 @@ interface loader { /** @file items.js 主要负责一切和道具相关的内容。 */ interface items { + /** + * 获得给定ratio下(不填默认当前地图倍率)该即捡即用类的道具获得时勇士属性的增加效果 + * @example core.getItemEffectValue('redGem',1) // 获得倍率为1时红宝石增加的属性值 + * @param itemId 道具id + * @param ratio 倍率,不填默认当前地图倍率 + */ + getItemEffectValue(itemId: string, ratio?: number): { [key: string]: number } + /** * 即捡即用类的道具获得时的效果 * @example core.getItemEffect('redPotion', 10) // 执行获得10瓶红血的效果 @@ -3042,4 +3050,4 @@ interface Main { declare let core: CoreMixin declare let flags: { [x: string]: any } -declare let hero : CoreMixin['status']['hero'] \ No newline at end of file +declare let hero: CoreMixin['status']['hero'] \ No newline at end of file