Certainly! Let's address the TODO by creating the OutGhostHouseScene as specified. We'll ensure that the ghost's state (position, texture, etc.) is preserved across scenes using Phaser's Registry, which is a global storage system ideal for sharing data between scenes.
Overview of Steps:
-
Update the PreloadScene to Ensure All Necessary Assets Are Loaded: Ensure that ghostHouseOutside is loaded, which it already is based on your provided code.
-
Modify Existing Scenes to Store Ghost State in the Registry:
- When the ghost's state changes (e.g., position, texture), update the Registry.
- When a scene is created, retrieve the ghost's state from the Registry to maintain continuity.
-
Create the OutGhostHouseScene:
- Display the exterior background.
- Use the same ghost model and controls as in
InGhostHouseScene.
- Allow transitioning back to
InGhostHouseScene, preserving the ghost's state.
-
Update the Game Configuration to Include the New Scene.
Let's implement each step in detail.
1. Updating the PreloadScene
Your PreloadScene already loads the ghostHouseOutside image:
this.load.image('ghostHouseOutside', 'assets/images/ghost-house-outside.jpg');
If you need any additional assets (like an exit door in the exterior scene), ensure they're loaded here.
2. Modifying Existing Scenes to Use the Registry
We'll use Phaser's Registry to store and retrieve the ghost's state. This ensures that when switching between scenes, the ghost retains its properties.
a. Storing Ghost State
In the InGhostHouseScene, whenever the ghost's state changes (position, texture, etc.), we should update the Registry.
Update InGhostHouseScene:
class InGhostHouseScene extends Phaser.Scene {
constructor() {
super({ key: 'InGhostHouseScene' });
}
create() {
// Retrieve ghost state from the Registry or set defaults
const ghostState = this.registry.get('ghostState') || {
x: 250,
y: 300,
angle: -90,
scale: 0.2,
texture: 'ghost',
weaponPicked: false,
isAwake: false
};
// Background: Ghost House
this.add.image(320, 240, 'ghostHouse').setDisplaySize(640, 480);
// Weapon setup
this.weapon = this.add.image(350, 325, 'weapon').setOrigin(0.5, 0.5);
this.weapon.setScale(0.2);
this.weapon.angle = 85;
this.weaponPicked = ghostState.weaponPicked;
// Ghost character setup in bed
this.ghost = this.add.image(ghostState.x, ghostState.y, ghostState.texture).setOrigin(0.5, 0.5);
this.ghost.setScale(ghostState.scale);
this.ghost.angle = ghostState.angle;
// Set awake state
this.isAwake = ghostState.isAwake;
// Wake-up button setup
this.wakeUpButton = this.add.text(320, 450, 'Wake Up', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(!this.isAwake)
.on('pointerdown', () => this.wakeUpGhost());
// "Go Back to Sleep" button setup
this.sleepButton = this.add.text(320, 450, 'Go Back to Sleep', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake)
.on('pointerdown', () => this.putGhostToSleep());
// Weapon Buttons
this.pickWeaponButton = this.add.text(320, 450, 'Pick Weapon', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake && !this.weaponPicked)
.on('pointerdown', () => this.pickWeapon());
this.dropWeaponButton = this.add.text(320, 450, 'Drop Weapon', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake && this.weaponPicked)
.on('pointerdown', () => this.dropWeapon());
// Open Door Button
this.openDoorButton = this.add.text(320, 450, 'Open Door', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake)
.on('pointerdown', () => this.openDoor());
// Keyboard controls for movement
this.cursors = this.input.keyboard.createCursorKeys();
// Left and Right buttons for touch input
this.leftButton = this.add.image(100, 400, 'leftButton').setInteractive();
this.rightButton = this.add.image(540, 400, 'rightButton').setInteractive();
// Scale and transparency for the buttons
this.leftButton.setScale(0.5).setAlpha(0.7);
this.rightButton.setScale(0.5).setAlpha(0.7);
// Button down events
this.leftButton.on('pointerdown', () => {
this.isMovingLeft = true;
});
this.rightButton.on('pointerdown', () => {
this.isMovingRight = true;
});
// Button up events (stop movement when button is released)
this.leftButton.on('pointerup', () => {
this.isMovingLeft = false;
});
this.rightButton.on('pointerup', () => {
this.isMovingRight = false;
});
// Track movement
this.isMovingLeft = false;
this.isMovingRight = false;
}
update() {
// If the ghost is awake, enable movement
if (this.isAwake) {
// Check keyboard input
if ((this.cursors.left.isDown || this.isMovingLeft) && this.ghost.x > 0) {
this.ghost.x -= 2; // Move left
} else if ((this.cursors.right.isDown || this.isMovingRight) && this.ghost.x < this.scale.width) {
this.ghost.x += 2; // Move right
}
// Check if the ghost is near the bed position to show the sleep button
if (this.ghost.x > 170 && this.ghost.x < 270) {
this.sleepButton.setVisible(true);
} else {
this.sleepButton.setVisible(false);
}
if (!this.weaponPicked) {
// Check if the ghost is near the weapon table position to show the pick weapon button
if (this.ghost.x > 300 && this.ghost.x < 400) {
this.pickWeaponButton.setVisible(true);
} else {
this.pickWeaponButton.setVisible(false);
}
} else {
// Check if the ghost is near the weapon table position to show the drop weapon button
if (this.ghost.x > 300 && this.ghost.x < 400) {
this.dropWeaponButton.setVisible(true);
} else {
this.dropWeaponButton.setVisible(false);
}
}
// Check if the ghost is near the door position to show the open door button
if (this.ghost.x > 430 && this.ghost.x < 530) {
this.openDoorButton.setVisible(true);
} else {
this.openDoorButton.setVisible(false);
}
// Update Registry with current ghost state
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX, // Assuming uniform scale
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
}
wakeUpGhost() {
if (!this.isAwake) {
// Move ghost to bottom of screen (floor level)
this.ghost.x = 260; // Center horizontally
this.ghost.y = 340; // Bottom near floor
this.ghost.angle = 0; // Set upright position
this.ghost.setScale(0.25);
this.isAwake = true; // Mark ghost as awake
this.wakeUpButton.setVisible(false); // Hide wake-up button
this.sleepButton.setVisible(true); // Show sleep button
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
}
putGhostToSleep() {
// Reset ghost position to bed and put it to sleep
this.ghost.x = 250; // Bed's x position
this.ghost.y = 300; // Bed's y position
this.ghost.angle = -90; // Lie down position
this.ghost.setScale(0.2); // Original size
this.isAwake = false; // Mark ghost as asleep
this.wakeUpButton.setVisible(true); // Show wake-up button again
this.sleepButton.setVisible(false); // Hide sleep button
// Reset ghost texture and weapon if weapon was picked up
if (this.weaponPicked) {
this.ghost.setTexture('ghost'); // Change back to original ghost texture
this.weapon.setVisible(true); // Make weapon visible again
this.weaponPicked = false; // Reset weapon picked state
}
// Hide weapon buttons
this.pickWeaponButton.setVisible(false);
this.dropWeaponButton.setVisible(false);
this.openDoorButton.setVisible(false);
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
pickWeapon() {
// Check if the weapon is already picked up
if (this.weaponPicked) return;
// Change ghost's image to "the-ghost"
this.ghost.setTexture('the-ghost');
// Hide weapon and pickWeaponButton, show dropWeaponButton
this.weapon.setVisible(false);
this.pickWeaponButton.setVisible(false);
this.dropWeaponButton.setVisible(true);
// Set weapon picked state to true
this.weaponPicked = true;
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
dropWeapon() {
// Check if the weapon is already dropped
if (!this.weaponPicked) return;
// Change ghost's image back to "ghost"
this.ghost.setTexture('ghost');
// Show weapon and pickWeaponButton, hide dropWeaponButton
this.weapon.setVisible(true);
this.pickWeaponButton.setVisible(true);
this.dropWeaponButton.setVisible(false);
// Set weapon picked state to false
this.weaponPicked = false;
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
openDoor() {
// Switch to OutGhostHouseScene
this.scene.start('OutGhostHouseScene');
}
}
b. Explanation:
-
Registry Usage: At the beginning of the create method, we retrieve the ghost's state from the Registry. If it doesn't exist (e.g., the first time), we set default values.
-
State Updates: In the update, wakeUpGhost, putGhostToSleep, pickWeapon, and dropWeapon methods, we update the Registry with the current state of the ghost. This ensures that any scene can access and modify the ghost's state as needed.
3. Creating the OutGhostHouseScene
Now, let's define the OutGhostHouseScene following similar patterns to InGhostHouseScene.
// Out Ghost House Scene: Shows the exterior and allows user interaction with the ghost
class OutGhostHouseScene extends Phaser.Scene {
constructor() {
super({ key: 'OutGhostHouseScene' });
}
create() {
// Retrieve ghost state from the Registry or set defaults
const ghostState = this.registry.get('ghostState') || {
x: 320,
y: 240,
angle: 0,
scale: 0.2,
texture: 'ghost',
weaponPicked: false,
isAwake: false
};
// Background: Exterior of Ghost House
this.add.image(320, 240, 'ghostHouseOutside').setDisplaySize(640, 480);
// Ghost character setup
this.ghost = this.add.image(ghostState.x, ghostState.y, ghostState.texture).setOrigin(0.5, 0.5);
this.ghost.setScale(ghostState.scale);
this.ghost.angle = ghostState.angle;
// Exit Door Button
this.exitDoorButton = this.add.text(320, 450, 'Exit House', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.on('pointerdown', () => this.exitHouse());
// Keyboard controls for movement
this.cursors = this.input.keyboard.createCursorKeys();
// Left and Right buttons for touch input
this.leftButton = this.add.image(100, 400, 'leftButton').setInteractive();
this.rightButton = this.add.image(540, 400, 'rightButton').setInteractive();
// Scale and transparency for the buttons
this.leftButton.setScale(0.5).setAlpha(0.7);
this.rightButton.setScale(0.5).setAlpha(0.7);
// Button down events
this.leftButton.on('pointerdown', () => {
this.isMovingLeft = true;
});
this.rightButton.on('pointerdown', () => {
this.isMovingRight = true;
});
// Button up events (stop movement when button is released)
this.leftButton.on('pointerup', () => {
this.isMovingLeft = false;
});
this.rightButton.on('pointerup', () => {
this.isMovingRight = false;
});
// Track movement
this.isMovingLeft = false;
this.isMovingRight = false;
}
update() {
// If the ghost is awake, enable movement
if (this.registry.get('ghostState').isAwake) {
// Check keyboard input
if ((this.cursors.left.isDown || this.isMovingLeft) && this.ghost.x > 0) {
this.ghost.x -= 2; // Move left
} else if ((this.cursors.right.isDown || this.isMovingRight) && this.ghost.x < this.scale.width) {
this.ghost.x += 2; // Move right
}
// Optional: Implement any scene-specific interactions here
// Update Registry with current ghost state
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.registry.get('ghostState').weaponPicked,
isAwake: this.registry.get('ghostState').isAwake
});
}
}
exitHouse() {
// Switch back to InGhostHouseScene
this.scene.start('InGhostHouseScene');
}
}
Explanation:
-
Background: Displays the exterior of the ghost house using ghostHouseOutside.
-
Ghost Setup: Retrieves the ghost's state from the Registry to maintain continuity.
-
Controls: Similar to InGhostHouseScene, includes keyboard and touch controls for movement.
-
Exit Door: Provides a button to return to the InGhostHouseScene.
-
Registry Updates: Ensures that any movement or state changes are updated in the Registry.
4. Updating the Game Configuration
Finally, include the new OutGhostHouseScene in the game configuration.
// Configuration for the Phaser game
const config = {
type: Phaser.AUTO,
width: 640,
height: 480,
parent: 'game-container',
scene: [PreloadScene, MainMenuScene, InGhostHouseScene, OutGhostHouseScene],
audio: {
disableWebAudio: false
}
};
5. Full Updated Code
For clarity, here's the complete updated code integrating all the changes:
// Preload Scene: Loads all assets and shows loading bar
class PreloadScene extends Phaser.Scene {
constructor() {
super({ key: 'PreloadScene' });
}
preload() {
// Add loading bar background
const loadingBarBg = this.add.graphics();
loadingBarBg.fillStyle(0x222222, 1);
loadingBarBg.fillRect(200, 220, 240, 50);
// Add loading bar
const loadingBar = this.add.graphics();
// Display loading text
const loadingText = this.add.text(320, 200, 'Loading...', { fontSize: '20px', fill: '#ffffff' });
loadingText.setOrigin(0.5, 0.5);
// Update loading bar as assets are loaded
this.load.on('progress', (value) => {
loadingBar.clear();
loadingBar.fillStyle(0xffffff, 1);
loadingBar.fillRect(200, 220, 240 * value, 50);
});
// Remove loading bar once complete
this.load.on('complete', () => {
loadingBar.destroy();
loadingBarBg.destroy();
loadingText.destroy();
});
const leftButtonDataURI = 'data:image/svg+xml;base64,' + btoa(`
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">
<rect x="5" y="5" width="90" height="90" fill="#333333" stroke="#ffffff" stroke-width="5"/>
<polygon points="60,30 40,50 60,70" fill="#ffffff"/>
</svg>
`);
const rightButtonDataURI = 'data:image/svg+xml;base64,' + btoa(`
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">
<rect x="5" y="5" width="90" height="90" fill="#333333" stroke="#ffffff" stroke-width="5"/>
<polygon points="40,30 60,50 40,70" fill="#ffffff"/>
</svg>
`);
// Load assets
this.load.image('mainMenu', 'assets/images/main-menu.png');
this.load.image('playButton', 'assets/images/play-button.png');
this.load.image('ghostHouse', 'assets/images/ghost-house.jpg');
this.load.image('ghostHouseOutside', 'assets/images/ghost-house-outside.jpg');
this.load.image('ghost', 'assets/images/ghost.webp');
this.load.image('the-ghost', 'assets/images/the-ghost.png');
this.load.image('weapon', 'assets/images/weapon.png');
this.load.audio('bgMusic', ['assets/audio/background-music.wav']);
this.load.image('leftButton', leftButtonDataURI);
this.load.image('rightButton', rightButtonDataURI);
}
create() {
// Start Main Menu Scene after loading
this.scene.start('MainMenuScene');
}
}
// Main Menu Scene: Displays main menu with play button and background music
class MainMenuScene extends Phaser.Scene {
constructor() {
super({ key: 'MainMenuScene' });
}
create() {
// Add main menu background image
this.add.image(320, 240, 'mainMenu').setDisplaySize(640, 480); // Centered
// Create a drop shadow for the play button
const shadow = this.add.graphics();
shadow.fillStyle(0x000000, 0.5); // Black color with some transparency
shadow.fillEllipse(320, 430, 200, 60); // Draw shadow ellipse (x, y, width, height)
// Add play button
const playButton = this.add.image(320, 390, 'playButton').setInteractive();
// Scale play button to make it much smaller
playButton.setScale(0.25); // Adjust the scale for a smaller button
// Add click listener to play button
playButton.on('pointerdown', () => {
console.log('Play button clicked!');
this.scene.start('InGhostHouseScene'); // Start In Ghost House Scene on button click
});
// Add background music
this.bgMusic = this.sound.add('bgMusic', { loop: true, volume: 0.5 });
this.bgMusic.play();
}
// Optional: Stop music when scene is shutdown
shutdown() {
if (this.bgMusic) {
this.bgMusic.stop();
}
}
}
// In Ghost House Scene: Shows the ghost's house and allows user interaction with the ghost
class InGhostHouseScene extends Phaser.Scene {
constructor() {
super({ key: 'InGhostHouseScene' });
}
create() {
// Retrieve ghost state from the Registry or set defaults
const ghostState = this.registry.get('ghostState') || {
x: 250,
y: 300,
angle: -90,
scale: 0.2,
texture: 'ghost',
weaponPicked: false,
isAwake: false
};
// Background: Ghost House
this.add.image(320, 240, 'ghostHouse').setDisplaySize(640, 480);
// Weapon setup
this.weapon = this.add.image(350, 325, 'weapon').setOrigin(0.5, 0.5);
this.weapon.setScale(0.2);
this.weapon.angle = 85;
this.weaponPicked = ghostState.weaponPicked;
// Ghost character setup in bed
this.ghost = this.add.image(ghostState.x, ghostState.y, ghostState.texture).setOrigin(0.5, 0.5);
this.ghost.setScale(ghostState.scale);
this.ghost.angle = ghostState.angle;
// Set awake state
this.isAwake = ghostState.isAwake;
// Wake-up button setup
this.wakeUpButton = this.add.text(320, 450, 'Wake Up', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(!this.isAwake)
.on('pointerdown', () => this.wakeUpGhost());
// "Go Back to Sleep" button setup
this.sleepButton = this.add.text(320, 450, 'Go Back to Sleep', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake)
.on('pointerdown', () => this.putGhostToSleep());
// Weapon Buttons
this.pickWeaponButton = this.add.text(320, 450, 'Pick Weapon', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake && !this.weaponPicked)
.on('pointerdown', () => this.pickWeapon());
this.dropWeaponButton = this.add.text(320, 450, 'Drop Weapon', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake && this.weaponPicked)
.on('pointerdown', () => this.dropWeapon());
// Open Door Button
this.openDoorButton = this.add.text(320, 450, 'Open Door', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.setVisible(this.isAwake)
.on('pointerdown', () => this.openDoor());
// Keyboard controls for movement
this.cursors = this.input.keyboard.createCursorKeys();
// Left and Right buttons for touch input
this.leftButton = this.add.image(100, 400, 'leftButton').setInteractive();
this.rightButton = this.add.image(540, 400, 'rightButton').setInteractive();
// Scale and transparency for the buttons
this.leftButton.setScale(0.5).setAlpha(0.7);
this.rightButton.setScale(0.5).setAlpha(0.7);
// Button down events
this.leftButton.on('pointerdown', () => {
this.isMovingLeft = true;
});
this.rightButton.on('pointerdown', () => {
this.isMovingRight = true;
});
// Button up events (stop movement when button is released)
this.leftButton.on('pointerup', () => {
this.isMovingLeft = false;
});
this.rightButton.on('pointerup', () => {
this.isMovingRight = false;
});
// Track movement
this.isMovingLeft = false;
this.isMovingRight = false;
}
update() {
// If the ghost is awake, enable movement
if (this.isAwake) {
// Check keyboard input
if ((this.cursors.left.isDown || this.isMovingLeft) && this.ghost.x > 0) {
this.ghost.x -= 2; // Move left
} else if ((this.cursors.right.isDown || this.isMovingRight) && this.ghost.x < this.scale.width) {
this.ghost.x += 2; // Move right
}
// Check if the ghost is near the bed position to show the sleep button
if (this.ghost.x > 170 && this.ghost.x < 270) {
this.sleepButton.setVisible(true);
} else {
this.sleepButton.setVisible(false);
}
if (!this.weaponPicked) {
// Check if the ghost is near the weapon table position to show the pick weapon button
if (this.ghost.x > 300 && this.ghost.x < 400) {
this.pickWeaponButton.setVisible(true);
} else {
this.pickWeaponButton.setVisible(false);
}
} else {
// Check if the ghost is near the weapon table position to show the drop weapon button
if (this.ghost.x > 300 && this.ghost.x < 400) {
this.dropWeaponButton.setVisible(true);
} else {
this.dropWeaponButton.setVisible(false);
}
}
// Check if the ghost is near the door position to show the open door button
if (this.ghost.x > 430 && this.ghost.x < 530) {
this.openDoorButton.setVisible(true);
} else {
this.openDoorButton.setVisible(false);
}
// Update Registry with current ghost state
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX, // Assuming uniform scale
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
}
wakeUpGhost() {
if (!this.isAwake) {
// Move ghost to bottom of screen (floor level)
this.ghost.x = 260; // Center horizontally
this.ghost.y = 340; // Bottom near floor
this.ghost.angle = 0; // Set upright position
this.ghost.setScale(0.25);
this.isAwake = true; // Mark ghost as awake
this.wakeUpButton.setVisible(false); // Hide wake-up button
this.sleepButton.setVisible(true); // Show sleep button
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
}
putGhostToSleep() {
// Reset ghost position to bed and put it to sleep
this.ghost.x = 250; // Bed's x position
this.ghost.y = 300; // Bed's y position
this.ghost.angle = -90; // Lie down position
this.ghost.setScale(0.2); // Original size
this.isAwake = false; // Mark ghost as asleep
this.wakeUpButton.setVisible(true); // Show wake-up button again
this.sleepButton.setVisible(false); // Hide sleep button
// Reset ghost texture and weapon if weapon was picked up
if (this.weaponPicked) {
this.ghost.setTexture('ghost'); // Change back to original ghost texture
this.weapon.setVisible(true); // Make weapon visible again
this.weaponPicked = false; // Reset weapon picked state
}
// Hide weapon buttons
this.pickWeaponButton.setVisible(false);
this.dropWeaponButton.setVisible(false);
this.openDoorButton.setVisible(false);
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
pickWeapon() {
// Check if the weapon is already picked up
if (this.weaponPicked) return;
// Change ghost's image to "the-ghost"
this.ghost.setTexture('the-ghost');
// Hide weapon and pickWeaponButton, show dropWeaponButton
this.weapon.setVisible(false);
this.pickWeaponButton.setVisible(false);
this.dropWeaponButton.setVisible(true);
// Set weapon picked state to true
this.weaponPicked = true;
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
dropWeapon() {
// Check if the weapon is already dropped
if (!this.weaponPicked) return;
// Change ghost's image back to "ghost"
this.ghost.setTexture('ghost');
// Show weapon and pickWeaponButton, hide dropWeaponButton
this.weapon.setVisible(true);
this.pickWeaponButton.setVisible(true);
this.dropWeaponButton.setVisible(false);
// Set weapon picked state to false
this.weaponPicked = false;
// Update Registry
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.weaponPicked,
isAwake: this.isAwake
});
}
openDoor() {
// Switch to OutGhostHouseScene
this.scene.start('OutGhostHouseScene');
}
}
// Out Ghost House Scene: Shows the exterior and allows user interaction with the ghost
class OutGhostHouseScene extends Phaser.Scene {
constructor() {
super({ key: 'OutGhostHouseScene' });
}
create() {
// Retrieve ghost state from the Registry or set defaults
const ghostState = this.registry.get('ghostState') || {
x: 320,
y: 240,
angle: 0,
scale: 0.2,
texture: 'ghost',
weaponPicked: false,
isAwake: false
};
// Background: Exterior of Ghost House
this.add.image(320, 240, 'ghostHouseOutside').setDisplaySize(640, 480);
// Ghost character setup
this.ghost = this.add.image(ghostState.x, ghostState.y, ghostState.texture).setOrigin(0.5, 0.5);
this.ghost.setScale(ghostState.scale);
this.ghost.angle = ghostState.angle;
// Exit Door Button
this.exitDoorButton = this.add.text(320, 450, 'Exit House', { fontSize: '20px', fill: '#ffffff' })
.setOrigin(0.5)
.setInteractive()
.on('pointerdown', () => this.exitHouse());
// Keyboard controls for movement
this.cursors = this.input.keyboard.createCursorKeys();
// Left and Right buttons for touch input
this.leftButton = this.add.image(100, 400, 'leftButton').setInteractive();
this.rightButton = this.add.image(540, 400, 'rightButton').setInteractive();
// Scale and transparency for the buttons
this.leftButton.setScale(0.5).setAlpha(0.7);
this.rightButton.setScale(0.5).setAlpha(0.7);
// Button down events
this.leftButton.on('pointerdown', () => {
this.isMovingLeft = true;
});
this.rightButton.on('pointerdown', () => {
this.isMovingRight = true;
});
// Button up events (stop movement when button is released)
this.leftButton.on('pointerup', () => {
this.isMovingLeft = false;
});
this.rightButton.on('pointerup', () => {
this.isMovingRight = false;
});
// Track movement
this.isMovingLeft = false;
this.isMovingRight = false;
}
update() {
// If the ghost is awake, enable movement
if (this.registry.get('ghostState').isAwake) {
// Check keyboard input
if ((this.cursors.left.isDown || this.isMovingLeft) && this.ghost.x > 0) {
this.ghost.x -= 2; // Move left
} else if ((this.cursors.right.isDown || this.isMovingRight) && this.ghost.x < this.scale.width) {
this.ghost.x += 2; // Move right
}
// Optional: Implement any scene-specific interactions here
// Update Registry with current ghost state
this.registry.set('ghostState', {
x: this.ghost.x,
y: this.ghost.y,
angle: this.ghost.angle,
scale: this.ghost.scaleX,
texture: this.ghost.texture.key,
weaponPicked: this.registry.get('ghostState').weaponPicked,
isAwake: this.registry.get('ghostState').isAwake
});
}
}
exitHouse() {
// Switch back to InGhostHouseScene
this.scene.start('InGhostHouseScene');
}
}
// Configuration for the Phaser game
const config = {
type: Phaser.AUTO,
width: 640,
height: 480,
parent: 'game-container',
scene: [PreloadScene, MainMenuScene, InGhostHouseScene, OutGhostHouseScene],
audio: {
disableWebAudio: false
}
};
// Initialize Phaser game
const game = new Phaser.Game(config);
window.addEventListener('resize', () => {
const canvas = document.querySelector('canvas');
if (canvas) {
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const scale = Math.min(windowWidth / 640, windowHeight / 480);
canvas.style.transform = `scale(${scale})`;
canvas.style.transformOrigin = 'top left';
}
});
6. Additional Recommendations
-
Consistent Button Visibility: Ensure that buttons in both scenes reflect the ghost's state accurately. For example, in the OutGhostHouseScene, you might want additional interactive elements depending on your game design.
-
State Management Enhancements: Depending on future complexity, consider using more advanced state management techniques or patterns, especially if multiple entities share states.
-
Responsive Design: You've already added a resize listener. Ensure that UI elements scale appropriately on different screen sizes.
-
Sound Management: If you plan to have different sounds in the exterior scene, ensure you load and manage them similarly.
-
Scene Transitions: For smoother user experience, consider adding transitions or animations when switching scenes.
By following the above implementation, the OutGhostHouseScene will seamlessly integrate into your game, preserving the ghost's state and ensuring a consistent user experience across scenes.