Files

69 lines
4.0 KiB
JavaScript

(function (root, factory) {
const api = factory();
if (typeof module === 'object' && module.exports) module.exports = api;
else root.LevelExtraFields = api;
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
'use strict';
const copy = value => JSON.parse(JSON.stringify(value));
const buff = (extra, id) => (Array.isArray(extra?.addBuff) ? extra.addBuff : []).find(item => item?.buffId === id);
const down = extra => buff(extra, 'pop-down-up')?.buffParam?.down === true;
function setBuff(extra, id, key, value) {
const result = copy(extra || {});
if (result.addBuff != null && !Array.isArray(result.addBuff)) throw new Error('附加效果列表必须是数组');
const list = result.addBuff || [];
if (value === null) {
if (result.addBuff) result.addBuff = list.filter(item => item?.buffId !== id);
} else {
let item = list.find(item => item?.buffId === id);
if (!item) { item = { addStack: 1, buffId: id, duration: 0, durationSetTo: true, permanent: true }; list.push(item); }
item.buffParam = { ...item.buffParam, [key]: value }; result.addBuff = list;
}
return result;
}
function pathAction(extra) {
for (const clip of (Array.isArray(extra?.aiClips) ? extra.aiClips : [])) {
for (const action of (Array.isArray(clip?.actions) ? clip.actions : [])) if (action?.method === 'movePath') return action;
}
return null;
}
function setPath(extra, points) {
const result = copy(extra || {});
if (result.aiClips != null && !Array.isArray(result.aiClips)) throw new Error('行为指令列表必须是数组');
const action = pathAction(result);
if (action) action.params = points.map(point => point.join(','));
else {
result.aiClips ||= [];
result.aiClips.push({ id: 'move-by-path', actions: [{ method: 'movePath', params: points.map(point => point.join(',')) }], conditions: [{ method: 'alwaysTrue' }] });
}
return result;
}
function validate(extra) {
const issues = [];
if (extra == null) return issues;
if (typeof extra !== 'object' || Array.isArray(extra)) return ['扩展参数必须是 JSON 对象'];
if (extra.addBuff != null && !Array.isArray(extra.addBuff)) return ['附加效果列表必须是数组'];
if (extra.aiClips != null && !Array.isArray(extra.aiClips)) return ['行为指令列表必须是数组'];
for (const [id, key, label, allowZero] of [['speed', 'speed', '移动速度', false], ['cool-down', 'duration', '倒计时', true]]) {
const value = buff(extra, id)?.buffParam?.[key];
if (value != null && (typeof value !== 'number' || !Number.isFinite(value) || (allowZero ? value < 0 : value <= 0))) issues.push(label + (allowZero ? '必须是非负数' : '必须大于 0'));
}
const lowered = buff(extra, 'pop-down-up')?.buffParam?.down;
// 历史数据含 0/1;保留原值,读取时仍与玩法一样仅 true 代表降下。
if (lowered != null && typeof lowered !== 'boolean' && lowered !== 0 && lowered !== 1) issues.push('初始升降状态必须是布尔值');
if (extra.hp != null && (typeof extra.hp !== 'number' || !Number.isFinite(extra.hp) || extra.hp <= 0)) issues.push('封印锁血量必须大于 0');
for (const clip of extra.aiClips || []) {
if (clip?.actions != null && !Array.isArray(clip.actions)) { issues.push('行为动作列表必须是数组'); continue; }
for (const action of clip?.actions || []) if (action?.method === 'movePath') {
// 旧关卡路径带有空占位项,玩法会忽略;不能因为编辑器新增表单而拒绝旧数据。
const points = Array.isArray(action.params) ? action.params.filter(point => {
const values = String(point || '').split(',').map(Number);
return values.length === 2 && values.every(Number.isFinite);
}) : [];
if (points.length < 2) issues.push('巡逻路径至少包含两个有效坐标点');
}
}
return issues;
}
return { buff, down, setBuff, pathAction, setPath, validate };
});