Skip to content

Commit

Permalink
refactor(gameobjects): create addText
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 27, 2025
1 parent 19122f2 commit 4f0a4ae
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 55 deletions.
5 changes: 5 additions & 0 deletions src/gameobjects/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Props {
y: number
text: string
onClick: () => void
fixed?: boolean
}

export function addButton(props: Props) {
Expand All @@ -19,6 +20,10 @@ export function addButton(props: Props) {
color(255, 255, 255),
])

if (props.fixed) {
button.use(fixed())
}

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

button.onHover(() => {
Expand Down
1 change: 1 addition & 0 deletions src/gameobjects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './music'
export * from './player'
export * from './projectile'
export * from './score'
export * from './text'
23 changes: 23 additions & 0 deletions src/gameobjects/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface Props {
width: number
height: number
x: number
y: number
text: string
fontSize?: number
}

export function addText(props: Props) {
const box = add([
rect(props.width, props.height),
pos(props.x, props.y),
anchor('center'),
color(255, 255, 255),
])

return box.add([
text(props.text, { size: props.fontSize }),
anchor('center'),
color(0, 0, 0),
])
}
20 changes: 8 additions & 12 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@ import {
addHealth,
addPlayer,
addScore,
addText,
playMusic,
} from '../gameobjects'
import { gameState, levels } from '../helpers'

scene(Scene.Game, () => {
gameState.init()

const textbox = add([
rect(600, 100),
pos(center().x, 80),
anchor('center'),
color(255, 255, 255),
])

textbox.add([
text('WASD or arrow keys to move\nLeft click to shoot'),
anchor('center'),
color(0, 0, 0),
])
addText({
width: 600,
height: 100,
x: center().x,
y: 80,
text: 'WASD or arrow keys to move\nLeft click to shoot',
})

addScore()
playMusic()
Expand Down
22 changes: 9 additions & 13 deletions src/scenes/lose.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { Scene, Sound } from '../constants'
import { addButton } from '../gameobjects'
import { addButton, addText } from '../gameobjects'

scene(Scene.Lose, () => {
const { x, y } = center()
const margin = 100

const textbox = add([
rect(400, 100),
pos(x, y - margin),
anchor('center'),
color(255, 255, 255),
])

textbox.add([
text('Game Over', { size: 48 }),
anchor('center'),
color(0, 0, 0),
])
addText({
width: 400,
height: 100,
x,
y: y - margin,
text: 'Game Over',
fontSize: 48,
})

addButton({
width: 220,
Expand Down
22 changes: 9 additions & 13 deletions src/scenes/title.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { Scene, Sound } from '../constants'
import { addButton } from '../gameobjects'
import { addButton, addText } from '../gameobjects'

scene(Scene.Title, () => {
const { x, y } = center()
const buttonHeight = 80

const textbox = add([
rect(400, 100),
pos(x, y - buttonHeight * 2),
anchor('center'),
color(255, 255, 255),
])

textbox.add([
text('Bubble Gun', { size: 48 }),
anchor('center'),
color(0, 0, 0),
])
addText({
width: 400,
height: 100,
x,
y: y - buttonHeight * 2,
text: 'Bubble Gun',
fontSize: 48,
})

addButton({
width: 220,
Expand Down
29 changes: 12 additions & 17 deletions src/scenes/tutorial.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import { Scene, Sound } from '../constants'
import { addPlayer } from '../gameobjects'
import { addButton } from '../gameobjects'
import { addButton, addPlayer, addText } from '../gameobjects'

scene(Scene.Tutorial, () => {
const { x } = center()

const textbox = add([
rect(600, 100),
pos(x, 80),
anchor('center'),
color(255, 255, 255),
])

textbox.add([
text('WASD or arrow keys to move\nLeft click to shoot'),
anchor('center'),
color(0, 0, 0),
])

addPlayer()

addButton({
width: 220,
height: 80,
Expand All @@ -31,5 +15,16 @@ scene(Scene.Tutorial, () => {
play(Sound.Hit)
go(Scene.Game)
},
fixed: true,
})

addText({
width: 600,
height: 100,
x,
y: 80,
text: 'WASD or arrow keys to move',
})

addPlayer()
})

0 comments on commit 4f0a4ae

Please sign in to comment.