Feature/advanced game mechanics #5
@@ -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() {
|
||||
if (this.type !== 3) {
|
||||
this.livesAmount -= 1;
|
||||
|
||||
if (this.livesAmount === 0) {
|
||||
this.alive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Запускает переход на новый уровень, возвращает мяч в дефоотное положение и отрисовывает кирпичи по карте уровня.
|
||||
*/
|
||||
|
||||
+12
-1
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user