Feature/advanced game mechanics #5

Merged
ilia merged 30 commits from feature/advanced-game-mechanics into main 2026-07-19 15:09:22 +00:00
5 changed files with 59 additions and 6 deletions
Showing only changes of commit 5e259f7efc - Show all commits
+13 -1
View File
@@ -9,16 +9,28 @@ export class Brick {
* @param {number} y координата положения кирпича по оси Y * @param {number} y координата положения кирпича по оси Y
* @param {number} width положительное числовое значение ширины кирпича * @param {number} width положительное числовое значение ширины кирпича
* @param {number} height положительное числовое значение высоты кирпича * @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.x = x;
this.y = y; this.y = y;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.type = type;
this.alive = true; this.alive = true;
this.livesAmount = this.type;
} }
/**
* В зависимости от типа и оставшихся жизней убивает кирпич
*/
kill() { kill() {
if (this.type !== 3) {
this.livesAmount -= 1;
if (this.livesAmount === 0) {
this.alive = false; this.alive = false;
} }
} }
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ export function layBricks(levelMap, brickWidth, brickHeight) {
for (let i = 0; i < levelMap.length; i++) { for (let i = 0; i < levelMap.length; i++) {
for (let j = 0; j < levelMap[i].length; j++) { for (let j = 0; j < levelMap[i].length; j++) {
if (levelMap[i][j] !== 0) { 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]));
} }
} }
} }
@@ -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++;
}
}
});
}); });
+10 -2
View File
@@ -63,9 +63,9 @@ export class Game {
return; return;
} }
const isAnyBrickAlive = this.bricks.some((brick) => brick.alive); const isLevelComplete = this._checkLevelCompletion();
if (!isAnyBrickAlive) { if (isLevelComplete) {
this.currentLevel += 1; this.currentLevel += 1;
if (this.currentLevel <= this.maxLevel) { 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);
}
/** /**
* Запускает переход на новый уровень, возвращает мяч в дефоотное положение и отрисовывает кирпичи по карте уровня. * Запускает переход на новый уровень, возвращает мяч в дефоотное положение и отрисовывает кирпичи по карте уровня.
*/ */
+12 -1
View File
@@ -23,9 +23,20 @@ function createPaddleView(paddle) {
/** /**
* Создает визуальное отображение кирпича с помощью Pixi.js * Создает визуальное отображение кирпича с помощью Pixi.js
* @param {Brick} brick экземпляр класса кирпич * @param {Brick} brick экземпляр класса кирпич
* @returns {Graphics} графическое отображение кирпича
*/ */
function createBrickView(brick) { 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');
}
} }
/** /**