From a062ade3c75678617a8ddce29090947672c51b80 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Sun, 27 Apr 2025 19:08:03 +0700 Subject: [PATCH] fix: close poup after replay --- src/game/levels/LevelManager.js | 15 ++++++++------- src/game/objects/Player.js | 5 +++-- src/game/scenes/Game.js | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/game/levels/LevelManager.js b/src/game/levels/LevelManager.js index 08a08df..21bea28 100644 --- a/src/game/levels/LevelManager.js +++ b/src/game/levels/LevelManager.js @@ -20,7 +20,7 @@ export class LevelManager { // Cập nhật UI this.scene.levelText.setText(`Level: ${levelId}`); - + // Xóa các trạm gác hiện tại if (this.scene.guards) { this.scene.guards.forEach(guard => { @@ -36,17 +36,17 @@ export class LevelManager { if (gridSize !== this.scene.gridSize) { this.scene.gridSize = gridSize; this.scene.grid.resize(gridSize, gridSize); - + // Tính toán lại vị trí lưới để căn giữa const width = this.scene.cameras.main.width; const height = this.scene.cameras.main.height; this.scene.gridOffsetX = (width - (gridSize * this.scene.cellSize)) / 2; this.scene.gridOffsetY = (height - (gridSize * this.scene.cellSize)) / 2; - + // Cập nhật vị trí của graphics this.scene.grid.graphics.x = this.scene.gridOffsetX; this.scene.grid.graphics.y = this.scene.gridOffsetY; - + if (this.scene.lightSystem) { this.scene.lightSystem.graphics.x = this.scene.gridOffsetX; this.scene.lightSystem.graphics.y = this.scene.gridOffsetY; @@ -76,7 +76,8 @@ export class LevelManager { // Thiết lập vị trí người chơi if (levelData.player) { - this.scene.player.moveTo(levelData.player.row, levelData.player.col); + // Sử dụng skipDetectionCheck = true để tránh kiểm tra phát hiện khi tải lại màn chơi + this.scene.player.moveTo(levelData.player.row, levelData.player.col, true); } // Thiết lập các ô sáng cố định (nếu có) @@ -90,7 +91,7 @@ export class LevelManager { if (levelData.guards) { levelData.guards.forEach(guardData => { let guard = null; - + switch (guardData.type) { case 'static': guard = new this.scene.StaticGuard( @@ -130,7 +131,7 @@ export class LevelManager { ); break; } - + if (guard) { this.scene.guards.push(guard); } diff --git a/src/game/objects/Player.js b/src/game/objects/Player.js index ec7b486..1e93779 100644 --- a/src/game/objects/Player.js +++ b/src/game/objects/Player.js @@ -24,7 +24,7 @@ export class Player { } // Di chuyển đến vị trí mới - moveTo(row, col) { + moveTo(row, col, skipDetectionCheck = false) { // Kiểm tra vị trí hợp lệ và không phải là tường if (!this.grid.isValidPosition(row, col) || this.grid.isWall(row, col)) { return false; @@ -45,7 +45,8 @@ export class Player { ease: 'Linear', onComplete: () => { // Kiểm tra xem người chơi có đang ở ô sáng không sau khi di chuyển - if (this.isInLitCell()) { + // Bỏ qua kiểm tra nếu skipDetectionCheck = true (khi đang tải lại màn chơi) + if (!skipDetectionCheck && this.isInLitCell()) { // Người chơi bị phát hiện this.scene.showDetectionPopup(); } diff --git a/src/game/scenes/Game.js b/src/game/scenes/Game.js index 1818559..522b00c 100644 --- a/src/game/scenes/Game.js +++ b/src/game/scenes/Game.js @@ -376,6 +376,11 @@ export class Game extends Phaser.Scene { // Hiển thị popup khi người chơi bị phát hiện showDetectionPopup() { + // Kiểm tra xem popup đã tồn tại chưa, nếu có thì không tạo mới + if (this.detectionPopup) { + return; // Đã có popup, không tạo thêm + } + const width = this.cameras.main.width; const height = this.cameras.main.height; @@ -386,9 +391,11 @@ export class Game extends Phaser.Scene { // Tạo container cho popup this.detectionPopup = this.add.container(width / 2, height / 2); this.detectionPopup.setDepth(100); + this.detectionPopup.popup = 'detection'; // Đánh dấu container // Background const bg = this.add.rectangle(0, 0, 300, 200, 0x000000, 0.8); + bg.popup = 'detection'; // Đánh dấu phần tử thuộc về popup // Thông báo const title = this.add.text(0, -60, 'Bạn đã bị phát hiện!', { @@ -396,14 +403,18 @@ export class Game extends Phaser.Scene { fill: '#FF0000' }); title.setOrigin(0.5, 0.5); + title.popup = 'detection'; // Đánh dấu phần tử thuộc về popup // Nút chơi lại const restartButton = this.add.rectangle(0, 20, 200, 40, 0x444444); + restartButton.popup = 'detection'; // Đánh dấu phần tử thuộc về popup + const restartText = this.add.text(0, 20, 'CHƠI LẠI', { font: '20px Arial', fill: '#ffffff' }); restartText.setOrigin(0.5, 0.5); + restartText.popup = 'detection'; // Đánh dấu phần tử thuộc về popup // Thêm các phần tử vào container this.detectionPopup.add([bg, title, restartButton, restartText]); @@ -421,10 +432,19 @@ export class Game extends Phaser.Scene { // Đóng popup phát hiện closeDetectionPopup() { if (this.detectionPopup) { + // Đảm bảo tất cả các phần tử con cũng được hủy + this.detectionPopup.removeAll(true); this.detectionPopup.destroy(); this.detectionPopup = null; this.isPaused = false; this.inputEnabled = true; + + // Đảm bảo rằng tất cả các phần tử liên quan đến popup đã được xóa + this.children.list.forEach(child => { + if (child.popup && child.popup === 'detection') { + child.destroy(); + } + }); } } @@ -437,6 +457,11 @@ export class Game extends Phaser.Scene { this.livesText.setText(`Lives: ${this.livesRemaining}`); } + // Đảm bảo tất cả các popup đã được đóng + if (this.detectionPopup) { + this.closeDetectionPopup(); + } + if (this.livesRemaining <= 0) { // Hết mạng, game over this.scene.start('GameOver', {