// 加载场景：全部本地自制素材 + 动画注册
import Phaser from "phaser";

const MAPS = ["house", "plaza", "bakery", "store", "clinic", "lake", "field", "well", "fogwall", "white_corridor", "white_ward"];
const NPCS = ["afu", "yuanyuan", "dabao", "mimi", "fu", "gui"];

export class BootScene extends Phaser.Scene {
  constructor() { super("boot"); }

  preload() {
    const a = "assets/";
    this.load.spritesheet("tiles", a + "tiles.png", { frameWidth: 16, frameHeight: 16 });
    this.load.spritesheet("player", a + "player.png", { frameWidth: 16, frameHeight: 24 });
    this.load.spritesheet("bird", a + "bird.png", { frameWidth: 16, frameHeight: 16 });
    this.load.spritesheet("sunflower", a + "sunflower.png", { frameWidth: 24, frameHeight: 24 });
    this.load.image("portrait_smile", a + "portrait_smile.png");
    this.load.image("portrait_real", a + "portrait_real.png");
    this.load.image("sparrow", a + "sparrow.png");
    for (const n of NPCS) this.load.spritesheet("npc_" + n, a + "npc_" + n + ".png", { frameWidth: 16, frameHeight: 24 });
    for (let i = 1; i <= 5; i++) this.load.spritesheet("extra" + i, a + "extra" + i + ".png", { frameWidth: 16, frameHeight: 16 });
    for (const m of MAPS) this.load.tilemapTiledJSON("map_" + m, a + "maps/" + m + ".json");
  }

  create() {
    // 雨滴纹理（程序生成）
    const g = this.make.graphics({ x: 0, y: 0 }, false);
    g.fillStyle(0xb8d8f0, 1).fillRect(0, 0, 1, 4);
    g.generateTexture("rainpx", 1, 4);
    g.destroy();

    // 主角四向行走（每朝向第 1 帧兼待机）
    const mk = (key: string, start: number) =>
      this.anims.create({ key, frames: this.anims.generateFrameNumbers("player", { start, end: start + 3 }), frameRate: 8, repeat: -1 });
    mk("walk-down", 0); mk("walk-up", 4); mk("walk-left", 8); mk("walk-right", 12);

    // 双子鸟张嘴
    this.anims.create({ key: "bird-sing", frames: this.anims.generateFrameNumbers("bird", { start: 0, end: 1 }), frameRate: 3, repeat: -1 });
    // 群演循环
    for (let i = 1; i <= 5; i++) {
      this.anims.create({ key: `extra${i}-walk`, frames: this.anims.generateFrameNumbers("extra" + i, { start: 0, end: 1 }), frameRate: 3, repeat: -1 });
    }
    this.scene.start("title");
  }
}
