Aller au contenu

Génération procédurale

utility.js
function mergeTable(t1, t2, jointSymbol1, jointSymbol2, priorityList = []) {
const joint1 = getCharPosInLevel(t1, jointSymbol1);
const joint2 = getCharPosInLevel(t2, jointSymbol2);
if (joint1 && joint2) {
const coord = joint1.sub(joint2);
const lines1 = t1.split('\n').map((line) => line.trimEnd());
const lines2 = t2.split('\n').map((line) => line.trimEnd());
const maxWidth1 = Math.max(...lines1.map((line) => line.length), 0);
const maxWidth2 = Math.max(...lines2.map((line) => line.length), 0);
const t1ShiftY = coord.y < 0 ? Math.abs(coord.y) : 0;
const t1ShiftX = coord.x < 0 ? Math.abs(coord.x) : 0;
const totalWidth = Math.max(
maxWidth1 + t1ShiftX,
coord.x + maxWidth2 + t1ShiftX
);
const totalHeight = Math.max(
lines1.length + t1ShiftY,
coord.y + lines2.length + t1ShiftY
);
const resultGrid = Array.from({ length: totalHeight }, () =>
Array(totalWidth).fill(' ')
);
for (let y = 0; y < lines1.length; y++) {
for (let x = 0; x < lines1[y].length; x++) {
const resultY = y + t1ShiftY;
const resultX = x + t1ShiftX;
if (
resultY >= 0 &&
resultY < resultGrid.length &&
resultX >= 0 &&
resultX < resultGrid[0].length
) {
resultGrid[resultY][resultX] = lines1[y][x];
}
}
}
for (let y = 0; y < lines2.length; y++) {
for (let x = 0; x < lines2[y].length; x++) {
const resultY = y + coord.y + t1ShiftY;
const resultX = x + coord.x + t1ShiftX;
if (
resultY >= 0 &&
resultY < resultGrid.length &&
resultX >= 0 &&
resultX < resultGrid[0].length
) {
const c1 = resultGrid[resultY][resultX];
const c2 = lines2[y][x];
const priority = () => {
if (priorityList.includes(c2)) {
const newCharPriority = priorityList.indexOf(c2);
const oldCharPriority = priorityList.indexOf(c1);
return newCharPriority > oldCharPriority;
} else return true;
};
if (priority()) resultGrid[resultY][resultX] = lines2[y][x];
}
}
}
return resultGrid.map((line) => line.join('')).join('\n');
} else return t1;
}
utility.js
function drawFromList(list, length) {
const newList = [];
for (let i = 0; i < length; i++) {
if (i % list.length === 0) list = shuffle(list);
newList.push(list[i % list.length]);
}
return newList;
}
utility.js
function getCharPosInLevel(map, symbol) {
const lines = map.split('\n').map((line) => line.trimEnd());
const positions = [];
for (let y = 0; y < lines.length; y++) {
for (let x = 0; x < lines[y].length; x++) {
if (lines[y][x] === symbol) {
positions.push(vec2(x, y));
}
}
}
return positions[randi(positions.length)];
}
level.js
const LEVELS = [
{
map: `
# ?
=====
`,
rooms: [
`
?
===
¿
=====
`,
`
?
¿ ===
==
`,
`
?
¿ =
==
`,
],
},
];
game.js
scene('game', () => {
const config = {
...LEVEL_CONFIG,
...LEVELS[CURRENT_LEVEL].config,
...{
tiles: {
...TILE_CONFIG,
...LEVELS[CURRENT_LEVEL].tiles,
},
},
};
const map = LEVELS[CURRENT_LEVEL].map.split('\n');
let map = LEVELS[CURRENT_LEVEL].map;
for (const e of drawFromList(LEVELS[CURRENT_LEVEL].rooms, 4)) {
map = mergeTable(map, e, '?', '¿', [' ', '=', '?', '#']);
}
map = map.split('\n');
const level = addLevel(map, config);
const utility = add([
background(config, level),
music(config),
multiplayerCamera(),
events(),
]);
setGravity(config.gravity);
setMessage(config);
setBackground(config.backgroundColor);
});