Skip to content

Commit

Permalink
[SOK-27] fix: hasCollision
Browse files Browse the repository at this point in the history
  • Loading branch information
shamemask committed Oct 1, 2024
1 parent 11532f6 commit 6b32d3a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
7 changes: 2 additions & 5 deletions packages/client/src/components/Game/collision.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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 &&
player.y < obstacle.y + obstacle.height &&
player.y + player.height > obstacle.y
)
}
export const detectEnemyCollision = (
export const hasEnemyCollision = (
rect1: Player | Enemy,
rect2: Obstacle | Enemy
): boolean => {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/components/Game/controls.tsx
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/components/Game/gameLoop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
* Основной игровой цикл, который обновляет состояние игры и перерисовывает экран каждый кадр.
Expand Down Expand Up @@ -42,7 +42,7 @@ export const gameLoop = (

// Проверка на столкновения между игроком и врагами
enemies.forEach(enemy => {
if (detectEnemyCollision(player, enemy)) {
if (hasEnemyCollision(player, enemy)) {
// Обработка столкновения: уменьшаем жизни
handlePlayerHit(
setPlayer,
Expand Down

0 comments on commit 6b32d3a

Please sign in to comment.