class Scene_Puzzle15 < Scene_MenuBase
  
  def initialize(id)
    @puzzle = $data_puzzle15s[id]
  end   
  #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  def start
    super

    set_init
    create_puzzle_window
    set_Tile_Tips
    even_proposition_random(item_max)
    @puzzle_window.get_board_info(@board)
    @puzzle_window.refresh
  end
  
  #--------------------------------------------------------------------------
  # ● タイルたちの初期設定
  #--------------------------------------------------------------------------
  def set_Tile_Tips
    
    @board = (item_max - 1).times.collect do |i|
      Tile_Tip.new(i,@puzzle.ncolumn , @puzzle.nrow , false)
    end
    @board.push Tile_Tip.new((item_max - 1),@puzzle.ncolumn , @puzzle.nrow , true)
  end    
  #--------------------------------------------------------------------------
  # ● 変数などのセット
  #--------------------------------------------------------------------------
  def set_init
    @list = []
    @count = 0
  end
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  def item_max
    return @puzzle.size
  end
  #--------------------------------------------------------------------------
  # ● コマンドウィンドウの作成
  #--------------------------------------------------------------------------
  def create_puzzle_window
    
    
    @puzzle_window = Window_Puzzle15.new(0, 0, Graphics.width, Graphics. height, @puzzle)
#~     @puzzle_window.get_board_info(@board)
    @puzzle_window.set_handler(:ok,     method(:on_category_ok))
    @puzzle_window.set_handler(:cancel, method(:return_scene)) if @puzzle.enable_cancel

    @puzzle_window.get_max_count(max_count)
    
    @puzzle_window.refresh
    @message_window = Window_Message.new

  end  
  #--------------------------------------------------------------------------
  # ● フレーム更新（ウェイト用）
  #--------------------------------------------------------------------------
  def update_for_wait
    update_basic
  end
  #--------------------------------------------------------------------------
  # ● ウェイト
  #--------------------------------------------------------------------------
  def wait(duration)
    duration.times {|i| update_for_wait if i < duration / 2 || !show_fast? }
  end
  #--------------------------------------------------------------------------
  # ● 早送り判定
  #--------------------------------------------------------------------------
  def show_fast?
    Input.press?(:A) || Input.press?(:C)
  end
  #--------------------------------------------------------------------------
  # ● ウェイト（早送り無効）
  #--------------------------------------------------------------------------
  def abs_wait(duration)
    duration.times {|i| update_for_wait }
  end
  #--------------------------------------------------------------------------
  # ● 短時間ウェイト（早送り無効）
  #--------------------------------------------------------------------------
  def abs_wait_short
    abs_wait(15)
  end
  #--------------------------------------------------------------------------
  # ● メッセージ表示が終わるまでウェイト
  #--------------------------------------------------------------------------
  def wait_for_message
    @message_window.update
    update_for_wait while $game_message.visible
  end
  #--------------------------------------------------------------------------
  # ● アニメーション表示が終わるまでウェイト
  #--------------------------------------------------------------------------
  def wait_for_animation
    update_for_wait
    update_for_wait while @spriteset.animation?
  end
  #--------------------------------------------------------------------------
  # ● エフェクト実行が終わるまでウェイト
  #--------------------------------------------------------------------------
  def wait_for_effect
    update_for_wait
    update_for_wait while @spriteset.effect?
  end
  # ●   
  def on_category_ok
    cell = @puzzle_window.index
    acell = cell_adjacent_empty?(cell)
    if acell >= 0
      Sound.play_ok
      @count += 1
      @puzzle_window.get_count(@count)
      transposition(cell ,acell)
      
      @puzzle_window.get_board_info(@board)
      if game_end?
        @puzzle_window.refresh
        @puzzle_window.game_end
        @puzzle_window.index = -1

        result_game
        $game_variables[@puzzle.variable] = @result_game 
        wait(20)
        @puzzle_window.refresh
        while !Input.trigger?(:C)
          update_for_wait
        end    
        if @count_flag
          result_message_txt
          reversi_message(2,0)

          wait_for_message    
        end  
        return_scene
      else
        @puzzle_window.refresh
        @puzzle_window.activate   

      end  
      
    else
      Sound.play_buzzer
      @puzzle_window.activate
    end    
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新（ウェイト用）
  #--------------------------------------------------------------------------
  def update_for_wait
    update_basic
  end  
  #--------------------------------------------------------------------------
  # ● ウェイト
  #--------------------------------------------------------------------------
  def wait(duration)
    duration.times {|i| update_for_wait if i < duration / 2 || !show_fast? }
  end  
  #--------------------------------------------------------------------------
  # ● 文章の表示
  #--------------------------------------------------------------------------
  def reversi_message(position,index,type = 0)

#~     $game_message.face_name = face_name
#~     $game_message.face_index = face_index

    $game_message.position = position
    if type == 0      # 文章データ
      $game_message.add(@list[index])
    end
  end    
  def result_message_txt
    numw = max_count - @count
    resulttxt = sprintf("残りカウント : %s ",numw )
    
    @list[0]  = resulttxt   
  end  
  
  def result_game
    numw = max_count - @count
    @result_game = numw >= 0 ? 1 : 2
    @result_game = 1 unless @count_flag
    
  end  
    #--------------------------------------------------------------------------
  # ● 目標カウントの計算
  #--------------------------------------------------------------------------
  def max_count
    return @puzzle.max_count
  end    
  # ● ゲーム終了の判定
  def game_end?
    @board.each_with_index do |tile , index|
      return false unless tile.id == index 
    end  
    return true
  end    
  # ● 指定したセルidがボードの内部にあるか
  def in_board?(cell)
    return false if cell < 0
    return false if cell > @board.size - 1
    return true
  end  
  # ● 隣接するセルid direction 1から9で指定
  def get_cell_adjacent(cell_id,direction)
    dy = (direction - 1) / 3 - 1
    dx = (direction - 1) % 3 - 1
    return cell_id + @puzzle.ncolumn * dy + dx
  end    
  
  # ● 空タイルに隣接するセルであるか 真ならば空セルのidを返す　偽ならば-1
  def cell_adjacent_empty?(cell_id)
    directions_cell_movable(cell_id).each do |d|
      acell = get_cell_adjacent(cell_id,d)
      if in_board?(acell)
        return acell if @board[acell].empty_tile
      end  
    end  
    return - 1
  end    
  
  # ● セルが移動可能な方向を返す
  def directions_cell_movable(cell_id)
    array = []
    array.push(4) if cell_id % @puzzle.ncolumn != 0
    array.push(6) if cell_id % @puzzle.ncolumn != @puzzle.ncolumn - 1
    array.push(2) if cell_id / @puzzle.ncolumn != 0
    array.push(8) if cell_id / @puzzle.ncolumn != @puzzle.nrow - 1
    return array
  end      
  
  # ● 互換
  def transposition(cell1 ,cell2)
    tile1 = @board[cell1]
    tile2 = @board[cell2]
    @board[cell1] = tile2
    @board[cell2] = tile1    
  end  
  # ● ランダムな互換
  def transposition_random
    rnd = rand(item_max - 1)
    rnd2 = rand(item_max - 1)
    while rnd == rnd2
      rnd2 = rand(item_max - 1 )
    end
    transposition(rnd ,rnd2)  
  end    
  # ● ランダムな偶置換
  def even_proposition_random(count)
    count.times do
      transposition_random
      transposition_random
    end
  end  
  
end  