class Game_Actor < Game_Battler
  
  #--------------------------------------------------------------------------
  # ● 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :lvup                     # レベルアップ変数
  attr_accessor :level_para_flag 
  
  #--------------------------------------------------------------------------
  # ● セットアップ
  #--------------------------------------------------------------------------
  alias tako1setup setup
  def setup(actor_id)
    tako1setup(actor_id)
    @lvup = init_lvup(actor_id)
    @level_para_flag = 0
    recover_all
  end  
  
  def init_lvup(actor_id)
    
    $lvupInitParas[ $lvupHeadInitParas[actor_id] ]
    
  end  
  #--------------------------------------------------------------------------
  # ● レベルアップ
  #--------------------------------------------------------------------------
  alias tako1level_up level_up
  def level_up
    tako1level_up
    @level_para_flag += 1

    
  end  
  #--------------------------------------------------------------------------
  # ● レベルアップによるパラメータの上昇
  #--------------------------------------------------------------------------
  def level_up_paraup

      for i in 0..6
        add_param(accpara[i], @lvup[i] ) if accpara[i]
      end  
    
    end  
    
  #レベルアップ対応
  def accpara
    [0,1,2,4,5,6]
  end  

  #--------------------------------------------------------------------------
  # ● レベルアップ増加率の上昇 
  #--------------------------------------------------------------------------
  def lvup_var_plus(index,value) 
       @lvup[index] += value 
  end 
  
  
end

class Scene_LvUp< Scene_MenuBase
    #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  def start
    super
    
    @restpoint = 6
    @temp_para=[0,0,0,0,0,0,0]
    @actor = get_actor
    @actor.level_up_paraup
    create_command_window
    create_lvuprest_window
    create_actorname_window
    @actorname_window.actor = @actor
    @command_window.actor = @actor
    @command_window.para_reset
    all_windows_update
  end
  #対象となるアクターの取得
  def get_actor
    $game_party.all_members.each do |actor|
      if actor.level_para_flag > 0
        return actor
      end  
      
    end
    return nil
  end  


  #--------------------------------------------------------------------------
  # ● コマンドウィンドウの作成
  #--------------------------------------------------------------------------
  def create_command_window
    @command_window = Window_LvUp.new
    @command_window.set_handler(:cancel,    method(:reset_para))
    @command_window.set_handler(:lvhp,      method(:command_lvhp))
    @command_window.set_handler(:lvmp,     method(:command_lvmp))
    @command_window.set_handler(:lvatk,     method(:command_lvatk))
    @command_window.set_handler(:lvmat,    method(:command_lvmat))
    @command_window.set_handler(:lvmdf, method(:command_lvmdf))
    @command_window.set_handler(:lvagi, method(:command_lvagi))
    @command_window.set_handler(:lvupok, method(:command_lvupok))
  end
  #--------------------------------------------------------------------------
  # ● 残りパラウィンドウの作成
  #--------------------------------------------------------------------------
  def create_lvuprest_window
    @lvuprest_window = Window_LvUpRest.new
    @lvuprest_window.para_rest = @restpoint
  end
  #--------------------------------------------------------------------------
  # ● アクター名ウィンドウの作成
  #--------------------------------------------------------------------------
  def create_actorname_window
    @actorname_window = Window_ActorName.new
    @actorname_window.actor = @actor
  end 
  #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def command_lvhp
    if @restpoint > 0
      @temp_para[0] += 1
      @restpoint -=1  
      @command_window.thp = @temp_para[0]
    end
    all_windows_update
    @command_window.activate    
  end
  #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def command_lvmp
    if @restpoint > 0
      @temp_para[1] += 1
      @restpoint -=1
      @command_window.tmp = @temp_para[1]
      
    end
    all_windows_update
    @command_window.activate  
  end
   #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def command_lvatk
    if @restpoint > 1
      @temp_para[2] += 1
      @restpoint -=2  
      @command_window.tatk = @temp_para[2]
      
    end
    all_windows_update
    @command_window.activate  
  end 
  #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def command_lvmat
    if @restpoint > 1
      @temp_para[4] += 1
      @restpoint -=2
      @command_window.tmat = @temp_para[4]  
      
    end
    all_windows_update
    @command_window.activate  
  end  
  #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def command_lvmdf
    if @restpoint > 1
      @temp_para[5] += 1
      @restpoint -=2
      @command_window.tmdf = @temp_para[5]
      
    end
    all_windows_update
    @command_window.activate  
  end
  #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def command_lvagi
    if @restpoint > 2
      @temp_para[6] += 1
      @restpoint -=3 
      @command_window.tagi = @temp_para[6]
      
    end
    all_windows_update
    @command_window.activate  
  end  
  #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def command_lvupok

    if @restpoint <= 0
      for i in 0..6
        @actor.lvup_var_plus(i,@temp_para[i])
        
      end
      @actor.level_para_flag -= 1
      @actor = get_actor 
      if @actor != nil
        @actor.level_up_paraup
      end  
     # @actor = get_actor
      
      if get_actor
        
        @command_window.activate
        @restpoint = 6
        reset_para
        @command_window.para_reset
        @command_window.update_para
        @command_window.actor = @actor
        @actorname_window.actor = @actor
        all_windows_update
      else  
        
        return_scene  

        
      end  
    
    else

      @command_window.activate
    end
    
  end  
  #--------------------------------------------------------------------------
  # ● コマンド［アイテム］
  #--------------------------------------------------------------------------
  def reset_para 
      for i in 0..7
        @temp_para[i] = 0
        @command_window.para_reset
      end
      @restpoint = 6
      @command_window.activate 
      all_windows_update
    end    
  #--------------------------------------------------------------------------
     
    

  
  def all_windows_update
    
    @command_window.update_para
    @lvuprest_window.para_rest =  @restpoint
    @lvuprest_window.refresh
    @actorname_window.refresh
  end
end  

class Window_LvUpRest < Window_Base
  attr_accessor :para_rest
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    super(250, 50, window_width, fitting_height(1))
    @para_rest = 6
    refresh
  end
  #--------------------------------------------------------------------------
  # ● ウィンドウ幅の取得
  #--------------------------------------------------------------------------
  def window_width
    return 100
  end
  #--------------------------------------------------------------------------
  # ● リフレッシュ
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
    draw_para(0, 0,  contents.width - 8)
  end

  #--------------------------------------------------------------------------
  # ● 通貨単位つき数値（所持金など）の描画
  #--------------------------------------------------------------------------
  def draw_para(x, y, width)
    unit = sprintf("残り　%s",  @para_rest)
    change_color(normal_color)
    draw_text(x, y, width, line_height, unit, 2)
  end
  #--------------------------------------------------------------------------
  # ● ウィンドウを開く
  #--------------------------------------------------------------------------
  def open
    refresh
    super
  end   
end  

class Window_LvUp < Window_Command
  attr_accessor :thp
  attr_accessor :tmp
  attr_accessor :tatk  
  attr_accessor :tmat  
  attr_accessor :tmdf
  attr_accessor :tagi 
  attr_accessor :actor
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    super(100, 100)

    @thp = 0
    @tmp = 0
    @tatk = 0
    @tmat = 0
    @tmdf = 0
    @tagi = 0
    @temp_para = [0,0,0,0,0,0,0,0]
  end
  #--------------------------------------------------------------------------
  # ● ウィンドウ幅の取得
  #--------------------------------------------------------------------------
  def window_width
    return 250
  end
  #--------------------------------------------------------------------------
  # ● 表示行数の取得
  #--------------------------------------------------------------------------
  def visible_line_number
    item_max
  end
  #--------------------------------------------------------------------------
  # ● コマンドリストの作成
  #--------------------------------------------------------------------------
  def make_command_list
    make_para
    add_command($templvhp,   :lvhp,   true)
    add_command($templvmp,  :lvmp,  true)
    add_command($templvatk,  :lvatk,  true)
    add_command($templvmat, :lvmat, true)
    add_command($templvmdf, :lvmdf, true)
    add_command($templvagi, :lvagi, true)
    add_command("決定", :lvupok, true) 
    
  end

  #パラの値の文章を作成
  def make_para
    
    $templvhp = Vocab::param(0)+ "(1)"
    $templvmp = Vocab::param(1)+ "(1)" 
    $templvatk = Vocab::param(2)+ "(2)"
    $templvmat = Vocab::param(4)+ "(2)"
    $templvmdf = Vocab::param(5)+ "(2)" 
    $templvagi = Vocab::param(6)+ "(3)"    
  end  
  
  def set_temp_para
    if @actor == nil
      
        @temp_para=[0,0,0,0,0,0,0]
      

      return
    end  
    @temp_para[0] = @thp + @actor.lvup[0] 
    @temp_para[1] = @tmp + @actor.lvup[1]  
    @temp_para[2] = @tatk + @actor.lvup[2] 
    @temp_para[3] = @tmat + @actor.lvup[3]  
    @temp_para[4] = @tmdf + @actor.lvup[4] 
    @temp_para[5] = @tagi + @actor.lvup[5]  
  end   
  #--------------------------------------------------------------------------
  # ● 経験値情報の描画
  #--------------------------------------------------------------------------
  def draw_lvupinfo(index) 
    ddx = item_rect_for_text(index).x
    ddy = item_rect_for_text(index).y
    
    st=@temp_para[index].to_s
    draw_text(item_rect_for_text(index), st,2)
  end  
  def make_paralist
    set_temp_para
    for i in 0..5
      draw_lvupinfo(i)
    end  
  end  
  #--------------------------------------------------------------------------
  # ● アップデート
  #--------------------------------------------------------------------------
  def update_para
    refresh
    make_paralist

  end 
  def para_reset

    @thp = 0
    @tmp = 0
    @tatk = 0
    @tmat = 0
    @tmdf = 0
    @tagi = 0    
    
  end
end

class Window_ActorName < Window_Base
  attr_accessor :actor
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    super(100, 50, window_width, fitting_height(1))
    refresh
  end
  #--------------------------------------------------------------------------
  # ● ウィンドウ幅の取得
  #--------------------------------------------------------------------------
  def window_width
    return 150
  end
  #--------------------------------------------------------------------------
  # ● リフレッシュ
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
    draw_name(0, 0, window_width)
  end

  #--------------------------------------------------------------------------
  # ● 通貨単位つき数値（所持金など）の描画
  #--------------------------------------------------------------------------
  def draw_name(x, y, width)
   
    change_color(normal_color)
    draw_text(x, y, width, line_height, @actor.name, 1) if @actor
  end
  #--------------------------------------------------------------------------
  # ● ウィンドウを開く
  #--------------------------------------------------------------------------
  def open
    refresh
    super
  end  
end  

