Skip to content

Commit

Permalink
refactor(gameobjects): modularize button
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 26, 2025
1 parent 56ff961 commit 6f7d54d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
35 changes: 35 additions & 0 deletions src/gameobjects/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
interface Props {
width: number
height: number
radius?: number
x: number
y: number
text: string
onClick: () => void
}

export function addButton(props: Props) {
const button = add([
rect(props.width, props.height, { radius: props.radius }),
pos(props.x, props.y),
area(),
scale(1),
anchor('center'),
outline(4),
color(255, 255, 255),
])

button.add([text(props.text), anchor('center'), color(0, 0, 0)])

button.onHover(() => {
button.scaleTo(1.1)
})

button.onHoverEnd(() => {
button.scaleTo(1)
})

button.onClick(props.onClick)

return button
}
1 change: 1 addition & 0 deletions src/gameobjects/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './bubble'
export * from './button'
export * from './enemy'
export * from './health'
export * from './hole'
Expand Down
35 changes: 12 additions & 23 deletions src/scenes/lose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Scene, Sound } from '../constants'
import { addButton } from '../gameobjects'

scene(Scene.Lose, () => {
const { x, y } = center()
Expand All @@ -17,28 +18,16 @@ scene(Scene.Lose, () => {
color(0, 0, 0),
])

const button = add([
rect(220, 80, { radius: 8 }),
pos(x, y + margin),
area(),
scale(1),
anchor('center'),
outline(4),
color(255, 255, 255),
])

button.add([text('Restart'), anchor('center'), color(0, 0, 0)])

button.onHover(() => {
button.scaleTo(1.1)
})

button.onHoverEnd(() => {
button.scaleTo(1)
})

button.onClick(() => {
play(Sound.Hit)
go(Scene.Game)
addButton({
width: 220,
height: 80,
radius: 8,
x,
y: y + margin,
text: 'Restart',
onClick() {
play(Sound.Hit)
go(Scene.Game)
},
})
})
35 changes: 12 additions & 23 deletions src/scenes/title.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Scene, Sound } from '../constants'
import { addButton } from '../gameobjects'

scene(Scene.Title, () => {
const { x, y } = center()
Expand All @@ -17,28 +18,16 @@ scene(Scene.Title, () => {
color(0, 0, 0),
])

const button = add([
rect(220, 80, { radius: 8 }),
pos(x, y + margin),
area(),
scale(1),
anchor('center'),
outline(4),
color(255, 255, 255),
])

button.add([text('Play'), anchor('center'), color(0, 0, 0)])

button.onHover(() => {
button.scaleTo(1.1)
})

button.onHoverEnd(() => {
button.scaleTo(1)
})

button.onClick(() => {
play(Sound.Hit)
go(Scene.Game)
addButton({
width: 220,
height: 80,
radius: 8,
x,
y: y + margin,
text: 'Play',
onClick() {
play(Sound.Hit)
go(Scene.Game)
},
})
})

0 comments on commit 6f7d54d

Please sign in to comment.