Skip to content

Commit 7fb9d2a

Browse files
committed
Add iterative blank controls utility
1 parent 9d2b98d commit 7fb9d2a

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/core/controls/blankControls.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @fileoverview Utility for creating blank control matrices
3+
*/
4+
5+
import type { ControlMatrix } from './types'
6+
7+
/**
8+
* Creates a blank control matrix with all controls set to false.
9+
* Optionally allows specific controls to be overridden.
10+
*
11+
* @param source - A source control matrix to use as a template
12+
* @param overrides - Optional partial control matrix to override specific controls
13+
* @returns A complete control matrix with all controls false except overrides
14+
*/
15+
export function blankControls(
16+
source: ControlMatrix,
17+
overrides?: Partial<ControlMatrix>
18+
): ControlMatrix {
19+
const blank = {} as ControlMatrix
20+
const keys = Object.keys(source) as (keyof ControlMatrix)[]
21+
22+
// Set all controls to false
23+
for (const key of keys) {
24+
blank[key] = overrides?.[key] ?? false
25+
}
26+
27+
return blank
28+
}

src/core/controls/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export {
2323
export { getControls } from './getControls'
2424

2525
export { mergeControls } from './mergeControls'
26+
27+
export { blankControls } from './blankControls'

src/game/components/GameRenderer.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import React, { useEffect, useRef, useState } from 'react'
22
import type { FrameInfo, KeyInfo, MonochromeBitmap } from '@lib/bitmap'
33
import { useAppDispatch, useAppSelector, type RootState } from '../store'
44
import { togglePause, showMap, hideMap, pause, unpause } from '../gameSlice'
5-
import { getControls, mergeControls, type ControlMatrix } from '@core/controls'
5+
import {
6+
getControls,
7+
mergeControls,
8+
blankControls,
9+
type ControlMatrix
10+
} from '@core/controls'
611
import { Map } from './Map'
712
import { bitmapToCollisionItem, type CollisionService } from '@/core/collision'
813
import { Collision } from '@/core/collision/constants'
@@ -140,20 +145,10 @@ const GameRenderer: React.FC<GameRendererProps> = ({
140145

141146
// If map is showing, use blank controls (all false) to prevent game input
142147
const controls = showMapState
143-
? {
144-
thrust: false,
145-
left: false,
146-
right: false,
147-
fire: false,
148-
shield: false,
149-
selfDestruct: false,
150-
pause: false,
151-
quit: false,
152-
nextLevel: false,
153-
extraLife: false,
148+
? blankControls(mergedControls, {
154149
// need to still detect the map key
155150
map: mergedControls.map
156-
}
151+
})
157152
: mergedControls
158153

159154
if (controls.map) {

0 commit comments

Comments
 (0)