Files
nixingnishang_level-editor/抓猪关卡转换/piggy-converter/converter.cjs
T

97 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(function (root) {
'use strict';
const defaults = {
animalGroup: {
1: { id: 1, collection: 'characters', length: 2, width: 1 },
100: { id: 2, collection: 'characters', length: 2, width: 1 },
101: { id: 4, collection: 'characters', length: 2, width: 1 },
103: { id: 5, collection: 'characters', length: 3, width: 2 },
// 依据源 LanguageConfig 匹配棋盘行为,大象按已确认的体型映射为张飞。
106: { id: 7, collection: 'characters', length: 2, width: 1 },
107: { id: 8, collection: 'characters', length: 2, width: 1 },
108: { id: 9, collection: 'characters', length: 2, width: 1 },
109: { id: 10, collection: 'characters', length: 2, width: 1 },
112: { id: 3, collection: 'characters', length: 1, width: 1 }
}, obstacleGroup: {}
};
const own = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
function convert(source, mapping = defaults, zero = 'north') {
if (source?.format || source?.placement || source?.slots || source?.animals) return {
ok: false, layout: null, errors: ['不支持该输入格式,仅支持 piggy-jam-config 原始关卡 JSON(size、animalGroup、obstacleGroup)'], warnings: [], unsupportedTypes: []
};
const errors = [], warnings = ['坐标转换:2 个源单位对应 1 个目标格,position 为对象中心,转换为目标占格左下角;此规则已对照 level1 目标布局,其他特殊类型仍须验收。'];
const unsupportedTypes = [];
const fail = message => errors.push(message);
const width = source?.size?.x / 2, height = source?.size?.y / 2;
const layout = { width, height, characters: [], aoes: [] };
if (![width, height].every(n => Number.isInteger(n) && n > 0 && n <= 64)) fail('size.x 和 size.y 必须为 2 到 128 的偶数,转换后棋盘为 1 到 64 格');
if (!['north', 'east', 'south', 'west'].includes(zero)) fail('角度零点无效');
const vectors = { north: [0, 1, 3], east: [1, 0, 2], south: [0, -1, 1], west: [-1, 0, 4] };
const directions = Object.keys(vectors), occupied = new Set();
for (const group of ['animalGroup', 'obstacleGroup']) {
if (!Array.isArray(source?.[group])) { fail(group + ' 必须是数组'); continue; }
source[group].forEach((object, index) => {
const name = group + '[' + index + ']';
const rule = own(mapping?.[group] || {}, object?.type) ? mapping[group][object.type] : null;
if (!rule) {
if (!unsupportedTypes.some(item => item.collection === group && item.type === object?.type)) unsupportedTypes.push({ collection: group, type: object?.type });
fail(name + ':类型 ' + object?.type + ' 未适配,未配置映射'); return;
}
if (!['characters', 'aoes'].includes(rule.collection) || !Number.isSafeInteger(rule.id) || rule.id < 1 || ![rule.length, rule.width].every(n => Number.isInteger(n) && n > 0 && n <= 64)) { fail(name + ':映射规则无效'); return; }
if (!Number.isFinite(object.angle) || object.angle % 90 !== 0 || !Number.isFinite(object.position?.x) || !Number.isFinite(object.position?.y)) { fail(name + ':坐标或角度无效,角度必须为 90 的倍数'); return; }
const angle = ((object.angle % 360) + 360) % 360;
const direction = directions[(directions.indexOf(zero) + angle / 90) % 4];
const [dx, dy, orientation] = vectors[direction];
const sx = dx ? rule.length : rule.width, sy = dx ? rule.width : rule.length;
// 源坐标以对象中心为锚点,先缩放,再减去目标包围盒半尺寸。
const col = object.position.x / 2 + width / 2 - sx / 2;
const row = object.position.y / 2 + height / 2 - sy / 2;
if (![col, row].every(Number.isInteger)) { fail(name + ':坐标无法对齐目标整数格;请核对坐标假设'); return; }
if (col < 0 || row < 0 || col + sx > width || row + sy > height) { fail(name + ':对象超出棋盘'); return; }
if (rule.collection === 'characters') {
for (let y = row; y < row + sy; y++) for (let x = col; x < col + sx; x++) {
const key = x + ',' + y;
if (occupied.has(key)) fail(name + ':军械占格重叠 ' + key);
occupied.add(key);
}
}
// 目标占格以包围盒左下角为锚点,负方向同样使用非负占格偏移。
const entry = [rule.id, col, row, orientation];
const extra = rule.extra ? JSON.parse(JSON.stringify(rule.extra)) : {};
const carriesTimer = group === 'animalGroup' && object.type === 101 && rule.collection === 'characters' && rule.id === 4;
if (carriesTimer && own(object, 'boomTime')) {
if (!Number.isFinite(object.boomTime) || object.boomTime < 0) { fail(name + ':boomTime 必须为非负有限数字'); return; }
if (extra.addBuff !== undefined && !Array.isArray(extra.addBuff)) { fail(name + ':映射附加参数 addBuff 必须为数组'); return; }
if (extra.addBuff?.some(buff => buff?.buffId === 'cool-down')) { fail(name + ':映射中的 cool-down 与源 boomTime 冲突,请移除重复计时配置'); return; }
extra.addBuff = [...(extra.addBuff || []), {buffId:'cool-down',buffParam:{duration:object.boomTime}}];
}
if (rule.extra || Object.keys(extra).length) entry.push(extra);
layout[rule.collection].push(entry);
const extras = Object.keys(object).filter(key => !['id', 'type', 'position', 'angle', 'skinIndex'].includes(key) && !(carriesTimer && key === 'boomTime'));
if (extras.length) fail(name + ':存在未支持的对象参数 ' + extras.join('、') + ',需人工确认转换规则');
});
}
if (source?.cloudContainer && Object.keys(source.cloudContainer).length) fail('存在云层配置 cloudContainer,暂不支持转换');
if (source?.openMole) fail('存在地鼠玩法 openMole,暂不支持转换');
if (source?.levelMode) fail('非默认 levelMode,暂不支持转换');
warnings.push('相机参数、皮肤编号、难度、奖励不导入;目标关卡级配置由编辑器所选模板继承。');
if (source?.animalGroup?.some(object => object?.type === 100)) warnings.push('源类型 100 近似对应为爆破箭矢;目标固定 3 次碰撞,跨项目玩法仍需验收。');
if (source?.animalGroup?.some(object => object?.type === 101 && mapping?.animalGroup?.[101]?.id === 4)) warnings.push('计时狮子的 boomTime 写入 cool-down 附加参数;目标模板 progression.boomTime 优先于此参数,且时长达到 180 秒时可能受目标难度上限限制,导入后须核对最终倒计时。');
if (source?.animalGroup?.some(object => object?.type === 103 && mapping?.animalGroup?.[103]?.id === 5)) warnings.push('大象转换为占格 3×2 的张飞,保留棋盘占格;目标战斗能力采用张飞配置,不代表源游戏能力。');
return { ok: errors.length === 0, layout: errors.length ? null : layout, errors: [...new Set(errors)], warnings, unsupportedTypes };
}
function outputName(file) {
const stem = file.replace(/\.json$/i, '').replace(/[<>:"/\\|?*\x00-\x1f]/g, '_').slice(0, 80);
return stem + '.json';
}
const targetTypes = [
[1, '箭', 2, 1], [2, '爆破箭矢', 2, 1], [3, '滚石', 1, 1], [4, '计时炸弹箭矢', 2, 1],
[5, '张飞', 3, 2], [6, '舞女', 2, 1], [7, '张角', 2, 1], [8, '黄忠', 2, 1],
[9, '赵云', 2, 1], [10, '攻城车', 2, 1], [11, '羽扇', 2, 1], [12, '木栅栏', 1, 1],
[13, '将旗', 1, 1], [14, '火船', 1, 1], [101, '拒马', 1, 1], [102, '铁索1×2', 2, 1], [103, '铁索2×2', 2, 2]
].map(([id, name, length, width]) => ({ id, name, length, width, collection: id >= 100 ? 'aoes' : 'characters' }));
const api = { convert, outputName, defaults, targetTypes };
if (typeof module !== 'undefined' && module.exports) module.exports = api;
else root.PiggyConverter = api;
})(globalThis);