54 lines
4.0 KiB
JavaScript
54 lines
4.0 KiB
JavaScript
(function (root) {
|
|
'use strict';
|
|
const defaults = { animalGroup: { 1: { id: 1, collection: 'characters', length: 2, width: 1 } }, obstacleGroup: {} };
|
|
function convert(source, mapping = defaults, zero = 'north') {
|
|
const errors = [], warnings = ['转换假设:size 为格数,position 为居中棋盘中的占格左下角,角度按顺时针递增。尚未核对原游戏代码或画面,导入后须检查朝向和占格。'];
|
|
const fail = message => errors.push(message);
|
|
const width = source?.size?.x, height = source?.size?.y;
|
|
const layout = { width, height, characters: [], aoes: [] };
|
|
if (![width, height].every(n => Number.isInteger(n) && n > 0 && n <= 64)) fail('size.x 和 size.y 必须为 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 = mapping?.[group]?.[object?.type];
|
|
if (!rule) { 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 + width / 2;
|
|
const row = object.position.y + height / 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];
|
|
if (rule.extra) entry.push(JSON.parse(JSON.stringify(rule.extra)));
|
|
layout[rule.collection].push(entry);
|
|
const extras = Object.keys(object).filter(key => !['id', 'type', 'position', 'angle', 'skinIndex'].includes(key));
|
|
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('相机参数、皮肤编号、难度、奖励不导入;目标关卡级配置由编辑器所选模板继承。');
|
|
return { ok: errors.length === 0, layout: errors.length ? null : layout, errors: [...new Set(errors)], warnings };
|
|
}
|
|
const api = { convert, defaults };
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
|
else root.PiggyConverter = api;
|
|
})(globalThis);
|