#==============================================================================
# ■ Open_Panel vol.1.41
#------------------------------------------------------------------------------
# ※『Game_Party_Coin』(必須)
# ※『Window_digits_number』(必須)
# ※『Coin_Game_Base』(必須)より下に挿入
# 11/09/10
#==============================================================================
module HIDE_PANEL
  #==============================================================================
  # ■ デバッグ(開発者用)『A』キーでパネル取得
  #==============================================================================
  DEBUG = false#true#
  #==============================================================================
  # ■ ゲーム設定
  #==============================================================================
  FARST_OPEN = 12                  # 最初のオープンパネル数
  OPEN_CHANCE = 6                  # パネルをオープンできる回数(1〜9)
  SPEED = 2                        # パネルのめくり速度(大=遅)
  PANEL_WAIT = 20                  # 最初のオープンパネルを閉じるまでの時間(1〜99)
  #==============================================================================
  # ■ アイコン設定 ※Iconsフォルダ内のファイル名を指定
  #==============================================================================
  COIN_ICON = "032-Item01"          # コインのアイコン
  GOLD_ICON = "034-Item03"          # お金のアイコン
  #==============================================================================
  # ■ グラフィック設定 ※Windowskinsフォルダ内のファイル名を指定
  #==============================================================================
  MARK = "panel_mark"              # パネルマーク画像
  BACK = "panel_background"        # オープンパネルの背景画像
  PANEL = "panel"                  # パネル画像
  SELECT = "poker_back"            # カーソル画像
  CURSOR = "cursor_panel"          # パネル選択カーソル画像
  #==============================================================================
  # ■ 選択肢とヘルプの設定
  #==============================================================================
  GAME_COMS = ["Start", "End"]
  GAME_HELPS = ["ゲームを開始します。", "ゲームを終了します。"]
  PANEL_HELP1 = "パネル"
  PANEL_HELP2 = "をオープンします。"
  # ※PANEL_HELP1 + 番号 + PANEL_HELP2 =パネル1をオープンします。
  PANEL_HELP3 = "全てのパネルをオープンしました。"
  #==============================================================================
  # ■ 音の設定 ※ファイル名を指定 ※Audioフォルダ内 #()のファイル名
  #==============================================================================
  BGM = "019-Field02"              # オープンパネル実行中のBGM(BGM)
  CHENGE_SE = "046-Book01"         # パネルめくりの効果音(SE)
  DECISION_SE = "002-System02"     # 決定キーの効果音(SE)
  CANCEL_SE = "003-System03"       # キャンセルキーの効果音(SE)
  BUZZER_SE = "004-System04"       # ブザー音の効果音(SE)
  HIT_SE = "015-Jump01"            # パネルが揃った場合の効果音(SE)
  NOT_SE = "017-Jump03"            # パネルが揃わない場合の効果音(SE)
  HIT_ME_MAX = "009-Fanfare03"     # 全てのパネルをオープンした場合の効果音(ME)
  SHUFFLE_ME = "016-Shock01"       # シャッフルオープンの効果音(ME)
  
  SHUFFLE_ME_TIME = 3              # シャッフルオープンの効果音(SHUFFLE_MEの演奏時間(秒))
  ME_MAX_TIME = 16                 # 大当りの待ち時間(HIT_ME_MAXの演奏時間(秒))
  #==============================================================================
  # ■ オープンパネルタイプの設定
  # COIN[n] = value　※n=タイプ番号($game_temp.panel_type から参照される)
  #==============================================================================
  COIN = [] # value=消費コイン
  COIN[0] = 500 #500枚賭け
  COIN[1] = 50
  COIN[2] = 5
  # □value=獲得コイン数(全てオープン)
  #==============================================================================
  # ■ パネルの絵柄パターンを設定(手動設定)
  #==============================================================================
  # PANELS[n] = [ value, ... ]
  # n=パターン番号(PATTERN[n]から参照される)
  # ※valueを必ず12個指定する
  # ※同じvalueを複数指定しない[w1,w1...]など
  # ※同じアイコンIDを使用するvalueを複数指定しない[w1,w2...]など
  #--------------------------------------------------------------------------
  # "i(ID)"      …i=アイテム,ID=アイテムID
  # "w(ID)"      …w=武器,ID=武器ID
  # "a(ID)"      …a=防具,ID=防具ID
  # "g(num)"     …g=お金,num=金額
  # "c(num)"     …c=コイン,num=枚数
  #==============================================================================
  PANELS = {}
  PANELS[0] = ["w1","w5","w9","w13","w17","w21","w25","w29","a1","a13","a25","i1"]
  PANELS[1] = ["c100","g1000","m9","w13","m7","w21","m5","w29","a1","m3","a25","m1"]
  PANELS[2] = ["m0","m1","m2","m3","m4","m5","m6","m7","m8","m9","m10","m11"]
  #==============================================================================
  # ■ パネルの絵柄パターン集を設定(手動設定)
  # ※指定したパネルの絵柄パターンからランダムに決定される
  #==============================================================================
  # PATTERN[n] = [ value, ... ]
  # n=パターン番号($game_temp.slot_pattern から参照される)
  # value= PANELS[n]のn番号を指定
  #==============================================================================
  PATTERN = {}
  PATTERN[0] = [0,0,0,1,1,1,2]
end
#==============================================================================
# ■ Window_select_panel
#==============================================================================
class Window_select_panel < Window_Selectable
  include HIDE_PANEL
  attr_accessor :panel
  attr_accessor :panel_pair
  attr_accessor :open_pair
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(helps,help_fonts)
    super(0,0,640,480)
    @column_max = 5
    self.index = -1
    self.active = false
    self.windowskin = Bitmap.new(1,1)
    @item_max = 25
    self.contents = Bitmap.new(width-32, row_max * 80)
    for i in 0...25
      x = (i%5*80)-(i%5*8)
      y = (i/5*80)-(i/5*8)
      pic = RPG::Cache.windowskin(HIDE_PANEL::CURSOR)
      self.contents.blt(x,y,pic,Rect.new(0,0,80,80))
    end
    pic = RPG::Cache.windowskin(HIDE_PANEL::CURSOR)
    @cursor_window = Sprite.new(Viewport.new(16,16,400,400))
    @cursor_window.bitmap = Bitmap.new(80,80)
    @cursor_window.bitmap.blt(0,0,pic,Rect.new(80,0,80,80))
    @cursor_window.viewport.z = self.z + 2
    @cursor_window.visible = false
    #…………………………………………………………………………………………………
    # ■アイテム描画ウィンドウの作成
    #…………………………………………………………………………………………………
    @item_window = Sprite.new
    @item_window.bitmap = Bitmap.new(320,32*12)
    @item_window.x = 320+96
    @item_window.y = 16
    #…………………………………………………………………………………………………
    # ■フラッシュウィンドウの作成
    #…………………………………………………………………………………………………
    @item_flash_window = Sprite.new
    @item_flash_window.bitmap = Bitmap.new(320,32*12)
    @item_flash_window.x = 320+96
    @item_flash_window.y = 16
    @item_flash_window.visible = false
    @bitmaps = {}
    @bitmaps_flash = {}
    @helps = helps
    @help_fonts = help_fonts
    make_helps(@helps)
    refresh
    # □パネルの作成
    make_window
    @panel_flash = 150
  end
  #--------------------------------------------------------------------------
  # ■ パネルの点滅更新
  #--------------------------------------------------------------------------
  def flash
    if @blink_opa
      if @panel_flash >= 150
        @blink_opa = false
      end
      @back_opa = -5
    else
      if @panel_flash <= 0
        @blink_opa = true
      end
      @back_opa = 5
    end
    @panel_flash -= @back_opa
    @panels.each{ |a| a.flash(Color.new(255,255,255,@panel_flash),0) }
    @item_flash_window.visible = true
    @item_flash_window.flash(Color.new(255,255,0,@panel_flash),0)
  end
  #--------------------------------------------------------------------------
  # ■ パネルの点滅更新の終了
  #--------------------------------------------------------------------------
  def flash_end
    @panel_flash = 150
    @panels.each{ |a| a.flash(Color.new(0,0,0,0),0) }
    @item_flash_window.flash(Color.new(0,0,0,0),0)
  end
  #--------------------------------------------------------------------------
  # ■ アイテムの獲得
  #--------------------------------------------------------------------------
  def gain_treasures?
    for treasure in HIDE_PANEL::PANELS[@rand_num]
      treasure.scan(/([iwagcsm])([0-9]+)/)
      type = $1 ; id = $2
      case type
      when "i"
        $game_party.gain_item(id.to_i,1)
      when "w"
        $game_party.gain_weapon(id.to_i,1)
      when "a"
        $game_party.gain_armor(id.to_i,1)
      when "g" # お金
        $game_party.gain_gold(id.to_i)
      when "c" # コイン
        $game_party.gain_coin(id.to_i)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 初期化
  #--------------------------------------------------------------------------
  def refresh
    @rand_num = nil
    @panel_pair = {}
    @open_pair = {}
    for i in 0...13
      @panel_pair[i] = []
      @open_pair[i] = nil
    end
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプBitmapを作成(軽量化)
  #--------------------------------------------------------------------------
  def make_helps(data)
    @helps_bitmap = Bitmap.new(640-32,32*data.size)
    for i in 0...data.size
      @helps_bitmap.draw_cache_text(8,i*32,640,32,data[i],0,@help_fonts)
    end
  end
  #--------------------------------------------------------------------------
  # ■ アイテムの取得
  #--------------------------------------------------------------------------
  def data
    return @helps[self.index]
  end
  #--------------------------------------------------------------------------
  # ■ ヘルプテキスト更新
  #--------------------------------------------------------------------------
  def update_help
    @help_window.contents = @helps_bitmap
    @help_window.oy = self.index*32
  end
  #--------------------------------------------------------------------------
  # ■ パネルの作成
  #--------------------------------------------------------------------------
  def make_window
    @panels = []
    @viewports = []
    for i in 0...25
      x = (i%5*64)+(i%5+1)*8+16
      y = (i/5*64)+(i/5*8)+16+8
      @viewports[i] = Viewport.new(x,y,64,64)
      @panels[i] = Sprite.new(@viewports[i])
      @panels[i].bitmap = Bitmap.new(64,64*16)
      @panels[i].viewport.z = self.z + 2
    end
    @panel = make_pattern?
    draw_panel?(@panel,[8]*25)
    make_panel?(@panel,[8]*25)
  end
  #--------------------------------------------------------------------------
  # ■ 解放
  #--------------------------------------------------------------------------
  if !$@;alias hidesp_select_panel_dispose dispose;end
  def dispose
    super
    @viewports.each{ |a| a.dispose }
    @panels.each{ |a| a.bitmap.dispose }
    @panels.each{ |a| a.dispose }
    @item_window.bitmap.dispose
    @item_window.dispose
    @item_flash_window.bitmap.dispose
    @item_flash_window.dispose
    
    @cursor_window.bitmap.dispose
    @cursor_window.viewport
    @cursor_window.dispose
    @bitmaps.each{ |a| a[1].dispose }
  end
  #--------------------------------------------------------------------------
  # ■ パネルパターンを作成
  #--------------------------------------------------------------------------
  def make_pattern?
    patterns = [] ; data = []
    if @rand_num == nil
      pa = HIDE_PANEL::PATTERN[$game_temp.panel_pattern]
      @rand_num = pa[rand(pa.size)]
    end
    panels = HIDE_PANEL::PANELS[@rand_num]
    panels.push("s0")
    for i in 0...25
      data.push(i)
    end
    while data != []
      n = rand(data.size)
      c = data[n]
      if c > 12
        @panel_pair[c-13][1] = patterns.size
        patterns.push(panels[c-13])
      else
        @panel_pair[c][0] = patterns.size
        patterns.push(panels[c])
      end
      data.delete_if{ |a| a == c }
    end
    return patterns
  end
  #--------------------------------------------------------------------------
  # ■ パネル絵柄の作成
  #--------------------------------------------------------------------------
  def check_panel?(n)
    data = @panel[n]
    name = "" ; icon_name = ""
    rect = Rect.new(0,0,24,24)
    data.scan(/([iwagcsm])([0-9]+)/)
    type = $1 ; id = $2
    x = 0 ; y = 0
    case type
    when "i"
      name = $data_items[id.to_i].name
      icon = RPG::Cache.icon($data_items[id.to_i].icon_name)
    when "w"
      name = $data_weapons[id.to_i].name
      icon = RPG::Cache.icon($data_weapons[id.to_i].icon_name)
    when "a"
      name = $data_armors[id.to_i].name
      icon = RPG::Cache.icon($data_armors[id.to_i].icon_name)
    when "g" # お金
      name = id.to_s + $data_system.words.gold
      icon = RPG::Cache.icon(HIDE_PANEL::GOLD_ICON)
    when "c" # コイン
      name = id.to_s + "枚"
      icon = RPG::Cache.icon(HIDE_PANEL::COIN_ICON)
    when "m" # 絵柄パネル
      icon = RPG::Cache.windowskin(HIDE_PANEL::MARK)
      rx = id.to_i%6*64
      ry = id.to_i/6*64
      rect = Rect.new(rx,ry,64,64)
    when "s" # シャッフルパネル
      icon = nil
    end
    # □アイコン描画位置を取得
    case type
    when "i","w","a","g","c"
      x = (64-icon.width)/2
      y = (64-icon.height)/2
    end
    bitmap = Bitmap.new(64,64)
    pic = RPG::Cache.windowskin(HIDE_PANEL::PANEL)
    # □パネルの作成
    for i in @panel_pair.keys
      if @panel_pair[i].include?(n)
        bitmap.blt(0,0,pic,Rect.new((i%6)*64,(i/6)*64,64,64))
        if icon != nil
          bitmap.blt(x,y,icon,rect)
        end
        bitmap.blt(0,0,pic,Rect.new(64,64*2,64,64))
        break
      end
    end
    #…………………………………………………………………………………………………
    # ■アイテム名+アイコンをBitmap描画して保管
    #…………………………………………………………………………………………………
    if name != "" && ! @bitmaps.include?(data)
      item_bitmap = Bitmap.new(320,32)
      item_bitmap.blt(4,4,icon,Rect.new(0,0,24,24))
      fonts = Copy_Font.new
      fonts.color = Color.new(255,255,255,255)
      fonts.shadow = true
      fonts.cache_key?
      item_bitmap.draw_cache_text(28+4,0,212,32,name,0,fonts)
      @bitmaps[data] = item_bitmap
      fonts.shadow = false
      fonts.cache_key?
      flash_bitmap = Bitmap.new(320,32)
      flash_bitmap.draw_cache_text(28+4,0,212,32,name,0,fonts)
      @bitmaps_flash[data] = flash_bitmap
    end
    return bitmap
  end
  #--------------------------------------------------------------------------
  # ■ アイテムの描画
  #--------------------------------------------------------------------------
  def draw_item?
    @item_window.bitmap.clear
    @item_flash_window.bitmap.clear
    n = 0
    for i in @open_pair.keys
      if @open_pair[i] != nil
        key = @panel[@panel_pair[i][0]]
        pic = @bitmaps[key]
        fpic = @bitmaps_flash[key]
        if pic != nil
          @item_window.bitmap.blt(0,@open_pair[i]*32,pic,Rect.new(0,0,320,32))
          @item_flash_window.bitmap.blt(0,@open_pair[i]*32,fpic,Rect.new(0,0,320,32))
        else
          n += 1
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ パネルの描画
  #--------------------------------------------------------------------------
  def draw_panel?(panel,type = [8]*25)
    @panel = panel
    for i in 0...25
      @panels[i].bitmap.clear
      pic = check_panel?(i)
      shadow = RPG::Cache.windowskin(HIDE_PANEL::PANEL)
      @panels[i].bitmap.blt(0,0,pic,Rect.new(0,0,64,64))
      for n in 1..3
        rect = Rect.new(8*n,64*n,64-n*16,64)
        @panels[i].bitmap.stretch_blt(rect,pic,Rect.new(0,0,64,64))
        @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*3,64*2,64,64))
      end
      rect = Rect.new(31,64*4,2,64)
      @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*5,64*2,64,64))
      for n in 1..3
        rect = Rect.new(8*(4-n),64*(n+4),64-(4-n)*16,64)
        @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*2,64*2,64,64))
        @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*4,64*2,64,64))
      end
      @panels[i].bitmap.blt(0,64*8,shadow,Rect.new(64*2,64*2,64,64))
      for n in 1..3
        rect = Rect.new(8*n,64*(n+8),64-n*16,64)
        @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*2,64*2,64,64))
        @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*3,64*2,64,64))
      end
      rect = Rect.new(31,64*12,2,64)
      @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*5,64*2,64,64))
      for n in 1..3
        rect = Rect.new(8*(4-n),64*(n+12),64-(4-n)*16,64)
        @panels[i].bitmap.stretch_blt(rect,pic,Rect.new(0,0,64,64))
        @panels[i].bitmap.stretch_blt(rect,shadow,Rect.new(64*4,64*2,64,64))
      end
      @panels[i].bitmap.blt(0,64*16,pic,Rect.new(0,0,64,64))
    end
  end
  #--------------------------------------------------------------------------
  # ■ パネルの更新
  #--------------------------------------------------------------------------
  def make_panel?(panel,type = [8]*25)
    @panel = panel
    for i in 0...25
      @panels[i].oy = 64*type[i]
      if type[i] == 16
        @panels[i].oy = 0
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの停止
  #--------------------------------------------------------------------------
  def active=(active)
    if active == false && @cursor_window != nil
      @cursor_window.opacity = 250
    end
    super
  end
  #--------------------------------------------------------------------------
  # ■ カーソルウィンドウの可視化
  #--------------------------------------------------------------------------
  def visible=(visible)
    @cursor_window.visible = visible
    super
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの更新
  #--------------------------------------------------------------------------
  def update_panel_cursor
    # □不可視化
    if self.index == -1
      @cursor_window.visible = false
      @cursor_window.opacity = 250
      return
    end
    # □点滅の更新
    if @cursor_window != nil && self.active
      if @cursor_panel
        if @cursor_window.opacity >= 250
          @cursor_panel = false
        end
       @cursor_panel_opa = -10
      else
        if @cursor_window.opacity <= 50
          @cursor_panel = true
        end
       @cursor_panel_opa = 10
      end
      @cursor_window.opacity -= @cursor_panel_opa
    end
  end
  #--------------------------------------------------------------------------
  # ■ 先頭の行の取得
  #--------------------------------------------------------------------------
  def top_row
    # ウィンドウ内容の転送元 Y 座標を、1 行の高さ 64 で割る
    return self.oy / 64
  end
  #--------------------------------------------------------------------------
  # ■ 先頭の行の設定
  #     row : 先頭に表示する行
  #--------------------------------------------------------------------------
  def top_row=(row)
    # row が 0 未満の場合は 0 に修正
    if row < 0
      row = 0
    end
    # row が row_max - 1 超の場合は row_max - 1 に修正
    if row > row_max - 1
      row = row_max - 1
    end
    # row に 1 行の高さ 64 を掛け、ウィンドウ内容の転送元 Y 座標とする
    self.oy = row * 64
  end
  #--------------------------------------------------------------------------
  # ■ 1 ページに表示できる行数の取得
  #--------------------------------------------------------------------------
  def page_row_max
    # ウィンドウの高さから、フレームの高さ 64 を引き、1 行の高さ 64 で割る
    return (self.height - 32) / 64
  end
  #--------------------------------------------------------------------------
  # ■ カーソルの矩形更新
  #--------------------------------------------------------------------------
  def update_cursor_rect
    # カーソル位置が 0 未満の場合
    if @index < 0
      self.cursor_rect.empty
      return
    end
    # 現在の行を取得
    row = @index / @column_max
    # 現在の行が、表示されている先頭の行より前の場合
    if row < self.top_row
      # 現在の行が先頭になるようにスクロール
      self.top_row = row
    end
    # 現在の行が、表示されている最後尾の行より後ろの場合
    if row > self.top_row + (self.page_row_max - 1)
      # 現在の行が最後尾になるようにスクロール
      self.top_row = row - (self.page_row_max - 1)
    end
    # カーソルの幅を計算
    cursor_width = self.width / @column_max - 32
    # カーソルの座標を計算
    x = (@index%@column_max*72)
    y = @index / @column_max * 72 - self.oy
    # カーソルの矩形を更新
    @cursor_window.ox = -x
    @cursor_window.oy = -y
    self.cursor_rect.set(x+8,y+24,64,32)
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新
  #--------------------------------------------------------------------------
  def update
    # □パネルカーソルの更新
    update_panel_cursor
    # □元の処理を無効化(superは呼び出す)
    @old_index = @index
    super
    @index = @old_index
    # カーソルの移動が可能な状態の場合
    if self.active && @item_max > 0 && @index >= 0
      # 方向ボタンの下が押された場合
      if Input.repeat?(Input::DOWN)
        if @index + @column_max < @item_max
          @index += @column_max
        else
          @index = @index % @column_max
        end
        # カーソルSEを演奏
        $game_system.se_play($data_system.cursor_se)
      end
      # 方向ボタンの上が押された場合
      if Input.repeat?(Input::UP)
        if @index - @column_max >= 0
          @index -= @column_max
        else
          @index = @index + @item_max - @column_max
        end
        # カーソルSEを演奏
        $game_system.se_play($data_system.cursor_se)
      end
      # 方向ボタンの右が押された場合
      if Input.repeat?(Input::RIGHT)
        if (@index + 1) % @column_max > 0
          @index += 1
        else
          @index = (@index + 1) - @column_max
        end
        # カーソルSEを演奏
        $game_system.se_play($data_system.cursor_se)
      end
      # 方向ボタンの左が押された場合
      if Input.repeat?(Input::LEFT)
        if @index % @column_max > 0
          @index -= 1
        else
          @index = (@index - 1) + @column_max
        end
        # カーソルSEを演奏
        $game_system.se_play($data_system.cursor_se)
      end
    end
    # ヘルプテキストを更新 (update_help は継承先で定義される)
    if self.active and @help_window != nil
      update_help
    end
    # カーソルの矩形を更新
    update_cursor_rect
  end
end
#==============================================================================
# ■ Scene_Panel
#==============================================================================
class Scene_Panel
  include HIDE_PANEL
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    game_reset?
    # □マップ BGM を記憶し、BGM を停止
    $game_temp.map_bgm = $game_system.playing_bgm
    $game_system.bgm_stop
    # □パネル BGM を演奏
    $game_system.bgm_play(RPG::AudioFile.new(HIDE_PANEL::BGM))
  end
  #--------------------------------------------------------------------------
  # ■ パネルの初期化
  #--------------------------------------------------------------------------
  def game_reset?
    # □パネルをオープンできる回数
    @open_count = HIDE_PANEL::OPEN_CHANCE
    # □アニメを行うパネルを格納
    @card = {}
    # □パネルの向きを格納
    @values = {}
    # □既に揃ったパネル配列
    @opens = []
    # □一時的に格納するパネル
    @pair = []
    # □既に揃ったパネルのペア配列
    @open_pairs = []
    # □アイテム表示順番を格納
    @numbers = []
    for c in 0...25
      @card[c] = false
      @values[c] = 8
    end
    @card_close = true
    @card_open = true
    @panel_step = 0
    @speed = HIDE_PANEL::SPEED
    # □パネルの初期状態(裏向き=8)
    @card_count = 8*@speed
    # □パネルのアニメーションフラグをOFF
    @move = false
    # □ゲーム実行中フラグをOFF
    @playing = false
  end
  #--------------------------------------------------------------------------
  # ■ メイン処理
  #--------------------------------------------------------------------------
  def main
    # ヘルプウィンドウを作成
    @help_window = Window_Help.new
    @help_window.opacity = 0
    @help_window.windowskin = Bitmap.new(1,1)
    @help_window.y = 480-64
    # □パネルウィンドウ背景を作成
    @panel_window_back = Window_Coin_Game_back.new(HIDE_PANEL::BACK,"Coin",HIDE_PANEL::COIN[$game_temp.panel_type],408,406)
    # □通常色/影色を設定
    n_color = Color.new(255,255,255,255)
    s_color = Color.new(0,0,0,200)
    help_fonts = Copy_Font.new(16,false,false,true,false,"ＭＳ ゴシック",n_color,s_color)
    # □パネルウィンドウを作成
    helps = []
    for i in 0...25
      helps.push(HIDE_PANEL::PANEL_HELP1 + (i+1).to_s + HIDE_PANEL::PANEL_HELP2)
    end
    helps.push(HIDE_PANEL::PANEL_HELP3)
    @panel_window = Window_select_panel.new(helps,help_fonts)
    @panel_window.help_window = @help_window
    # □コイン数値ウィンドウを作成
    fonts = Copy_Font.new(26,true,false,true,false,["Arial","ＭＳ ゴシック"],n_color,s_color)
    @coin_window = Window_digits_number.new(440,415,100,$game_party.coin,10,10,fonts)
    # □カウントダウンウィンドウを作成
    fonts = Copy_Font.new(18,true,false,true,false,["Arial","ＭＳ ゴシック"],n_color,s_color)
    @count_time = HIDE_PANEL::PANEL_WAIT
    @count_down_window = Window_digits_number.new(360-64-8,416+1,100,@count_time,2,0,fonts)
    @count_down_window.visible = false
    # □残り回数ウィンドウを作成
    fonts = Copy_Font.new(46,true,false,true,false,["Arial","ＭＳ ゴシック"],n_color,s_color)
    fonts.colors = [Color.new(255,0,0,255)]
    @count_window = Window_digits_number.new(376,379,100,@open_count,1,10,fonts)
    # □ゲーム開始選択ウィンドウを作成
    fonts = Copy_Font.new(22,true,false,true,false,["Georgia","ＭＳ ゴシック"],n_color,s_color)
    @panel_window_back.draw_text(304,400,160,32,"Chance",0,fonts)
    com = HIDE_PANEL::GAME_COMS
    pic = HIDE_PANEL::SELECT
    helps = HIDE_PANEL::GAME_HELPS
    helps.push("決定キーで開始します。 待ち時間：")
    @game_select_window = Window_Picture_Select.new(0,384,128*2,64,com,pic,fonts,helps,help_fonts)
    # □ヘルプウィンドウの関連付け
    @game_select_window.help_window = @help_window
    @game_select_window.index = 0
    @game_select_window.active = true
    @game_select_window.visible = true
    # □選択肢の文字色を初期化
    exchange_colors?
    
    # トランジション実行
    Graphics.transition
    # メインループ
    loop do
      # ゲーム画面を更新
      Graphics.update
      # 入力情報を更新
      Input.update
      # フレーム更新
      update
      # 画面が切り替わったらループを中断
      if $scene != self
        break
      end
    end
    # トランジション準備
    Graphics.freeze
    # ウィンドウを解放
    @help_window.dispose
    @panel_window_back.dispose
    @panel_window.dispose
    @coin_window.dispose
    @count_window.dispose
    @game_select_window.dispose
  end
  #--------------------------------------------------------------------------
  # ■ ゲームの終了
  #--------------------------------------------------------------------------
  def panel_end
    unless @playing
      # □ポーカー開始前の BGM に戻す
      $game_system.bgm_play($game_temp.map_bgm)
      # マップ画面に切り替え
      $scene = Scene_Map.new
      return
    end
  end
  #--------------------------------------------------------------------------
  # ■ 選択肢の色更新
  #--------------------------------------------------------------------------
  def exchange_colors?
    # □ゲーム選択色を変更
    @game_select_window.colors[0] = $game_party.coin < HIDE_PANEL::COIN[$game_temp.panel_type]
    @game_select_window.refresh
  end
  #--------------------------------------------------------------------------
  # ■ 最初のオープンパネルを選択
  #--------------------------------------------------------------------------
  def first_open?
    opens = [] ; data = []
    for i in 0...25
      data.push(i)
    end
    while opens.size < HIDE_PANEL::FARST_OPEN
      n = rand(data.size)
      c = data[n]
      opens.push(c)
      data.delete_if{ |a| a == c }
    end
    return opens
  end
  #--------------------------------------------------------------------------
  # ■ アニメ開始フラグを初期化
  #--------------------------------------------------------------------------
  def reset_card?
    for i in 0...25
      @card[i] = false
    end
  end
  #--------------------------------------------------------------------------
  # ■ 揃ったパネルを取得
  #--------------------------------------------------------------------------
  def check_open?(num)
    check = false
    for i in @panel_window.panel_pair.keys
      data = @panel_window.panel_pair[i]
      if num.include?(data[0]) && num.include?(data[1])
        unless @open_pairs.include?(i)
          @panel_window.open_pair[i] = @numbers.size
          @open_pairs.push(i)
          @panel_window.panel[data[0]].scan(/([iwagcsm])([0-9]+)/)
          if $1 != "m"
            @numbers.push(i)
          end
          check = true
        end
      end
    end
    return check
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新(ゲーム開始)
  #--------------------------------------------------------------------------
  def update_select
    #…………………………………………………………………………………………………
    # ■ C ボタンが押された場合
    #…………………………………………………………………………………………………
    if Input.trigger?(Input::C)
      unless @move
        case @game_select_window.index
        when 0 #□ゲーム開始
          if $game_party.coin >= HIDE_PANEL::COIN[$game_temp.panel_type]
            @playing = true
            @card_close = true
            # □初めにめくるパネルを選択
            @opens = first_open?
            for i in @opens
              @card[i] = true
              @values[i] = 16
            end
            @card_open = true
            @game_select_window.visible = false
            @game_select_window.index = 2
            @count_down_window.visible = true
            @panel_step = 1
          else
            # ブザー SE を演奏
            $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::BUZZER_SE))
            @panel_step = 0
            @playing = false
          end
        when 1 #□ゲーム終了
          # □決定SEを演奏
          $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::DECISION_SE))
          panel_end
        end
      end
      return
    end
    #…………………………………………………………………………………………………
    # ■ B ボタンが押された場合
    #…………………………………………………………………………………………………
    if Input.trigger?(Input::B)
      # □キャンセルSEを演奏
      $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::CANCEL_SE))
      @playing = false
      panel_end
      return
    end
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新(パネルの選択)
  #--------------------------------------------------------------------------
  def update_panel
    unless @move
      if Input.trigger?(Input::A) && HIDE_PANEL::DEBUG
        @open_count = 9
        p @opens
        return
      end
      #…………………………………………………………………………………………………
      # ■ C ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.trigger?(Input::C)
        #…………………………………………………………………………………………………
        # ■ 既にオープンのパネルを選択した場合
        #…………………………………………………………………………………………………
        if @opens.include?(@panel_window.index) or @pair.include?(@panel_window.index)
          # □ブザーSEを演奏
          $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::BUZZER_SE))
          return
        end
        
        @panel_window.active = false
        reset_card?
        if @values[@panel_window.index] == 8 && @card_close
          @card_close = false
        end
        #…………………………………………………………………………………………………
        # ■ 裏から表に変わるアニメーションを実行
        #…………………………………………………………………………………………………
        if @card_close == false
          @card_close = true
          @card[@panel_window.index] = true
          @card_count = @speed * 8
          @card_open = true
        end
        @panel_step = 4
        return
      end
      #…………………………………………………………………………………………………
      # ■ B ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.trigger?(Input::B)
        # □キャンセルSEを演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::CANCEL_SE))
        # □表向きのパネルを全て裏向きに変更する
        move_close?
        @panel_window.index = -1
        @panel_step = 13
        return
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 表のパネルを裏にする設定
  #--------------------------------------------------------------------------
  def move_close?
    @card_close = false
    for i in @values.keys
      if @values[i] == 8
        @card[i] = false
      else
        @card[i] = true
      end
    end
    @card_count = 0
    @card_open = false
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新
  #--------------------------------------------------------------------------
  def update
    #…………………………………………………………………………………………………
    # ■ 回転数値の更新(所持コイン)
    #…………………………………………………………………………………………………
    @coin_window.update($game_party.coin)
    @count_window.update(@open_count,@open_count < 2 ? 1 : 0)
    #…………………………………………………………………………………………………
    # ■ ゲーム開始ウィンドウの更新
    #…………………………………………………………………………………………………
    if @game_select_window.active
      @game_select_window.update
      update_select
    end
    #…………………………………………………………………………………………………
    # ■ パネルウィンドウの更新
    #…………………………………………………………………………………………………
    if @panel_window.active
      @panel_window.update
      update_panel
    end
    #…………………………………………………………………………………………………
    # ■ ゲームの流れ
    #…………………………………………………………………………………………………
    case @panel_step
    when 1
      # □コインを減らす(賭け金)
      if $game_party.coin >= HIDE_PANEL::COIN[$game_temp.panel_type]
        $game_party.lose_coin(HIDE_PANEL::COIN[$game_temp.panel_type])
        # □ゲーム選択色を変更
        exchange_colors?
        @playing = true
        @game_select_window.active = false
        @panel_step = 2
      else
        # ブザー SE を演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::BUZZER_SE))
        @panel_step = 0
        @playing = false
      end
      return
    when 2
      # □パネルの開始
      if card_anim?(@card_close,@card)
        @game_select_window.active = false
        @game_select_window.reset?
        @panel_step = 8
      end
      return
    when 3
      @panel_window.visible = true
      # □交換するパネルの選択待ち
      return
    when 4
      # □交換するパネルの向きを変更(個別)
      if card_anim?(@card_close,@card)
        @panel_step = 5
      end
      return
    when 5
      #…………………………………………………………………………………………………
      # ■ シャッフルパネルの場合
      #…………………………………………………………………………………………………
      @panel_window.panel[@panel_window.index].scan(/([iwagcsm])([0-9]+)/)
      if $1 == "s"
        # □2枚目の場合は、1枚目を削除
        if @pair.size == 1
          @opens.delete_if{ |a| a == @pair[0] }
        end
        @pair = []
        # □シャッフルパネルを追加
        @opens.push(@panel_window.index)
        # □全パネルをオープンした場合
        if @opens.size == 25
          @panel_step = 14
          return
        end
        @time = 20
        @panel_step = 17
        return
      end
      #…………………………………………………………………………………………………
      # ■ 1枚目の場合は戻る
      #…………………………………………………………………………………………………
      @pair.push(@panel_window.index)
      if @pair.size == 1
        @panel_window.active = true
        @panel_step = 3
        return
      end
      #…………………………………………………………………………………………………
      # ■ パネルが揃った場合(パネルを取得)
      #…………………………………………………………………………………………………
      if check_open?(@pair)
        @time = 6
        # □待ち時間
        while @time > 0
          @time -= 1
          Graphics.update
        end
        # □パネルが揃った SE を演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::HIT_SE))
        @opens.push(@pair[0])
        @opens.push(@pair[1])
        # □アイテムを描画
        @panel_window.draw_item?
        @pair = []
        #…………………………………………………………………………………………………
        # ■ 全パネルをオープンした場合
        #…………………………………………………………………………………………………
        if @opens.size == 25
          @panel_step = 14
          return
        end
        @panel_window.active = true
        @panel_step = 3
        return
      #…………………………………………………………………………………………………
      # ■ パネルが揃わなかった場合
      #…………………………………………………………………………………………………
      else
        @time = 6
        # □待ち時間
        while @time > 0
          @time -= 1
          Graphics.update
        end
        # □パネルが揃った SE を演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::NOT_SE))
        
        # □オープン回数を減らす
        @open_count -= 1
        if @open_count <= 0
          # □表向きのパネルを全て裏向きに変更する
          move_close?
          @panel_window.index = -1
          @panel_window.visible = false
          @time = 20
          @panel_step = 18
          return
        end
        reset_card?
        if @values[@panel_window.index] == 16 && @card_close == false
          @card_close = true
        end
        # □表から裏に変わるアニメーションを実行
        if @card_close == true
          @card_close = false
          @card[@pair[0]] = true
          @card[@pair[1]] = true
          @card_count = 0
          @card_open = false
        end
        @time = 20
        @panel_step = 6
      end
      return
    when 6
      # □待ち時間(オープンしたパネルを戻す)
      while @time > 0
        @time -= 1
        Graphics.update
      end
      @opens.delete_if{ |a| a == @pair[0] }
      @opens.delete_if{ |a| a == @pair[1] }
      @panel_step = 7
      return
    when 7
      # □交換するパネルの向きを変更
      if card_anim?(@card_close,@card)
        @pair = []
        
        @panel_window.active = true
        @panel_step = 3
      end
      return
    when 8
      reset_card?
      if @values[@panel_window.index] == 16 && @card_close == false
        @card_close = true
      end
      # □表から裏に変わるアニメーションを実行
      if @card_close == true
        @card_close = false
        for i in @opens
          @card[i] = true
        end
        @card_count = 0
        @card_open = false
      end
      # □パネル選択の開始
      @game_select_window.index = -1
      @game_select_window.visible = false
      @time = 400
      @panel_step = 9
      return
    when 9
      # □待ち時間(最初のオープンパネルを戻す)
      while @time > 0
        # □回転数値の更新(所持コイン)
        @coin_window.update($game_party.coin)
        if @count_down_window.count_down(@count_time)
          @count_down_window.visible = false
          @count_down_window.count_stop(@count_time)
          @count_down_window.refresh(@count_time)
          @time = 0
        end
        # ゲーム画面を更新
        Graphics.update
        # 入力情報を更新
        Input.update
        # □決定キーが押された場合はカウントダウンの終了
        if Input.trigger?(Input::C)
          @count_down_window.visible = false
          @count_down_window.count_stop(@count_time)
          @count_down_window.refresh(@count_time)
          @time = 0
        end
      end
      @opens = []
      @panel_step = 10
      return
    when 10
      # □交換するパネルの向きを変更
      if card_anim?(@card_close,@card)
        @panel_step = 11
      end
      return
    when 11
      # □パネル交換の処理フラグをON
      @panel_window.active = true
      @panel_window.index = 0
      @panel_window.visible = true
      @panel_step = 3
      return
    when 12
      # □待ち時間(シャッフルME演奏待ち)
      while @time > 0
        @time -= 1
        Graphics.update
      end
      # □新しいパターンのパネルに更新(アニメ無し)
      @panel_window.draw_panel?(@new_pattern,@values)
      # □パネルの更新(アニメ無し)
      @panel_window.make_panel?(@panel_window.panel,@values)
      @panel_window.active = true
      @panel_step = 3
      return
    when 13
      # □交換するパネルの向きを変更
      if card_anim?(@card_close,@card)
        game_reset?
        @panel_window.refresh
        panel = @panel_window.make_pattern?
        @panel_window.draw_panel?(panel,[8]*25)
        @panel_window.make_panel?(panel,[8]*25)
        @panel_window.draw_item?
        @panel_window.active = false
        @panel_window.index = -1
        @panel_window.visible = false
        @game_select_window.index = 0
        @game_select_window.active = true
        @game_select_window.visible = true
        @panel_step = 0
      end
      return
    when 14
      # □パネルを全てオープンした場合
      if @opens.size == 25
        @panel_window.visible = false
        @panel_window.index = 25
        @panel_window.update_help
        # □アイテムの獲得
        @panel_window.gain_treasures?
        $game_system.me_play(RPG::AudioFile.new(HIDE_PANEL::HIT_ME_MAX))
        @time = HIDE_PANEL::ME_MAX_TIME*25
        @panel_step = 15
      end
      return
    when 15
      # □待ち時間(当り)
      while @time > 0
        @time -= 1
        # □回転数値の更新(所持コイン)
        @coin_window.update($game_party.coin)
        # □パネルの点滅更新
        @panel_window.flash
        Graphics.update
      end
      # □パネルの点滅更新を終了
      @panel_window.flash_end
      @panel_step = 16
      return
    when 16
      # □表向きのパネルを全て裏向きに変更する
      move_close?
      @panel_step = 13
      return
    when 17
      # □待ち時間(シャッフル待ち)
      while @time > 0
        @time -= 1
        Graphics.update
      end
      # □表向きのパネルを全て裏向きに変更する
      move_close?
      for i in @values.keys
        @values[i] = 8
      end
      old_opens = []
      for i in @opens
        old_opens.push(@panel_window.panel[i])
      end
      # □新しいパターンを取得
      @new_pattern = @panel_window.make_pattern?
      new_opens = []
      for i in 0...@new_pattern.size
        for n in old_opens
          if @new_pattern[i] == n && ! new_opens.include?(i)
            new_opens.push(i)
          end
        end
      end
      for i in 0...25
        if new_opens.include?(i)
          @values[i] = 0
        else
          @values[i] = 8
        end
      end
      @opens = new_opens.uniq
      # □シャッフル効果音を演奏
      $game_system.me_play(RPG::AudioFile.new(HIDE_PANEL::SHUFFLE_ME))
      @time = HIDE_PANEL::SHUFFLE_ME_TIME*25
      @panel_step = 12
      return
    when 18
      # □待ち時間(当り)
      while @time > 0
        @time -= 1
        # □回転数値の更新(所持コイン)
        @coin_window.update($game_party.coin)
      end
      @panel_step = 16
      return
    end
  end
  #--------------------------------------------------------------------------
  # ■ パネルめくりSEを演奏を許可？
  #--------------------------------------------------------------------------
  def se_play?
    se = {}
    for i in 0...25
      se[i] = false
    end
    return se
  end
  #--------------------------------------------------------------------------
  # ■ パネルのアニメーションを実行
  #--------------------------------------------------------------------------
  def card_anim?(close,num)
    #…………………………………………………………………………………………………
    # ■ 裏から表に変わるアニメーションを実行
    #…………………………………………………………………………………………………
    if close
      if @move == false
        # □パネルめくりSEを演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::CHENGE_SE))
      end
      card_pos = @speed * 16
      stop_pos = 8
      open = true
      @panel_window.visible = false
    #…………………………………………………………………………………………………
    # ■ 表から裏に変わるアニメーションを実行
    #…………………………………………………………………………………………………
    else
      if @move == false && @card != se_play?
        # □パネルめくりSEを演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_PANEL::CHENGE_SE))
      end
      card_pos = @speed * 8
      stop_pos = 0
      open = false
      @panel_window.visible = false
    end
    if @card_open == open
      if @card_count != card_pos
        @card_count += 1
        if @card_count % @speed == 0
          for i in num.keys
            if num[i] == true
              @values[i] = @card_count/@speed
            end
          end
          @move = true
          @panel_window.make_panel?(@panel_window.panel,@values)
        end
      elsif @card_count >= card_pos
        @move = false
        return true
      end
    end
    return false
  end
end