Skip to content

Commit

Permalink
feat: add cursor keys
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 15, 2024
1 parent 67697c2 commit 867d9dd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/events/cursors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Player } from '../types'

// pixels per second
const SPEED = 320

export function addCursorKeys(player: Player) {
onKeyDown('left', () => {
player.move(-SPEED, 0)
})

onKeyDown('right', () => {
player.move(SPEED, 0)
})

onKeyDown('up', () => {
player.move(0, -SPEED)
})

onKeyDown('down', () => {
player.move(0, SPEED)
})
}
1 change: 1 addition & 0 deletions src/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cursors'
1 change: 1 addition & 0 deletions src/gameobjects/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './player'
5 changes: 5 additions & 0 deletions src/gameobjects/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { bean } from '../sprites'

export function addPlayer() {
return add([pos(120, 80), bean])
}
5 changes: 3 additions & 2 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { bean } from '../sprites'
import { addCursorKeys } from '../events'
import { addPlayer } from '../gameobjects'

scene('game', () => {
add([pos(120, 80), bean])
addCursorKeys(addPlayer())

onClick(() => addKaboom(mousePos()))
})
3 changes: 3 additions & 0 deletions src/types/gameobject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { GameObj, PosComp, SpriteComp } from 'kaboom'

export type Player = GameObj<PosComp | SpriteComp>
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 './gameobject'
export * from './scene'
export * from './sprite'

0 comments on commit 867d9dd

Please sign in to comment.