2026-07-14 19:11:15 +03:00
|
|
|
import './style.css';
|
2026-07-16 08:30:14 +03:00
|
|
|
import { Application, Assets, Container, Graphics, Sprite } from 'pixi.js';
|
2026-07-16 09:05:55 +03:00
|
|
|
import {
|
|
|
|
|
BALL_INITIAL_ANGLE,
|
|
|
|
|
BALL_RADIUS,
|
|
|
|
|
BALL_SPEED,
|
2026-07-16 12:46:09 +03:00
|
|
|
BRICK_HEIGHT,
|
|
|
|
|
BRICK_WIDTH,
|
2026-07-16 09:05:55 +03:00
|
|
|
CONTAINER_HEIGHT,
|
|
|
|
|
CONTAINER_WIDTH,
|
|
|
|
|
PADDLE_HEIGHT,
|
|
|
|
|
PADDLE_WIDTH,
|
|
|
|
|
} from './config';
|
2026-07-16 12:54:33 +03:00
|
|
|
import { calculateCollision } from './lib/calculateCollision/calculateCollision';
|
2026-07-16 14:14:37 +03:00
|
|
|
import { calculateDirection } from './lib/calculateDirection/calculateDirection';
|
2026-07-14 19:44:13 +03:00
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
// Create a new application
|
|
|
|
|
const app = new Application();
|
|
|
|
|
|
|
|
|
|
// Initialize the application
|
2026-07-16 08:30:14 +03:00
|
|
|
await app.init({ background: '#1099bb', width: CONTAINER_WIDTH, height: CONTAINER_HEIGHT });
|
2026-07-14 19:44:13 +03:00
|
|
|
|
|
|
|
|
// Append the application canvas to the document body
|
|
|
|
|
document.body.appendChild(app.canvas);
|
|
|
|
|
|
|
|
|
|
// Create and add a container to the stage
|
2026-07-16 08:30:14 +03:00
|
|
|
const container = new Container({
|
|
|
|
|
eventMode: 'static',
|
|
|
|
|
hitArea: app.screen,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
container.x = 0;
|
|
|
|
|
container.y = 0;
|
2026-07-14 19:44:13 +03:00
|
|
|
|
|
|
|
|
app.stage.addChild(container);
|
|
|
|
|
|
2026-07-16 08:30:14 +03:00
|
|
|
const paddle = new Graphics().rect(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT).fill('#fff000');
|
|
|
|
|
container.addChild(paddle);
|
2026-07-14 19:44:13 +03:00
|
|
|
|
2026-07-16 08:30:14 +03:00
|
|
|
container.on('pointermove', (event) => {
|
|
|
|
|
const localPosition = container.toLocal(event.global);
|
2026-07-14 19:44:13 +03:00
|
|
|
|
2026-07-16 08:30:14 +03:00
|
|
|
if (localPosition.x < CONTAINER_WIDTH - PADDLE_WIDTH) {
|
|
|
|
|
paddle.x = localPosition.x;
|
|
|
|
|
}
|
2026-07-14 19:44:13 +03:00
|
|
|
});
|
2026-07-16 09:05:55 +03:00
|
|
|
|
2026-07-16 12:46:09 +03:00
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 09:05:55 +03:00
|
|
|
const ball = new Graphics().circle(0, 0, BALL_RADIUS).fill('#ffffff');
|
2026-07-16 14:14:37 +03:00
|
|
|
ball.x = 100;
|
|
|
|
|
ball.y = 100;
|
2026-07-16 09:05:55 +03:00
|
|
|
container.addChild(ball);
|
|
|
|
|
|
|
|
|
|
const leftBoundary = BALL_RADIUS;
|
|
|
|
|
const rightBoundary = CONTAINER_WIDTH - BALL_RADIUS;
|
|
|
|
|
const topBoundary = BALL_RADIUS;
|
|
|
|
|
const bottomBoundary = CONTAINER_HEIGHT - BALL_RADIUS;
|
2026-07-16 09:41:10 +03:00
|
|
|
const paddleTop = CONTAINER_HEIGHT - PADDLE_HEIGHT;
|
2026-07-16 12:54:33 +03:00
|
|
|
const bricksBottom = BRICK_HEIGHT;
|
2026-07-16 09:05:55 +03:00
|
|
|
|
|
|
|
|
let horizontalSpeed = BALL_SPEED * Math.cos(BALL_INITIAL_ANGLE);
|
|
|
|
|
let verticalSpeed = -1 * BALL_SPEED * Math.sin(BALL_INITIAL_ANGLE);
|
|
|
|
|
|
|
|
|
|
app.ticker.add((time) => {
|
2026-07-16 12:54:33 +03:00
|
|
|
// Базоввое взаимодействие мяча и кирпича
|
|
|
|
|
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;
|
|
|
|
|
|
2026-07-16 14:14:37 +03:00
|
|
|
const ballObject = { left: ballLeft, right: ballRight, top: ballTop, bottom: ballBottom };
|
|
|
|
|
const brickObject = { left: brickLeft, right: brickRight, top: brickTop, bottom: brickBottom };
|
2026-07-16 12:54:33 +03:00
|
|
|
|
2026-07-16 14:14:37 +03:00
|
|
|
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;
|
2026-07-16 12:54:33 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Не даем мячу выйти за границы стен слева / справав и меняем направление
|
2026-07-16 09:05:55 +03:00
|
|
|
if (ball.x <= leftBoundary || ball.x >= rightBoundary) {
|
|
|
|
|
ball.x = ball.x <= leftBoundary ? leftBoundary : rightBoundary;
|
|
|
|
|
horizontalSpeed *= -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 12:54:33 +03:00
|
|
|
// Не даем мячу выйти за границы стен сверху / снизу и меняем направление
|
2026-07-16 09:05:55 +03:00
|
|
|
if (ball.y <= topBoundary || ball.y >= bottomBoundary) {
|
|
|
|
|
ball.y = ball.y <= topBoundary ? topBoundary : bottomBoundary;
|
|
|
|
|
verticalSpeed *= -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 09:41:10 +03:00
|
|
|
// Базоввое взаимодействие мяча и ракетки
|
|
|
|
|
if (
|
|
|
|
|
verticalSpeed > 0 &&
|
|
|
|
|
ball.y + BALL_RADIUS >= paddleTop &&
|
|
|
|
|
ball.x >= paddle.x &&
|
|
|
|
|
ball.x <= paddle.x + PADDLE_WIDTH
|
|
|
|
|
) {
|
|
|
|
|
verticalSpeed *= -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 09:05:55 +03:00
|
|
|
ball.x += horizontalSpeed * time.deltaTime;
|
|
|
|
|
ball.y += verticalSpeed * time.deltaTime;
|
|
|
|
|
});
|
2026-07-14 19:44:13 +03:00
|
|
|
})();
|