Skip to content

Commit fbf2519

Browse files
fix(helpers): improve device scale ratio so tiles fit in screen
1 parent 07228f8 commit fbf2519

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/helpers/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './background'
22
export * from './pipes'
3+
export * from './scale'
34
export * from './solution'

src/helpers/scale.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
enum Breakpoint {
2+
sm = 576,
3+
md = 768,
4+
lg = 992,
5+
xl = 1200,
6+
xxl = 1400,
7+
}
8+
9+
/**
10+
* Gets device scale.
11+
*
12+
* @returns - Scale between 0 and 1.
13+
*/
14+
export function getDeviceScale() {
15+
const scale = window.innerWidth / Breakpoint.xxl
16+
17+
if (scale > 1) {
18+
return 1
19+
}
20+
21+
return scale
22+
}

src/levels/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TILE_SIZE } from '../constants'
2+
import { getDeviceScale } from '../helpers'
23
import { levels } from './levels'
34
import { getTiles } from './tiles'
45

@@ -10,7 +11,7 @@ import { getTiles } from './tiles'
1011
*/
1112
export function getLevel(level: number) {
1213
const { map, scale } = levels[level]
13-
const tileScale = (scale * window.innerWidth) / 1920
14+
const tileScale = scale * getDeviceScale()
1415

1516
const tileWidth = TILE_SIZE * tileScale
1617
const tileHeight = TILE_SIZE * tileScale

src/levels/levels.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ export const levels: Level[] = [
9191
' ║╚═╬═╝║ ',
9292
' ╚══╩══╝ ',
9393
],
94-
scale: 0.8,
94+
scale: 0.65,
9595
},
9696
]

src/scenes/game.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { EmptyToFilledPipe, Scene, Sound, Tag } from '../constants'
2-
import { checkSolution, rotatePipe, setRandomBackgroundColor } from '../helpers'
2+
import {
3+
checkSolution,
4+
getDeviceScale,
5+
rotatePipe,
6+
setRandomBackgroundColor,
7+
} from '../helpers'
38
import { getLevel, hasLevel } from '../levels'
49

510
scene(Scene.game, (levelNumber: number) => {
@@ -8,7 +13,10 @@ scene(Scene.game, (levelNumber: number) => {
813
}
914

1015
add([
11-
text(String(levelNumber), { align: 'center', size: 42 }),
16+
text(String(levelNumber), {
17+
align: 'center',
18+
size: Math.max(64 * getDeviceScale(), 42),
19+
}),
1220
pos(center().x, 24),
1321
color(15, 16, 53),
1422
])

0 commit comments

Comments
 (0)