Skip to content

Commit

Permalink
feat: improve game init function
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 26, 2024
1 parent 846f201 commit 80306f7
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 33 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ <h1 class="title mobile">Code Arcade</h1>
</main>

<script type="module">
import { initKaboom } from './src/helpers'
initKaboom()
import { initGame } from './src/helpers'
initGame()
</script>

<script type="module" src="src/index.ts"></script>
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/kaboom.ts → src/helpers/game.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import kaboom from 'kaboom'

export function initKaboom() {
import { initGameScene } from '../scenes'

export function initGame() {
kaboom({
width: 600,
height: 600,
canvas: document.querySelector('canvas')!,
})

initGameScene()
}
2 changes: 1 addition & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './cursors'
export * from './editor'
export * from './kaboom'
export * from './game'
export * from './level'
4 changes: 3 additions & 1 deletion src/helpers/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ const titles = document.querySelectorAll(
) as NodeListOf<HTMLHeadingElement>

export function renderLevel(level: Level) {
titles.forEach((title) => (title.innerText = level.title))
titles.forEach(
(title) => (title.innerText = `Level ${level.level}: ${level.title}`),
)
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import './style.css'
import './scenes'

import { Scene } from './types'

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

go('game', level)
go(Scene.game, level)

// press F1
// debug.inspect = true
18 changes: 14 additions & 4 deletions src/levels/0.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { addCursorKeys } from '../helpers'
import { Scene, Sprite } from '../types'

export const title = 'Level 0: Hello, World!'
export const level = 0
export const title = 'Hello, World!'

export function prescript() {
loadSprite('player', 'sprites/bean.png')
const player = add([sprite('player'), pos(80, 40), area()])
addCursorKeys(player)
loadSprite(Sprite.player, 'sprites/bean.png')
loadSprite(Sprite.exit, 'sprites/door.png')

addCursorKeys(
add([sprite(Sprite.player), pos(50, 50), area(), Sprite.player]),
)
add([sprite(Sprite.exit), pos(500, 500), area(), Sprite.exit])

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

export const script = `
Expand Down
53 changes: 31 additions & 22 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import { editorView, getLevel, initKaboom, renderLevel } from '../helpers'
import { editorView, getLevel, initGame, renderLevel } from '../helpers'
import { Scene } from '../types'

const buttons = document.querySelectorAll('.run')
export function initGameScene() {
scene(Scene.game, async (currentLevel: number) => {
const level = await getLevel(currentLevel)
renderLevel(level)

scene('game', async (currentLevel: number) => {
const level = await getLevel(currentLevel)
renderLevel(level)
editorView.dispatch({
changes: {
from: 0,
to: editorView.state.doc.length,
insert: level.script.trim(),
},
})

editorView.dispatch({
changes: {
from: 0,
to: editorView.state.doc.length,
insert: level.script.trim(),
},
})
function run() {
initGame()
level.prescript()
const script = editorView.state.doc.toString()
eval(script)
level.postscript()
}

function run() {
initKaboom()
level.prescript()
const script = editorView.state.doc.toString()
eval(script)
level.postscript()
}
run()

run()
const buttons = document.querySelectorAll(
'.run',
) as NodeListOf<HTMLButtonElement>

buttons.forEach((button) => button.addEventListener('click', run))
})
buttons.forEach((oldButton) => {
const newButton = oldButton.cloneNode(true) as HTMLButtonElement
newButton.addEventListener('click', run)
oldButton.parentNode?.replaceChild(newButton, oldButton)
})
})
}
2 changes: 1 addition & 1 deletion src/scenes/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import './game'
export * from './game'
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './level'
export * from './scene'
export * from './sprite'
1 change: 1 addition & 0 deletions src/types/level.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Level {
level: number
title: string
prescript: () => void
script: string
Expand Down
4 changes: 4 additions & 0 deletions src/types/sprite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Sprite {
exit = 'exit',
player = 'player',
}

0 comments on commit 80306f7

Please sign in to comment.