From 4b02ce2eeba16414ef24daa458b859174c44625b Mon Sep 17 00:00:00 2001 From: Daniil <77277774+shamemask@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:21:54 +0300 Subject: [PATCH] =?UTF-8?q?[SOK-27]=20fix:=20+hasCollision=20=D0=BE=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BE=D0=BA=20=D0=BA=D0=BE=D0=BB?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/src/components/Game/collision.tsx | 8 ++++-- .../client/src/components/Game/controls.tsx | 26 +++++++++---------- .../client/src/components/Game/gameLoop.tsx | 4 +-- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/client/src/components/Game/collision.tsx b/packages/client/src/components/Game/collision.tsx index 0a83414..6eca1ae 100644 --- a/packages/client/src/components/Game/collision.tsx +++ b/packages/client/src/components/Game/collision.tsx @@ -1,6 +1,9 @@ import { Enemy, Obstacle, Player } from '@/components/Game/gameTypes' -export const hasCollision = (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 && @@ -8,7 +11,8 @@ export const hasCollision = (player: Player, obstacle: Obstacle): boolean => { player.y + player.height > obstacle.y ) } -export const hasEnemyCollision = ( + +export const detectEnemyCollision = ( rect1: Player | Enemy, rect2: Obstacle | Enemy ): boolean => { diff --git a/packages/client/src/components/Game/controls.tsx b/packages/client/src/components/Game/controls.tsx index fa92ca5..ad4e3f4 100644 --- a/packages/client/src/components/Game/controls.tsx +++ b/packages/client/src/components/Game/controls.tsx @@ -1,6 +1,6 @@ import { ControlsProps, KeyMap } from '@/components/Game/gameTypes' -import { hasCollision } from '@/components/Game/collision' +import { detectCollision } from '@/components/Game/collision' const keyMap: KeyMap = {} @@ -37,20 +37,20 @@ export const updatePlayerMovement = (props: ControlsProps) => { } // Обработка столкновений с препятствиями - props.obstacles.forEach(obstacle => { - if (hasCollision({ ...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) - ) - } + const hasCollision = props.obstacles.find(obstacle => { + return detectCollision({ ...prevPlayer, x: newX, y: newY }, obstacle) }) + // Если есть столкновение, то вернуть предыдущую позицию + if (hasCollision) { + 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 20fed15..876c4a9 100644 --- a/packages/client/src/components/Game/gameLoop.tsx +++ b/packages/client/src/components/Game/gameLoop.tsx @@ -2,7 +2,7 @@ import { handlePlayerHit, resetPlayerPosition } from './player' import { updateEnemyPositions, respawnEnemies } from './enemy' import { clearCanvas, drawPlayer, drawEnemies, drawObstacles } from './utils' import { Enemy, Obstacle, Player } from '@/components/Game/gameTypes' -import { hasEnemyCollision } from '@/components/Game/collision' +import { detectEnemyCollision } from '@/components/Game/collision' /** * Основной игровой цикл, который обновляет состояние игры и перерисовывает экран каждый кадр. @@ -42,7 +42,7 @@ export const gameLoop = ( // Проверка на столкновения между игроком и врагами enemies.forEach(enemy => { - if (hasEnemyCollision(player, enemy)) { + if (detectEnemyCollision(player, enemy)) { // Обработка столкновения: уменьшаем жизни handlePlayerHit( setPlayer,