Skip to content

Commit

Permalink
Refactor audio and ghost functions (#9)
Browse files Browse the repository at this point in the history
* refactor: unlockAudioForiOS

* Refactor ghost functions to use arrow functions

* Refactor ghost targeting functions to use arrow functions

* Refactor functions in pacman.ts
  • Loading branch information
caffeinated-pixels authored Dec 25, 2023
1 parent 08d3dd5 commit 9cf710e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
23 changes: 12 additions & 11 deletions src/app/functions/game-setup/initiateAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
powerPillSound,
} from '../../constants/audioObjects'

// fix for getting audio to play on iOS
export const initiateAudio = () => {
const unlockAudioForiOS = () => {
const audioToUnlock = [
startGameSound,
munchSound,
Expand All @@ -17,15 +16,17 @@ export const initiateAudio = () => {
ghostEatenSound,
powerPillSound,
]
document.addEventListener('touchstart', unlockAudioForiOS)

function unlockAudioForiOS() {
audioToUnlock.forEach((audio) => {
audio.play()
audio.pause()
audio.currentTime = 0
})
audioToUnlock.forEach((audio) => {
audio.play()
audio.pause()
audio.currentTime = 0
})

document.removeEventListener('touchstart', unlockAudioForiOS)
}

document.removeEventListener('touchstart', unlockAudioForiOS)
}
// fix for getting audio to play on iOS
export const initiateAudio = () => {
document.addEventListener('touchstart', unlockAudioForiOS)
}
2 changes: 1 addition & 1 deletion src/app/functions/ghosts/ghostFrighten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const frightenGhosts = (state: GameState) => {
})
}

function unFrightenGhosts(state: GameState) {
const unFrightenGhosts = (state: GameState) => {
state.ghosts.forEach((ghost) => {
ghost.isFrightened = false
ghost.isFlashing = false
Expand Down
8 changes: 4 additions & 4 deletions src/app/functions/ghosts/ghostTargeting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Ghost } from './create-ghosts'
import { calcDistance, getIndexCoords } from './getCoords'
import { GameState } from '../game-setup/initializeState'

function getBlinkysTarget(state: GameState) {
const getBlinkysTarget = (state: GameState) => {
// Blinky's target is Pacman's current tile
return state.pacmanCurrentIndex
}

function getPinkysTarget(state: GameState) {
const getPinkysTarget = (state: GameState) => {
// Pinky's target is 4 ahead of Pacman's current tile
const pacmanXY = getIndexCoords(state.pacmanCurrentIndex)

Expand All @@ -25,7 +25,7 @@ function getPinkysTarget(state: GameState) {
return state.pacmanCurrentIndex + fourTileOffset
}

function getInkysTarget(state: GameState) {
const getInkysTarget = (state: GameState) => {
// inky has the most complex targeting scheme!!!
// we find 2 tile offset from pacman's heading and draw a line from blinky's position
// we then double the distance and continue past the offset in the same direction
Expand Down Expand Up @@ -55,7 +55,7 @@ function getInkysTarget(state: GameState) {
return targetTileIndex
}

function getClydesTarget(state: GameState, clyde: Ghost) {
const getClydesTarget = (state: GameState, clyde: Ghost) => {
const pacmanXY = getIndexCoords(state.pacmanCurrentIndex)
const clydeXY = getIndexCoords(clyde.currentIndex)

Expand Down
14 changes: 7 additions & 7 deletions src/app/functions/pac-man/pacman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const removePacman = (pacmanCurrentTile: HTMLDivElement, state: GameState) => {
pacmanCurrentTile.innerHTML = ''
}

function didPacmanEatDot(state: GameState) {
const didPacmanEatDot = (state: GameState) => {
if (state.squares[state.pacmanCurrentIndex].classList.contains('pac-dot')) {
state.squares[state.pacmanCurrentIndex].classList.remove('pac-dot')
state.squares[state.pacmanCurrentIndex].classList.add('blank')
Expand All @@ -46,7 +46,7 @@ function didPacmanEatDot(state: GameState) {
}
}

function didPacmanEatPowerPill(state: GameState) {
const didPacmanEatPowerPill = (state: GameState) => {
if (
state.squares[state.pacmanCurrentIndex].classList.contains('power-pill')
) {
Expand All @@ -63,7 +63,7 @@ function didPacmanEatPowerPill(state: GameState) {
}
}

function addBonusToBoard(state: GameState) {
const addBonusToBoard = (state: GameState) => {
if (state.dotsEaten === 70 && !state.firstBonusRemoved) {
state.squares[490].classList.add('bonus-cherry')
setTimeout(() => removeFirstCherry(state), 10000)
Expand All @@ -75,17 +75,17 @@ function addBonusToBoard(state: GameState) {
}
}

function removeFirstCherry(state: GameState) {
const removeFirstCherry = (state: GameState) => {
state.squares[490].classList.remove('bonus-cherry')
state.firstBonusRemoved = true
}

function removeSecondCherry(state: GameState) {
const removeSecondCherry = (state: GameState) => {
state.squares[490].classList.remove('bonus-cherry')
state.secondBonusRemoved = true
}

function didPacmanEatBonus(state: GameState) {
const didPacmanEatBonus = (state: GameState) => {
if (
state.squares[state.pacmanCurrentIndex].classList.contains('bonus-cherry')
) {
Expand All @@ -102,7 +102,7 @@ function didPacmanEatBonus(state: GameState) {
}
}

function didPacmanEatGhost(state: GameState) {
const didPacmanEatGhost = (state: GameState) => {
const pacmanCurrentSquare = state.squares[state.pacmanCurrentIndex]
if (pacmanCurrentSquare.classList.contains('frightened-ghost')) {
const ghost = whichGhostWasEaten(state, pacmanCurrentSquare)
Expand Down

0 comments on commit 9cf710e

Please sign in to comment.