feat: Добавлена функцияя для пересборки отображений кирпичей и удаления оставшихся отображений кирпичей из контейнера при смене уровня
This commit is contained in:
+6
-4
@@ -17,7 +17,7 @@ import { LEVELS } from './const/levels';
|
||||
import { Game } from './game';
|
||||
import { calculateCollision } from './lib/calculateCollision/calculateCollision';
|
||||
import { calculateDirection } from './lib/calculateDirection/calculateDirection';
|
||||
import { createGameView, syncronizeViewsWithGame } from './view';
|
||||
import { createGameView, rebuildBrickViews, syncronizeViewsWithGame } from './view';
|
||||
|
||||
(async () => {
|
||||
// Create a new application
|
||||
@@ -42,17 +42,19 @@ import { createGameView, syncronizeViewsWithGame } from './view';
|
||||
|
||||
const game = new Game(LEVELS);
|
||||
const views = createGameView(game, container);
|
||||
const currentLevel = game.currentLevel;
|
||||
|
||||
container.on('pointermove', (event) => {
|
||||
const localPosition = container.toLocal(event.global);
|
||||
game.paddle.moveTo(localPosition.x, 0, CONTAINER_WIDTH);
|
||||
});
|
||||
|
||||
console.log(game.paddle.x, game.paddle.y);
|
||||
|
||||
app.ticker.add((time) => {
|
||||
game.update(time.deltaTime);
|
||||
|
||||
if (game.currentLevel !== currentLevel) {
|
||||
rebuildBrickViews(views, game, container);
|
||||
}
|
||||
syncronizeViewsWithGame(views, game);
|
||||
console.log(game.paddle.x, game.paddle.y);
|
||||
});
|
||||
})();
|
||||
|
||||
+20
-1
@@ -42,7 +42,7 @@ function createBrickView(brick) {
|
||||
/**
|
||||
* Создает визуальное отображение всех сущностей в игре с помощью Pixi.js и добавляет в контейнер
|
||||
* @param {Game} game экземпляр класса игра
|
||||
* @param {Container} container контейнер Pixi.js
|
||||
* @param {Container} container экземпляр класса контейнер Pixi.js
|
||||
*/
|
||||
export function createGameView(game, container) {
|
||||
try {
|
||||
@@ -97,3 +97,22 @@ export function syncronizeViewsWithGame(views, game) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Пересоздает отображения кирпичей под текущий уровень игры.
|
||||
* @param {object} views объект с визуальными отображениями сущностей игры
|
||||
* @param {Game} game экземпляр класса игра
|
||||
* @param {Container} container экземпляр класса контейнер Pixi.js
|
||||
*/
|
||||
export function rebuildBrickViews(views, game, container) {
|
||||
for (const brickView of views.bricks) {
|
||||
container.removeChild(brickView);
|
||||
brickView.destroy();
|
||||
}
|
||||
|
||||
views.bricks = game.bricks.map((brick) => {
|
||||
const brickView = createBrickView(brick);
|
||||
container.addChild(brickView);
|
||||
return brickView;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user