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
4 changed files with 40 additions and 12 deletions
Showing only changes of commit 0f057c1add - Show all commits
+14 -5
View File
@@ -16,7 +16,7 @@ export class Ball {
this.radius = radius; this.radius = radius;
this.verticalSpeed = verticalSpeed; this.verticalSpeed = verticalSpeed;
this.horizontalSpeed = horizontalSpeed; this.horizontalSpeed = horizontalSpeed;
this.isOut = false; this.status = 'idle';
this.defaultX = x; this.defaultX = x;
this.defaultY = y; this.defaultY = y;
@@ -36,11 +36,20 @@ export class Ball {
/** /**
* Возвращает значения к дефолтным * Возвращает значения к дефолтным
*/ */
reset() { reset(x = this.defaultX, y = this.defaultY) {
this.x = this.defaultX; this.x = x;
this.y = this.defaultY; this.y = y;
this.verticalSpeed = this.defaultVerticalSpeed; this.verticalSpeed = this.defaultVerticalSpeed;
this.horizontalSpeed = this.defaultHorizontalSpeed; this.horizontalSpeed = this.defaultHorizontalSpeed;
this.isOut = false; this.status = 'idle';
}
/**
* Запускает мяч с ракетки - переводит из ожидания в игру
*/
launch() {
if (this.status === 'idle') {
this.status = 'moving';
}
} }
} }
+21 -6
View File
@@ -29,15 +29,17 @@ export class Game {
this.currentLevel = 0; this.currentLevel = 0;
this.maxLevel = levels.length - 1; this.maxLevel = levels.length - 1;
this.paddle = new Paddle(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
this.ball = new Ball( this.ball = new Ball(
CONTAINER_WIDTH / 2 - BALL_RADIUS / 2, 0,
CONTAINER_HEIGHT / 2 - BALL_RADIUS / 2, 0,
BALL_RADIUS, BALL_RADIUS,
BALL_SPEED * Math.cos(toRadians(BALL_INITIAL_ANGLE)), BALL_SPEED * Math.cos(toRadians(BALL_INITIAL_ANGLE)),
-1 * BALL_SPEED * Math.sin(toRadians(BALL_INITIAL_ANGLE)), -1 * BALL_SPEED * Math.sin(toRadians(BALL_INITIAL_ANGLE)),
); );
this.paddle = new Paddle(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT); this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT);
this._placeBallOnPaddle();
} }
/** /**
@@ -49,15 +51,21 @@ export class Game {
return; return;
} }
// Пока мяч не запущен - держим его на ракетке и не считаем физику
if (this.ball.status === 'idle') {
this._placeBallOnPaddle();
return;
}
tick(this, CONTAINER_WIDTH, CONTAINER_HEIGHT, deltaTime); tick(this, CONTAINER_WIDTH, CONTAINER_HEIGHT, deltaTime);
if (this.ball.isOut) { if (this.ball.status === 'out') {
this.livesAmount -= 1; this.livesAmount -= 1;
if (this.livesAmount === 0) { if (this.livesAmount === 0) {
this.status = 'over'; this.status = 'over';
} else { } else {
this.ball.reset(); this._placeBallOnPaddle();
} }
return; return;
@@ -88,7 +96,14 @@ export class Game {
* Запускает переход на новый уровень, возвращает мяч в дефоотное положение и отрисовывает кирпичи по карте уровня. * Запускает переход на новый уровень, возвращает мяч в дефоотное положение и отрисовывает кирпичи по карте уровня.
*/ */
_proceedToNextLevel() { _proceedToNextLevel() {
this.ball.reset(); this._placeBallOnPaddle();
this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT); this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT);
} }
/**
* Ставит мяч по центру ракетки
*/
_placeBallOnPaddle() {
this.ball.reset(this.paddle.x + this.paddle.width / 2, this.paddle.y - this.ball.radius);
}
} }
+1 -1
View File
@@ -61,7 +61,7 @@ export function tick(game, containerWidth, containerHeight, deltaTime) {
// Проверяем выход за границу стены снизу // Проверяем выход за границу стены снизу
if (ball.y >= bottomBoundary) { if (ball.y >= bottomBoundary) {
game.ball.isOut = true; game.ball.status = 'out';
return; return;
} }
+4
View File
@@ -49,6 +49,10 @@ import { createGameView, rebuildBrickViews, syncronizeViewsWithGame } from './vi
game.paddle.moveTo(localPosition.x, 0, CONTAINER_WIDTH); game.paddle.moveTo(localPosition.x, 0, CONTAINER_WIDTH);
}); });
container.on('pointerdown', () => {
game.ball.launch();
});
app.ticker.add((time) => { app.ticker.add((time) => {
game.update(time.deltaTime); game.update(time.deltaTime);