Files

112 lines
8.1 KiB
JavaScript

'use strict';
function readExtraInput() {
let extra;
try { extra = JSON.parse($('extra').value); } catch { throw new Error('扩展参数不是有效的 JSON'); }
if (!extra || typeof extra !== 'object' || Array.isArray(extra)) throw new Error('扩展参数必须是 JSON 对象');
const path = $('extra-path');
if (path?.dataset.dirty === 'true') {
const points = path.readPoints();
if (points.length < 2) throw new Error('巡逻路径至少需要两个点');
extra = window.LevelExtraFields.setPath(extra, points);
}
return extra;
}
function refreshExtraEffective() {
const output = $('extra-effective-countdown');
if (!output) return;
const effective = state.result?.board?.units?.find(unit => unit.id === 'u-' + String(state.selected?.index + 1).padStart(6, '0'))?.timer?.duration;
output.textContent = effective == null ? '实际初始倒计时:待校验' : '实际初始倒计时:' + effective + ' 秒';
}
function renderExtraFields(entry, definition) {
const host = $('extra-fields'), fields = window.LevelExtraFields, extra = entry[4] || {};
host.replaceChildren();
const heading = document.createElement('h2'); heading.textContent = '附加配置'; host.append(heading);
const editable = state.view === 'source';
function note(text) { const node = document.createElement('p'); node.className = 'extra-note'; node.textContent = text; host.append(node); }
function commit(change) {
if (!editable || state.busy) return;
// 高级 JSON 的未应用修改先进入同一次撤销记录,避免被控件覆盖。
const next = change(readExtraInput());
const problems = fields.validate(next);
if (problems.length) throw new Error(problems[0]);
mutate(() => { entry[4] = next; });
message('附加配置已更新,尚未保存');
}
function field(label, control) {
const wrapper = document.createElement('label'); wrapper.textContent = label;
control.disabled = !editable; wrapper.append(control); host.append(wrapper); return control;
}
function number(id, label, value, minimum, change, fallback) {
const input = document.createElement('input'); input.id = id; input.type = 'number'; input.step = 'any'; input.min = minimum;
input.value = value ?? ''; input.placeholder = fallback == null ? '默认' : '默认:' + fallback;
input.onchange = guarded(() => {
if (!input.checkValidity()) { input.reportValidity(); throw new Error(label + '数值无效'); }
const value = input.value === '' ? null : Number(input.value);
commit(extra => change(extra, value));
});
return field(label, input);
}
function inheritedValue(array, index) { return Array.isArray(array) && array[index] != null ? array[index] : null; }
if (definition.collection === 'characters') {
const current = fields.buff(extra, 'speed')?.buffParam?.speed;
number('extra-speed', '移动速度', current, 0.000001, (extra, value) => fields.setBuff(extra, 'speed', 'speed', value), definition.moveSpeed);
}
if (definition.boardBehavior === 'burrowingBlocker') {
const select = document.createElement('select'); select.id = 'extra-down';
for (const [value, text] of [['up', '升起 · 阻挡'], ['down', '降下 · 可穿过']]) { const option = document.createElement('option'); option.value = value; option.textContent = text; select.append(option); }
select.value = fields.down(extra) ? 'down' : 'up';
select.onchange = guarded(() => commit(extra => fields.setBuff(extra, 'pop-down-up', 'down', select.value === 'down')));
field('初始升降状态', select);
}
if (definition.boardBehavior === 'countdownBomb') {
const index = state.level.characters.map((entry, index) => ({ entry, index })).filter(item => definitionFor(item.entry)?.boardBehavior === 'countdownBomb')
.sort((a, b) => b.entry[2] - a.entry[2] || a.entry[1] - b.entry[1]).findIndex(item => item.index === state.selected.index);
const override = inheritedValue(state.level.progression?.boomTime, index);
number('extra-countdown', '附加倒计时(秒)', fields.buff(extra, 'cool-down')?.buffParam?.duration, 0, (extra, value) => fields.setBuff(extra, 'cool-down', 'duration', value), 180);
note(override === null ? '来源:附加配置或默认值;较长倒计时还会按难度限制。' : '当前优先使用关卡倒计时:' + override + ' 秒;附加倒计时仅作后备。');
note(''); host.lastElementChild.id = 'extra-effective-countdown'; refreshExtraEffective();
}
if (definition.boardBehavior === 'cage') {
const index = state.level.aoes.slice(0, state.selected.index).filter(item => definitionFor(item)?.boardBehavior === 'cage').length;
const override = inheritedValue(state.level.progression?.caveDesNum, index);
number('extra-hp', '附加血量', extra.hp, 0.000001, (extra, value) => { if (value === null) delete extra.hp; else extra.hp = value; return extra; }, definition.defaultHp || 3);
note(override === null ? '实际血量:' + (extra.hp ?? definition.defaultHp ?? 3) : '当前优先使用关卡血量:' + override + ';附加血量仅作后备。');
}
if (definition.boardBehavior === 'movingHazard') {
const path = fields.pathAction(extra)?.params || [];
const editor = document.createElement('div'); editor.className = 'path-editor'; editor.id = 'extra-path';
editor.dataset.dirty = 'false'; editor.readPoints = readRows;
editor.addEventListener('input', () => { editor.dataset.dirty = 'true'; });
const title = document.createElement('h3'); title.textContent = '巡逻路径'; editor.append(title);
const columns = document.createElement('div'); columns.className = 'path-columns';
for (const name of ['列', '行']) { const label = document.createElement('span'); label.textContent = name; columns.append(label); }
editor.append(columns);
const rows = document.createElement('div'); rows.className = 'path-rows'; editor.append(rows);
const points = path.map(point => String(point || '').split(',').map(Number)).filter(point => point.length === 2 && point.every(Number.isFinite));
function readRows() { return [...rows.children].map(row => [...row.querySelectorAll('input')].map(input => {
if (input.value === '' || !input.checkValidity()) throw new Error('路径坐标必须是棋盘内的整数');
return Number(input.value);
})); }
function addPoint(point) {
const row = document.createElement('div'); row.className = 'path-row';
for (const [axis, value] of point.entries()) {
const input = document.createElement('input'); input.type = 'number'; input.min = 0; input.max = (axis ? state.level.height : state.level.width) - 1;
input.step = 1; input.value = value; input.disabled = !editable; input.setAttribute('aria-label', '路径点' + (axis ? '行' : '列')); row.append(input);
}
const remove = document.createElement('button'); remove.type = 'button'; remove.title = '删除路径点'; remove.setAttribute('aria-label', '删除路径点'); remove.innerHTML = '<i data-lucide="trash-2"></i>'; remove.disabled = !editable;
remove.onclick = () => { row.remove(); editor.dataset.dirty = 'true'; }; row.append(remove); rows.append(row);
}
points.forEach(addPoint);
const actions = document.createElement('div'); actions.className = 'path-actions';
const add = document.createElement('button'); add.type = 'button'; add.id = 'extra-path-add'; add.innerHTML = '<i data-lucide="plus"></i>'; add.title = '添加路径点'; add.setAttribute('aria-label', '添加路径点'); add.disabled = !editable;
add.onclick = () => { addPoint([entry[1], entry[2]]); editor.dataset.dirty = 'true'; icons(); };
const apply = document.createElement('button'); apply.type = 'button'; apply.id = 'extra-path-apply'; apply.textContent = '应用路径'; apply.disabled = !editable;
apply.onclick = guarded(() => { const points = readRows(); if (points.length < 2) throw new Error('巡逻路径至少需要两个点'); commit(extra => fields.setPath(extra, points)); });
actions.append(add, apply); editor.append(actions); host.append(editor);
}
if (definition.collection !== 'characters' && definition.boardBehavior !== 'cage') note('此对象没有游戏当前支持的附加参数。');
}