Skip to content

Commit

Permalink
feat(levels): insert 2
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 29, 2024
1 parent 7590324 commit 12dc4cf
Show file tree
Hide file tree
Showing 16 changed files with 384 additions and 343 deletions.
4 changes: 1 addition & 3 deletions src/levels/1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ add([text('Uncomment the exit')])
`

export const script = `
/**
* Single-line comments start with 2 forward slashes //
*/
// Single-line comments start with 2 forward slashes
// console.log('This is commented out')
console.log('This is not commented out')
Expand Down
80 changes: 22 additions & 58 deletions src/levels/10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,45 @@ import {
} from '../templates'

export const level = 10
export const title = 'forEach'
export const title = 'For Loop'

const password = Array.from(Array(42).keys())
.map(() => 'answer')
.join('')

export const prescript = `
${loadPlayer()}
${loadExit()}
loadSprite('spike', 'sprites/spike.png')
loadSprite('key', 'sprites/key.png')
const player = add([
sprite('player'),
pos(40, 80),
area(),
body(),
anchor('center'),
'player',
])
const player = add([sprite('player'), pos(100, 100), area(), 'player'])
add([sprite('exit'), pos(500, 500), area(), 'exit'])
add([sprite('key'), pos(center()), area(), 'key', { password: '${password}' }])
${registerPlayerKeys()}
${registerWinCondition(level)}
const map = [
' ',
' ',
'^^^^^ ^^^',
' ',
'^ ^^^^ ',
'^ ^^^',
' ^ ',
'^^^^ ^^^ ',
]
const SPIKES_COUNT = map.join('').split(' ').join('').length
const TILE_SIZE = 64
map.forEach((row, rowIndex) => {
row.split('').forEach((column, columnIndex) => {
if (column === '^') {
add([
sprite('spike'),
area(),
pos(TILE_SIZE * columnIndex, TILE_SIZE * rowIndex),
opacity(0),
'spike',
])
}
})
})
onCollide('player', 'spike', (player, spike) => {
spike.opacity = 1
player.destroy()
addKaboom(player.pos)
})
onUpdate(() => {
const { x, y } = player.pos
if (x < 0 || y < 0 || x > width() || y > height()) {
player.moveTo(40, 80)
}
if (get('spike').length < SPIKES_COUNT) {
throw new Error('There must be ' + SPIKES_COUNT + ' spikes!')
onCollide('key', 'player', (key) => {
if (key.password === '${password}') {
key.destroy()
add([sprite('exit'), pos(500, 500), area(), 'exit'])
} else {
debug.log('Incorrect password')
}
})
add([text('Invisible spikes')])
add([text('Repeat the password')])
`

export const script = `
/**
* forEach() is an iterative method
* For loops repeat a block of code
*/
const spikes = get('spike')
spikes[0].opacity = 0
const key = get('key')[0]
// password = 'answer' repeated 42 times
let password = 'answer' + 'answer'
key.password = password
`
97 changes: 45 additions & 52 deletions src/levels/11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import {
} from '../templates'

export const level = 11
export const title = 'Loops'
export const title = 'forEach'

export const prescript = `
${loadPlayer()}
${loadExit()}
loadSprite('enemy', 'sprites/ghosty.png')
loadSprite('wall', 'sprites/steel.png')
loadSprite('spike', 'sprites/spike.png')
const player = add([
sprite('player'),
pos(40, 60),
pos(40, 80),
area(),
body(),
anchor('center'),
Expand All @@ -28,66 +27,60 @@ add([sprite('exit'), pos(500, 500), area(), 'exit'])
${registerPlayerKeys()}
${registerWinCondition(level)}
const ENEMY_SPEED = 500
let cancelEnemyUpdate;
function addEnemy() {
const enemy = add([
sprite('enemy'),
pos(center()),
area(),
body(),
'enemy',
])
cancelEnemyUpdate = enemy.onUpdate(() => {
if (player) {
const dir = player.pos.sub(enemy.pos).unit()
enemy.move(dir.scale(ENEMY_SPEED))
const map = [
' ',
' ',
'^^^^^ ^^^',
' ',
'^ ^^^^ ',
'^ ^^^',
' ^ ',
'^^^^ ^^^ ',
]
const SPIKES_COUNT = map.join('').split(' ').join('').length
const TILE_SIZE = 64
map.forEach((row, rowIndex) => {
row.split('').forEach((column, columnIndex) => {
if (column === '^') {
add([
sprite('spike'),
area(),
pos(TILE_SIZE * columnIndex, TILE_SIZE * rowIndex),
opacity(0),
'spike',
])
}
}).cancel
return enemy
}
addEnemy()
})
})
onCollide('player', 'enemy', (player, enemy) => {
if (typeof cancelEnemyUpdate === 'function') {
cancelEnemyUpdate()
}
onCollide('player', 'spike', (player, spike) => {
spike.opacity = 1
player.destroy()
addKaboom(player.pos)
})
onDestroy('enemy', () => {
addEnemy()
onUpdate(() => {
const { x, y } = player.pos
if (x < 0 || y < 0 || x > width() || y > height()) {
player.moveTo(40, 80)
}
if (get('spike').length < SPIKES_COUNT) {
throw new Error('There must be ' + SPIKES_COUNT + ' spikes!')
}
})
add([text('Protect yourself')])
add([text('Invisible spikes')])
`

export const script = `
/**
* Can you build a fortress to protect yourself?
* forEach() is an iterative method
*/
const wall = {
width: 64,
height: 64,
}
add([
sprite('wall'),
pos(wall.width * 6, wall.height * 6),
area(),
body({ isStatic: true }),
])
`

export const postscript = `
if (get('enemy').length) {
get('enemy')[0].moveTo(center())
}
const spikes = get('spike')
spikes[0].opacity = 0
`
78 changes: 68 additions & 10 deletions src/levels/12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,88 @@ import {
} from '../templates'

export const level = 12
export const title = 'setTimeout'
export const title = 'Loops'

export const prescript = `
${loadPlayer()}
${loadExit()}
loadSprite('enemy', 'sprites/ghosty.png')
loadSprite('wall', 'sprites/steel.png')
const player = add([sprite('player'), pos(50, 50), area(), 'player'])
const player = add([
sprite('player'),
pos(40, 60),
area(),
body(),
anchor('center'),
'player',
])
add([sprite('exit'), pos(500, 500), area(), 'exit'])
${registerPlayerKeys()}
${registerWinCondition(level)}
add([text('Wait for the exit')])
const ENEMY_SPEED = 500
let cancelEnemyUpdate;
function addEnemy() {
const enemy = add([
sprite('enemy'),
pos(center()),
area(),
body(),
'enemy',
])
cancelEnemyUpdate = enemy.onUpdate(() => {
if (player) {
const dir = player.pos.sub(enemy.pos).unit()
enemy.move(dir.scale(ENEMY_SPEED))
}
}).cancel
return enemy
}
addEnemy()
onCollide('player', 'enemy', (player, enemy) => {
if (typeof cancelEnemyUpdate === 'function') {
cancelEnemyUpdate()
}
player.destroy()
addKaboom(player.pos)
})
onDestroy('enemy', () => {
addEnemy()
})
add([text('Protect yourself')])
`

export const script = `
/**
* setTimeout() executes a function once the timer expires
* Can you build a fortress to protect yourself?
*/
const MILLISECOND = 1
const SECOND = MILLISECOND * 1000
const MINUTE = SECOND * 60
const wall = {
width: 64,
height: 64,
}
add([
sprite('wall'),
pos(wall.width * 6, wall.height * 6),
area(),
body({ isStatic: true }),
])
`

setTimeout(() => {
add([sprite('exit'), pos(center()), area(), 'exit'])
}, 5 * MINUTE)
export const postscript = `
if (get('enemy').length) {
get('enemy')[0].moveTo(center())
}
`
34 changes: 9 additions & 25 deletions src/levels/13.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,30 @@ import {
} from '../templates'

export const level = 13
export const title = 'setInterval'
export const title = 'setTimeout'

export const prescript = `
${loadPlayer()}
${loadExit()}
const player = add([sprite('player'), pos(50, 80), area(), anchor('center'), 'player'])
const exit = add([
sprite('exit'),
pos(center()),
area(),
anchor('center'),
'exit',
])
const player = add([sprite('player'), pos(50, 50), area(), 'player'])
${registerPlayerKeys()}
${registerWinCondition(level)}
add([text('Exit in a loop')])
add([text('Wait for the exit')])
`

export const script = `
/**
* setInterval() calls a function at specified intervals
* setTimeout() executes a function once the timer expires
*/
const MILLISECOND = 1
const SECOND = MILLISECOND * 1000
const MINUTE = SECOND * 60
const exit = get('exit')[0]
setInterval(() => {
exit.moveTo(
randi(width()),
randi(height()),
)
}, SECOND)
`

export const postscript = `
const player = get('player')[0]
${registerPlayerKeys(50)}
setTimeout(() => {
add([sprite('exit'), pos(center()), area(), 'exit'])
}, 5 * MINUTE)
`
Loading

0 comments on commit 12dc4cf

Please sign in to comment.