From 959c1b4f87c9469007ce207ed93f75677ce64cc7 Mon Sep 17 00:00:00 2001 From: Daniil <77277774+shamemask@users.noreply.github.com> Date: Mon, 30 Sep 2024 19:39:50 +0300 Subject: [PATCH] =?UTF-8?q?[SOK-27]=20fix:=20=D0=9D=D0=B0=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=B8=D0=BB=20=D0=B4=D0=B2=D0=B8=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D1=83=20=D0=BA=D0=BE=D0=BB=D0=B8=D0=B7=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=81=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B9=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/client/src/components/Game/Game.tsx | 14 ++++---- .../client/src/components/Game/controls.tsx | 34 +++++++++++-------- .../client/src/components/Game/gameLoop.tsx | 9 +++-- .../client/src/components/Game/gameTypes.tsx | 9 +++++ .../client/src/components/Game/player.tsx | 7 ++-- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/packages/client/src/components/Game/Game.tsx b/packages/client/src/components/Game/Game.tsx index d516d12..30965ff 100644 --- a/packages/client/src/components/Game/Game.tsx +++ b/packages/client/src/components/Game/Game.tsx @@ -8,10 +8,10 @@ import { handleKeyUp, updatePlayerMovement, } from '@/components/Game/controls' -import { Obstacle } from '@/components/Game/gameTypes' +import { ControlsProps, Obstacle } from '@/components/Game/gameTypes' import { initializeObstacle } from '@/components/Game/obstacle' -const speedFactor = 0.3 +const speedFactor = 1 const livesUse = 3 export const Game: React.FC = () => { @@ -54,13 +54,15 @@ export const Game: React.FC = () => { } const canvas = getCanvas() if (!canvas) return - updatePlayerMovement( + const moveProps: ControlsProps = { player, setPlayer, speedFactor, - canvas.width, - canvas.height - ) + obstacles, + canvasWidth: canvas.width, + canvasHeight: canvas.height, + } + updatePlayerMovement(moveProps) gameLoop( timestamp, context, diff --git a/packages/client/src/components/Game/controls.tsx b/packages/client/src/components/Game/controls.tsx index 3bc2e88..c0bffc9 100644 --- a/packages/client/src/components/Game/controls.tsx +++ b/packages/client/src/components/Game/controls.tsx @@ -1,4 +1,5 @@ -import { KeyMap, Player } from '@/components/Game/gameTypes' +import { ControlsProps, KeyMap } from '@/components/Game/gameTypes' +import { detectCollision } from '@/components/Game/player' const keyMap: KeyMap = {} @@ -13,16 +14,10 @@ export const handleKeyUp = (key: string) => { } // Функция для обновления позиции игрока на основе нажатых клавиш -export const updatePlayerMovement = ( - player: Player, - setPlayer: React.Dispatch>, - speedFactor: number, - canvasWidth: number, - canvasHeight: number -) => { - const speed = player.speed * speedFactor - - setPlayer(prevPlayer => { +export const updatePlayerMovement = (props: ControlsProps) => { + const speed = props.player.speed * props.speedFactor + + props.setPlayer(prevPlayer => { let newX = prevPlayer.x let newY = prevPlayer.y @@ -40,9 +35,20 @@ export const updatePlayerMovement = ( newX += speed } - // Ограничение движения по краям canvas - newX = Math.max(0, Math.min(newX, canvasWidth - prevPlayer.width)) - newY = Math.max(0, Math.min(newY, canvasHeight - prevPlayer.height)) + // Обработка столкновений с препятствиями + props.obstacles.forEach(obstacle => { + if (detectCollision({ ...prevPlayer, x: newX, y: newY }, obstacle)) { + newX = prevPlayer.x + newY = prevPlayer.y + } else { + // Ограничение движения по краям canvas + newX = Math.max(0, Math.min(newX, props.canvasWidth - prevPlayer.width)) + newY = Math.max( + 0, + Math.min(newY, props.canvasHeight - prevPlayer.height) + ) + } + }) return { ...prevPlayer, x: newX, y: newY } }) diff --git a/packages/client/src/components/Game/gameLoop.tsx b/packages/client/src/components/Game/gameLoop.tsx index 72af25d..4a2c3de 100644 --- a/packages/client/src/components/Game/gameLoop.tsx +++ b/packages/client/src/components/Game/gameLoop.tsx @@ -1,8 +1,4 @@ -import { - handlePlayerHit, - resetPlayerPosition, - updatePlayerPosition, -} from './player' +import { handlePlayerHit, resetPlayerPosition } from './player' import { updateEnemyPositions, respawnEnemies, @@ -23,6 +19,9 @@ import { Enemy, Obstacle, Player } from '@/components/Game/gameTypes' * @param lives - Текущее количество жизней игрока. * @param setLives - Функция для изменения количества жизней игрока. * @param speedFactor - Коэффициент скорости. + * @param handleGameOver - Обработчик события окончания игры. + * @param isPaused - Флаг паузы. + * @param isGameOver - Флаг окончания игры. */ export const gameLoop = ( timestamp: number, diff --git a/packages/client/src/components/Game/gameTypes.tsx b/packages/client/src/components/Game/gameTypes.tsx index 619df42..40b511a 100644 --- a/packages/client/src/components/Game/gameTypes.tsx +++ b/packages/client/src/components/Game/gameTypes.tsx @@ -26,3 +26,12 @@ export interface Obstacle { export interface KeyMap { [key: string]: boolean } + +export interface ControlsProps { + player: Player + setPlayer: React.Dispatch> + speedFactor: number + obstacles: Obstacle[] + canvasWidth: number + canvasHeight: number +} diff --git a/packages/client/src/components/Game/player.tsx b/packages/client/src/components/Game/player.tsx index b67c2dc..8b22038 100644 --- a/packages/client/src/components/Game/player.tsx +++ b/packages/client/src/components/Game/player.tsx @@ -1,4 +1,3 @@ -import { respawnEnemies } from '@/components/Game/enemy' import { Enemy, Obstacle, Player } from '@/components/Game/gameTypes' export const initializePlayer = () => ({ @@ -10,7 +9,10 @@ export const initializePlayer = () => ({ direction: { x: 0, y: 0 }, }) -const detectCollision = (player: Player, obstacle: Obstacle): boolean => { +export const detectCollision = ( + player: Player, + obstacle: Obstacle +): boolean => { return ( player.x < obstacle.x + obstacle.width && player.x + player.width > obstacle.x && @@ -50,6 +52,7 @@ export const resetPlayerPosition = ( /** * Функция для обработки столкновения игрока с врагом. + * @param setPlayer - Функция для обновления состояния игрока. * @param setLives - Функция для изменения количества жизней игрока. * @param resetPlayerPosition - Функция для сброса позиции игрока. * @param respawnEnemies - Функция для респауна врагов.