let dialogue1 ="哈喽，还记得你的童年是什么样子的嘛？" 
let dialogue2 = "童年所经历的一切，都会在时光里发酵成甜蜜的回忆。"
let dialogue3 = "几岁时的一棵树一株草一片天一条街，或许就是你心灵的栖息地和灵魂的中转站。"
let dialogue4 = "希望你的一生都可以被童年治愈。"
let dialogue5 = "那么请你来说几个可以准确描述你童年记忆的关键词吧~"
let dialogue6 = "通关成功，接下来我们进入下一个主题房间。"
let T1 = -1;
let T2 = -1;
let T3 = 0;
let T4 = 0;
let T5 = 0;
let T6 = 0;
let showImgClick = false; // 点击开关
let showSecondText = true; // 新增变量控制第二段文字是否显示过
let soundSensorActivated = false; // 是否激活声音传感器
let timeSinceLastSound = 0; // 上次检测到声音的时间
function preload() {//图片加载
  img1 = loadImage("fangjian1.jpg");
  img2 = loadImage("datu1.png");
  img3 = loadImage("fangjian2.jpg");
  img4 = loadImage("datu2.png");
  img5 = loadImage("fangjian3.jpg");
  img6 = loadImage("datu3.png");
  img8 = loadImage("kuang.png");
  for (let i = 0; i < 12; i++) {
    zoulu[i] = loadImage(i + ".png");//走路小人左朝向
  }
  for (let i = 12; i < 24; i++) {
    zoulu2[i - 12] = loadImage(i + ".png");//右朝向
  }
}

function setup() {
  createCanvas(1920, 1080);
  imageMode(CENTER);
  textAlign(CENTER, CENTER);
  textSize(20);
  fill(255);//文本模式大小
  currentText = text1;
  console.log(frameRate);
  cRen = createVector(250, height / 2);
  vRen = createVector(0, 0);
  vRen2 = createVector(0, 0);
  t = 1000 / aRate; // Set the frame duration 

  // Set initial values
  showImage1 = false;
  showImage2 = true;
  isMoving = false;
  mic = new p5.AudioIn();
  mic.start();//在setup函数中启动声音传感器
}



function draw() {
  background(0);

  if (scene === 1) {
    scene1();
  } else if (scene === 2) {
    scene2();
  } else if (scene === 3) {
    scene3();
  }//场景切换
}

function keyPressed() {
  if ((scene === 1 || scene === 3) && (key === "b" || key === "B")) {
    scene = 2;
  } else if (scene === 2 && (key === "c" || key === "C")) {
    scene = 3;
  } else if (scene === 2 && (key === "a" || key === "A")) {
    scene = 1;
  }//关键按钮
  if (showImage1 || showImage2) {//控制左右键的按下
    if (keyCode === LEFT_ARROW) {//按左键
      vRen = createVector(-5, 0);
      vRen2 = createVector(-5, 0);//走路小人运动向量
      showImage1 = true;
      showImage2 = false;
      img7FrameIndex = 0;
      prevTime = millis();
      isMoving = true; // 开始动
    } else if (keyCode === RIGHT_ARROW) {//按右键
      vRen = createVector(5, 0);
      vRen2 = createVector(5, 0);
      showImage1 = false;
      showImage2 = true;
      img7FrameIndex = 0;
      prevTime = millis();
      isMoving = true; // Start moving
    }
  }
  
 
}

function keyReleased() {
  if (keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW) {
    vRen = createVector(0, 0);
    vRen2 = createVector(0, 0);
    isMoving = false; // Stop moving
    prevTime = millis(); // Reset frame timer
  }
}//控制左右键的释放后，不用该，有其他的可以填

function keyTyped() {//此处链接按键
  if (key === "q" || key === "Q") {
    if (showImage3) {
      showImage3 = false;
      showImage4 = true;
      currentText = "";
    } else if (showImage4) {
      showImage3 = true;
      showImage4 = false;
      currentText = text1;
    }
  } else if (keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW) {
    if (showImage1 && keyCode === LEFT_ARROW) {
      vRen = createVector(-5, 0);
      vRen2 = createVector(-5, 0);
      showImage1 = true;
      showImage2 = false;
      img7FrameIndex = 0;
      prevTime = millis();
      isMoving = true; // Start moving
    } else if (showImage2 && keyCode === RIGHT_ARROW) {
      vRen = createVector(5, 0);
      vRen2 = createVector(5, 0);
      showImage1 = false;
      showImage2 = true;
      img7FrameIndex = 0;
      prevTime = millis();
      isMoving = true; // Start moving
    }
  }
}



function scene1() {
  background(0);
  image(img1, width / 2, height / 2);//场景背景1


   
  if (showImage1) {//左右键控制的变量，具体控制小人运动
    if (isMoving) {
      animateZoulu();
      let nextX = cRen.x + vRen.x;
      if (nextX >= 250 && nextX <= 1670) {//限制小人不走出画外
        cRen.x = nextX;
      }
    }
    image(zoulu[img7FrameIndex], cRen.x, cRen.y);
  } else if (showImage2) {
    if (isMoving) {
      animateZoulu2();
      let nextX = cRen.x + vRen2.x;
      if (nextX >= 250 && nextX <= 1670) {
        cRen.x = nextX;
      }
    }
    image(zoulu2[img7FrameIndex], cRen.x, cRen.y);//一直到这里，作用同上
  }
  if (showImage3) {
  text(currentText, width / 2, height * 0.8);//墙上提示词，不按时
  }else if(showImage4){
    image(img2, width / 2, height / 2);
    text(text2, width / 2, height * 0.8);//放大图，和缩小提示词
  }
  if (showImg8) { //激活显示图片的开关
    image(img8, width / 2, height / 2);
  }
  img7X = cRen.x;
  img7Y = cRen.y;//有关小人走路的变量，不动

  fill(255);
  text(text3, 1720, 350);//门上提示词

  //对白字幕
  if (T1 === 0 && showSecondText){
    T2 = 420;
    showSecondText = false; // 第二段文字只出现一次
  } else if (T1 > 0) {
    textFont("Simsun");
    textSize(35);
    text(dialogue1,width * 0.5, height * 0.7);
    T1--;
  } else if (T2 > 0) {
    textFont("Simsun");
    textSize(35);
    text(dialogue2, width * 0.5, height * 0.7);
    T2--;
    if (T2 === 0){
      T3 = 540;
      ;
    }
  } else if (T3 > 0) {
    textFont("Simsun");
    textSize(35);
    text(dialogue3, width * 0.5, height * 0.7);
    T3--;
    if (T3 === 0){
      T4 = 420;
    }
  } else if (T4 > 0) {
    textFont("Simsun");
    textSize(35);
    text(dialogue4, width * 0.5, height * 0.7);
    T4--;
    if (T4 === 0){
      T5 = 420;
    }
  } else if (T5 > 0) {
    textFont("Simsun");
    textSize(35);
    text(dialogue5, width * 0.5, height * 0.7);
    T5--;
    if (T5 === 0){
    soundSensorActivated = true;//激活声音传感器
    }
    if (soundSensorActivated) {//如果声音传感器被激活
    let currentSoundTime = millis();
    if (mic.getLevel() > 0.1) {
    lastSoundTime = currentSoundTime;
    } else if (currentSoundTime - lastSoundTime > 5000) {
      // 如果五秒内没有声音，则继续等待声音输入
      if (T7 === 0) 
        T7 = 420; // 停顿十秒
    }
  }

}

  else if (T7 > 0) {

    textFont("Simsun");
    textSize(35);
    text(dialogue6, width * 0.5, height * 0.7);
    T6--;
  }
}
//对话框出现与否
function mouseClicked() {
  showImg8 = !showImg8;// 每次点击开关状态取反
}
//字幕出现的时长
function mousePressed(){
  if (T1 === -1) {
    textIndex = 0;
    textLoading = true;
    T1 = 420;
  } else if (T1 === 0) {
    T2 = 420;
  } else if (T2 === -1){
    T3 = 540;
    
  }
}在第五段话开始后能够声音识别，识别到回答声音结束后，再等十秒开始第六段话的播放，否则不播放，保持静止