From 6b32d3a50088b9efed5f9ba1fadd1a5835d9c976 Mon Sep 17 00:00:00 2001 From: Daniil <77277774+shamemask@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:08:45 +0300 Subject: [PATCH] [SOK-27] fix: hasCollision --- packages/client/src/components/Game/collision.tsx | 7 ++----- packages/client/src/components/Game/controls.tsx | 4 ++-- packages/client/src/components/Game/gameLoop.tsx | 4 ++-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/client/src/components/Game/collision.tsx b/packages/client/src/components/Game/collision.tsx index 8f994e8..0a83414 100644 --- a/packages/client/src/components/Game/collision.tsx +++ b/packages/client/src/components/Game/collision.tsx @@ -1,9 +1,6 @@ import { Enemy, Obstacle, Player } from '@/components/Game/gameTypes' -export const detectCollision = ( - player: Player, - obstacle: Obstacle -): boolean => { +export const hasCollision = (player: Player, obstacle: Obstacle): boolean => { return ( player.x < obstacle.x + obstacle.width && player.x + player.width > obstacle.x && @@ -11,7 +8,7 @@ export const detectCollision = ( player.y + player.height > obstacle.y ) } -export const detectEnemyCollision = ( +export const hasEnemyCollision = ( 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 b9466a6..fa92ca5 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 { detectCollision } from '@/components/Game/collision' +import { hasCollision } from '@/components/Game/collision' const keyMap: KeyMap = {} @@ -38,7 +38,7 @@ export const updatePlayerMovement = (props: ControlsProps) => { // Обработка столкновений с препятствиями props.obstacles.forEach(obstacle => { - if (detectCollision({ ...prevPlayer, x: newX, y: newY }, obstacle)) { + if (hasCollision({ ...prevPlayer, x: newX, y: newY }, obstacle)) { newX = prevPlayer.x newY = prevPlayer.y } else { diff --git a/packages/client/src/components/Game/gameLoop.tsx b/packages/client/src/components/Game/gameLoop.tsx index 876c4a9..20fed15 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 { detectEnemyCollision } from '@/components/Game/collision' +import { hasEnemyCollision } from '@/components/Game/collision' /** * Основной игровой цикл, который обновляет состояние игры и перерисовывает экран каждый кадр. @@ -42,7 +42,7 @@ export const gameLoop = ( // Проверка на столкновения между игроком и врагами enemies.forEach(enemy => { - if (detectEnemyCollision(player, enemy)) { + if (hasEnemyCollision(player, enemy)) { // Обработка столкновения: уменьшаем жизни handlePlayerHit( setPlayer,