From afb8e3dcda8b8234d220a63775bfc5468d5616d2 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 May 2024 22:30:54 -0400 Subject: [PATCH] refactor(levels): create helper to init level --- src/helpers/level.ts | 27 ++++++++++++++++++++++++++- src/levels/0.ts | 12 +++--------- src/levels/1.ts | 11 +++-------- src/levels/2.ts | 11 +++-------- src/levels/3.ts | 11 +++-------- src/levels/4.ts | 11 +++-------- src/levels/5.ts | 12 +++--------- src/scenes/game.ts | 11 +++++++++-- 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/helpers/level.ts b/src/helpers/level.ts index e3a2739..1c44e4d 100644 --- a/src/helpers/level.ts +++ b/src/helpers/level.ts @@ -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 { return import(`../levels/${level}.ts`) } @@ -8,8 +14,27 @@ const titles = document.querySelectorAll( '.title', ) as NodeListOf +/** + * 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) + }) +} diff --git a/src/levels/0.ts b/src/levels/0.ts index f98e5d6..7f7dbd6 100644 --- a/src/levels/0.ts +++ b/src/levels/0.ts @@ -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 = ` diff --git a/src/levels/1.ts b/src/levels/1.ts index bcf4d6f..870a9e2 100644 --- a/src/levels/1.ts +++ b/src/levels/1.ts @@ -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?")]) } diff --git a/src/levels/2.ts b/src/levels/2.ts index 9c37142..1ff9422 100644 --- a/src/levels/2.ts +++ b/src/levels/2.ts @@ -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 = ` diff --git a/src/levels/3.ts b/src/levels/3.ts index 98cb144..0911b89 100644 --- a/src/levels/3.ts +++ b/src/levels/3.ts @@ -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?')]) } diff --git a/src/levels/4.ts b/src/levels/4.ts index cbd1d7e..942a04f 100644 --- a/src/levels/4.ts +++ b/src/levels/4.ts @@ -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')]) } diff --git a/src/levels/5.ts b/src/levels/5.ts index 0e34404..f1bb12a 100644 --- a/src/levels/5.ts +++ b/src/levels/5.ts @@ -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')]) } diff --git a/src/scenes/game.ts b/src/scenes/game.ts index 1d8c5ff..f646e0a 100644 --- a/src/scenes/game.ts +++ b/src/scenes/game.ts @@ -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({