#เวลาแทนเงิน
# 23-11-2013
# V.1
# by jojo741963

module TIME
  
#ไอดีของตัวแปรสำหรับจำค่าเวลาที่มี
GOLD = 1

#ไอดีของตัวแปรอันที่สอง ไว้จำเวลาที่ใช้ไปแล้ว
TIME_USED = 2

#ไอดีของตัวแปรอันที่สาม ไว้จำเวลาที่ได้รับมาทั้งหมด
TIME_SAVE = 3

#ไอดีของตัวสวิตช์ ถ้าเปิด จะไม่มีการเพิ่มเงิน(เวลา)
TIME_STOP = 1

end


def gain_gold_form_time    
  $game_variables[TIME::TIME_SAVE] += Graphics.frame_count / Graphics.frame_rate
  $game_variables[TIME::TIME_SAVE] -= $game_variables[TIME::TIME_USED]
  
  unless $game_switches[TIME::TIME_STOP]
    $game_variables[TIME::GOLD] += $game_variables[TIME::TIME_SAVE]
    $game_variables[TIME::GOLD] -= $game_variables[TIME::TIME_USED]
  end
  
  $game_variables[TIME::TIME_USED] = Graphics.frame_count / Graphics.frame_rate
  @gold_window.refresh if @gold_window != nil
end

#ส่วนที่เพิ่มเข้ามา
class Scene_Map
  alias gain_gold_form_time_update update #กำหนดให่้หัวเรื่องมีการอัพเดท
  def update
    gain_gold_form_time_update #อัพเดทหัวเรื่อง
    gain_gold_form_time
  end
end
class Scene_Menu
  alias gain_gold_form_time_update update #กำหนดให่้หัวเรื่องมีการอัพเดท
  def update
    gain_gold_form_time_update #อัพเดทหัวเรื่อง
    gain_gold_form_time
  end
end
class Window_Message < Window_Selectable
  alias gain_gold_form_time_update update #กำหนดให่้หัวเรื่องมีการอัพเดท
  def update
    gain_gold_form_time_update #อัพเดทหัวเรื่อง
    gain_gold_form_time
  end
end
#จบ

#==============================================================================
# * Window_Gold
#------------------------------------------------------------------------------
#  It is the window which indicates the gold.
#==============================================================================

class Window_Gold < Window_Base
  #--------------------------------------------------------------------------
  # - Object initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 160, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = $fontface
    self.contents.font.size = $fontsize
    refresh
  end
  #--------------------------------------------------------------------------
  # - Refreshment
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    cx = contents.text_size($data_system.words.gold).width
    self.contents.font.color = normal_color
    #self.contents.draw_text(4, 0, 120-cx-2, 32, $game_party.gold.to_s, 2)
    self.contents.draw_text(4, 0, 120-cx-2, 32, $game_variables[TIME::GOLD].to_s, 2)
    self.contents.font.color = system_color
    self.contents.draw_text(124-cx, 0, cx, 32, $data_system.words.gold, 2)
  end
end


#==============================================================================
# * Window_ShopBuy
#------------------------------------------------------------------------------
#  In the shop picture, it is the window which indicates the summary of the 
# commodity which can be purchased.
#==============================================================================

class Window_ShopBuy < Window_Selectable
  #--------------------------------------------------------------------------
  # - Object initialization
  #     shop_goods : Commodity
  #--------------------------------------------------------------------------
  def initialize(shop_goods)
    super(0, 128, 368, 352)
    @shop_goods = shop_goods
    refresh
    self.index = 0
  end
  #--------------------------------------------------------------------------
  # - Acquisition of item
  #--------------------------------------------------------------------------
  def item
    return @data[self.index]
  end
  #--------------------------------------------------------------------------
  # - Refreshment
  #--------------------------------------------------------------------------
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @data = []
    for goods_item in @shop_goods
      case goods_item[0]
      when 0
        item = $data_items[goods_item[1]]
      when 1
        item = $data_weapons[goods_item[1]]
      when 2
        item = $data_armors[goods_item[1]]
      end
      if item != nil
        @data.push(item)
      end
    end
    # If the number of items is not 0, it draws up bit map, drawing all item
    @item_max = @data.size
    if @item_max > 0
      self.contents = Bitmap.new(width - 32, row_max * 32)
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
      for i in 0...@item_max
        draw_item(i)
      end
    end
  end
  #--------------------------------------------------------------------------
  # - Drawing of item
  #     index : Item number
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    # Acquiring the frequency of possession of the item
    case item
    when RPG::Item
      number = $game_party.item_number(item.id)
    when RPG::Weapon
      number = $game_party.weapon_number(item.id)
    when RPG::Armor
      number = $game_party.armor_number(item.id)
    end
    # If price is not below the money in hand, at the same time the frequency of possession 99, usually in letter color
    # So if is not, it sets to invalid letter color
    #if item.price <= $game_party.gold and number < 99
    if item.price <= $game_variables[TIME::GOLD] and number < 99
      self.contents.font.color = normal_color
    else
      self.contents.font.color = disabled_color
    end
    x = 4
    y = index * 32
    rect = Rect.new(x, y, self.width - 32, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    bitmap = RPG::Cache.icon(item.icon_name)
    opacity = self.contents.font.color == normal_color ? 255 : 128
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
    self.contents.draw_text(x + 240, y, 88, 32, item.price.to_s, 2)
  end
  #--------------------------------------------------------------------------
  # - Help text renewal
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_text(self.item == nil ? "" : self.item.description)
  end
end


#==============================================================================
# * Scene_Shop
#------------------------------------------------------------------------------
#  It is the class which processes the shop picture.
#==============================================================================

class Scene_Shop
  #--------------------------------------------------------------------------
  # - Main processing
  #--------------------------------------------------------------------------
  def main
    # Drawing up the help window
    @help_window = Window_Help.new
    # Drawing up the command window
    @command_window = Window_ShopCommand.new
    # Drawing up the Goldwyn dough
    @gold_window = Window_Gold.new
    @gold_window.x = 480
    @gold_window.y = 64
    # Drawing up the dummy window
    @dummy_window = Window_Base.new(0, 128, 640, 352)
    # Drawing up the purchase window
    @buy_window = Window_ShopBuy.new($game_temp.shop_goods)
    @buy_window.active = false
    @buy_window.visible = false
    @buy_window.help_window = @help_window
    # Drawing up the sale window
    @sell_window = Window_ShopSell.new
    @sell_window.active = false
    @sell_window.visible = false
    @sell_window.help_window = @help_window
    # Drawing up the quantity input window
    @number_window = Window_ShopNumber.new
    @number_window.active = false
    @number_window.visible = false
    # Drawing up the status window
    @status_window = Window_ShopStatus.new
    @status_window.visible = false
    # Transition execution
    Graphics.transition
    # Main loop
    loop do
      # Renewing the game picture
      Graphics.update
      # Updating the information of input
      Input.update
      # Frame renewal
      update
      # When the picture changes, discontinuing the loop
      if $scene != self
        break
      end
    end
    # Transition preparation
    Graphics.freeze
    # Releasing the window
    @help_window.dispose
    @command_window.dispose
    @gold_window.dispose
    @dummy_window.dispose
    @buy_window.dispose
    @sell_window.dispose
    @number_window.dispose
    @status_window.dispose
  end
  #--------------------------------------------------------------------------
  # - Frame renewal
  #--------------------------------------------------------------------------
  def update
    gain_gold_form_time
    # Renewing the window
    @help_window.update
    @command_window.update
    @gold_window.update
    @dummy_window.update
    @buy_window.update
    @sell_window.update
    @number_window.update
    @status_window.update
    # When the command window is active,: Update_command is called
    if @command_window.active
      update_command
      return
    end
    # When the purchase window is active,: Update_buy is called
    if @buy_window.active
      update_buy
      return
    end
    # When the sale window is active,: Update_sell is called
    if @sell_window.active
      update_sell
      return
    end
    # When the quantity input window is active,: Update_number is called
    if @number_window.active
      update_number
      return
    end
  end
  #--------------------------------------------------------------------------
  # - When frame renewal (the command window is active)
  #--------------------------------------------------------------------------
  def update_command
    # The B when button is pushed
    if Input.trigger?(Input::B)
      # Performing cancellation SE
      $game_system.se_play($data_system.cancel_se)
      # Change to map picture
      $scene = Scene_Map.new
      return
    end
    # When C button is pushed
    if Input.trigger?(Input::C)
      # It diverges at cursor position of the command window
      case @command_window.index
      when 0  # It purchases
        # Performing decision SE
        $game_system.se_play($data_system.decision_se)
        # State of window to purchase mode
        @command_window.active = false
        @dummy_window.visible = false
        @buy_window.active = true
        @buy_window.visible = true
        @buy_window.refresh
        @status_window.visible = true
      when 1  # It sells
        # Performing decision SE
        $game_system.se_play($data_system.decision_se)
        # State of window to sale mode
        @command_window.active = false
        @dummy_window.visible = false
        @sell_window.active = true
        @sell_window.visible = true
        @sell_window.refresh
      when 2  # It stops
        # Performing decision SE
        $game_system.se_play($data_system.decision_se)
        # Change to map picture
        $scene = Scene_Map.new
      end
      return
    end
  end
  #--------------------------------------------------------------------------
  # - When frame renewal (the purchase window is active)
  #--------------------------------------------------------------------------
  def update_buy
    # Setting the item of the status window
    @status_window.item = @buy_window.item
    # The B when button is pushed
    if Input.trigger?(Input::B)
      # Performing cancellation SE
      $game_system.se_play($data_system.cancel_se)
      # State of window to early mode
      @command_window.active = true
      @dummy_window.visible = true
      @buy_window.active = false
      @buy_window.visible = false
      @status_window.visible = false
      @status_window.item = nil
      # Eliminating the help text
      @help_window.set_text("")
      return
    end
    # When C button is pushed
    if Input.trigger?(Input::C)
      # Acquiring the item
      @item = @buy_window.item
      # When the item is invalid, or when price it is on from the money in hand
      if @item == nil or @item.price > $game_variables[TIME::GOLD]#$game_party.gold
        # Performing buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Acquiring the frequency of possession of the item
      case @item
      when RPG::Item
        number = $game_party.item_number(@item.id)
      when RPG::Weapon
        number = $game_party.weapon_number(@item.id)
      when RPG::Armor
        number = $game_party.armor_number(@item.id)
      end
      # Already, when 99 places it has kept
      if number == 99
        # Performing buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Performing decision SE
      $game_system.se_play($data_system.decision_se)
      # Calculating the maximum purchase possible quantity
      #max = @item.price == 0 ? 99 : $game_party.gold / @item.price
      max = @item.price == 0 ? 99 : $game_variables[TIME::GOLD] / @item.price
      max = [max, 99 - number].min
      # State of window to quantity input mode
      @buy_window.active = false
      @buy_window.visible = false
      @number_window.set(@item, max, @item.price)
      @number_window.active = true
      @number_window.visible = true
    end
  end
  #--------------------------------------------------------------------------
  # - When frame renewal (the sale window is active)
  #--------------------------------------------------------------------------
  def update_sell
    # The B when button is pushed
    if Input.trigger?(Input::B)
      # Performing cancellation SE
      $game_system.se_play($data_system.cancel_se)
      # State of window to early mode
      @command_window.active = true
      @dummy_window.visible = true
      @sell_window.active = false
      @sell_window.visible = false
      @status_window.item = nil
      # Eliminating the help text
      @help_window.set_text("")
      return
    end
    # When C button is pushed
    if Input.trigger?(Input::C)
      # Acquiring the item
      @item = @sell_window.item
      # Setting the item of the status window
      @status_window.item = @item
      # When the item is invalid, or when price 0 (sale failure) is
      if @item == nil or @item.price == 0
        # Performing buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Performing decision SE
      $game_system.se_play($data_system.decision_se)
      # Acquiring the frequency of possession of the item
      case @item
      when RPG::Item
        number = $game_party.item_number(@item.id)
      when RPG::Weapon
        number = $game_party.weapon_number(@item.id)
      when RPG::Armor
        number = $game_party.armor_number(@item.id)
      end
      # The frequency of possession of largest sale quantity = item
      max = number
      # State of window to quantity input mode
      @sell_window.active = false
      @sell_window.visible = false
      @number_window.set(@item, max, @item.price / 2)
      @number_window.active = true
      @number_window.visible = true
      @status_window.visible = true
    end
  end
  #--------------------------------------------------------------------------
  # - When frame renewal (the quantity input window is active)
  #--------------------------------------------------------------------------
  def update_number
    # The B when button is pushed
    if Input.trigger?(Input::B)
      # Performing cancellation SE
      $game_system.se_play($data_system.cancel_se)
      # Non setting the quantity input window to active invisibility
      @number_window.active = false
      @number_window.visible = false
      # It diverges at cursor position of the command window
      case @command_window.index
      when 0  # It purchases
        # State of window to purchase mode
        @buy_window.active = true
        @buy_window.visible = true
      when 1  # It sell
        # State of window to sale mode
        @sell_window.active = true
        @sell_window.visible = true
        @status_window.visible = false
      end
      return
    end
    # When C button is pushed
    if Input.trigger?(Input::C)
      # Performing shop SE
      $game_system.se_play($data_system.shop_se)
      # Non setting the quantity input window to active invisibility
      @number_window.active = false
      @number_window.visible = false
      # It diverges at cursor position of the command window
      case @command_window.index
      when 0  # It purchases
        # Purchase processing
        $game_variables[TIME::GOLD] -= @number_window.number * @item.price
        #$game_party.lose_gold(@number_window.number * @item.price)
        case @item
        when RPG::Item
          $game_party.gain_item(@item.id, @number_window.number)
        when RPG::Weapon
          $game_party.gain_weapon(@item.id, @number_window.number)
        when RPG::Armor
          $game_party.gain_armor(@item.id, @number_window.number)
        end
        # Refreshing each window
        @gold_window.refresh
        @buy_window.refresh
        @status_window.refresh
        # State of window to purchase mode
        @buy_window.active = true
        @buy_window.visible = true
      when 1  # It sells
        # Sale processing
        $game_variables[TIME::GOLD] += @number_window.number * (@item.price / 2)
        #$game_party.gain_gold(@number_window.number * (@item.price / 2))
        case @item
        when RPG::Item
          $game_party.lose_item(@item.id, @number_window.number)
        when RPG::Weapon
          $game_party.lose_weapon(@item.id, @number_window.number)
        when RPG::Armor
          $game_party.lose_armor(@item.id, @number_window.number)
        end
        # Refreshing each window
        @gold_window.refresh
        @sell_window.refresh
        @status_window.refresh
        # State of window to sale mode
        @sell_window.active = true
        @sell_window.visible = true
        @status_window.visible = false
      end
      return
    end
  end
end