import { getTextureByName } from "./utils";
import { props } from "../props";
export class BackBoxs extends engine.Container {
  //盒子1
  private box1: engine.Container;
  //盒子2
  private box2: engine.Container;
  //盒子3
  private box3: engine.Container;
  //盒子4
  private box4: engine.Container;
  //盒子5
  private box5: engine.Container;
  private backBoxs=[];
  constructor() {
    super();
  }
  setup() {
    let initX , initY = 0;
    this.box1 = new engine.Sprite(getTextureByName("后狗盒"));
    initX = this.box1.x = 188;
    initY = this.box1.y = 616;

    this.box2 = new engine.Sprite(getTextureByName("后猫盒"));
    this.box3 = new engine.Sprite(getTextureByName("后草泥马盒"));
    this.box4 = new engine.Sprite(getTextureByName("后狗盒"));
    this.box5 = new engine.Sprite(getTextureByName("后猫盒"));
    this.backBoxs = [this.box1, this.box2, this.box3, this.box4, this.box5];
    for (let i = 0; i < this.backBoxs.length; i++) {
      // this.backBoxs[i].scaleX = 0.5;
      // this.backBoxs[i].scaleY = 0.5;
      if(i == 0) {
        this.backBoxs[i].x = initX;
      } else {
        this.backBoxs[i].x = this.backBoxs[i - 1].x + this.backBoxs[i].width + props.backmargin;
      }
      this.backBoxs[i].y = initY;
      
      this.addChild(this.backBoxs[i]);
    }
    this.addEventListener(engine.Event.ENTER_FRAME, this.frameMove, this);
  }
  frameMove() {
    this.backBoxs.forEach((item) => {
      item.x -= props.moveSpeed;
      if (item.x <= 0) {
        let lastitem = this.backBoxs.shift();
        lastitem.x = this.backBoxs[this.backBoxs.length - 1].x + item.width + props.backmargin;
        this.backBoxs.push(lastitem);
      }
    })
  }
}