=begin ========================================================================
* ziifee's Spin Command for RPG Tankentai Sideview Battle System
  By ziifee ( [url]http://neomemo.web.fc2.com/[/url] )
   <SBS Only>
    -This script is only for the Tankentai SBS WITHOUT the ATB installed.
   <Image Required>
     Spin40 : Spin40.png is required in the Graphics/System folder.
=end # ========================================================================
 
#==============================================================================
# ■ Ziifee
#==============================================================================
 
module Zii
  # ▼ Spin Command/Icon Index Number
  FIGHT = 132                        # Fight
  ESCAPE = 9                         # Escape
  ATTACK = 1                         # Attack (Default)
  GUARD = 7                          # Guard
  SKILL = 6                          # Skill
  ITEM = 8                           # Item
 
  # ▼ Spin Command/Direction of Rotation ( "normal" or "reverse" )
  #   Determines how Spin Command rotates according to left/right key press.
  TURN = "normal"
 
  # ▼ Face Graphics (true: Use battle face graphic / false: don't use faces)
  STATUS_FACE = true
 
  # ▼ Actor Names (true: Show actor names / false: Don't show )
  STATUS_LINE = true
 
  # ▼ Actor Name Text Size ( VX default size: 20 )
  LINE_SIZE = 14
 
  #--------------------------------------------------------------------------
  # ● 通常回転 の判定
  #--------------------------------------------------------------------------
  def self.turn_normal?
    return false if TURN == "reverse"
    return true  if TURN == "normal"
    return true
  end
  #--------------------------------------------------------------------------
  # ● バトルオプション [顔グラフィック] の判定
  #--------------------------------------------------------------------------
  def self.battle_face?
    return true if STATUS_FACE
    return false
  end
  #--------------------------------------------------------------------------
  # ● バトルステートオプション [名前] の判定
  #--------------------------------------------------------------------------
  def self.line_name?
    return true if STATUS_LINE
    return false
  end
end
 
#==============================================================================
# ¡ Window_Base
#==============================================================================
 
class Window_Base
  #--------------------------------------------------------------------------
  # ● 描绘脸谱
  #     face_name  : 脸谱图像文件名
  #     face_index : 脸谱图像索引
  #     x          : 描绘目标 X 坐标
  #     y          : 描绘目标 Y 坐标
  #     size       : 显示大小
  #     opacity    :不透明度
  #--------------------------------------------------------------------------
  def draw_face(face_name, face_index, x, y, size = 96, opacity = 255)
    bitmap = Cache.face(face_name)
    rect = Rect.new(0, 0, 0, 0)
    rect.x = face_index % 4 * 96 + (96 - size) / 2
    rect.y = face_index / 4 * 96 + (96 - size) / 2
    rect.width = size
    rect.height = size
    self.contents.blt(x, y, bitmap, rect, opacity)
    bitmap.dispose
  end
  #--------------------------------------------------------------------------
  # ● 描绘角色脸谱
  #--------------------------------------------------------------------------
  def draw_actor_face(actor, x, y, size = 96, opacity = 255)
    draw_face(actor.face_name, actor.face_index, x, y, size, opacity)
  end
end
 
# ▼ 回転コマンド
#==============================================================================
# ■ Window_SpinCommand
#------------------------------------------------------------------------------
#  回転用コマンド選択を行うウィンドウです。
#==============================================================================
 
class Window_SpinCommand < Window_Base
  #--------------------------------------------------------------------------
  # ● 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_reader   :index                    # カーソル位置
  attr_reader   :help_window              # ヘルプウィンドウ
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #     cx / cy  : 中心の X座標 / Y座標
  #     commands : コマンド配列 (内容 は [name, kind, pull, enabled?])
  #     setting  : 設定ハッシュ ("R"=>半径 "S"=>速さ "G"=>背景 "L"=>文字)
  #--------------------------------------------------------------------------
  def initialize(cx, cy, commands, setting = {})
    @radius    = setting.has_key?("R") ? setting["R"] : 40  # 描画半径
    @speed     = setting.has_key?("S") ? setting["S"] : 36  # 回転速さ
    @spin_back = setting.has_key?("G") ? setting["G"] : ""  # 背景画像
    @spin_line = setting.has_key?("L") ? setting["L"] : nil # 文字位置
    x, y = cx - @radius - 28 , cy - @radius - 28
    #width = height = @radius * 2 + 56
    height = 210   #210  这个是那五个转轮内容的坐标
    width = 210    #210
    super(x, y, width, height)
    self.opacity = 0
    @index = 0
    @commands = commands                                    # コマンド
    @spin_right = true
    @spin_count = 0
    update_cursor
  end
  #--------------------------------------------------------------------------
  # ▽ スピン画像を描画する (描画内容 強化用)
  #     i  : インデックス
  #     cx : 表示 中心位置 X座標
  #     cy : 表示 中心位置 Y座標
  #--------------------------------------------------------------------------
  def draw_spin_graphic(i, cx, cy)
    case command_kind(i)
    when "icon"
      draw_icon(command_pull(i), cx - 12, cy - 12, command_enabled?(i))
    end
  end
  #--------------------------------------------------------------------------
  # ★ リフレッシュ バグ回避用
  #--------------------------------------------------------------------------
  def refresh
    set_spin
  end
  #--------------------------------------------------------------------------
  # ★ 項目の描画 バグ回避用
  #--------------------------------------------------------------------------
  def draw_item(index, enabled = true)
    @commands[index][3] = enabled
    set_spin
  end
  #--------------------------------------------------------------------------
  # ● 現在のコマンド名を取得する
  #--------------------------------------------------------------------------
  def command_name(index = @index)
    return "" if index < 0
    name = @commands[index][0]
    return name != nil ? name : ""
  end
  #--------------------------------------------------------------------------
  # ● コマンドの種類を取得
  #--------------------------------------------------------------------------
  def command_kind(index)
    result = @commands[index][1]
    return result != nil ? result : ""
  end
  #--------------------------------------------------------------------------
  # ● コマンドの引数 を取得
  #--------------------------------------------------------------------------
  def command_pull(index)
    result = @commands[index][2]
    return result != nil ? result : ""
  end
  #--------------------------------------------------------------------------
  # ● コマンドの有効フラグを取得
  #--------------------------------------------------------------------------
  def command_enabled?(index)
    result = @commands[index][3]
    return result != nil ? result : true
  end
  #--------------------------------------------------------------------------
  # ● 名前の位置に index を設定する
  #--------------------------------------------------------------------------
  def set_index(name)
    n = -1
    for i in [email]0...@commands.size[/email]
      n = i if @commands[i][0] == name
    end
    @index = n if n >= 0
    update_cursor
    call_update_help
    set_spin
  end
  #--------------------------------------------------------------------------
  # ● カーソル位置の設定
  #     index : 新しいカーソル位置
  #--------------------------------------------------------------------------
  def index=(index)
    @index = index
    update_cursor
    call_update_help
    set_spin
  end
  #--------------------------------------------------------------------------
  # ● 中心のX座標を取得
  #--------------------------------------------------------------------------
  def center_x
    return contents.width / 2
  end
  #--------------------------------------------------------------------------
  # ● 中心のY座標を取得
  #--------------------------------------------------------------------------
  def center_y
    return contents.height / 2
  end
  #--------------------------------------------------------------------------
  # ● 項目数の取得
  #--------------------------------------------------------------------------
  def item_max
    return @commands.size
  end
  #--------------------------------------------------------------------------
  # ● 背景の設定 (再定義 向き)
  #--------------------------------------------------------------------------
  def set_background
    return if @spin_back == ""
    bitmap = Cache.system(@spin_back)
    rect = Rect.new(-34, 0-10, 210, 250)
    self.contents.blt(12-4, 12+10, bitmap, rect)
  end
 
  #--------------------------------------------------------------------------
  # ● 文章の設定 (再定義 向き)
  #--------------------------------------------------------------------------
  def set_text
    return if @spin_line == nil
    y = center_y - WLH / 2 + @spin_line
    self.contents.draw_text(center_x - 48, y, 96, WLH, command_name, 1)
  end
  #--------------------------------------------------------------------------
  # ● スピンアイコンの角度の差を取得する
  #--------------------------------------------------------------------------
  def angle_size
    return (Math::PI * 2 / item_max)
  end
  #--------------------------------------------------------------------------
  # ● スピンアイコン回転時のカウント を設定する
  #--------------------------------------------------------------------------
  def set_spin_count
    @spin_count = angle_size * 360 / @speed
    set_spin(true)
  end
  #--------------------------------------------------------------------------
  # ● スピン設定 の実行
  #     spin : 回転フラグ (true の時回転中)
  #--------------------------------------------------------------------------
  def set_spin(spin = false)
    self.contents.clear
    set_background
    angle = spin ? @speed * @spin_count / 360 : 0
    angle = @spin_right ? angle : -angle
    for i in 0...item_max
      n = (i - @index) * angle_size + angle
      cx = @radius * Math.sin(n) + center_x
      cy = - @radius * Math.cos(n) + center_y
      draw_spin_graphic(i, cx, cy)
    end
    set_text
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update
    super
    update_cursor
    if @spin_count > 0
      @spin_count -= 1
      set_spin(@spin_count >= 1)
      return
    end
    update_command
  end
  #--------------------------------------------------------------------------
  # ● コマンドの移動可能判定
  #--------------------------------------------------------------------------
  def command_movable?
    return false if @spin_count > 0
    return false if (not visible or not active)
    return false if (index < 0 or index > item_max or item_max == 0)
    return false if (@opening or @closing)
    return true
  end
  #--------------------------------------------------------------------------
  # ● コマンドを右に移動
  #--------------------------------------------------------------------------
  def command_right
    @index = (@index + 1) % item_max
    @spin_right = true
    set_spin_count
  end
  #--------------------------------------------------------------------------
  # ● コマンドを左に移動
  #--------------------------------------------------------------------------
  def command_left
    @index = (@index - 1 + item_max) % item_max
    @spin_right = false
    set_spin_count
  end
  #--------------------------------------------------------------------------
  # ● コマンド選択の更新
  #--------------------------------------------------------------------------
  def update_command
    if command_movable?
      if Input.press?(Input::RIGHT)
        Sound.play_cursor
        Zii.turn_normal? ? command_right : command_left
      end
      if Input.press?(Input::LEFT)
        Sound.play_cursor
        Zii.turn_normal? ? command_left : command_right
      end
    end
    call_update_help
  end
  #--------------------------------------------------------------------------
  # ● カーソルの更新
  #--------------------------------------------------------------------------
  def update_cursor
    if @index < 0
      self.cursor_rect.empty
    else
      rect = Rect.new(0, 0, 24, 24)
      rect.x = center_x - rect.width / 2
      rect.y = center_y - rect.height / 2 - @radius
      self.cursor_rect = rect
      self.z = 200
    end
  end
  #--------------------------------------------------------------------------
  # ● ヘルプウィンドウの設定
  #     help_window : 新しいヘルプウィンドウ
  #--------------------------------------------------------------------------
  def help_window=(help_window)
    @help_window = help_window
    call_update_help
  end
  #--------------------------------------------------------------------------
  # ● ヘルプウィンドウ更新メソッドの呼び出し
  #--------------------------------------------------------------------------
  def call_update_help
    if self.active and @help_window != nil
       update_help
    end
  end
  #--------------------------------------------------------------------------
  # ● ヘルプウィンドウの更新 (内容は継承先で定義する)
  #--------------------------------------------------------------------------
  def update_help
  end
end
 
#==============================================================================
# ¡ Window_LineHelp
#------------------------------------------------------------------------------
# @ƒXƒLƒ‹‚âƒAƒCƒeƒ€‚Ìà–¾AƒAƒNƒ^[‚̃Xƒe[ƒ^ƒX‚Ȃǂð•\ަ‚·‚éƒEƒBƒ“ƒhƒE‚Å‚·B
#==============================================================================
 
class Window_LineHelp < Window_Base
  #--------------------------------------------------------------------------
  # œ ƒIƒuƒWƒFƒNƒg‰Šú‰»  -16, 0,
  #--------------------------------------------------------------------------
  def initialize
    super(-16, 0, 576, WLH + 32)
    self.opacity = 0
  end
  #--------------------------------------------------------------------------
  # œ ƒeƒLƒXƒgÝ’è
  #     text  : ƒEƒBƒ“ƒhƒE‚É•\ަ‚·‚é•¶Žš—ñ
  #     align : ƒAƒ‰ƒCƒ“ƒƒ“ƒg (0..¶‘µ‚¦A1..’†‰›‘µ‚¦A2..‰E‘µ‚¦)
  #--------------------------------------------------------------------------
  def set_text(text, align = 0)
    if text != @text or align != @align
      self.contents.clear
      back_color = Color.new(0, 0, 0, 80)
      self.contents.fill_rect(0, y = 12, contents.width, WLH - y, back_color)
      self.contents.font.color = normal_color
      self.contents.draw_text(20, 0, self.width - 72, WLH, text, align)
      @text = text
      @align = align
    end
  end
end
 
#==============================================================================
# ■ Window_ActorCommand
#==============================================================================
 
class Window_ActorCommand < Window_SpinCommand
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化  
  #--------------------------------------------------------------------------
  def initialize
    s1 = [Vocab::attack, "icon", Zii::ATTACK, true]
    s2 = [Vocab::skill,  "icon", Zii::SKILL,  true]
    s3 = [Vocab::guard,  "icon", Zii::GUARD,  true]
    s4 = [Vocab::item,   "icon", Zii::ITEM,   true]
    s5 = [Vocab::escape, "icon", Zii::ESCAPE, $game_troop.can_escape]
    setting = {"R"=>40, "S"=>52, "G"=>"Spin40", "L"=>-12}
    super(72, 356, [s1, s2, s3, s4, s5], setting)#72,356
    self.active = false
    set_spin
  end
  #--------------------------------------------------------------------------
  # ● セットアップ
  #     actor : アクター
  #--------------------------------------------------------------------------
  def setup(actor)
    @commands[0][2] = Zii::ATTACK
    @commands[1][0] = Vocab::skill
    if actor.weapons[0] != nil
      n = actor.weapons[0].icon_index
      @commands[0][2] = n if n > 0
    end
    @commands[1][0] = actor.class.skill_name if actor.class.skill_name_valid
    self.index = 0
    set_spin
  end
end
 
#==============================================================================
# ■ Window_PartyCommand
#==============================================================================
 
class Window_PartyCommand < Window_SpinCommand
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    s1 = [Vocab::fight,  "icon", Zii::FIGHT,  true]
    s2 = [Vocab::escape, "icon", Zii::ESCAPE, $game_troop.can_escape]
    setting = {"R"=>40, "S"=>52, "G"=>"Spin40", "L"=>-12}
    super(72, 356, [s1, s2], setting)
    self.active = false
    set_spin
  end
end
 
 
#==============================================================================
# ¡ Window_BattleStatus
#==============================================================================
 
class Window_BattleStatus < Window_Selectable
  #--------------------------------------------------------------------------
  # œ ƒIƒuƒWƒFƒNƒg‰Šú‰» ‰ü
  #--------------------------------------------------------------------------
  def initialize
    super(0, 230, 710, 240)
    @column_max = 1
    refresh
    self.active = false
    self.opacity = 0
  end
  #--------------------------------------------------------------------------
  # œ ƒXƒe[ƒg‚ÌXV ‚̉º’n•`‰æ
  #--------------------------------------------------------------------------
  def draw_neomemo7_back
    @neomemo7_clear = false
    for index in 0...@item_max
      x = index * 96
      self.contents.clear_rect(x + 72, WLH * 3, 24, 24)
      next unless Zii.battle_face?
      actor = $game_party.members[index]
      next if actor.hp <= 0
      bitmap = Cache.face(actor.face_name)
      rect = Rect.new(0, 0, 22, 22)
      rect.x = actor.face_index % 4 * 96 + 72
      rect.y = actor.face_index / 4 * 96 + 72
      self.contents.blt(x + 72, WLH * 3, bitmap, rect, 192)
    end
  end
  #--------------------------------------------------------------------------
  # œ €–Ú‚Ì•`‰æ ‰ü
  #--------------------------------------------------------------------------
  def draw_item(index)
    x = index * 144 + 20
    x2 = 20
    y = 15
    rect = Rect.new(x, 0, 96, 96)
    self.contents.clear_rect(rect)
    self.contents.font.color = normal_color
    self.contents.font.size = 12
    actor = $game_party.members[index]
    draw_actor_state2(actor, x + 72 + 40, WLH)
    x = index * 144
    x3 = 25
    draw_actor_hp(actor, x + x2 + x3 + 6, WLH * 2 + 63, 95)
    draw_actor_mp(actor, x + x2 + x3 + 18, WLH * 3 + 52, 95)
  end
  #--------------------------------------------------------------------------
  # œ ƒJ[ƒ\ƒ‹‚ÌXV
  #--------------------------------------------------------------------------
  def update_cursor
    y = 15
    if @index < 0                   # ƒJ[ƒ\ƒ‹ˆÊ’u‚ª 0 –¢–ž‚Ìê‡
      self.cursor_rect.empty        # ƒJ[ƒ\ƒ‹‚𖳌ø‚Æ‚·‚é
    else                            # ƒJ[ƒ\ƒ‹ˆÊ’u‚ª 0 ˆÈã‚Ìê‡
      #rect = Rect.new(index * 161 - 12, 45, 137, 115)
      #self.cursor_rect = rect
    end
  end
end
 
 
#==============================================================================
# ¡ Scene_Battle
#------------------------------------------------------------------------------
# @ƒoƒgƒ‹‰æ–ʂ̈—‚ðs‚¤ƒNƒ‰ƒX‚Å‚·B
#==============================================================================
 
class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # œ î•ñ•\ަƒrƒ…[ƒ|[ƒg‚ÌXV
  #--------------------------------------------------------------------------
  alias :neomemo13_update_info_viewport :update_info_viewport
  def update_info_viewport
    ox = @info_viewport.ox
    neomemo13_update_info_viewport
    @info_viewport.ox = ox
    @status_window.update
  end
  #--------------------------------------------------------------------------
  # œ ƒAƒNƒ^[ƒRƒ}ƒ“ƒh‘I‘ð‚ÌŠJŽn
  #--------------------------------------------------------------------------
  alias :neomemo13_start_actor_command_selection :start_actor_command_selection
  def start_actor_command_selection
    neomemo13_start_actor_command_selection
    @actor_command_window.visible = true
  end
  #--------------------------------------------------------------------------
  # œ ƒAƒNƒ^[ƒRƒ}ƒ“ƒh‘I‘ð‚ÌXV
  #--------------------------------------------------------------------------
  alias :neomemo13_update_actor_command_selection :update_actor_command_selection
  def update_actor_command_selection
    return unless @actor_command_window.command_movable?
    neomemo13_update_actor_command_selection
  end
 
  #--------------------------------------------------------------------------
  # œ ƒAƒCƒeƒ€‘I‘ð‚ÌŠJŽn
  #--------------------------------------------------------------------------
  alias :neomemo13_start_item_selection :start_item_selection
  def start_item_selection
    neomemo13_start_item_selection
    @item_window.dispose if @item_window != nil
    @help_window.dispose if @help_window != nil
    @help_window = Window_LineHelp.new
    @item_window = Window_Item.new(60, 70, 320, 232)
    @item_window.help_window = @help_window
    @item_window.opacity = 0
  end
end