From 44a40d0112fa053d2a58f6ba28a3424e3b44505a Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 1 Jun 2024 01:50:27 -0400 Subject: [PATCH] fix(levels): improve levels --- src/levels/0.ts | 2 +- src/levels/12.ts | 3 ++- src/levels/13.ts | 20 ++++++++++++-------- src/levels/14.ts | 7 ++++--- src/levels/15.ts | 2 +- src/levels/16.ts | 11 +++++------ src/levels/17.ts | 2 +- src/levels/18.ts | 8 ++++---- src/levels/19.ts | 9 +++++++-- src/levels/20.ts | 9 +++++++-- src/levels/5.ts | 2 +- src/templates/sprites.ts | 4 ++-- src/templates/texts.ts | 12 +++++++++++- 13 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/levels/0.ts b/src/levels/0.ts index 6b203ff..7f609fe 100644 --- a/src/levels/0.ts +++ b/src/levels/0.ts @@ -17,7 +17,7 @@ ${registerPlayerMovement()} ${registerWinCondition(level)} ${addText('WASD or arrow keys to move')} -add([text('Goal: reach the exit'), pos(0, 64), color(0, 0, 0)]) +${addText('Goal: reach the exit', { pos: '0, height() - 32' })} ` export const script = ` diff --git a/src/levels/12.ts b/src/levels/12.ts index 6539615..0ad1347 100644 --- a/src/levels/12.ts +++ b/src/levels/12.ts @@ -80,8 +80,9 @@ export const script = ` * forEach() is an iterative method */ +// hint: there are 25 spikes in this level const spikes = get('spike') -spikes[0].opacity = 0 +spikes[0].opacity = 1 ` export const postscript = ` diff --git a/src/levels/13.ts b/src/levels/13.ts index 52625e0..cce2c40 100644 --- a/src/levels/13.ts +++ b/src/levels/13.ts @@ -62,12 +62,12 @@ onDestroy('enemy', () => { addEnemy() }) -${addText('Protect yourself')} +${addText('Block yourself')} ` export const script = ` /** - * Can you build a fortress to protect yourself? + * Can you build a fortress to block the enemy? */ const block = { @@ -75,12 +75,16 @@ const block = { height: 64, } -add([ - sprite('block'), - pos(block.width * 6, block.height * 6), - area(), - body({ isStatic: true }), -]) +function addBlock(x, y) { + add([ + sprite('block'), + pos(x, y), + area(), + body({ isStatic: true }), + ]) +} + +addBlock(block.width * 3, block.height * 3) ` export const postscript = ` diff --git a/src/levels/14.ts b/src/levels/14.ts index 5a80703..2ed2f1a 100644 --- a/src/levels/14.ts +++ b/src/levels/14.ts @@ -11,12 +11,12 @@ export const title = 'setTimeout' export const prescript = ` ${loadExit()} -${addPlayer({ pos: '100, 100' })} +${addPlayer({ pos: 'center()' })} ${registerPlayerMovement()} ${registerWinCondition(level)} -${addText('Wait for the exit')} +${addText('Wait for exit?')} ` export const script = ` @@ -28,7 +28,8 @@ const MILLISECOND = 1 const SECOND = MILLISECOND * 1000 const MINUTE = SECOND * 60 +// can we speed this up? setTimeout(() => { - add([sprite('exit'), pos(center()), area(), 'exit']) + add([sprite('exit'), area(), 'exit']) }, 5 * MINUTE) ` diff --git a/src/levels/15.ts b/src/levels/15.ts index a1fbd87..4bebf54 100644 --- a/src/levels/15.ts +++ b/src/levels/15.ts @@ -34,5 +34,5 @@ setInterval(() => { randi(width()), randi(height()), ) -}, SECOND) +}, 2 * SECOND) ` diff --git a/src/levels/16.ts b/src/levels/16.ts index afc82a2..3afa17a 100644 --- a/src/levels/16.ts +++ b/src/levels/16.ts @@ -2,7 +2,6 @@ import { addPlayer, loadExit, loadKey, - loadSignal, registerPlayerMovement, registerWinCondition, } from '../templates' @@ -13,7 +12,6 @@ export const title = 'Repetition is key' export const prescript = ` ${loadExit()} ${loadKey()} -${loadSignal()} ${addPlayer({ pos: 'center()' })} @@ -39,7 +37,6 @@ ${registerPlayerMovement()} ${registerWinCondition(level)} onCollide('key', 'player', (key) => { - play('signal', { volume: 0.2, speed: 2 }) keys-- key.destroy() message.text = getMessage() @@ -63,7 +60,9 @@ export const script = ` * Can we speed things up? */ -const player = get('player')[0] -const key = get('key')[0] -// player.moveTo(key.pos) +function collectKey() { + const player = get('player')[0] + const key = get('key')[0] + key && player.moveTo(key.pos) +} ` diff --git a/src/levels/17.ts b/src/levels/17.ts index f9cd3e0..58a7db5 100644 --- a/src/levels/17.ts +++ b/src/levels/17.ts @@ -34,7 +34,7 @@ export const script = ` const key = get('key')[0] -// password = JSON string of object containing "level" and "year" +// password = JSON string of object containing key-value pairs of "level" and "year" let password key.password = password diff --git a/src/levels/18.ts b/src/levels/18.ts index 453adea..0a56853 100644 --- a/src/levels/18.ts +++ b/src/levels/18.ts @@ -12,14 +12,14 @@ export const level = 18 export const title = 'JSON.parse' const password = btoa(String(Date.now())) -const passwordJSON = JSON.stringify({ password }) +const json = JSON.stringify({ password }) export const prescript = ` ${loadExit()} ${loadKey()} ${addPlayer({ pos: '100, 100' })} -add([sprite('key'), pos(center()), area(), 'key', { password: '${passwordJSON}' }]) +add([sprite('key'), pos(center()), area(), 'key', { json: '${json}' }]) ${registerPlayerMovement()} ${registerWinCondition(level)} @@ -35,8 +35,8 @@ export const script = ` const key = get('key')[0] -// parse the password from \`key.password\` -console.log(key.password) +// parse the "password" from \`key.json\` +// console.log(key.json) let password key.password = password diff --git a/src/levels/19.ts b/src/levels/19.ts index 4fc5eae..66c2e37 100644 --- a/src/levels/19.ts +++ b/src/levels/19.ts @@ -35,12 +35,17 @@ export const script = ` const key = get('key')[0] -// example of a successful Promise +// example of successful Promise const examplePromise = Promise.resolve('some value') examplePromise.then((value) => { console.log(value) + key.password = value }) // set \`key.password\` to the resolved value of \`key.promise\` -console.log('is promise?', key.promise instanceof Promise) +console.log('is promise?', isPromise(key.promise)) + +function isPromise(value) { + return value instanceof Promise +} ` diff --git a/src/levels/20.ts b/src/levels/20.ts index 92053d8..ca899b2 100644 --- a/src/levels/20.ts +++ b/src/levels/20.ts @@ -35,12 +35,17 @@ export const script = ` const key = get('key')[0] -// example of a failed Promise +// example of failed Promise const examplePromise = Promise.reject('some value') examplePromise.catch((value) => { console.log(value) + key.password = value }) // set \`key.password\` to the rejected value of \`key.promise\` -console.log('is promise?', key.promise instanceof Promise) +console.log('is promise?', isPromise(key.promise)) + +function isPromise(value) { + return value instanceof Promise +} ` diff --git a/src/levels/5.ts b/src/levels/5.ts index a06d6fb..790585e 100644 --- a/src/levels/5.ts +++ b/src/levels/5.ts @@ -16,7 +16,7 @@ ${loadExit()} ${registerPlayerMovement()} ${registerWinCondition(level)} -${addText('Exit is not in view?')} +${addText('Exit not in view?')} ` export const script = ` diff --git a/src/templates/sprites.ts b/src/templates/sprites.ts index d24859f..8c51569 100644 --- a/src/templates/sprites.ts +++ b/src/templates/sprites.ts @@ -2,7 +2,7 @@ * Loads and adds exit. * * @param options - Options. - * @returns - Game code. + * @returns - Script. */ export const addExit = ({ pos = '' } = {}) => ` ${loadExit()} @@ -13,7 +13,7 @@ add([sprite('exit'), anchor('center'), area(), pos(${pos}), 'exit']) * Loads and adds player. * * @param options - Options. - * @returns - Game code. + * @returns - Script. */ export const addPlayer = ({ pos = '' } = {}) => ` ${loadPlayer()} diff --git a/src/templates/texts.ts b/src/templates/texts.ts index 9508c7f..e0307cf 100644 --- a/src/templates/texts.ts +++ b/src/templates/texts.ts @@ -1 +1,11 @@ -export const addText = (text: string) => `add([text(${JSON.stringify(text)})])` +/** + * Adds white text with black background. + * + * @param text - Text. + * @param options - Options. + * @returns - Script. + */ +export const addText = (text: string, { pos = '' } = {}) => ` +add([rect(width(), 32), color(0, 0, 0), pos(${pos}), z(100)]) +add([text(${JSON.stringify(text)}), pos(${pos}), z(100)]) +`