Skip to content

Commit

Permalink
fix(levels): improve levels
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jun 1, 2024
1 parent cb366e4 commit 44a40d0
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/levels/0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
3 changes: 2 additions & 1 deletion src/levels/12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
20 changes: 12 additions & 8 deletions src/levels/13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,29 @@ 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 = {
width: 64,
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 = `
Expand Down
7 changes: 4 additions & 3 deletions src/levels/14.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -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)
`
2 changes: 1 addition & 1 deletion src/levels/15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ setInterval(() => {
randi(width()),
randi(height()),
)
}, SECOND)
}, 2 * SECOND)
`
11 changes: 5 additions & 6 deletions src/levels/16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
addPlayer,
loadExit,
loadKey,
loadSignal,
registerPlayerMovement,
registerWinCondition,
} from '../templates'
Expand All @@ -13,7 +12,6 @@ export const title = 'Repetition is key'
export const prescript = `
${loadExit()}
${loadKey()}
${loadSignal()}
${addPlayer({ pos: 'center()' })}
Expand All @@ -39,7 +37,6 @@ ${registerPlayerMovement()}
${registerWinCondition(level)}
onCollide('key', 'player', (key) => {
play('signal', { volume: 0.2, speed: 2 })
keys--
key.destroy()
message.text = getMessage()
Expand All @@ -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)
}
`
2 changes: 1 addition & 1 deletion src/levels/17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/levels/18.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/levels/19.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
`
9 changes: 7 additions & 2 deletions src/levels/20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
`
2 changes: 1 addition & 1 deletion src/levels/5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ${loadExit()}
${registerPlayerMovement()}
${registerWinCondition(level)}
${addText('Exit is not in view?')}
${addText('Exit not in view?')}
`

export const script = `
Expand Down
4 changes: 2 additions & 2 deletions src/templates/sprites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Loads and adds exit.
*
* @param options - Options.
* @returns - Game code.
* @returns - Script.
*/
export const addExit = ({ pos = '' } = {}) => `
${loadExit()}
Expand All @@ -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()}
Expand Down
12 changes: 11 additions & 1 deletion src/templates/texts.ts
Original file line number Diff line number Diff line change
@@ -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)])
`

0 comments on commit 44a40d0

Please sign in to comment.