Skip to content

Commit

Permalink
feat(levels): insert level 17
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jun 1, 2024
1 parent b991364 commit ae20f3f
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 81 deletions.
60 changes: 35 additions & 25 deletions src/levels/17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
} from '../templates'

export const level = 17
export const title = 'Methods'
export const hint = 'Call the methods in a for loop'
export const title = 'Properties'
export const hint = "Edit the body's mass"

const mass = 1000

export const prescript = `
${loadBlock()}
Expand All @@ -19,55 +21,63 @@ ${addExit()}
${registerPlayerMovement()}
${registerWinCondition(level)}
${addText('Exit the maze')}
${addText('Sokoban')}
const player = get('player')[0]
player.moveTo = () => {}
const map = [
'#########',
'# #',
'####### #',
'# #',
'# #######',
'# #',
'####### #',
'# #',
'#########',
'###########',
'###########',
'## ##',
'######## ##',
'## ##',
'###########',
'## ####',
'## #### ##',
'## # ##',
'###########',
'###########',
]
const tileSize = 64
const level = addLevel(map, {
tileWidth: 64,
tileHeight: 64,
tileWidth: tileSize,
tileHeight: tileSize,
pos: vec2(-tileSize, -tileSize),
tiles: {
'#': () => [
sprite('block'),
area(),
body({ isStatic: true }),
body({ mass: ${mass} }),
'block'
],
}
})
onUpdate(() => {
level.get('block').forEach((block) => {
if (block.mass < 1) {
throw new Error('Block mass cannot be less than 1')
}
})
})
`

export const script = `
/**
* A method is a function defined within an object
*/
const player = get('player')[0]
const SPEED = 300
player.moveUp = function() { this.move(0, -SPEED) }
player.moveLeft = function() { this.move(-SPEED, 0) }
player.moveDown = function() { this.move(0, SPEED) }
player.moveRight = function() { this.move(SPEED, 0) }
player.moveRight()
onCollide('block', 'player', (block) => {
block.mass = ${mass}
})
`

export const postscript = `
const exit = get('exit')[0]
if (exit) {
exit.moveTo(95, 480)
exit.moveTo(480, 480)
}
`
73 changes: 52 additions & 21 deletions src/levels/18.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,73 @@
import {
addExit,
addPlayer,
addText,
loadExit,
loadKey,
registerPasswordCheck,
registerPlayerMovement,
loadBlock,
registerDisableMovement,
registerWinCondition,
} from '../templates'

export const level = 18
export const title = 'JSON.stringify'
export const hint = '{ level: _, year: _ }'

const password = JSON.stringify({ level, year: new Date().getFullYear() })
export const title = 'Methods'
export const hint = 'Call the methods in a for loop'

export const prescript = `
${loadExit()}
${loadKey()}
${addPlayer({ pos: '100, 100' })}
add([sprite('key'), pos(center()), area(), 'key', { password: '${password}' }])
${loadBlock()}
${addPlayer({ pos: '95, 100' })}
${addExit()}
${registerPlayerMovement()}
${registerDisableMovement()}
${registerWinCondition(level)}
${registerPasswordCheck(password)}
${addText('Stringify the password')}
${addText('Exit the maze')}
const player = get('player')[0]
player.moveTo = () => {}
const map = [
'#########',
'# #',
'####### #',
'# #',
'# #######',
'# #',
'####### #',
'# #',
'#########',
]
addLevel(map, {
tileWidth: 64,
tileHeight: 64,
tiles: {
'#': () => [
sprite('block'),
area(),
body({ isStatic: true }),
],
}
})
`

export const script = `
/**
* JSON.stringify() converts data into a string
* A method is a function defined within an object
*/
const key = get('key')[0]
const player = get('player')[0]
const SPEED = 300
player.moveUp = function() { this.move(0, -SPEED) }
player.moveLeft = function() { this.move(-SPEED, 0) }
player.moveDown = function() { this.move(0, SPEED) }
player.moveRight = function() { this.move(SPEED, 0) }
// password = JSON string of object containing "level" and "year"
let password
player.moveRight()
`

key.password = password
export const postscript = `
const exit = get('exit')[0]
if (exit) {
exit.moveTo(95, 480)
}
`
15 changes: 7 additions & 8 deletions src/levels/19.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@ import {
} from '../templates'

export const level = 19
export const title = 'JSON.parse'
export const hint = 'console.log(key.json)'
export const title = 'JSON.stringify'
export const hint = '{ level: _, year: _ }'

const password = btoa(String(Date.now()))
const json = JSON.stringify({ password })
const password = JSON.stringify({ level, year: new Date().getFullYear() })

export const prescript = `
${loadExit()}
${loadKey()}
${addPlayer({ pos: '100, 100' })}
add([sprite('key'), pos(center()), area(), 'key', { json: '${json}' }])
add([sprite('key'), pos(center()), area(), 'key', { password: '${password}' }])
${registerPlayerMovement()}
${registerWinCondition(level)}
${registerPasswordCheck(password)}
${addText('Parse the password')}
${addText('Stringify the password')}
`

export const script = `
/**
* JSON.parse() converts string into data
* JSON.stringify() converts data into a string
*/
const key = get('key')[0]
// parse the "password" from \`key.json\`
// password = JSON string of object containing "level" and "year"
let password
key.password = password
Expand Down
27 changes: 9 additions & 18 deletions src/levels/20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,35 @@ import {
} from '../templates'

export const level = 20
export const title = 'Fullfilled Promise'
export const hint = 'key.promise.then(...)'
export const title = 'JSON.parse'
export const hint = 'console.log(key.json)'

const password = btoa(String(Date.now()))
const json = JSON.stringify({ password })

export const prescript = `
${loadExit()}
${loadKey()}
${addPlayer({ pos: '100, 100' })}
add([sprite('key'), pos(center()), area(), 'key', { promise: Promise.resolve('${password}') }])
add([sprite('key'), pos(center()), area(), 'key', { json: '${json}' }])
${registerPlayerMovement()}
${registerWinCondition(level)}
${registerPasswordCheck(password)}
${addText('Pass the Promise')}
${addText('Parse the password')}
`

export const script = `
/**
* A Promise produces a value in the future
* If a Promise succeeded, it will produce a resolved value
* JSON.parse() converts string into data
*/
const key = get('key')[0]
// example of successful Promise
const examplePromise = Promise.resolve('some value')
examplePromise.then((value) => {
console.log(value)
key.password = value
})
// parse the "password" from \`key.json\`
let password
// set \`key.password\` to the resolved value of \`key.promise\`
console.log('is promise?', isPromise(key.promise))
function isPromise(value) {
return value instanceof Promise
}
key.password = password
`
18 changes: 9 additions & 9 deletions src/levels/21.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from '../templates'

export const level = 21
export const title = 'Rejected Promise'
export const hint = 'key.promise.catch(...)'
export const title = 'Fullfilled Promise'
export const hint = 'key.promise.then(...)'

const password = btoa(String(Date.now()))

Expand All @@ -19,31 +19,31 @@ ${loadExit()}
${loadKey()}
${addPlayer({ pos: '100, 100' })}
add([sprite('key'), pos(center()), area(), 'key', { promise: Promise.reject('${password}') }])
add([sprite('key'), pos(center()), area(), 'key', { promise: Promise.resolve('${password}') }])
${registerPlayerMovement()}
${registerWinCondition(level)}
${registerPasswordCheck(password)}
${addText('Catch the Promise')}
${addText('Pass the Promise')}
`

export const script = `
/**
* A Promise produces a value in the future
* If a Promise failed, it will produce a rejected value
* If a Promise succeeded, it will produce a resolved value
*/
const key = get('key')[0]
// example of failed Promise
const examplePromise = Promise.reject('some value')
examplePromise.catch((value) => {
// example of successful Promise
const examplePromise = Promise.resolve('some value')
examplePromise.then((value) => {
console.log(value)
key.password = value
})
// set \`key.password\` to the rejected value of \`key.promise\`
// set \`key.password\` to the resolved value of \`key.promise\`
console.log('is promise?', isPromise(key.promise))
function isPromise(value) {
Expand Down
52 changes: 52 additions & 0 deletions src/levels/22.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
addPlayer,
addText,
loadExit,
loadKey,
registerPasswordCheck,
registerPlayerMovement,
registerWinCondition,
} from '../templates'

export const level = 22
export const title = 'Rejected Promise'
export const hint = 'key.promise.catch(...)'

const password = btoa(String(Date.now()))

export const prescript = `
${loadExit()}
${loadKey()}
${addPlayer({ pos: '100, 100' })}
add([sprite('key'), pos(center()), area(), 'key', { promise: Promise.reject('${password}') }])
${registerPlayerMovement()}
${registerWinCondition(level)}
${registerPasswordCheck(password)}
${addText('Catch the Promise')}
`

export const script = `
/**
* A Promise produces a value in the future
* If a Promise failed, it will produce a rejected value
*/
const key = get('key')[0]
// 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?', isPromise(key.promise))
function isPromise(value) {
return value instanceof Promise
}
`

0 comments on commit ae20f3f

Please sign in to comment.