Skip to content

Commit

Permalink
[SOK-27] fix: изменил работу handleKey функций
Browse files Browse the repository at this point in the history
  • Loading branch information
shamemask committed Sep 30, 2024
1 parent f2af052 commit 4e0b9b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
16 changes: 12 additions & 4 deletions packages/client/src/components/Game/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,23 @@ export const Game: React.FC = () => {
)

useEffect(() => {
window.addEventListener('keydown', handleKeyDown)
window.addEventListener('keyup', handleKeyUp)
const handleKeyDownWrapper = (event: KeyboardEvent) =>
handleKeyDown(event.key)
const handleKeyUpWrapper = (event: KeyboardEvent) => handleKeyUp(event.key)

window.addEventListener('keydown', handleKeyDownWrapper)
window.addEventListener('keyup', handleKeyUpWrapper)

return () => {
window.removeEventListener('keydown', handleKeyDown)
window.removeEventListener('keyup', handleKeyUp)
window.removeEventListener('keydown', handleKeyDownWrapper)
window.removeEventListener('keyup', handleKeyUpWrapper)
}
}, [])

const getCanvas = () => canvasRef.current

const getContext = () => getCanvas()?.getContext('2d')

useEffect(() => {
if (gameStarted && !isPaused) {
const canvas = canvasRef.current
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/components/Game/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { KeyMap, Player } from '@/components/Game/gameTypes'
const keyMap: KeyMap = {}

// Обработчик нажатия клавиш
export const handleKeyDown = (event: KeyboardEvent) => {
keyMap[event.key] = true
export const handleKeyDown = (key: string) => {
keyMap[key] = true
}

// Обработчик отпускания клавиш
export const handleKeyUp = (event: KeyboardEvent) => {
keyMap[event.key] = false
export const handleKeyUp = (key: string) => {
delete keyMap[key]
}

// Функция для обновления позиции игрока на основе нажатых клавиш
Expand Down

0 comments on commit 4e0b9b3

Please sign in to comment.