From 5e259f7efc82270ea90738cb6e22105cbacb4abf Mon Sep 17 00:00:00 2001 From: Ilia Mashkov Date: Sun, 19 Jul 2026 10:51:58 +0300 Subject: [PATCH] =?UTF-8?q?feat(brick):=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B8=D0=BF=D1=8B=20=D0=BA?= =?UTF-8?q?=D0=B8=D1=80=D0=BF=D0=B8=D1=87=D0=B5=D0=B9:=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=8B=D1=87=D0=BD=D1=8B=D0=B9,=20=D1=81=202=20=D0=B6=D0=B8?= =?UTF-8?q?=D0=B7=D0=BD=D1=8F=D0=BC=D0=B8,=20=D0=BD=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D0=B7=D1=80=D1=83=D1=88=D0=B0=D0=B5=D0=BC=D1=8B=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entities/brick/brick.js | 16 ++++++++++++-- src/entities/brick/layBricks/layBricks.js | 2 +- .../brick/layBricks/layBricks.test.js | 22 +++++++++++++++++++ src/game.js | 12 ++++++++-- src/view.js | 13 ++++++++++- 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/entities/brick/brick.js b/src/entities/brick/brick.js index 8151678..bf770e5 100644 --- a/src/entities/brick/brick.js +++ b/src/entities/brick/brick.js @@ -9,16 +9,28 @@ export class Brick { * @param {number} y координата положения кирпича по оси Y * @param {number} width положительное числовое значение ширины кирпича * @param {number} height положительное числовое значение высоты кирпича + * @param {number} type тип блока. 1 - обычный, 2 - больше 1 жизни, 3 - неразрушаемый */ - constructor(x, y, width, height) { + constructor(x, y, width, height, type) { this.x = x; this.y = y; this.width = width; this.height = height; + this.type = type; this.alive = true; + this.livesAmount = this.type; } + /** + * В зависимости от типа и оставшихся жизней убивает кирпич + */ kill() { - this.alive = false; + if (this.type !== 3) { + this.livesAmount -= 1; + + if (this.livesAmount === 0) { + this.alive = false; + } + } } } diff --git a/src/entities/brick/layBricks/layBricks.js b/src/entities/brick/layBricks/layBricks.js index 1e2858f..1a26e50 100644 --- a/src/entities/brick/layBricks/layBricks.js +++ b/src/entities/brick/layBricks/layBricks.js @@ -26,7 +26,7 @@ export function layBricks(levelMap, brickWidth, brickHeight) { for (let i = 0; i < levelMap.length; i++) { for (let j = 0; j < levelMap[i].length; j++) { if (levelMap[i][j] !== 0) { - bricks.push(new Brick(j * brickWidth, i * brickHeight, brickWidth, brickHeight)); + bricks.push(new Brick(j * brickWidth, i * brickHeight, brickWidth, brickHeight, levelMap[i][j])); } } } diff --git a/src/entities/brick/layBricks/layBricks.test.js b/src/entities/brick/layBricks/layBricks.test.js index 0a67871..70fe366 100644 --- a/src/entities/brick/layBricks/layBricks.test.js +++ b/src/entities/brick/layBricks/layBricks.test.js @@ -65,4 +65,26 @@ describe('layBricks', () => { } } }); + + it('Верно записывает тип кирпича', () => { + const levelMap = [ + [2, 1, 3], + [1, 2, 0], + ]; + const brickWidth = 1; + const brickHeight = 1; + + const bricks = layBricks(levelMap, brickWidth, brickHeight); + let index = 0; + + for (let i = 0; i < levelMap.length; i++) { + for (let j = 0; j < levelMap[i].length; j++) { + if (levelMap[i][j] === 0) { + continue; + } + expect(bricks[index].type).toBe(levelMap[i][j]); + index++; + } + } + }); }); diff --git a/src/game.js b/src/game.js index f37876d..d73919f 100644 --- a/src/game.js +++ b/src/game.js @@ -63,9 +63,9 @@ export class Game { return; } - const isAnyBrickAlive = this.bricks.some((brick) => brick.alive); + const isLevelComplete = this._checkLevelCompletion(); - if (!isAnyBrickAlive) { + if (isLevelComplete) { this.currentLevel += 1; if (this.currentLevel <= this.maxLevel) { @@ -76,6 +76,14 @@ export class Game { } } + /** + * Проверяет завершен ли текущий уровень + * @returns {boolean} + */ + _checkLevelCompletion() { + return this.bricks.every((brick) => brick.type === 3 || !brick.alive); + } + /** * Запускает переход на новый уровень, возвращает мяч в дефоотное положение и отрисовывает кирпичи по карте уровня. */ diff --git a/src/view.js b/src/view.js index 99b76af..7fcb508 100644 --- a/src/view.js +++ b/src/view.js @@ -23,9 +23,20 @@ function createPaddleView(paddle) { /** * Создает визуальное отображение кирпича с помощью Pixi.js * @param {Brick} brick экземпляр класса кирпич + * @returns {Graphics} графическое отображение кирпича */ function createBrickView(brick) { - return new Graphics().rect(0, 0, brick.width, brick.height).fill('#000fff'); + const brickView = new Graphics().rect(0, 0, brick.width, brick.height); + + switch (brick.type) { + case 2: + return brickView.fill('#00ff00'); + case 3: + return brickView.fill('#ff00ff'); + case 1: + default: + return brickView.fill('#000fff'); + } } /**