refactor: Формат хранения кирпичей в классе Game переписан с вложенного массива на обычный массив. Входные данные для значений каждого кирпича берутся из карты уровней
This commit is contained in:
+22
-24
@@ -66,36 +66,34 @@ export function tick(game, containerWidth, containerHeight, deltaTime) {
|
||||
}
|
||||
|
||||
// Базоввое взаимодействие мяча и кирпича
|
||||
for (const row of bricks) {
|
||||
for (const brick of row) {
|
||||
if (!brick.alive) {
|
||||
continue;
|
||||
}
|
||||
for (const brick of bricks) {
|
||||
if (!brick.alive) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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 ballLeft = ball.x - ball.radius;
|
||||
const ballRight = ball.x + ball.radius;
|
||||
const ballTop = ball.y - ball.radius;
|
||||
const ballBottom = ball.y + ball.radius;
|
||||
|
||||
const brickLeft = brick.x;
|
||||
const brickRight = brick.x + brick.width;
|
||||
const brickTop = brick.y;
|
||||
const brickBottom = brick.y + brick.height;
|
||||
const brickLeft = brick.x;
|
||||
const brickRight = brick.x + brick.width;
|
||||
const brickTop = brick.y;
|
||||
const brickBottom = brick.y + brick.height;
|
||||
|
||||
const ballObject = { left: ballLeft, right: ballRight, top: ballTop, bottom: ballBottom };
|
||||
const brickObject = { left: brickLeft, right: brickRight, top: brickTop, bottom: brickBottom };
|
||||
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 isCollided = calculateCollision(ballObject, brickObject);
|
||||
|
||||
if (isCollided) {
|
||||
const directions = calculateDirection(ballObject, brickObject);
|
||||
if (isCollided) {
|
||||
const directions = calculateDirection(ballObject, brickObject);
|
||||
|
||||
if (directions !== null) {
|
||||
ball.horizontalSpeed *= directions[0];
|
||||
ball.verticalSpeed *= directions[1];
|
||||
brick.kill();
|
||||
return;
|
||||
}
|
||||
if (directions !== null) {
|
||||
ball.horizontalSpeed *= directions[0];
|
||||
ball.verticalSpeed *= directions[1];
|
||||
brick.kill();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@ describe('tick', () => {
|
||||
});
|
||||
|
||||
it('Возвращает корректное значние и не изменяет свойства game при неверых размерах контейнера', () => {
|
||||
const game = new Game(1, 1);
|
||||
const levels = [[[1]]];
|
||||
const game = new Game(levels);
|
||||
const ballX = game.ball.x;
|
||||
const ballY = game.ball.y;
|
||||
expect(tick(game, -1, -1, 1)).toBeNull();
|
||||
@@ -20,7 +21,8 @@ describe('tick', () => {
|
||||
});
|
||||
|
||||
it('Возвращает корректное значние и не изменяет свойства game при неверном значении времени', () => {
|
||||
const game = new Game(1, 1);
|
||||
const levels = [[[1]]];
|
||||
const game = new Game(levels);
|
||||
const ballX = game.ball.x;
|
||||
const ballY = game.ball.y;
|
||||
expect(tick(game, 1, 1, -1)).toBeNull();
|
||||
@@ -29,7 +31,8 @@ describe('tick', () => {
|
||||
});
|
||||
|
||||
it('Меняет направление мяча при столкновении со стеной', () => {
|
||||
const game = new Game(1, 1);
|
||||
const levels = [[[1]]];
|
||||
const game = new Game(levels);
|
||||
game.ball.x = 0;
|
||||
game.ball.horizontalSpeed = -10;
|
||||
tick(game, CONTAINER_WIDTH, CONTAINER_HEIGHT, 1);
|
||||
@@ -38,7 +41,8 @@ describe('tick', () => {
|
||||
});
|
||||
|
||||
it('Меняет направление мяча при столкновении с потолком', () => {
|
||||
const game = new Game(0, 0);
|
||||
const levels = [[[1]]];
|
||||
const game = new Game(1);
|
||||
game.ball.y = 0;
|
||||
game.ball.verticalSpeed = -10;
|
||||
tick(game, CONTAINER_WIDTH, CONTAINER_HEIGHT, 1);
|
||||
@@ -47,8 +51,9 @@ describe('tick', () => {
|
||||
});
|
||||
|
||||
it('Убивает кирпич при столкновении и отражает мяч', () => {
|
||||
const game = new Game(1, 1);
|
||||
const brick = game.bricks[0][0];
|
||||
const levels = [[[1]]];
|
||||
const game = new Game(levels);
|
||||
const brick = game.bricks[0];
|
||||
|
||||
game.ball.x = brick.x + game.ball.radius + 1;
|
||||
game.ball.y = brick.y + brick.height + game.ball.radius + 1;
|
||||
@@ -62,8 +67,9 @@ describe('tick', () => {
|
||||
});
|
||||
|
||||
it('Убивает только один кирпич за тик', () => {
|
||||
const game = new Game(2, 1);
|
||||
const [firstBrick, secondBrick] = game.bricks[0];
|
||||
const levels = [[[1, 1]]];
|
||||
const game = new Game(levels);
|
||||
const [firstBrick, secondBrick] = game.bricks;
|
||||
|
||||
game.ball.x = firstBrick.x + game.ball.radius + 1;
|
||||
game.ball.y = firstBrick.y + firstBrick.height + game.ball.radius + 1;
|
||||
@@ -72,7 +78,7 @@ describe('tick', () => {
|
||||
|
||||
tick(game, CONTAINER_WIDTH, CONTAINER_HEIGHT, 0.1);
|
||||
|
||||
const killedCount = game.bricks[0].filter((b) => !b.alive).length;
|
||||
const killedCount = game.bricks.filter((b) => !b.alive).length;
|
||||
expect(killedCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user