feat: Базовое взаимодействие мяча и кирпичей
This commit is contained in:
+27
-2
@@ -11,6 +11,7 @@ import {
|
|||||||
PADDLE_HEIGHT,
|
PADDLE_HEIGHT,
|
||||||
PADDLE_WIDTH,
|
PADDLE_WIDTH,
|
||||||
} from './config';
|
} from './config';
|
||||||
|
import { calculateCollision } from './lib/calculateCollision/calculateCollision';
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
// Create a new application
|
// Create a new application
|
||||||
@@ -60,18 +61,42 @@ import {
|
|||||||
const topBoundary = BALL_RADIUS;
|
const topBoundary = BALL_RADIUS;
|
||||||
const bottomBoundary = CONTAINER_HEIGHT - BALL_RADIUS;
|
const bottomBoundary = CONTAINER_HEIGHT - BALL_RADIUS;
|
||||||
const paddleTop = CONTAINER_HEIGHT - PADDLE_HEIGHT;
|
const paddleTop = CONTAINER_HEIGHT - PADDLE_HEIGHT;
|
||||||
|
const bricksBottom = BRICK_HEIGHT;
|
||||||
|
|
||||||
let horizontalSpeed = BALL_SPEED * Math.cos(BALL_INITIAL_ANGLE);
|
let horizontalSpeed = BALL_SPEED * Math.cos(BALL_INITIAL_ANGLE);
|
||||||
let verticalSpeed = -1 * BALL_SPEED * Math.sin(BALL_INITIAL_ANGLE);
|
let verticalSpeed = -1 * BALL_SPEED * Math.sin(BALL_INITIAL_ANGLE);
|
||||||
|
|
||||||
app.ticker.add((time) => {
|
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 isCollided = calculateCollision(
|
||||||
|
{ left: ballLeft, right: ballRight, top: ballTop, bottom: ballBottom },
|
||||||
|
{ left: brickLeft, right: brickRight, top: brickTop, bottom: brickBottom },
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isCollided) {
|
||||||
|
verticalSpeed *= -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Не даем мячу выйти за границы стен слева / справав и меняем направление
|
||||||
if (ball.x <= leftBoundary || ball.x >= rightBoundary) {
|
if (ball.x <= leftBoundary || ball.x >= rightBoundary) {
|
||||||
ball.x = ball.x <= leftBoundary ? leftBoundary : rightBoundary;
|
ball.x = ball.x <= leftBoundary ? leftBoundary : rightBoundary;
|
||||||
horizontalSpeed *= -1;
|
horizontalSpeed *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Не даем выйти за границы стен сверху / снизу и меняем направление
|
// Не даем мячу выйти за границы стен сверху / снизу и меняем направление
|
||||||
if (ball.y <= topBoundary || ball.y >= bottomBoundary) {
|
if (ball.y <= topBoundary || ball.y >= bottomBoundary) {
|
||||||
ball.y = ball.y <= topBoundary ? topBoundary : bottomBoundary;
|
ball.y = ball.y <= topBoundary ? topBoundary : bottomBoundary;
|
||||||
verticalSpeed *= -1;
|
verticalSpeed *= -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user