Team Ai
Apppublic

broadfield-dev/stack-overflow-game

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
ModeSelectScene.js126 linesDownload Raw Back to scenes
1import Phaser from 'phaser';2import { GAME_WIDTH, GAME_HEIGHT, BORDER_OFFSET } from '../constants.js';3import SoundGenerator from '../utils/SoundGenerator.js';4 5export default class ModeSelectScene extends Phaser.Scene {6  constructor() {7    super({ key: 'ModeSelectScene' });8  }9 10  create() {11    // Use the title backdrop12    const titleImage = this.add.image(BORDER_OFFSET, 0, 'title');13    titleImage.setOrigin(0, 0);14    titleImage.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);15 16    // Dim the background by 50%17    const dimOverlay = this.add.rectangle(GAME_WIDTH / 2 + BORDER_OFFSET, GAME_HEIGHT / 2, GAME_WIDTH, GAME_HEIGHT, 0x000000, 0.5);18    dimOverlay.setDepth(5);19 20    // Title21    const titleText = this.add.bitmapText(GAME_WIDTH / 2 + BORDER_OFFSET, 60, 'pixel-font', 'MODE SELECT', 10).setOrigin(0.5);22    titleText.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);23    titleText.setDepth(10);24 25    // Classic mode option26    this.classicText = this.add.bitmapText(GAME_WIDTH / 2 + BORDER_OFFSET, 100, 'pixel-font', '> CLASSIC', 10).setOrigin(0.5);27    this.classicText.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);28    this.classicText.setDepth(10);29    this.classicText.setInteractive({ useHandCursor: true });30 31    const classicDesc = this.add.bitmapText(GAME_WIDTH / 2 + BORDER_OFFSET, 115, 'pixel-font', '7 STANDARD PIECES', 10).setOrigin(0.5);32    classicDesc.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);33    classicDesc.setDepth(10);34 35    // Advanced mode option36    this.advancedText = this.add.bitmapText(GAME_WIDTH / 2 + BORDER_OFFSET, 145, 'pixel-font', '  ADVANCED', 10).setOrigin(0.5);37    this.advancedText.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);38    this.advancedText.setDepth(10);39    this.advancedText.setInteractive({ useHandCursor: true });40 41    const advancedDesc = this.add.bitmapText(GAME_WIDTH / 2 + BORDER_OFFSET, 160, 'pixel-font', 'EXTRA UNIQUE PIECES', 10).setOrigin(0.5);42    advancedDesc.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);43    advancedDesc.setDepth(10);44 45    // Track selected mode46    this.selectedMode = 'classic';47 48    // Hover effects49    this.classicText.on('pointerover', () => {50      if (this.selectedMode !== 'classic') {51        SoundGenerator.playMove();52        this.selectedMode = 'classic';53        this.updateSelection();54      }55    });56 57    this.advancedText.on('pointerover', () => {58      if (this.selectedMode !== 'advanced') {59        SoundGenerator.playMove();60        this.selectedMode = 'advanced';61        this.updateSelection();62      }63    });64 65    // Click handlers66    this.classicText.on('pointerdown', () => {67      SoundGenerator.playRotate();68      this.startGame('classic');69    });70 71    this.advancedText.on('pointerdown', () => {72      SoundGenerator.playRotate();73      this.startGame('advanced');74    });75 76    // Keyboard controls77    const upKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);78    const downKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN);79    const spaceKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);80    const enterKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);81 82    upKey.on('down', () => {83      if (this.selectedMode !== 'classic') {84        SoundGenerator.playMove();85        this.selectedMode = 'classic';86        this.updateSelection();87      }88    });89 90    downKey.on('down', () => {91      if (this.selectedMode !== 'advanced') {92        SoundGenerator.playMove();93        this.selectedMode = 'advanced';94        this.updateSelection();95      }96    });97 98    spaceKey.on('down', () => {99      SoundGenerator.playRotate();100      this.startGame(this.selectedMode);101    });102 103    enterKey.on('down', () => {104      SoundGenerator.playRotate();105      this.startGame(this.selectedMode);106    });107  }108 109  updateSelection() {110    if (this.selectedMode === 'classic') {111      this.classicText.setText('> CLASSIC');112      this.advancedText.setText('  ADVANCED');113    } else {114      this.classicText.setText('  CLASSIC');115      this.advancedText.setText('> ADVANCED');116    }117  }118 119  startGame(mode) {120    // Store the selected mode in the registry so GameScene can access it121    this.registry.set('gameMode', mode);122    this.scene.start('GameScene');123  }124}125 126