const gridWidth = 72
const gridHeight = 72
const gridsInX = 14
const gridsInY = 10
class Map extends egret.Sprite {
  private grids:any = []
  private touchArea: egret.Bitmap 
  public constructor() {
    super();
    this.init()
  }

  // 初始化(给开始按钮绑定点击事件)
  private init() {
    for (let i = 0; i < gridsInY; i++) {
      this.grids.push([])
      for (let j = 0; j < gridsInX; j++) {
        const grid = new MapGrid()
        grid.x = j * gridWidth
        grid.y = i * gridHeight
        this.grids[i].push(grid)
        this.addChild(grid)
      }
    }
    
    this.touchArea = new egret.Bitmap()
    this.touchArea.texture = RES.getRes('mask_jpg')
    this.touchArea.alpha = 0
    this.addChild(this.touchArea)
  }

  public getSurroundedGrids(x, y) {
    const left = Math.max(x / gridWidth - 2)
    const top = Math.max(y / gridHeight - 2)
    const grids = []
    for (let i = left; i < 5 + left; i++) {
      for (let j = top; j < 5 + top; j++) {
        if (i >= 0 && j >= 0 && i < gridsInX && j < gridsInY) {
          grids.push(this.grids[j][i])
        }
      }
    }
    return grids
  }

  public showSurroundedGrids(x, y) {
    const surroundedGrids = this.getSurroundedGrids(x, y)
    surroundedGrids.forEach(grid => {
      grid.alpha = 1 
    })
    this.touchArea.alpha = 1
    this.touchArea.x = x - 2 * gridWidth
    this.touchArea.y = y - 2 * gridHeight

    setTimeout(() => {
      surroundedGrids.forEach(grid => {
        grid.alpha = 0
      })
      this.touchArea.alpha = 0
    }, 1000);
  }

  public release() {
  }
}