2026-07-17 11:06:27 +03:00
|
|
|
import {
|
|
|
|
|
BALL_INITIAL_ANGLE,
|
|
|
|
|
BALL_RADIUS,
|
|
|
|
|
BALL_SPEED,
|
2026-07-19 13:20:10 +03:00
|
|
|
BALL_SPEED_LEVEL_STEP,
|
|
|
|
|
BALL_SPEED_TIME_STEP,
|
2026-07-17 11:06:27 +03:00
|
|
|
BRICK_HEIGHT,
|
|
|
|
|
BRICK_WIDTH,
|
|
|
|
|
CONTAINER_HEIGHT,
|
|
|
|
|
CONTAINER_WIDTH,
|
|
|
|
|
PADDLE_HEIGHT,
|
|
|
|
|
PADDLE_WIDTH,
|
|
|
|
|
} from './config';
|
|
|
|
|
import { Ball } from './entities/ball/ball';
|
|
|
|
|
import { layBricks } from './entities/brick/layBricks/layBricks';
|
|
|
|
|
import { Paddle } from './entities/paddle/paddle';
|
2026-07-18 10:18:38 +03:00
|
|
|
import { tick } from './lib/tick/tick';
|
2026-07-17 11:06:27 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Класс игры c информацией о всех игровых сущностях
|
|
|
|
|
*/
|
|
|
|
|
export class Game {
|
|
|
|
|
/**
|
2026-07-18 17:47:33 +03:00
|
|
|
* @param {number[][][]} levels список уровней, каждый элемент которого - карта расположения блоков
|
2026-07-17 11:06:27 +03:00
|
|
|
*/
|
2026-07-18 17:47:33 +03:00
|
|
|
constructor(levels) {
|
2026-07-18 13:01:43 +03:00
|
|
|
this.livesAmount = 3;
|
|
|
|
|
this.status = 'in_process';
|
2026-07-18 18:03:56 +03:00
|
|
|
this.levels = levels;
|
2026-07-18 17:47:33 +03:00
|
|
|
this.currentLevel = 0;
|
|
|
|
|
this.maxLevel = levels.length - 1;
|
2026-07-18 13:01:43 +03:00
|
|
|
|
2026-07-19 11:24:37 +03:00
|
|
|
this.paddle = new Paddle(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
|
2026-07-19 13:15:50 +03:00
|
|
|
this.ball = new Ball(0, 0, BALL_RADIUS, BALL_SPEED, BALL_INITIAL_ANGLE);
|
2026-07-18 18:03:56 +03:00
|
|
|
this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT);
|
2026-07-19 11:24:37 +03:00
|
|
|
|
|
|
|
|
this._placeBallOnPaddle();
|
2026-07-17 11:06:27 +03:00
|
|
|
}
|
2026-07-17 12:32:47 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Изменяет значения сущностей в зависимости от времени
|
|
|
|
|
* @param {*} deltaTime изменение времени из Ticker
|
|
|
|
|
*/
|
|
|
|
|
update(deltaTime) {
|
2026-07-18 13:01:43 +03:00
|
|
|
if (this.status !== 'in_process') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-07-18 18:03:56 +03:00
|
|
|
|
2026-07-19 11:24:37 +03:00
|
|
|
// Пока мяч не запущен - держим его на ракетке и не считаем физику
|
|
|
|
|
if (this.ball.status === 'idle') {
|
|
|
|
|
this._placeBallOnPaddle();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 12:32:47 +03:00
|
|
|
tick(this, CONTAINER_WIDTH, CONTAINER_HEIGHT, deltaTime);
|
2026-07-18 13:01:43 +03:00
|
|
|
|
2026-07-19 11:24:37 +03:00
|
|
|
if (this.ball.status === 'out') {
|
2026-07-18 13:01:43 +03:00
|
|
|
this.livesAmount -= 1;
|
|
|
|
|
|
|
|
|
|
if (this.livesAmount === 0) {
|
|
|
|
|
this.status = 'over';
|
|
|
|
|
} else {
|
2026-07-19 11:24:37 +03:00
|
|
|
this._placeBallOnPaddle();
|
2026-07-18 13:01:43 +03:00
|
|
|
}
|
2026-07-18 18:03:56 +03:00
|
|
|
|
|
|
|
|
return;
|
2026-07-18 13:01:43 +03:00
|
|
|
}
|
2026-07-18 13:14:47 +03:00
|
|
|
|
2026-07-19 13:20:10 +03:00
|
|
|
this.ball.increaseSpeed(BALL_SPEED_TIME_STEP * deltaTime);
|
|
|
|
|
|
2026-07-19 10:51:58 +03:00
|
|
|
const isLevelComplete = this._checkLevelCompletion();
|
2026-07-18 13:14:47 +03:00
|
|
|
|
2026-07-19 10:51:58 +03:00
|
|
|
if (isLevelComplete) {
|
2026-07-18 18:03:56 +03:00
|
|
|
this.currentLevel += 1;
|
|
|
|
|
|
|
|
|
|
if (this.currentLevel <= this.maxLevel) {
|
|
|
|
|
this._proceedToNextLevel();
|
|
|
|
|
} else {
|
|
|
|
|
this.status = 'completed';
|
|
|
|
|
}
|
2026-07-18 13:14:47 +03:00
|
|
|
}
|
2026-07-17 12:32:47 +03:00
|
|
|
}
|
2026-07-18 18:03:56 +03:00
|
|
|
|
2026-07-19 10:51:58 +03:00
|
|
|
/**
|
|
|
|
|
* Проверяет завершен ли текущий уровень
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
_checkLevelCompletion() {
|
|
|
|
|
return this.bricks.every((brick) => brick.type === 3 || !brick.alive);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 18:03:56 +03:00
|
|
|
/**
|
|
|
|
|
* Запускает переход на новый уровень, возвращает мяч в дефоотное положение и отрисовывает кирпичи по карте уровня.
|
|
|
|
|
*/
|
|
|
|
|
_proceedToNextLevel() {
|
2026-07-19 13:20:10 +03:00
|
|
|
this.ball.increaseSpeed(BALL_SPEED_LEVEL_STEP);
|
2026-07-19 11:24:37 +03:00
|
|
|
this._placeBallOnPaddle();
|
2026-07-18 18:03:56 +03:00
|
|
|
this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT);
|
|
|
|
|
}
|
2026-07-19 11:24:37 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ставит мяч по центру ракетки
|
|
|
|
|
*/
|
|
|
|
|
_placeBallOnPaddle() {
|
|
|
|
|
this.ball.reset(this.paddle.x + this.paddle.width / 2, this.paddle.y - this.ball.radius);
|
|
|
|
|
}
|
2026-07-17 11:06:27 +03:00
|
|
|
}
|