Skip to content

Commit 8292eb6

Browse files
committed
fix minimap rendering and added incremental jump height
1 parent 743ac1a commit 8292eb6

10 files changed

Lines changed: 124 additions & 100 deletions

File tree

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -359,29 +359,31 @@ async function loadSpriteSheetTileset(manifest) {
359359
}
360360

361361
let minimapColor = 'rgba(0, 0, 0, 0)'
362-
try {
363-
const imgData = ctx.getImageData(0, 0, width, width).data
364-
const colorCounts = {}
365-
let maxCount = 0
366-
for (let i = 0; i < imgData.length; i += 4) {
367-
const r = imgData[i]
368-
const g = imgData[i + 1]
369-
const b = imgData[i + 2]
370-
const a = imgData[i + 3]
371-
372-
if (a < 128 || (r < 10 && g < 10 && b < 10)) continue
373-
const rgb = `rgb(${r}, ${g}, ${b})`
374-
colorCounts[rgb] = (colorCounts[rgb] || 0) + 1
375-
376-
if (colorCounts[rgb] > maxCount && colorCounts[rgb] > (imgData.length / 4) * 0.1) {
377-
maxCount = colorCounts[rgb]
378-
minimapColor = rgb
362+
if (tile.type !== "empty") {
363+
364+
try {
365+
const imgData = ctx.getImageData(0, 0, width, width).data
366+
const colorCounts = {}
367+
let maxCount = 0
368+
for (let i = 0; i < imgData.length; i += 4) {
369+
const r = imgData[i]
370+
const g = imgData[i + 1]
371+
const b = imgData[i + 2]
372+
const a = imgData[i + 3]
373+
374+
if (a < 128) continue
375+
const rgb = `rgb(${r}, ${g}, ${b})`
376+
colorCounts[rgb] = (colorCounts[rgb] || 0) + 1
377+
378+
if (colorCounts[rgb] > maxCount && colorCounts[rgb] > (imgData.length / 4) * 0.1) {
379+
maxCount = colorCounts[rgb]
380+
minimapColor = rgb
381+
}
379382
}
383+
} catch (e) {
384+
console.warn("could not calculate minimap color", e)
380385
}
381-
} catch (e) {
382-
console.warn("could not calculate minimap color", e)
383386
}
384-
385387
const tileObject = {
386388
...tile,
387389
minimapColor: minimapColor,
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ export function calcAdjacentAdjacency(idx, tile = editor.selectedTile, tiles = e
159159
*/
160160
function getJumpHeight(heightInTiles, yInertia, tileSize) {
161161
const gravity = ((0.7 * yInertia) + 0.5) * (tileSize / 64)
162-
const heightInPixels = heightInTiles * tileSize
163-
return Math.sqrt(2 * gravity * heightInPixels)
162+
const maxHeightInPixels = heightInTiles * tileSize
163+
const minHeightInPixels = (heightInTiles * 0.3) * tileSize
164+
return {
165+
max: Math.sqrt(2 * gravity * maxHeightInPixels),
166+
min: Math.sqrt(2 * gravity * minHeightInPixels)
167+
}
164168
}
165169

166170
function getJumpSpeed(jumpLengthInTiles, jumpForce, yInertia, tilesize) {
@@ -225,7 +229,9 @@ function scanLevelOnPlay() {
225229

226230
export function updatePhysicsConstants() {
227231
const ratio = player.tileSize / 64
228-
player.jump = getJumpHeight(player.jumpHeight + 0.3, player.yInertia, player.tileSize)
232+
const jumpInfo = getJumpHeight(player.jumpHeight + 0.3, player.yInertia, player.tileSize)
233+
player.jump = jumpInfo.max
234+
player.minJump = jumpInfo.min
229235
player.speed = getJumpSpeed(player.jumpWidth - 1, player.jump, player.yInertia, player.tileSize)
230236
player.x = editor.playerSpawn.x * player.tileSize
231237
player.y = editor.playerSpawn.y * player.tileSize
@@ -500,13 +506,13 @@ function mechanics(dt, tileIdx, tileId, tx, ty, x, y, w, h) {
500506
const idx = ty * editor.map.w + tx
501507
const bounceTile = tiles[idx]
502508
if ((bounceTile & 15) == 0) {
503-
player.vy = -getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize)
509+
player.vy = -getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize).max
504510
} else if ((bounceTile & 15) == 1) {
505-
player.vx = -getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize)
511+
player.vx = -getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize).max
506512
} else if ((bounceTile & 15) == 2) {
507-
player.vy = getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize)
513+
player.vy = getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize).max
508514
} else if ((bounceTile & 15) == 3) {
509-
player.vx = getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize)
515+
player.vx = getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize).max
510516
}
511517
}
512518
}
@@ -769,6 +775,10 @@ function updatePhysics(dt) {
769775
player.vy += gravity * dt
770776
}
771777

778+
if (player.vy < -player.minJump && !key("up") && !input.jumpButton) {
779+
player.vy = -player.minJump
780+
}
781+
772782
if (player.vy > player.tileSize * 0.8) {
773783
player.vy = player.tileSize * 0.8
774784
}
@@ -992,7 +1002,7 @@ function handleEnemyCollision(enemy, dt) {
9921002

9931003
if (py < ey) {
9941004
// player stomped on enemy
995-
player.vy = -getJumpHeight(5, player.yInertia, player.tileSize)
1005+
player.vy = -getJumpHeight(5, player.yInertia, player.tileSize).max
9961006
return true
9971007
} else {
9981008
killPlayer()

dist/assets/site-CWigJk1P.js

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assets/site-CadXMsZ6.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

dist/editor.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
<link rel="modulepreload" href="data:text/javascript;base64,aW1wb3J0IHsgbG9hZE1hcEZyb21EYXRhLCBsb2FkT3duZXJEYXRhIH0gZnJvbSAnLi9maWxlLXV0aWxzLmpzJw0KaW1wb3J0IHsgaW5pdCB9IGZyb20gJy9qYXZhc2NyaXB0L3NpdGUuanMnDQppbXBvcnQgeyBzdGF0ZSB9IGZyb20gJy9qYXZhc2NyaXB0L3N0YXRlLmpzJw0KY29uc3QgeyB1c2VyIH0gPSBzdGF0ZQ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vYWRPd25lckRhdGEobGV2ZWwpDQogICAgfSBlbHNlIGlmIChsZXZlbCAmJiBsZXZlbC5lcnJvcikgew0KICAgIH0NCiAgICBpbml0KCkNCiAgfSkNCn0gZWxzZSB7DQogIGluaXQoKQ0KfQ0K">
1111
<link rel="modulepreload" href="/assets/editor-C3bxFxBx.js">
1212
<link rel="modulepreload" href="/assets/renderer-CJ2M1ROd.js">
13-
<link rel="modulepreload" href="/assets/file-utils-dbG1zRsN.js">
13+
<link rel="modulepreload" href="/assets/file-utils-DejAchk5.js">
1414
<link rel="modulepreload" href="/assets/ui-Ck83WllN.js">
15-
<link rel="modulepreload" href="/assets/platformer-C7UeabRD.js">
15+
<link rel="modulepreload" href="/assets/platformer-EAeHrkyG.js">
1616
<link rel="modulepreload" href="/assets/state-BcRWoonl.js">
1717
<link rel="modulepreload" href="data:text/javascript;base64,Y29uc3Qgc2VydmVyVXJsID0gd2luZG93LmxvY2F0aW9uLm9yaWdpbg0KDQpjb25zdCBzZXJ2ZXJVUkwgPSB3aW5kb3cubG9jYXRpb24ub3JpZ2luDQoNCmV4cG9ydCBhc3luYyBmdW5jdGlvbiB1cGxvYWRMZXZlbChkYXRhTGlzdCkgew0KICBjb25zdCBwYXlsb2FkID0ge30NCiAgZGF0YUxpc3QuZm9yRWFjaChkYXRhcG9pbnQgPT4gew0KICAgIHBheWxvYWRbZGF0YXBvaW50WzBdXSA9IGRhdGFwb2ludFsxXQ0KICB9KQ0KDQogIGNvbnN0IHVwbG9hZCA9IGF3YWl0IGZldGNoKGAke3NlcnZlclVSTH0vYXBpL3VwbG9hZGAsIHsNCiAgICBtZXRob2Q6ICdQT1NUJywNCiAgICBoZWFkZXJzOiB7ICdDb250ZW50LVR5cGUnOiAnYXBwbGljYXRpb24vanNvbicgfSwNCiAgICBjcmVkZW50aWFsczogJ2luY2x1ZGUnLA0KICAgIGJvZHk6IEpTT04uc3RyaW5naWZ5KHBheWxvYWQpDQogIH0pDQogIGNvbnN0IGxldmVsSWQgPSBhd2FpdCB1cGxvYWQuanNvbigpDQogIHJldHVybiBsZXZlbElkLmxldmVsSWQNCn0NCg0KZXhwb3J0IGZ1bmN0aW9uIHBsYXkobGV2ZWxJZCwgZmluaXNoZWQpIHsNCiAgY29uc3QgcGF5bG9hZCA9IHsgbGV2ZWxJZDogbGV2ZWxJZCwgZmluaXNoZWQ6IGZpbmlzaGVkIH0NCg0KICBmZXRjaChgJHtzZXJ2ZXJVcmx9L2FwaS9wbGF5YCwgew0KICAgIG1ldGhvZDogIlBPU1QiLA0KICAgIGJvZHk6IEpTT04uc3RyaW5naWZ5KHBheWxvYWQpDQogIH0pDQp9DQo=">
1818

1919
<link rel="preconnect" href="https://fonts.googleapis.com">
2020
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
2121
<link href="https://fonts.googleapis.com/css2?family=Fredoka:wght@300..700&display=swap" rel="stylesheet">
2222
<title>Platformed - Editor</title>
23-
<script type="module" crossorigin src="/assets/editor-Z8_L270F.js"></script>
23+
<script type="module" crossorigin src="/assets/editor-Ffl2RdB4.js"></script>
2424
<link rel="modulepreload" crossorigin href="/assets/modulepreload-polyfill-EeOZK34R.js">
25-
<link rel="modulepreload" crossorigin href="/assets/site-CadXMsZ6.js">
25+
<link rel="modulepreload" crossorigin href="/assets/site-CWigJk1P.js">
2626
<link rel="stylesheet" crossorigin href="/assets/login-GD92ah_R.css">
2727
<link rel="stylesheet" crossorigin href="/assets/style-D-kSfpz6.css">
2828
</head>

dist/level.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
99
<link href="https://fonts.googleapis.com/css2?family=Fredoka:wght@300..700&display=swap" rel="stylesheet">
1010
<title>Platformed</title>
11-
<script type="module" crossorigin src="/assets/level-BtZqIXAd.js"></script>
11+
<script type="module" crossorigin src="/assets/level-C4bHY-wr.js"></script>
1212
<link rel="modulepreload" crossorigin href="/assets/modulepreload-polyfill-EeOZK34R.js">
13-
<link rel="modulepreload" crossorigin href="/assets/site-CadXMsZ6.js">
13+
<link rel="modulepreload" crossorigin href="/assets/site-CWigJk1P.js">
1414
<link rel="stylesheet" crossorigin href="/assets/level-DEkZhbzX.css">
1515
<link rel="stylesheet" crossorigin href="/assets/home-ClEfntOQ.css">
1616
<link rel="stylesheet" crossorigin href="/assets/style-D-kSfpz6.css">

frontend/javascript/file-utils.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -359,29 +359,31 @@ async function loadSpriteSheetTileset(manifest) {
359359
}
360360

361361
let minimapColor = 'rgba(0, 0, 0, 0)'
362-
try {
363-
const imgData = ctx.getImageData(0, 0, width, width).data
364-
const colorCounts = {}
365-
let maxCount = 0
366-
for (let i = 0; i < imgData.length; i += 4) {
367-
const r = imgData[i]
368-
const g = imgData[i + 1]
369-
const b = imgData[i + 2]
370-
const a = imgData[i + 3]
371-
372-
if (a < 128 || (r < 10 && g < 10 && b < 10)) continue
373-
const rgb = `rgb(${r}, ${g}, ${b})`
374-
colorCounts[rgb] = (colorCounts[rgb] || 0) + 1
375-
376-
if (colorCounts[rgb] > maxCount && colorCounts[rgb] > (imgData.length / 4) * 0.1) {
377-
maxCount = colorCounts[rgb]
378-
minimapColor = rgb
362+
if (tile.type !== "empty") {
363+
364+
try {
365+
const imgData = ctx.getImageData(0, 0, width, width).data
366+
const colorCounts = {}
367+
let maxCount = 0
368+
for (let i = 0; i < imgData.length; i += 4) {
369+
const r = imgData[i]
370+
const g = imgData[i + 1]
371+
const b = imgData[i + 2]
372+
const a = imgData[i + 3]
373+
374+
if (a < 128) continue
375+
const rgb = `rgb(${r}, ${g}, ${b})`
376+
colorCounts[rgb] = (colorCounts[rgb] || 0) + 1
377+
378+
if (colorCounts[rgb] > maxCount && colorCounts[rgb] > (imgData.length / 4) * 0.1) {
379+
maxCount = colorCounts[rgb]
380+
minimapColor = rgb
381+
}
379382
}
383+
} catch (e) {
384+
console.warn("could not calculate minimap color", e)
380385
}
381-
} catch (e) {
382-
console.warn("could not calculate minimap color", e)
383386
}
384-
385387
const tileObject = {
386388
...tile,
387389
minimapColor: minimapColor,

frontend/javascript/platformer.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ export function calcAdjacentAdjacency(idx, tile = editor.selectedTile, tiles = e
159159
*/
160160
function getJumpHeight(heightInTiles, yInertia, tileSize) {
161161
const gravity = ((0.7 * yInertia) + 0.5) * (tileSize / 64)
162-
const heightInPixels = heightInTiles * tileSize
163-
return Math.sqrt(2 * gravity * heightInPixels)
162+
const maxHeightInPixels = heightInTiles * tileSize
163+
const minHeightInPixels = (heightInTiles * 0.3) * tileSize
164+
return {
165+
max: Math.sqrt(2 * gravity * maxHeightInPixels),
166+
min: Math.sqrt(2 * gravity * minHeightInPixels)
167+
}
164168
}
165169

166170
function getJumpSpeed(jumpLengthInTiles, jumpForce, yInertia, tilesize) {
@@ -225,7 +229,9 @@ function scanLevelOnPlay() {
225229

226230
export function updatePhysicsConstants() {
227231
const ratio = player.tileSize / 64
228-
player.jump = getJumpHeight(player.jumpHeight + 0.3, player.yInertia, player.tileSize)
232+
const jumpInfo = getJumpHeight(player.jumpHeight + 0.3, player.yInertia, player.tileSize)
233+
player.jump = jumpInfo.max
234+
player.minJump = jumpInfo.min
229235
player.speed = getJumpSpeed(player.jumpWidth - 1, player.jump, player.yInertia, player.tileSize)
230236
player.x = editor.playerSpawn.x * player.tileSize
231237
player.y = editor.playerSpawn.y * player.tileSize
@@ -500,13 +506,13 @@ function mechanics(dt, tileIdx, tileId, tx, ty, x, y, w, h) {
500506
const idx = ty * editor.map.w + tx
501507
const bounceTile = tiles[idx]
502508
if ((bounceTile & 15) == 0) {
503-
player.vy = -getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize)
509+
player.vy = -getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize).max
504510
} else if ((bounceTile & 15) == 1) {
505-
player.vx = -getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize)
511+
player.vx = -getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize).max
506512
} else if ((bounceTile & 15) == 2) {
507-
player.vy = getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize)
513+
player.vy = getJumpHeight(player.bouncePadHeight, player.yInertia, player.tileSize).max
508514
} else if ((bounceTile & 15) == 3) {
509-
player.vx = getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize)
515+
player.vx = getJumpHeight(player.bouncePadHeight, player.xInertia, player.tileSize).max
510516
}
511517
}
512518
}
@@ -769,6 +775,10 @@ function updatePhysics(dt) {
769775
player.vy += gravity * dt
770776
}
771777

778+
if (player.vy < -player.minJump && !key("up") && !input.jumpButton) {
779+
player.vy = -player.minJump
780+
}
781+
772782
if (player.vy > player.tileSize * 0.8) {
773783
player.vy = player.tileSize * 0.8
774784
}
@@ -992,7 +1002,7 @@ function handleEnemyCollision(enemy, dt) {
9921002

9931003
if (py < ey) {
9941004
// player stomped on enemy
995-
player.vy = -getJumpHeight(5, player.yInertia, player.tileSize)
1005+
player.vy = -getJumpHeight(5, player.yInertia, player.tileSize).max
9961006
return true
9971007
} else {
9981008
killPlayer()

0 commit comments

Comments
 (0)