diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-06-29 11:40:46 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-06-29 11:40:46 -0400 |
commit | f5fcce110a915fca1b114001962170733276e5df (patch) | |
tree | 6ce82c649d7377b42e75c9feb88d73e4aa15d713 /client/src/types.ts | |
parent | ghost (diff) | |
download | tuxman-f5fcce110a915fca1b114001962170733276e5df.tar.gz tuxman-f5fcce110a915fca1b114001962170733276e5df.tar.bz2 tuxman-f5fcce110a915fca1b114001962170733276e5df.zip |
audio, finalize gameplay, wrap around map, stuff
Diffstat (limited to 'client/src/types.ts')
-rw-r--r-- | client/src/types.ts | 106 |
1 files changed, 103 insertions, 3 deletions
diff --git a/client/src/types.ts b/client/src/types.ts index 76f5116..01f4e3a 100644 --- a/client/src/types.ts +++ b/client/src/types.ts @@ -2,10 +2,84 @@ export const ATLAS_TILE_WIDTH = 32 export const GAME_MAP_COUNT = 4 +const loadAudio = (src: string, callback: (node: GameAudio) => void) => { + + let actx = new AudioContext() + + fetch(src, {mode: "cors"}) + .then((resp) => { + return resp.arrayBuffer() + }) + .then((ebuf) => { + actx.decodeAudioData(ebuf, (abuf) => { + + var sourceNode = null, + startedAt = 0, + pausedAt = 0, + playing = false + + var play = (loop: boolean) => { + var offset = pausedAt + + sourceNode = actx.createBufferSource() + sourceNode.connect(actx.destination) + sourceNode.buffer = abuf + sourceNode.start(0, offset) + sourceNode.loop = loop + + startedAt = actx.currentTime - offset + pausedAt = 0 + playing = true + } + + var pause = () => { + var elapsed = actx.currentTime - startedAt + stop() + pausedAt = elapsed + }; + + var stop = () => { + if (sourceNode) { + sourceNode.disconnect() + sourceNode.stop(0) + sourceNode = null + } + pausedAt = 0 + startedAt = 0 + playing = false + } + + var getPlaying = (): boolean => { + return playing + } + + callback({play, pause, stop, getPlaying}) + }) + }) +} + +let EMPTY_AUDIO: GameAudio = { + play: (_loop: boolean) => {}, + pause: () => {}, + stop: () => {}, + getPlaying: () => true, +} + +export var INTRO_AUDIO = EMPTY_AUDIO +export var DEATH_AUDIO = EMPTY_AUDIO +export var MOVE_AUDIO = EMPTY_AUDIO +export var GHOST_AUDIO = EMPTY_AUDIO + +loadAudio('sfx/intro.mp3', (node) => INTRO_AUDIO = node) +loadAudio('sfx/death.mp3', (node) => DEATH_AUDIO = node) +loadAudio('sfx/move.wav', (node) => MOVE_AUDIO = node) +loadAudio('sfx/ghost.wav', (node) => GHOST_AUDIO = node) + export enum Tile { EMPTY = 0, WALL = 1, GHOST_WALL = 2, + GHOST_EXIT = 11, FOOD = 3, PLAYER_SPAWN_1 = 4, PLAYER_SPAWN_2 = 5, @@ -68,8 +142,11 @@ export type Ghost = { pos: Vec2, type: GhostType, target: Vec2, + targetQueue: Vec2[], state: GhostState, currentDirection: Rotation, + hasRespawned: boolean, + framesInBox: number, } export type KeyMap = { @@ -104,8 +181,14 @@ export type Player = { pos: Vec2, moveRotation: Rotation, inputRotation: Rotation, + velocityRotation: Rotation, moving: boolean, - name?: string + thiccLeft: number, + name?: string, + dead: boolean, + framesDead: number, + atePellets: number, + } export type PlayerInput = { @@ -151,7 +234,8 @@ export enum SpawnIndex { PAC_SPAWN_2 = 2, PAC_SPAWN_3 = 3, PAC_SPAWN_4 = 4, - GHOST_SPAWN = 0 + GHOST_SPAWN = 0, + GHOST_EXIT = 5 } export type Map = { @@ -177,10 +261,26 @@ export type GameState = { items: Items, frame: number, rng: number, - mapId: number | undefined + mapId: number | undefined, + roundTimer: number, + endRoundTimer: number } export type Frame = { data: GameState, input: Input } + +export type GameAudio = { + getPlaying: () => boolean, + play: (loop: boolean) => void, + pause: () => void, + stop: () => void +} + +export type BoundingBox = { + x: number, + y: number, + w: number, + h: number +} |