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
3 changed files with 27 additions and 3 deletions
Showing only changes of commit fea05e6b97 - Show all commits
+2
View File
@@ -16,6 +16,7 @@ export class Ball {
this.radius = radius;
this.verticalSpeed = verticalSpeed;
this.horizontalSpeed = horizontalSpeed;
this.isOut = false;
this.defaultX = x;
this.defaultY = y;
@@ -40,5 +41,6 @@ export class Ball {
this.y = this.defaultY;
this.verticalSpeed = this.defaultVerticalSpeed;
this.horizontalSpeed = this.defaultHorizontalSpeed;
this.isOut = false;
}
}
+16
View File
@@ -23,6 +23,9 @@ export class Game {
* @param {number} rowAmount количество кирпичей по оси Y, неотрицателное, целое число
*/
constructor(columnAmount, rowAmount) {
this.livesAmount = 3;
this.status = 'in_process';
this.ball = new Ball(
CONTAINER_WIDTH / 2 - BALL_RADIUS,
CONTAINER_HEIGHT / 2 - BALL_RADIUS,
@@ -40,6 +43,19 @@ export class Game {
* @param {*} deltaTime изменение времени из Ticker
*/
update(deltaTime) {
if (this.status !== 'in_process') {
return;
}
tick(this, CONTAINER_WIDTH, CONTAINER_HEIGHT, deltaTime);
if (this.ball.isOut) {
this.livesAmount -= 1;
if (this.livesAmount === 0) {
this.status = 'over';
} else {
this.ball.reset();
}
}
}
}
+9 -3
View File
@@ -43,12 +43,18 @@ export function tick(game, containerWidth, containerHeight, deltaTime) {
ball.horizontalSpeed *= -1;
}
// Не даем мячу выйти за границы стен сверху / снизу и меняем направление
if (ball.y <= topBoundary || ball.y >= bottomBoundary) {
ball.y = ball.y <= topBoundary ? topBoundary : bottomBoundary;
// Не даем мячу выйти за границу стены сверху и меняем направление
if (ball.y <= topBoundary) {
ball.y = topBoundary;
ball.verticalSpeed *= -1;
}
// Проверяем выход за границу стены снизу
if (ball.y >= bottomBoundary) {
game.ball.isOut = true;
return;
}
// Базоввое взаимодействие мяча и кирпича
for (const row of bricks) {
for (const brick of row) {