-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SOK-40] Game map #23
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { HandlePlayerHit, resetPlayerPosition } from './player' | ||
import { updateEnemyPositions, respawnEnemies } from './enemy' | ||
import { HandlePlayerHit } from './player' | ||
import { updateEnemyPositions } from './enemy' | ||
import { clearCanvas, drawPlayer, drawEnemies, drawObstacles } from './utils' | ||
import { Enemy, Obstacle, Player } from '@/components/Game/gameTypes' | ||
import { detectEnemyCollision } from '@/components/Game/collision' | ||
|
@@ -31,15 +31,20 @@ export const gameLoop = ( | |
drawPlayer(context, playerRef.current) | ||
drawEnemies(context, enemiesRef.current) | ||
|
||
// Проверка на столкновения между игроком и врагами | ||
let collisionOccurred = false | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вроде же всегда будет false, значит всегда заходит в There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Переписал |
||
enemiesRef.current.forEach(enemy => { | ||
if (detectEnemyCollision(playerRef.current, enemy)) { | ||
// Обработка столкновения: уменьшаем жизни | ||
if (!collisionOccurred && detectEnemyCollision(playerRef.current, enemy)) { | ||
collisionOccurred = true | ||
HandlePlayerHit( | ||
livesRef, | ||
handleGameOver, | ||
() => resetPlayerPosition(playerRef), | ||
() => respawnEnemies(enemiesRef) | ||
() => { | ||
playerRef.current = { ...playerRef.current, x: 400, y: 560 } | ||
}, | ||
() => { | ||
enemiesRef.current = enemiesRef.current.map(e => ({ ...e })) | ||
} | ||
) | ||
} | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
import { Obstacle } from '@/components/Game/gameTypes' | ||
import { getRandomEdgePosition } from '@/components/Game/utils' | ||
|
||
export const initializeObstacle = (): Obstacle[] => { | ||
const obstacles: Obstacle[] = [] | ||
for (let i = 0; i < 10; i++) { | ||
const { x, y } = getRandomEdgePosition(800, 600) | ||
const obstacle: Obstacle = { x, y, width: 50, height: 50 } | ||
obstacles.push(obstacle) | ||
} | ||
return obstacles | ||
return [ | ||
{ x: 0, y: 0, width: 120, height: 50 }, | ||
{ x: 200, y: 0, width: 70, height: 50 }, | ||
{ x: 350, y: 0, width: 70, height: 50 }, | ||
{ x: 500, y: 0, width: 120, height: 50 }, | ||
{ x: 700, y: 0, width: 100, height: 50 }, | ||
|
||
{ x: 0, y: 130, width: 50, height: 120 }, | ||
{ x: 130, y: 130, width: 50, height: 70 }, | ||
{ x: 260, y: 130, width: 50, height: 200 }, | ||
{ x: 390, y: 130, width: 50, height: 70 }, | ||
{ x: 520, y: 130, width: 50, height: 120 }, | ||
{ x: 650, y: 130, width: 50, height: 125 }, | ||
|
||
{ x: 0, y: 330, width: 120, height: 50 }, | ||
{ x: 200, y: 330, width: 150, height: 50 }, | ||
{ x: 350, y: 330, width: 70, height: 50 }, | ||
{ x: 500, y: 330, width: 120, height: 50 }, | ||
{ x: 700, y: 330, width: 100, height: 100 }, | ||
|
||
{ x: 0, y: 460, width: 50, height: 140 }, | ||
{ x: 130, y: 530, width: 50, height: 70 }, | ||
{ x: 260, y: 480, width: 50, height: 120 }, | ||
{ x: 390, y: 460, width: 50, height: 70 }, | ||
{ x: 520, y: 490, width: 50, height: 120 }, | ||
{ x: 650, y: 480, width: 50, height: 120 }, | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,22 @@ export const drawObstacles = ( | |
context: CanvasRenderingContext2D, | ||
obstacles: Obstacle[] | ||
) => { | ||
const SPRITE_SIZE = 50 | ||
|
||
obstacles.forEach(obstacle => { | ||
context.drawImage(wallSprite, obstacle.x, obstacle.y) | ||
const horizontalCount = Math.ceil(obstacle.width / SPRITE_SIZE) | ||
const verticalCount = Math.ceil(obstacle.height / SPRITE_SIZE) | ||
|
||
for (let i = 0; i < horizontalCount; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Возможно
Но может быть это на любителя. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поменял |
||
for (let j = 0; j < verticalCount; j++) { | ||
const x = obstacle.x + i * SPRITE_SIZE | ||
const y = obstacle.y + j * SPRITE_SIZE | ||
|
||
const width = Math.min(SPRITE_SIZE, obstacle.width - i * SPRITE_SIZE) | ||
const height = Math.min(SPRITE_SIZE, obstacle.height - j * SPRITE_SIZE) | ||
|
||
context.drawImage(wallSprite, 0, 0, width, height, x, y, width, height) | ||
} | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Возможно лучше дефолтны объект создать, и копировать его, подменяя позицию.
Что-то типо:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, не подумал об этом, так действительно лучше