Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/createGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export const createGame = <T extends string>(
const soundPlayer = initSoundPlayer(config)
const camera = initCamera(config)
const renderer = initRenderer(config)
const dialog = initDialog(config)
const dialog = initDialog({
...config,
soundPlayer,
})
const prompt = initPrompt(config)
const messageBox = initMessageBox(config)
const gameFilter = initFilter(renderer.canvas, config.filter)
Expand Down
75 changes: 57 additions & 18 deletions src/dialog.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Char, getColorFrompalette, TextFx } from './lib'
import { RendererParams } from './renderer'
import { type Sound } from 'pfxr'
import { SoundPlayer } from './sound.js'

export type DialogParams = {
dialogBackground: string | number
dialogColor: string | number
dialogBorder: string | number
dialogInternvalMs?: number
colors: RendererParams['colors']
soundPlayer?: SoundPlayer
}

const CANVAS_SIZE = 384
Expand Down Expand Up @@ -36,6 +38,9 @@ export class Dialog {
#lastFrameTime = 0

#textFx: TextFx
#soundPlayer?: SoundPlayer
#currentVoiceOverride?: { template?: 'BLIP' | 'HIT' | 'PICKUP' | 'JUMP' | 'FALL' | 'POWERUP' | 'LASER' | 'BLIP_RANDOM', seed?: number | null }
#dialogSeed?: number

isOpen = false

Expand All @@ -44,22 +49,17 @@ export class Dialog {
#contentColor: string
#borderColor: string

#animationIntervalMs?: number

#boxHeight: number
#boxWidth: number
#boxX: number
#boxY: number

constructor(params: DialogParams) {
this.#configColors = params.colors
this.#backgroundColor = getColorFrompalette(
params.dialogBackground,
params.colors,
)
this.#contentColor = getColorFrompalette(params.dialogColor, params.colors)
this.#borderColor = getColorFrompalette(params.dialogBorder, params.colors)
this.#animationIntervalMs = params.dialogInternvalMs
this.#backgroundColor = this.#getColor(params.dialogBackground)
this.#contentColor = this.#getColor(params.dialogColor)
this.#borderColor = this.#getColor(params.dialogBorder)
this.#soundPlayer = params.soundPlayer

this.#canvas = document.createElement('canvas')
this.#canvas.style.setProperty('position', 'absolute')
Expand All @@ -85,10 +85,11 @@ export class Dialog {
this.#textFx = new TextFx('|', this.#contentColor, params.colors)
}

async open(text: string) {
if (text.length <= 0) return
async open(text: string, defaultVoice?: { template?: 'BLIP' | 'HIT' | 'PICKUP' | 'JUMP' | 'FALL' | 'POWERUP' | 'LASER' | 'BLIP_RANDOM', seed?: number | null } | null, voiceOverride?: { template?: 'BLIP' | 'HIT' | 'PICKUP' | 'JUMP' | 'FALL' | 'POWERUP' | 'LASER' | 'BLIP_RANDOM', seed?: number | null }) {
this.isOpen = true
this.#canvas.style.setProperty('display', 'block')
this.#currentVoiceOverride = voiceOverride || defaultVoice || undefined
this.#dialogSeed = undefined // Reset dialog seed for new dialog

this.#remainingLines = this.#textFx.parseText(text, MAX_CHARS_PER_LINE)
this.#currentLineQueue = this.#remainingLines.shift()
Expand Down Expand Up @@ -124,11 +125,7 @@ export class Dialog {

#update = (time: number) => {
this.#animationId = requestAnimationFrame(this.#update)
if (
time - this.#lastFrameTime <
(this.#animationIntervalMs || ANIMATION_INTERVAL_MS)
)
return
if (time - this.#lastFrameTime < ANIMATION_INTERVAL_MS) return
this.#lastFrameTime = time
if (
this.#currentLineQueue?.length === 0 &&
Expand All @@ -140,7 +137,42 @@ export class Dialog {

let newChar = this.#currentLineQueue?.shift()

if (newChar) this.#displayedLines[this.#lineCursor]?.push(newChar)
if (newChar) {
this.#displayedLines[this.#lineCursor]?.push(newChar)
// Play MIDI-style voice sound for non-space characters - only if voice is explicitly defined
if (this.#soundPlayer && this.#currentVoiceOverride && this.#currentVoiceOverride.template && newChar.value !== ' ') {
const template = this.#currentVoiceOverride.template
const seed = this.#currentVoiceOverride.seed

let finalTemplate = template
let characterSeed = seed ?? 0

if (template === 'BLIP_RANDOM') {
// BLIP_RANDOM: new random seed for each character
finalTemplate = 'BLIP'
characterSeed = Math.floor(Math.random() * 1000)
} else if (seed === null) {
// seed=null: random seed per dialog (not per character)
characterSeed = this.#dialogSeed ?? (this.#dialogSeed = Math.floor(Math.random() * 1000))
}

// Play voice sample with appropriate variation
const velocity = 0.8 + Math.random() * 0.4 // 0.8-1.2 volume variation

// Determine if we should use variation
let useVariation = false
if (template === 'BLIP_RANDOM') {
// BLIP_RANDOM: each character gets random variation
useVariation = true
} else if (seed === null) {
// seed=null: use random variation within template
useVariation = true
}

// Play voice sample
this.#soundPlayer.playVoiceSample(finalTemplate, velocity, useVariation)
}
}
this.#render(time)
}

Expand All @@ -150,6 +182,8 @@ export class Dialog {
this.#lineCursor = 0
this.#resolvePromise?.()
this.#animationId && cancelAnimationFrame(this.#animationId)

// No cleanup needed for individual sounds
}

#resizeCanvas = () => {
Expand Down Expand Up @@ -185,6 +219,11 @@ export class Dialog {
this.#ctx.fillStyle = this.#backgroundColor
this.#ctx.fillRect(this.#boxX, this.#boxY, this.#boxWidth, this.#boxHeight)
}

#getColor(color: string | number) {
if (typeof color === 'string') return color
return this.#configColors[color] ?? 'black'
}
}

export const initDialog = (params: DialogParams) => new Dialog(params)
2 changes: 1 addition & 1 deletion src/gameApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const initGameApi = <T extends string>(
symbol: T,
params: Unwrap<Partial<Omit<ActorState<T>, 'symbol'>>>,
) => gameState.actors.setAll(symbol, params),
openDialog: (text: string) => dialog.open(text),
openDialog: (text: string, voiceOverride?: { template?: 'BLIP' | 'HIT' | 'PICKUP' | 'JUMP' | 'FALL' | 'POWERUP' | 'LASER' | 'BLIP_RANDOM', seed?: number | null }) => dialog.open(text, undefined, voiceOverride),
prompt: (...options: string[]) => prompt.open(...options),
openMenu: (options: MenuOption) => prompt.openMenu(options),
openMessage: (...args: string[]) => messageBox.open(args),
Expand Down
2 changes: 1 addition & 1 deletion src/gameLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class GameLoop<T extends string> {
}
}
async #openDialog(actor: ActorFacade<T>) {
if (actor.dialog) await this.#dialog.open(actor.dialog)
if (actor.dialog) await this.#dialog.open(actor.dialog, actor.voice)
}

async #end(actor: ActorFacade<T>) {
Expand Down
8 changes: 8 additions & 0 deletions src/gameState/actorFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export class ActorFacade<T extends string> {
this.#setActor('dialog', value)
}

get voice() {
return this.#getActor()?.voice ?? null
}

set voice(value: ActorState<T>['voice']) {
this.#setActor('voice', value)
}

get visible() {
return this.#getActor()?.visible ?? false
}
Expand Down
1 change: 1 addition & 0 deletions src/gameState/actors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export class Actors<T extends string> {
sprite: template.sprite ?? null,
position: [x, y],
dialog: template.dialog ?? null,
voice: template.voice ?? null,
isOnScreen: false,
end: template.end ?? null,
sound: template.sound ?? null,
Expand Down
1 change: 1 addition & 0 deletions src/gameState/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type ActorState<T extends string> = {
sprite: Tile | null
sound: UnTuplify<PlaySoundArgs> | null
dialog: string | null
voice: { template?: 'BLIP' | 'HIT' | 'PICKUP' | 'JUMP' | 'FALL' | 'POWERUP' | 'LASER' | 'BLIP_RANDOM', seed?: number | null } | null
solid: boolean
visible: boolean
isOnScreen: boolean
Expand Down
Loading