All files / Hungry-Hippo-Game/src/game/scenes Game.ts

0% Statements 0/834
0% Branches 0/1
0% Functions 0/1
0% Lines 0/834

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
/**
 * @file Game.ts
 * @description Main Phaser Scene for Hippo Game: Manages gameplay, player logic, scoring, food spawning, and sync via WebSocket.
 */

import { Scene } from 'phaser';
import { EventBus } from '../EventBus';
import { AAC_DATA, AacVerb } from '../../Foods';
import { Hippo } from '../Hippo';
import { Edge, EdgeSlideStrategy } from '../EdgeSlideStrategy';
import { movementStore } from './MovementStore';
import { ModeSettings } from '../../config/gameModes';
import { HIPPO_COLORS } from '../../config/hippoColors';

interface FoodState {
  instanceId: string;
  foodId: string;
  x: number;
  y: number;
  effect: AacVerb | null;
}

export class Game extends Scene {
  private sessionId!: string;
  private hippo: Hippo | null = null;
  private foods!: Phaser.Physics.Arcade.Group;
  private foodSpawnTimer?: Phaser.Time.TimerEvent;
  private currentTargetFoodId: string | null = null;
  private currentTargetFoodEffect: AacVerb | null = null;
  private playerScores: Record<string, number> = {};
  private players: Record<string, Hippo> = {};
  private edgeAssignments: Record<string, string> = {};
  private availableEdges = [ 'bottom','top','right', 'left' ];
  private cursors!: Phaser.Types.Input.Keyboard.CursorKeys;
  private sendMessage!: (msg: any) => void;
  private localPlayerId!: string;
  private usePointerControl = false;
  private isKeyboardActive = false;
 
  /**
   * Variable to track time left
   */
  private timerText!: Phaser.GameObjects.Text;


  // Handlers for cleanup
    private onSyncFoodState: any;
    private onApplyPlayerEffect: any;
    private onExternalMessage: any;
    private onStartGame: any;
    private onTimerUpdate: any;
    private unsubscribeMove?: () => void;



  private role: string = 'Hippo Player';
  private lastSentX: number | null = null;
  private lastSentY: number | null = null;
  private lastMoveSentAt: number = 0;
  private modeSettings: ModeSettings = { fruitSpeed: 100, allowPenalty: true, allowEffect: true }; // fallback
  //private pendingHippoPlayers: string[] = [];

  // Variable to track if user has interacted with the game
  private hasUserInteracted = false;
  // Variable to track if swipe hint is shown
  private swipeHint?: Phaser.GameObjects.Image;


  //private hippoGroup!: Phaser.Physics.Arcade.Group;



  constructor() {
    super('Game');
  }

  /**
   * Defensive: Phaser calls init() without arguments, so allow empty/no-op.
   * Real game data is passed by your React wrapper later!
   */
  init(data: { 
    sendMessage: (msg: any) => void; 
      localPlayerId: string; 
      sessionId: string;
      role: string;
      connectedUsers?: { color: string | undefined; userId: string; role: string }[];
      modeSettings?: ModeSettings;
      localPlayerEdge?: string;
  }) {
    this.sendMessage = data.sendMessage;
    this.localPlayerId = data.localPlayerId;
    this.sessionId = data.sessionId;
    this.role = data.role;

    if (data.modeSettings) {
      this.modeSettings = data.modeSettings;
    }
    if (data.connectedUsers) {
      data.connectedUsers
      .filter(u => u.role === 'Hippo Player')
      .forEach(u => {
        const edgeForPlayer = (u.userId === this.localPlayerId) ? data.localPlayerEdge : undefined;
        console.log(`[Game.init] Adding player ${u.userId} with color ${u.color} and edge ${edgeForPlayer}`);
        this.addPlayer(u.userId, u.color, edgeForPlayer as Edge | undefined);
      });
    }
    this.role = data.role;
  }


  preload() {
    console.log('[Game] Preload called');
    this.load.image('background', '/assets/presenterBg.webp');
    this.load.image('swipeHand', '/assets/swipeHand.png');
    this.load.image('tree', '/assets/tree.webp');

    AAC_DATA.categories.forEach(category => {
      category.foods.forEach(food => {
        if (food.imagePath) {
          this.load.image(food.id, food.imagePath);
        }
      });
    });
    // this.load.spritesheet('character', '/assets/spritesheet.png', {
    //   frameWidth: 350,
    //   frameHeight: 425
    // });


    HIPPO_COLORS.forEach(h => {
      this.load.image(h.color + 'Hippo', h.imgSrc);
    });

  }


  private getEdgePosition(edge: string, sprite?: Phaser.GameObjects.Sprite) {
    const w = this.scale.width;
    const h = this.scale.height;
    // const marginX = w * 0.05;
    // const marginY = h * 0.05;

    const marginX = sprite ? sprite.displayWidth / 2 : w * 0.05;
    const marginY = sprite ? sprite.displayHeight / 2 : h * 0.05;


    switch (edge) {
      case 'top': return { x: w / 2, y: marginY };
      case 'bottom': return { x: w / 2, y: h - marginY };
      case 'left': return { x: marginX, y: h / 2 };
      case 'right': return { x: w - marginX, y: h / 2 };
      default: return { x: w / 2, y: h / 2 };
    }
  }

  public addPlayer(playerId: string, color?: string, hippoEdge?: Edge) {
    if (!(playerId in this.playerScores)) this.playerScores[playerId] = 0;
    if (!(playerId in this.players)) {
      const edge = hippoEdge || (this.availableEdges.shift() || 'bottom') as Edge;
      const { x, y } = this.getEdgePosition(edge);
      const slideDistance = (edge === 'top' || edge === 'bottom') ? this.scale.width * 0.8 : this.scale.height * 0.8;

      let spriteKey = 'character';
      const strategy = new EdgeSlideStrategy(edge, slideDistance);
      if (color) {
        spriteKey = color + 'Hippo';
      }
      //console.log(`[addPlayer] ${playerId} -> color: ${color}, spriteKey: ${spriteKey}`);

      const playerSprite = new Hippo(this, x, y, spriteKey, strategy);

      //this.hippoGroup.add(playerSprite);

      // Remove the player from available edges if it was passed
      if (hippoEdge) {
        const index = this.availableEdges.indexOf(hippoEdge);
        if (index > -1) {
          this.availableEdges.splice(index, 1);
        }
      }
      this.edgeAssignments[playerId] = edge;

      playerSprite.setScale(0.12);
     
      playerSprite.setCollideWorldBounds(true);
      playerSprite.setImmovable(true);
      switch (edge) {
        case 'top': playerSprite.setAngle(180); break;
        case 'left': playerSprite.setAngle(90); break;
        case 'right': playerSprite.setAngle(-90); break;
        case 'bottom': playerSprite.setAngle(0); break;
      }
      if (this.foods) {
        this.physics.add.overlap(playerSprite, this.foods, (_hippo, fruit) => {
          if (fruit instanceof Phaser.Tilemaps.Tile) return;
          const fruitGO = fruit instanceof Phaser.GameObjects.GameObject ? fruit : null;
          if (fruitGO) this.handleFruitCollision(playerId, fruitGO);
        });
      }
      playerSprite.setTargetPosition(x, y);
      this.players[playerId] = playerSprite;
      if (playerId === this.localPlayerId && this.role !== 'Spectator') {
        this.hippo = playerSprite;
        const camera = this.cameras.main;
        switch (edge) {
          case 'bottom':
            camera.setRotation(0);
            break;
          case 'right':
            camera.setRotation(Math.PI / 2);
            break;
          case 'top':
            camera.setRotation(Math.PI);
            break;
          case 'left':
            camera.setRotation(-Math.PI / 2);
            break;
        }
      }
    }
  }



  /**
   * Maps standard arrow key inputs (cursors) to the correct direction
   * for the player's edge of the screen, so "up" is always "toward the center."
   * 
   * @param edge   The edge ('top', 'bottom', 'right', 'left') where this player is located.
   * @param cursors Phaser's cursor key object (from createCursorKeys()).
   * @returns A virtual cursors object where isDown for left/right/up/down is remapped appropriately.
   *
   * Example Usage:
   * ```
   * const edge = this.edgeAssignments[this.localPlayerId] as Edge;
   * const mappedCursors = this.getEdgeCursors(edge, this.cursors);
   * this.hippo.update(mappedCursors);
   * ```
   */

private getEdgeCursors(edge: Edge, cursors: Phaser.Types.Input.Keyboard.CursorKeys) {
  switch (edge) {
    case 'bottom':
      return cursors;
    case 'top':
      return {
        left:  { isDown: cursors.right.isDown },
        right: { isDown: cursors.left.isDown },
        up:    { isDown: cursors.down.isDown },
        down:  { isDown: cursors.up.isDown }
      } as Phaser.Types.Input.Keyboard.CursorKeys;
    case 'right':
      // For right edge, up is right, down is left
      return {
        left:  { isDown: cursors.down.isDown },
        right: { isDown: cursors.up.isDown },
        up:    { isDown: cursors.right.isDown },
        down:  { isDown: cursors.left.isDown }
      } as Phaser.Types.Input.Keyboard.CursorKeys;
    case 'left':
      // For left edge, up is left, down is right
      return {
        left:  { isDown: cursors.up.isDown },
        right: { isDown: cursors.down.isDown },
        up:    { isDown: cursors.left.isDown },
        down:  { isDown: cursors.right.isDown }
      } as Phaser.Types.Input.Keyboard.CursorKeys;
    default:
      return cursors;
  }
}


/**
 * Handles pointer (touch or mouse) movement for the local Hippo player.
 *
 * Determines the world coordinates based on the main camera rotation,
 * clamps the movement along the allowed edge (horizontal for top/bottom, vertical for left/right),
 * updates the Hippo's facing direction, and sends the new target position.
 *
 * This method is only active for the local (non-spectator) Hippo and enables pointer control mode.
 *
 * @param {Phaser.Input.Pointer} pointer - The Phaser pointer event with screen (and derived world) coordinates.
 *
 * Usage:
 * Called in response to 'pointerdown' and 'pointermove' events for player control.
 */

  private handlePointer(pointer: Phaser.Input.Pointer) {
  // Only allow local hippo and if not spectator
  if (!this.hippo || this.role === 'Spectator') return;
  this.usePointerControl = true;

  // Determine which edge this hippo is assigned to
  const edge = this.edgeAssignments[this.localPlayerId] as Edge;
  const prevX = this.hippo.targetX;
  const prevY = this.hippo.targetY;

  const camera = this.cameras.main;
  const worldPoint = camera.getWorldPoint(pointer.x, pointer.y);

  
  if (edge === 'top' || edge === 'bottom') {
    // Allow sliding left/right only; y stays fixed
    // Clamp to play area if needed
    const minX = this.hippo.displayWidth / 2;
    const maxX = this.scale.width - this.hippo.displayWidth / 2;

    const x = Phaser.Math.Clamp(worldPoint.x, minX, maxX);
    const y = this.hippo.y;
    this.hippo.updatePointerFlip(prevX, prevY, edge, x, y);
    this.hippo.setTargetPosition(x, y);
  } else if (edge === 'left' || edge === 'right') {
    // Allow sliding up/down only; x stays fixed
    const minY = this.hippo.displayHeight/2;
    const maxY = this.scale.height - this.hippo.displayHeight/2;
    const x = this.hippo.x;

    const y = Phaser.Math.Clamp(worldPoint.y, minY, maxY);
    this.hippo.updatePointerFlip(prevX, prevY, edge, x, y);

    this.hippo.setTargetPosition(x, y);
  }
}


/**
 * Phaser's built-in scene creation method.
 *
 * Initializes the background, input handlers (keyboard and pointer),
 * foods physics group, and swipe hint animation.
 * Also sets up all movement and game event subscriptions, camera orientation for the player,
 * and registers custom event listeners (effects, game over, timer, etc.).
 *
 * This is the main method where the scene is brought to life after assets are loaded.
 * 
 * Usage:
 * Called automatically by Phaser after preload.
 * Sets up everything for the current player/role.
 */



  create() {
    const bg = this.add.image(512, 512, 'background');
    bg.setOrigin(0.5, 0.5);
    bg.setDisplaySize(this.scale.width, this.scale.height);

    const tree = this.add.image(512, 512, 'tree');
    tree.setOrigin(0.5, 0.5);
    tree.setScale(0.35);
    tree.setDepth(3);


    if (this.role === 'Spectator'){this.physics.pause();}
        this.input.keyboard!.on('keydown', () => {
      this.isKeyboardActive = true;
      this.usePointerControl = false;
    });
    this.input.keyboard!.on('keyup', () => {
      this.isKeyboardActive = false;
    });


   // this.hippoGroup = this.physics.add.group();


    this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
        if (!this.isKeyboardActive) this.usePointerControl = true;
        this.handlePointer(pointer);
      });
    this.input.on('pointermove', (pointer: Phaser.Input.Pointer) => {
        if (pointer.isDown && !this.isKeyboardActive) {
          this.usePointerControl = true;
          this.handlePointer(pointer);
        }
      });

  
    
    this.foods = this.physics.add.group();
    this.cursors = this.input!.keyboard!.createCursorKeys();


     this.input.once('pointerdown', () => {
      this.hasUserInteracted = true;
      this.swipeHint?.destroy();
    });

    // Check if user has interacted with the game on keyboard
    this.input.keyboard?.on('keydown', () => {
      if (!this.hasUserInteracted) {
        this.hasUserInteracted = true;
        this.swipeHint?.destroy();
      }
    });



    this.onSyncFoodState = (foodStates: FoodState[]) => this.syncFoodState(foodStates);
    this.onApplyPlayerEffect = (data: { targetUserId: string, effect: AacVerb }) => {
    if (data.targetUserId !== this.localPlayerId) {
      this.applyEffectToPlayer(data.targetUserId, data.effect);
    }
  };
  this.onExternalMessage = (data: any) => {
    if (data.type == 'gameOver') {
      this.handleGameOver();
    }
  };
  this.onStartGame = () => {
    this.requestStartTimer();
  };
  this.onTimerUpdate = (secondsLeft: number) => {
    this.updateTimerUI(secondsLeft);
  };

  // Subscribe EventBus with named handlers
  EventBus.on('sync-food-state', this.onSyncFoodState);
  EventBus.on('apply-player-effect', this.onApplyPlayerEffect);
  EventBus.on('external-message', this.onExternalMessage);
  EventBus.on('start-game', this.onStartGame);
  EventBus.on('TIMER_UPDATE', this.onTimerUpdate);
  EventBus.on('players-updated', this.handlePlayersUpdated, this);

  // movementStore subscription (and save the unsubscribe function!)
    this.unsubscribeMove = movementStore.subscribe(({ userId, x, y }) => {
    const player = this.players[userId];
    if (player && userId !== this.localPlayerId) {
      const edge = this.edgeAssignments[userId] as Edge;
      const prevX = player.targetX;
      const prevY = player.targetY;
      player.updatePointerFlip(prevX, prevY, edge, x, y);
      player.setTargetPosition(x, y);
      player.hasSynced = true;
    }
    });

    // Listen for shutdown/destroy events (Phaser scene events)
    this.events.on('shutdown', this.shutdown, this);
    this.events.on('destroy', this.destroy, this);

    
    
    
    EventBus.emit('current-scene-ready', this);
    EventBus.emit('edges-ready', this.edgeAssignments); 
    
    
    // Delay animation for swipe hand
    this.time.delayedCall(100, () => {
      if (this.role === 'Hippo Player') {
        // Play animation only for the hippo users, not on spectator
        const edge = this.edgeAssignments[this.localPlayerId] as Edge;

        let hintX = this.scale.width / 2;
        let hintY = this.scale.height - 100;
        let tweenProps: Partial<Phaser.Types.Tweens.TweenBuilderConfig> = {};
        let handAngle = 0; 

        // Determine hand position and angle based on edge
        switch (edge) {
          case 'top':
            hintY = 140;
            handAngle = 180; // Upside down
            tweenProps = {
              x: {
                from: hintX - 60,
                to: hintX + 60
              }
            };
            break;
          case 'left':
            hintX = 140;
            hintY = this.scale.height / 2;
            handAngle = -270; // Pointing right
            tweenProps = {
              y: {
                from: hintY - 60,
                to: hintY + 60
              }
            };
            break;
          case 'right':
            hintX = this.scale.width - 140;
            hintY = this.scale.height / 2;
            handAngle = 270; // Pointing left
            tweenProps = {
              y: {
                from: hintY - 60,
                to: hintY + 60
              }
            };
            break;
          case 'bottom':
          default:
            hintY = this.scale.height - 140;
            handAngle = 0; // No rotation
            tweenProps = {
              x: {
                from: hintX - 60,
                to: hintX + 60
              }
            };
            break;
        }

        this.swipeHint = this.add.image(hintX, hintY, 'swipeHand')
          .setOrigin(0.5)
          .setDepth(1000)
          .setScale(0.6)
          .setAngle(handAngle)
          .setScrollFactor(0); // Important: stays on screen even if camera moves

        this.tweens.add({
          targets: this.swipeHint,
          ...tweenProps,
          duration: 800,
          ease: 'Sine.easeInOut',
          yoyo: true,
          repeat: -1
        });
      }
    });

  }




  
    
  update() {
  

    if (this.hippo && this.role !== 'Spectator') {
      if (this.usePointerControl) {
        this.hippo.update(); // <-- no cursors, triggers lerp to target
      } else if (this.cursors) {


        /** 
         * Keyboard arrow keys, remapped to match the player's edge view.
         * This ensures that 'up' always means 'toward the center of the board' from the player's perspective.
         */
       const edge = this.edgeAssignments[this.localPlayerId] as Edge;
        this.hippo.update(this.getEdgeCursors(edge, this.cursors)); 
        
      }
      const newX = this.hippo.x;
      const newY = this.hippo.y;
      if (this.hasUserInteracted && (this.lastSentX !== newX || this.lastSentY !== newY)) {
        const now = Date.now();
        if (!this.lastMoveSentAt || now - this.lastMoveSentAt > 40) {
          this.lastSentX = newX;
          this.lastSentY = newY;
          this.lastMoveSentAt = now;
          try {
            this.sendMessage?.({
              type: 'PLAYER_MOVE',
              payload: {
                sessionId: this.sessionId,
                userId: this.localPlayerId,
                x: newX,
                y: newY
              }
            });
          } catch (e) {
            console.error('[Game.update] Failed to send player movement:', e);
          }
        }
      }
    }
    for (const [id, hippo] of Object.entries(this.players)) {
      if (id !== this.localPlayerId && hippo.hasSynced) {
        hippo.update();
      }
    }
  }

  private handlePlayersUpdated(updatedUsers: { userId: string; role: string; color?: string }[]) {
  const activeIds = updatedUsers.map(u => u.userId);
  for (const userId of Object.keys(this.players)) {
    if (!activeIds.includes(userId)) {
      // Remove the Hippo sprite
      this.players[userId].destroy();
      delete this.players[userId];
      delete this.playerScores[userId];
      delete this.edgeAssignments[userId];
    }
  }
}

  private applyEffectToPlayer(targetUserId: string, effect: AacVerb) {
    const targetHippo = this.players[targetUserId];
    const edge = this.edgeAssignments[targetUserId];
    if (!targetHippo) return;
    switch(effect.id) {
      case 'freeze':
        targetHippo.freeze(2000);
        break;
      case 'burn':
        targetHippo.setTint(0xff0000);
        this.time.delayedCall(1000, () => {
          targetHippo.clearTint();
        });
        break;
      case 'grow':
        targetHippo.setTint(0x00FF00);
        targetHippo.setScale(0.3);

        // Use snapToEdge() from Hippo, or call getEdgePosition with the sprite
    if (typeof targetHippo.snapToEdge === "function") {
        targetHippo.snapToEdge(edge); // This will call getEdgePosition with itself!
    }

        this.time.delayedCall(5000, () => {
          targetHippo.clearTint();
          targetHippo.setScale(0.12);

          if (typeof targetHippo.snapToEdge === "function") {
            targetHippo.snapToEdge(edge); // Snap again after shrink
        }

        });
        break;
    }
  }


  private handleFruitCollision(playerId: string, fruit: Phaser.GameObjects.GameObject) {
    if (!fruit.active || fruit.getData('eatenBy')) return;
    if (this.role === 'Spectator') return;

    fruit.setData('eatenBy', playerId);

    if (this.players[playerId] === this.hippo) {
      if ('disableBody' in fruit) {
        (fruit as Phaser.Physics.Arcade.Image).disableBody(true, true);
      }

      if ('texture' in fruit && fruit instanceof Phaser.GameObjects.Sprite) {
        const sprite = fruit as Phaser.GameObjects.Sprite;
        const foodId = sprite.texture.key;
        const isCorrect = foodId === this.currentTargetFoodId;
        if (isCorrect && this.currentTargetFoodEffect && this.modeSettings.allowEffect) {
          this.applyEffectToPlayer(playerId, this.currentTargetFoodEffect);
          this.sendMessage({
            type: 'PLAYER_EFFECT_APPLIED',
            payload: {
              sessionId: this.sessionId,
              targetUserId: playerId,
              effect: this.currentTargetFoodEffect
            }
          });
        }

        try {
          this.sendMessage({
            type: 'FRUIT_EATEN_BY_PLAYER',
            payload: {
              sessionId: this.sessionId,
              userId: playerId,
              isCorrect,
              allowPenalty: this.modeSettings.allowPenalty,
              effect: this.modeSettings.allowEffect ? this.currentTargetFoodEffect?.id : null
            },
          });
        } catch (e) {
          console.error('[Game.handleFruitCollision] Error sending score update:', e);
        }
        if (isCorrect) {
          this.currentTargetFoodEffect = null;
        }
        const instanceId = fruit.getData('instanceId');
        if (instanceId) {
            EventBus.emit('fruit-eaten', { instanceId });
        }
      }
    }
  }

  public applyModeSettings(settings: ModeSettings) {
   // console.log('[Game] Applying mode settings:', settings);
    this.modeSettings = settings;
  }

  /**
   * Synchronizes the food state with the server.
   * @param serverFoods Array of food states from the server.
   */
  private syncFoodState(serverFoods: FoodState[]) {
    // Get existing food sprites and their IDs
    const existingFoodSprites = this.foods.getChildren() as Phaser.Physics.Arcade.Image[];
    const serverFoodIds = new Set(serverFoods.map(f => f.instanceId));

    // Update existing sprites and create new ones
    serverFoods.forEach(foodState => {
      let existingSprite = existingFoodSprites.find(sprite => sprite.getData('instanceId') === foodState.instanceId);

      // If it exists, update its position smoothly using interpolation
      if (existingSprite) {
        this.tweens.add({
          targets: existingSprite,
          x: foodState.x * this.scale.width,
          y: foodState.y * this.scale.height,
          duration: 50,
          ease: 'Linear'
        });
      } else {
        // If it doesn't exist, create it on the client side
        const spawnX = foodState.x * this.scale.width;
        const spawnY = foodState.y * this.scale.height;
        const newFood = this.foods.create(spawnX, spawnY, foodState.foodId) as Phaser.Physics.Arcade.Image;
        newFood.setDepth(2);
        newFood.setData('instanceId', foodState.instanceId);
        newFood.setScale(0.12);
        newFood.body?.setCircle(newFood.width * 0.5);
        
        // If the food has an effect, apply the tint color
        if (foodState.effect && foodState.effect.color) {
          const tintColor = parseInt(foodState.effect.color.replace('#', '0x'));
          newFood.setTint(tintColor);
        }
      }
    });

    // Remove any client-side sprites that no longer exist on the server
    existingFoodSprites.forEach(sprite => {
      if (!serverFoodIds.has(sprite.getData('instanceId'))) {
        sprite.destroy();
      }
    });
  }


  public setTargetFood(foodId: string, effect: AacVerb | null = null) {
    this.currentTargetFoodId = foodId;
    this.currentTargetFoodEffect = effect;
  }

  /**
   * Removes a food item by its instance ID.
   * @param instanceId The unique identifier of the food item to remove.
   */
  public removeFoodByInstanceId(instanceId: string) {
    this.foods.children.each((child: any) => {
      if (child.getData('instanceId') === instanceId) {
        child.destroy();
        return false;
      }
      return true;
    });
  }

  /**
   * 
   * @param secondsLeft the number of seconds left in this session
   * If a text for the timer exists, updates the text to the number of seconds left.
   */
  private updateTimerUI(secondsLeft: number)
  {
    if(!this.timerText)
    {
      return;
    }
    if(this.timerText)
    {
      this.timerText.setText(`Time: ${secondsLeft}`);
    }
  }

  /**
   * This method handles the game when the timer hits 0.
   */
  private handleGameOver()
  {
    this.add.rectangle(512, 384, 1024, 768, 0x000000, 0.7).setDepth(10);
    this.add.text(512, 384, 'Game Over', {
      fontSize: '64px',
      color: '#ffffff'
    }).setOrigin(0.5).setDepth(11);

    this.physics.pause();

    if(this.foodSpawnTimer)
    {
      this.foodSpawnTimer.remove(false);
    }
  }

  /**
   * This method requests system for a start timer.
   */
  public requestStartTimer()
  {
    if(this.sendMessage)
    {
      this.sendMessage({ type: 'START_TIMER', payload: { sessionId: this.sessionId} });
    }
  }

  public getEdgeAssignments(): Record<string, string> {
    return this.edgeAssignments;

  }



shutdown() {
  // Remove all EventBus listeners
  EventBus.off('sync-food-state', this.onSyncFoodState);
  EventBus.off('apply-player-effect', this.onApplyPlayerEffect);
  EventBus.off('external-message', this.onExternalMessage);
  EventBus.off('start-game', this.onStartGame);
  EventBus.off('TIMER_UPDATE', this.onTimerUpdate);

  // Unsubscribe from movementStore
  if (this.unsubscribeMove) {
    this.unsubscribeMove();
    this.unsubscribeMove = undefined;
  }
}

destroy() {
  this.shutdown(); // Always clean up
  this.time?.removeAllEvents?.();
  this.tweens?.killAll?.();
   if (this.foods) {
    this.foods.clear(true, true); // <--- DESTROY ALL SPRITES!
  }
  this.input?.removeAllListeners?.();
  }
}