Fixed enderman level background and level picker in unlock

This commit is contained in:
Renan LE CARO 2025-03-13 08:52:20 +01:00
parent f28ce15495
commit e14e958686
6 changed files with 94 additions and 55 deletions

21
dist/index.html vendored

File diff suppressed because one or more lines are too long

View file

@ -26,7 +26,6 @@ Object.entries(palette).forEach(([code, color]) => {
function renderAllLevels() { function renderAllLevels() {
allLevels.forEach((level, levelIndex) => { allLevels.forEach((level, levelIndex) => {
addLevelEditorToList(level, levelIndex) addLevelEditorToList(level, levelIndex)
}) })
} }
@ -46,6 +45,8 @@ function addLevelEditorToList(level, levelIndex) {
<button data-offset-x="0" data-offset-y="-1" data-level="${levelIndex}">U</button> <button data-offset-x="0" data-offset-y="-1" data-level="${levelIndex}">U</button>
<button data-offset-x="0" data-offset-y="1" data-level="${levelIndex}">D</button> <button data-offset-x="0" data-offset-y="1" data-level="${levelIndex}">D</button>
<input type="color" value="${level.color || ''}" data-level="${levelIndex}" /> <input type="color" value="${level.color || ''}" data-level="${levelIndex}" />
<input type="number" value="${level.svg || (hashCode(level.name) % backgrounds.length)}" data-level="${levelIndex}" data-num-val="svg" />
<button data-level="${levelIndex}" data-set-bg-svg="true" >${svg ? 'replace' : 'set'}</button> <button data-level="${levelIndex}" data-set-bg-svg="true" >${svg ? 'replace' : 'set'}</button>
@ -54,6 +55,8 @@ function addLevelEditorToList(level, levelIndex) {
<div class="level-bricks-preview" id="bricks-of-${levelIndex}" > <div class="level-bricks-preview" id="bricks-of-${levelIndex}" >
</div> </div>
`; `;
document.getElementById('levels').appendChild(div) document.getElementById('levels').appendChild(div)
renderLevelBricks(levelIndex) renderLevelBricks(levelIndex)
@ -63,11 +66,21 @@ function addLevelEditorToList(level, levelIndex) {
function updateLevelBackground(levelIndex) { function updateLevelBackground(levelIndex) {
const div = document.getElementById("bricks-of-" + levelIndex) const div = document.getElementById("bricks-of-" + levelIndex)
const {svg, color}= allLevels[levelIndex] const level = allLevels[levelIndex]
Object.assign(div.style, svg ? const {svg, color} = level
{backgroundImage:`url('data:image/svg+xml,${encodeURIComponent(svg)}')`, backgroundColor:'transparent'} : if (color) {
{backgroundImage:'none', backgroundColor:color||'#111'} Object.assign(div.style, {backgroundImage: 'none', backgroundColor: color})
) } else {
const index = svg || (hashCode(level.name) % backgrounds.length)
const svgSource=backgrounds[index]
console.log(index)
div.setAttribute('data-svg',svgSource)
Object.assign(div.style, {
backgroundImage: `url("data:image/svg+xml;UTF8,${encodeURIComponent(svgSource)}")`,
backgroundColor: 'transparent'
})
}
} }
function renderLevelBricks(levelIndex) { function renderLevelBricks(levelIndex) {
@ -90,7 +103,6 @@ function renderLevelBricks(levelIndex) {
} }
document.getElementById('levels').addEventListener('change', e => { document.getElementById('levels').addEventListener('change', e => {
const levelIndexStr = e.target.getAttribute('data-level') const levelIndexStr = e.target.getAttribute('data-level')
if (levelIndexStr) { if (levelIndexStr) {
@ -185,10 +197,12 @@ function colorPixel(e) {
setBrick(levelIndex, parseInt(index), applying) setBrick(levelIndex, parseInt(index), applying)
} }
} }
function setBrick(levelIndex, index, chr) { function setBrick(levelIndex, index, chr) {
const bricks = allLevels[levelIndex].bricks const bricks = allLevels[levelIndex].bricks
allLevels[levelIndex].bricks = bricks.substring(0, index) + chr + bricks.substring(index + 1); allLevels[levelIndex].bricks = bricks.substring(0, index) + chr + bricks.substring(index + 1);
} }
document.getElementById('levels').addEventListener('mousedown', e => { document.getElementById('levels').addEventListener('mousedown', e => {
const index = parseInt(e.target.getAttribute('data-set-color-of')) const index = parseInt(e.target.getAttribute('data-set-color-of'))
const level = e.target.getAttribute('data-level') const level = e.target.getAttribute('data-level')
@ -238,3 +252,13 @@ function save() {
body: JSON.stringify(allLevels, null, 2) body: JSON.stringify(allLevels, null, 2)
}) })
} }
function hashCode(string) {
var hash = 0;
for (var i = 0; i < string.length; i++) {
var code = string.charCodeAt(i);
hash = ((hash << 5) - hash) + code;
hash = hash & hash; // Convert to 32bit integer
}
return Math.abs(hash);
}

View file

@ -34,6 +34,7 @@ ${fs.readFileSync('./editclient.css').toString()}
<script> <script>
let allLevels = ${fs.readFileSync(srcPath).toString()}; let allLevels = ${fs.readFileSync(srcPath).toString()};
let palette = ${fs.readFileSync('src/palette.json').toString()}; let palette = ${fs.readFileSync('src/palette.json').toString()};
let backgrounds = ${fs.readFileSync('src/backgrounds.json').toString()};
${fs.readFileSync('./editclient.js').toString()} ${fs.readFileSync('./editclient.js').toString()}
</script> </script>
</body> </body>

View file

@ -495,13 +495,13 @@ function getPossibleUpgrades() {
function shuffleLevels(nameToAvoid: string | null = null) { function shuffleLevels(nameToAvoid: string | null = null) {
const target = nextRunOverrides?.level; const target = nextRunOverrides?.level;
delete nextRunOverrides.level; delete nextRunOverrides.level;
const firstLevel = nextRunOverrides?.level const firstLevel = target
? allLevels.filter((l) => l.name === target) ? allLevels.filter((l) => l.name === target)
: []; : [];
const restInRandomOrder = allLevels const restInRandomOrder = allLevels
.filter((l) => totalScoreAtRunStart >= l.threshold) .filter((l) => totalScoreAtRunStart >= l.threshold)
.filter((l) => l.name !== nextRunOverrides?.level) .filter((l) => l.name !== target)
.filter((l) => l.name !== nameToAvoid || allLevels.length === 1) .filter((l) => l.name !== nameToAvoid || allLevels.length === 1)
.sort(() => Math.random() - 0.5); .sort(() => Math.random() - 0.5);

View file

@ -151,8 +151,8 @@
"name": "Enderman", "name": "Enderman",
"size": 10, "size": 10,
"bricks": "___________gggggggg__gggggggg__gggggggg__gggggggg__vvvggvvv__gggggggg__gggggggg__gggggggg_____________________", "bricks": "___________gggggggg__gggggggg__gggggggg__gggggggg__vvvggvvv__gggggggg__gggggggg__gggggggg_____________________",
"svg": null, "svg": "",
"color": "#ffffff" "color": "#26a269"
}, },
{ {
"name": "Mushroom", "name": "Mushroom",

View file

@ -11,7 +11,6 @@ const rawLevelsList = _rawLevelsList as RawLevel[];
export const appVersion = _appVersion as string; export const appVersion = _appVersion as string;
let attributed = 0;
let levelIconHTMLCanvas = document.createElement("canvas"); let levelIconHTMLCanvas = document.createElement("canvas");
const levelIconHTMLCanvasCtx = levelIconHTMLCanvas.getContext("2d", { const levelIconHTMLCanvasCtx = levelIconHTMLCanvas.getContext("2d", {
@ -69,8 +68,7 @@ export const allLevels = rawLevelsList
let svg = level.svg!==null && backgrounds[level.svg] let svg = level.svg!==null && backgrounds[level.svg]
if (!level.color && !svg) { if (!level.color && !svg) {
svg = backgrounds[attributed % backgrounds.length]; svg = backgrounds[hashCode(level.name) % backgrounds.length];
attributed++;
} }
return { return {
...level, ...level,
@ -95,3 +93,14 @@ export const upgrades = rawUpgrades.map((u) => ({
...u, ...u,
icon: icons["icon:" + u.id], icon: icons["icon:" + u.id],
})) as Upgrade[]; })) as Upgrade[];
function hashCode(string:string){
let hash = 0;
for (let i = 0; i < string.length; i++) {
let code = string.charCodeAt(i);
hash = ((hash<<5)-hash)+code;
hash = hash & hash; // Convert to 32bit integer
}
return Math.abs(hash);
}