class Scene_Puzzle_4cycle < Scene_MenuBase
  
  #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  def start
    super

    set_init
    create_puzzle_window
    set_Tile_Tips
    proposition_random
    @puzzle_window.get_board_info(@board)
    @puzzle_window.refresh
  end
  
  #--------------------------------------------------------------------------
  # ● タイルたちの初期設定
  #--------------------------------------------------------------------------
  def set_Tile_Tips
    @board = item_max.times.collect do |i|
      Tile_Tip_Cycle4.new(i,@bwidth , @bheight , false)
    end
  end    
  #--------------------------------------------------------------------------
  # ● 変数などのセット
  #--------------------------------------------------------------------------
  def set_init
    @list = []
    @count = 0
    @bwidth = $data_puzzle4cycle_width 
    @bheight = $data_puzzle4cycle_height 
  end
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  def item_max
    return @bwidth * @bheight
  end
  #--------------------------------------------------------------------------
  # ● コマンドウィンドウの作成
  #--------------------------------------------------------------------------
  def create_puzzle_window
    @puzzle_window = Window_Puzzle_4cycle.new(0, 0, Graphics.width, Graphics.height,@bwidth,@bheight)
    @puzzle_window.set_handler(:ok,     method(:on_category_ok))
    @puzzle_window.set_handler(:cancel, method(:return_scene))
    @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
    Sound.play_ok
    @count += 1
    @puzzle_window.get_count(@count)
    transform_4circle
      
    @puzzle_window.get_board_info(@board)
    if game_end? 
      process_game_end 
    else
      @puzzle_window.refresh
      @puzzle_window.activate   
    end  
  end
  
  
  def process_game_end 
    @puzzle_window.refresh
    @puzzle_window.game_end
    $game_variables[$MINIGAMERESULT_puzzle4circle] += 1#変数へ勝敗結果を入れる
    wait(20)
    @puzzle_window.refresh
    while !Input.trigger?(:C)
      update_for_wait
    end          
    return_scene    
  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 game_end?
    @board.each_with_index do |tile , index|
      return false unless tile.id == index 
    end  
    return true
  end    

  # ● カーソル内のセルを回転して配置しなおす
  def transform_4circle
    index = @puzzle_window.index
    col_max = @puzzle_window.col_max
    a = @board[index]
    @board[index] = @board[index + col_max]
    @board[index + col_max] = @board[index + col_max + 1]
    @board[index + col_max + 1] = @board[index + 1] 
    @board[index + 1] = a
    
  end
  
  # ● ランダムな置換
  def proposition_random
    new_board = []
    board = @board.dup
    while board.size > 0
      a = rand(board.size)
      new_board.push(board[a])
      board.delete(board[a])
    end
    @board = new_board
  end  

  
end  