Skip to content

Commit

Permalink
feat(helpers): save level data to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 27, 2024
1 parent 1f15d8d commit 2dbcd9e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/helpers/level.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Level, Scene, Sprite } from '../types'
import { Data, Level, Scene, Sprite } from '../types'

/**
* Gets level by number.
Expand Down Expand Up @@ -40,10 +40,12 @@ export function initLevel(level: number, cleanup?: () => void) {
loadSprite(Sprite.exit, 'sprites/door.png')

onCollide(Sprite.player, Sprite.exit, () => {
go(Scene.game, level + 1)

if (typeof cleanup === 'function') {
cleanup()
}

const nextLevel = level + 1
go(Scene.game, nextLevel)
setData(Data.level, nextLevel)
})
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import './style.css'
import './scenes'

import { Scene } from './types'
import { Data, Scene } from './types'

const level = Number(new URLSearchParams(location.search).get('level')) || 0
const level =
Number(
new URLSearchParams(location.search).get('level') || getData(Data.level),
) || 0

go(Scene.game, level)

Expand Down
3 changes: 3 additions & 0 deletions src/types/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum Data {
level = 'level',
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './data'
export * from './level'
export * from './scene'
export * from './sprite'

0 comments on commit 2dbcd9e

Please sign in to comment.