Skip to content

Commit

Permalink
refactor(levels): create helper to init level
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 27, 2024
1 parent ef9ee20 commit afb8e3d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 53 deletions.
27 changes: 26 additions & 1 deletion src/helpers/level.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { Level } from '../types'
import { Level, Scene, Sprite } from '../types'

/**
* Gets level by number.
*
* @param - Level number.
* @returns - Level config.
*/
export function getLevel(level: number): Promise<Level> {
return import(`../levels/${level}.ts`)
}
Expand All @@ -8,8 +14,27 @@ const titles = document.querySelectorAll(
'.title',
) as NodeListOf<HTMLHeadingElement>

/**
* Renders level title.
*
* @param level - Level config.
*/
export function renderLevel(level: Level) {
titles.forEach(
(title) => (title.innerText = `${level.level}. ${level.title}`),
)
}

/**
* Initializes level.
*
* @param level - Level number.
*/
export function initLevel(level: number) {
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, level + 1)
})
}
12 changes: 3 additions & 9 deletions src/levels/0.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { addCursorKeys } from '../helpers'
import { Scene, Sprite } from '../types'
import { addCursorKeys, initLevel } from '../helpers'
import { Sprite } from '../types'

export const level = 0
export const title = 'Hello, World!'

export function prescript() {
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')

initLevel(level)
addCursorKeys(
add([sprite(Sprite.player), pos(50, 50), area(), Sprite.player]),
)
add([sprite(Sprite.exit), pos(500, 500), area(), Sprite.exit])

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, level + 1)
})
}

export const script = `
Expand Down
11 changes: 3 additions & 8 deletions src/levels/1.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { addCursorKeys } from '../helpers'
import { Scene, Sprite } from '../types'
import { addCursorKeys, initLevel } from '../helpers'
import { Sprite } from '../types'

export const level = 1
export const title = 'Comments'

export function prescript() {
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')
initLevel(level)

addCursorKeys(
add([sprite(Sprite.player), pos(50, 50), area(), Sprite.player]),
)

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, level + 1)
})

add([text("Where's the exit?")])
}

Expand Down
11 changes: 3 additions & 8 deletions src/levels/2.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { addCursorKeys } from '../helpers'
import { Scene, Sprite } from '../types'
import { addCursorKeys, initLevel } from '../helpers'
import { Sprite } from '../types'

export const level = 2
export const title = 'Strings'

export function prescript() {
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')
initLevel(level)

addCursorKeys(
add([sprite(Sprite.player), pos(center()), area(), Sprite.player]),
)

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, level + 1)
})
}

export const script = `
Expand Down
11 changes: 3 additions & 8 deletions src/levels/3.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { addCursorKeys } from '../helpers'
import { Scene, Sprite } from '../types'
import { addCursorKeys, initLevel } from '../helpers'
import { Sprite } from '../types'

export const level = 3
export const title = 'Numbers'

export function prescript() {
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')
initLevel(level)

addCursorKeys(
add([sprite(Sprite.player), pos(500, 500), area(), Sprite.player]),
)

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, level + 1)
})

add([text('Exit is not in view?')])
}

Expand Down
11 changes: 3 additions & 8 deletions src/levels/4.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { addCursorKeys } from '../helpers'
import { Scene, Sprite } from '../types'
import { addCursorKeys, initLevel } from '../helpers'
import { Sprite } from '../types'

export const level = 4
export const title = 'Booleans'

export function prescript() {
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')
initLevel(level)

addCursorKeys(
add([sprite(Sprite.player), pos(center()), area(), Sprite.player]),
)

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, level + 1)
})

add([text('Exit is not truthy')])
}

Expand Down
12 changes: 3 additions & 9 deletions src/levels/5.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { addCursorKeys } from '../helpers'
import { Scene, Sprite } from '../types'
import { addCursorKeys, initLevel } from '../helpers'
import { Sprite } from '../types'

export const level = 5
export const title = 'Arrays'

export function prescript() {
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')
initLevel(level)
loadSprite(Sprite.wall, 'sprites/steel.png')

addCursorKeys(
add([sprite(Sprite.player), pos(center()), area(), body(), Sprite.player]),
)

add([sprite(Sprite.exit), pos(500, 500), area(), Sprite.exit])

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, 0)
})

add([text('Escape the walls')])
}

Expand Down
11 changes: 9 additions & 2 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import {
initGame,
renderLevel,
} from '../helpers'
import { Scene } from '../types'
import { Level, Scene } from '../types'

export function initGameScene() {
scene(Scene.game, async (currentLevel: number) => {
const level = await getLevel(currentLevel)
let level: Level

try {
level = await getLevel(currentLevel)
} catch (error) {
return go(Scene.game, 0)
}

renderLevel(level)

editorView.dispatch({
Expand Down

0 comments on commit afb8e3d

Please sign in to comment.