#==============================================================================
# ■ PLAY_DICE vol.1.00
#------------------------------------------------------------------------------
# ※サイコロを用いたスクリプトに必須
# 『SicBo 1.00』
# 12/03/03
#==============================================================================
module HIDE_PLAY_DICE
  #==============================================================================
  # ■ デバッグ(開発者用)
  #==============================================================================
  DEBUG = false                    # [true/false]
  DEBUG_NUMBER = 1
  #==============================================================================
  # ■ サイコロ設定
  #==============================================================================
  SPEED = 2                        # サイコロ回転速度(大=遅)
  #==============================================================================
  # ■ グラフィック設定 ※Windowskinsフォルダ内のファイル名を指定
  #==============================================================================
  DICE1_6 = "dice_1-6b"            # サイコロ1-6画像(8x8)
  DICE2_5 = "dice_2-5b"            # サイコロ2-5画像(8x8)
  DICE3_4 = "dice_3-4b"            # サイコロ3-4画像(8x8)
  WIDTH = 60                       # サイコロの幅
  HEIGHT = 60                      # サイコロの高さ
end
#==============================================================================
# ■ Window_Play_Dice
#==============================================================================
class Window_Play_Dice
  include HIDE_PLAY_DICE
  #--------------------------------------------------------------------------
  # ■ 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :debug                    # デバッグ実行フラグ
  attr_accessor :debug_number             # ダイス数字(デバッグ)
  attr_accessor :visible                  # 可視フラグ
  attr_accessor :active                   # 更新フラグ
  attr_accessor :stop                     # 更新停止フラグ
  attr_accessor :count_x                  # 横回転用カウンタ
  attr_accessor :count_y                  # 縦回転用カウンタ
  attr_accessor :turn_x                   # 回転X軸値
  attr_accessor :turn_y                   # 回転Y軸値
  attr_accessor :speed                    # 回転速度
  attr_accessor :number                   # ダイス数字
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(x,y,z0)
    dice1 = RPG::Cache.windowskin(HIDE_PLAY_DICE::DICE1_6)
    dice2 = RPG::Cache.windowskin(HIDE_PLAY_DICE::DICE2_5)
    dice3 = RPG::Cache.windowskin(HIDE_PLAY_DICE::DICE3_4)
    @width = HIDE_PLAY_DICE::WIDTH
    @height = HIDE_PLAY_DICE::HEIGHT
    @play_dice = Sprite.new(Viewport.new(x,y,@width,@height))
    @play_dice.bitmap = Bitmap.new(@width*8*3,@height*8)
    @play_dice.bitmap.blt(@width*8*0,0,dice1,Rect.new(0,0,dice1.width,dice1.height))
    @play_dice.bitmap.blt(@width*8*1,0,dice2,Rect.new(0,0,dice2.width,dice2.height))
    @play_dice.bitmap.blt(@width*8*2,0,dice3,Rect.new(0,0,dice3.width,dice3.height))
    @play_dice.visible = false
    @debug_number = HIDE_PLAY_DICE::DEBUG_NUMBER
    @debug = HIDE_PLAY_DICE::DEBUG
    @active = false ; @stop = false
    @speed = HIDE_PLAY_DICE::SPEED
    @cx = 8*3
    @cy = 8
    @turn_p = 1
    # □ナンバーの初期化
    reset
    @count_x = 0 ; @count_y = 1
    @turn_x = 0 ; @turn_y = 0
  end
  #--------------------------------------------------------------------------
  # ■ カーソルウィンドウの可視化
  #--------------------------------------------------------------------------
  def visible=(visible)
    @visible = visible
    @play_dice.visible = @visible
  end
  #--------------------------------------------------------------------------
  # ■ 更新
  #--------------------------------------------------------------------------
  def update
    if @active
      # □横回転の乱数を取得
      rand2 = rand(2) * @turn_p
      # □数字変更の乱数を取得
      rand = rand(6)
      if (@count_y == 0 or @count_y == 4) && @rand_c != rand
        if @count_y == 0
          @rand_c = rand
          case @count_x
          when 3,7 ##1-6
            case @rand_c
            when 2 ## 2-5
              @count_x = 11*@speed ; @count_y = 6*@speed
            when 3 ## 2-5
              @count_x = 15*@speed ; @count_y = 2*@speed
            when 4 ## 3-4
              @count_x = 17*@speed ; @count_y = 2*@speed
            when 5 ## 3-4
              @count_x = 21*@speed ; @count_y = 6*@speed
            end
          when 11,15 ##2-5
            case @rand_c
            when 2 ## 1-6
              @count_x = 1*@speed ; @count_y = 2*@speed
            when 3 ## 1-6
              @count_x = 3*@speed ; @count_y = 6*@speed
            when 4 ## 3-4
              @count_x = 19*@speed ; @count_y = 6*@speed
            when 5 ## 3-4
              @count_x = 23*@speed ; @count_y = 2*@speed
            end
          when 19,23 ##3-4
            case @rand_c
            when 2 ## 1-6
              @count_x = 5*@speed ; @count_y = 6*@speed
            when 3 ## 1-6
              @count_x = 7*@speed ; @count_y = 2*@speed
            when 4 ## 2-5
              @count_x = 9*@speed ; @count_y = 2*@speed
            when 5 ## 2-5
              @count_x = 13*@speed ; @count_y = 6*@speed
            end
          end
        elsif @count_y == 4
          case @count_x
          when 3,7 ##1-6
            case @rand_c
            when 2 ## 2-5
              @count_x = 11*@speed ; @count_y = 2*@speed
            when 3 ## 2-5
              @count_x = 15*@speed ; @count_y = 6*@speed
            when 4 ## 3-4
              @count_x = 17*@speed ; @count_y = 6*@speed
            when 5 ## 3-4
              @count_x = 21*@speed ; @count_y = 2*@speed
            end
          when 11,15 ##2-5
            case @rand_c
            when 2 ## 1-6
              @count_x = 1*@speed ; @count_y = 6*@speed
            when 3 ## 1-6
              @count_x = 3*@speed ; @count_y = 2*@speed
            when 4 ## 3-4
              @count_x = 19*@speed ; @count_y = 2*@speed
            when 5 ## 3-4
              @count_x = 23*@speed ; @count_y = 6*@speed
            end
          when 19,23 ##3-4
            case @rand_c
            when 2 ## 1-6
              @count_x = 5*@speed ; @count_y = 2*@speed
            when 3 ## 1-6
              @count_x = 7*@speed ; @count_y = 6*@speed
            when 4 ## 2-5
              @count_x = 9*@speed ; @count_y = 6*@speed
            when 5 ## 2-5
              @count_x = 13*@speed ; @count_y = 2*@speed
            end
          end
        end
      else
        @count_x += rand2
      end
    end
    #…………………………………………………………………………………………………
    # □縦回転の更新
    #…………………………………………………………………………………………………
    if rand2 == 0
      @count_y += 1
    end
    if @count_y > @cy*@speed
      @count_y = 0
    end
    #…………………………………………………………………………………………………
    # □横回転の更新
    #…………………………………………………………………………………………………
    if @count_x > 23*@speed
      @turn_p = -1
      @count_x = 23*@speed - 1
    elsif @count_x < 0
      @turn_p = 1
      @count_x = 0*@speed + 1
    end    
    #…………………………………………………………………………………………………
    # □回転ox/oyの取得
    #…………………………………………………………………………………………………
    @turn_x = [[@count_x/@speed,0].max,23].min
    @turn_y = [[@count_y/@speed,0].max,7].min
    #…………………………………………………………………………………………………
    # □更新の停止
    #…………………………………………………………………………………………………
    if @stop && @active
      if @turn_x % 2 == 0 && (@turn_y == 1 or @turn_y == 5)
        # □縦横回転の更新
        turn
        case @turn_x
        when 0..7
          @number = @turn_y == 5 ? 6 : 1
        when 8..15
          @number = @turn_y == 5 ? 5 : 2
        when 16..23
          @number = @turn_y == 5 ? 4 : 3
        else
          p "Error!" ;Error!
        end
        # □デバッグ(直接数字を指定)
        if @debug_number != 0 && @debug
          case @debug_number
          when 1
            @turn_x = 0 ; @turn_y = 1
          when 2
            @turn_x = 8 ; @turn_y = 1
          when 3
            @turn_x = 16 ; @turn_y = 1
          when 4
            @turn_x = 16 ; @turn_y = 5
          when 5
            @turn_x = 8 ; @turn_y = 5
          when 6
            @turn_x = 0 ; @turn_y = 5
          end
          @number = @debug_number
          turn(ox = @turn_x,oy = @turn_y)
        end
        @active = false
        return
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ ナンバーの初期化
  #--------------------------------------------------------------------------
  def reset
    @number = 0
  end
  #--------------------------------------------------------------------------
  # ■ 縦横回転の更新
  #--------------------------------------------------------------------------
  def turn(ox = @turn_x,oy = @turn_y)
    if @active
      turn_x(ox) ; turn_y(oy)
    end
  end
  #--------------------------------------------------------------------------
  # ■ 横回転の更新
  #--------------------------------------------------------------------------
  def turn_x(ox = @turn_x)
    @play_dice.ox = ox*@width
  end
  #--------------------------------------------------------------------------
  # ■ 縦回転の更新
  #--------------------------------------------------------------------------
  def turn_y(oy = @turn_y)
    @play_dice.oy = oy*@height
  end
  #--------------------------------------------------------------------------
  # ■ 解放
  #--------------------------------------------------------------------------
  def dispose
    @play_dice.viewport.dispose
    @play_dice.bitmap.dispose
    @play_dice.dispose
  end
end