refactor: Интеграция новой разделенной логики

This commit is contained in:
Ilia Mashkov
2026-07-18 10:53:38 +03:00
parent 82dcc3e0b5
commit 3c86eb1f98
2 changed files with 14 additions and 83 deletions
+11 -83
View File
@@ -4,15 +4,19 @@ import {
BALL_INITIAL_ANGLE,
BALL_RADIUS,
BALL_SPEED,
BRICK_COLUMN_AMOUNT,
BRICK_HEIGHT,
BRICK_ROW_AMOUNT,
BRICK_WIDTH,
CONTAINER_HEIGHT,
CONTAINER_WIDTH,
PADDLE_HEIGHT,
PADDLE_WIDTH,
} from './config';
import { Game } from './game';
import { calculateCollision } from './lib/calculateCollision/calculateCollision';
import { calculateDirection } from './lib/calculateDirection/calculateDirection';
import { createGameView, syncronizeViewsWithGame } from './view';
(async () => {
// Create a new application
@@ -35,95 +39,19 @@ import { calculateDirection } from './lib/calculateDirection/calculateDirection'
app.stage.addChild(container);
const paddle = new Graphics().rect(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT).fill('#fff000');
container.addChild(paddle);
const game = new Game(BRICK_COLUMN_AMOUNT, BRICK_ROW_AMOUNT);
const views = createGameView(game, container);
container.on('pointermove', (event) => {
const localPosition = container.toLocal(event.global);
if (localPosition.x < CONTAINER_WIDTH - PADDLE_WIDTH) {
paddle.x = localPosition.x;
}
game.paddle.moveTo(localPosition.x, 0, CONTAINER_WIDTH);
});
const bricksRow = Array.from({ length: Math.floor(CONTAINER_WIDTH / BRICK_WIDTH) }).map((_, index) => {
const brick = new Graphics().rect(0, 0, BRICK_WIDTH, BRICK_HEIGHT).fill('#000fff');
brick.x = index * BRICK_WIDTH;
brick.y = 1;
container.addChild(brick);
return brick;
});
const ball = new Graphics().circle(0, 0, BALL_RADIUS).fill('#ffffff');
ball.x = 100;
ball.y = 100;
container.addChild(ball);
const leftBoundary = BALL_RADIUS;
const rightBoundary = CONTAINER_WIDTH - BALL_RADIUS;
const topBoundary = BALL_RADIUS;
const bottomBoundary = CONTAINER_HEIGHT - BALL_RADIUS;
const paddleTop = CONTAINER_HEIGHT - PADDLE_HEIGHT;
const bricksBottom = BRICK_HEIGHT;
let horizontalSpeed = BALL_SPEED * Math.cos(BALL_INITIAL_ANGLE);
let verticalSpeed = -1 * BALL_SPEED * Math.sin(BALL_INITIAL_ANGLE);
console.log(game.paddle.x, game.paddle.y);
app.ticker.add((time) => {
// Базоввое взаимодействие мяча и кирпича
for (let i = bricksRow.length - 1; i >= 0; i--) {
const currentBrick = bricksRow[i];
const brickLeft = currentBrick.x;
const brickRight = currentBrick.x + BRICK_WIDTH;
const brickTop = currentBrick.y;
const brickBottom = currentBrick.y + BRICK_HEIGHT;
const ballLeft = ball.x - BALL_RADIUS;
const ballRight = ball.x + BALL_RADIUS;
const ballTop = ball.y - BALL_RADIUS;
const ballBottom = ball.y + BALL_RADIUS;
const ballObject = { left: ballLeft, right: ballRight, top: ballTop, bottom: ballBottom };
const brickObject = { left: brickLeft, right: brickRight, top: brickTop, bottom: brickBottom };
const isCollided = calculateCollision(ballObject, brickObject);
const directions = calculateDirection(ballObject, brickObject);
if (isCollided && directions !== null) {
horizontalSpeed *= directions[0];
verticalSpeed *= directions[1];
container.removeChild(currentBrick);
currentBrick.destroy();
bricksRow.splice(i, 1);
break;
}
}
// Не даем мячу выйти за границы стен слева / справав и меняем направление
if (ball.x <= leftBoundary || ball.x >= rightBoundary) {
ball.x = ball.x <= leftBoundary ? leftBoundary : rightBoundary;
horizontalSpeed *= -1;
}
// Не даем мячу выйти за границы стен сверху / снизу и меняем направление
if (ball.y <= topBoundary || ball.y >= bottomBoundary) {
ball.y = ball.y <= topBoundary ? topBoundary : bottomBoundary;
verticalSpeed *= -1;
}
// Базоввое взаимодействие мяча и ракетки
if (
verticalSpeed > 0 &&
ball.y + BALL_RADIUS >= paddleTop &&
ball.x >= paddle.x &&
ball.x <= paddle.x + PADDLE_WIDTH
) {
verticalSpeed *= -1;
}
ball.x += horizontalSpeed * time.deltaTime;
ball.y += verticalSpeed * time.deltaTime;
game.update(time.deltaTime);
syncronizeViewsWithGame(views, game);
console.log(game.paddle.x, game.paddle.y);
});
})();