TheoAllen

Theo - Select Item Scene

May 24th, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.28 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Select Item Scene
  3. # Version : 1.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_SelectItem] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.05.25 - Finished script
  12. # 2013.05.24 - Started script
  13. # =============================================================================
  14. =begin
  15.  
  16.   Perkenalan :
  17.   Script ini sama seperti halnya show choice. Hanya saja yang dipilih disini
  18.   adalah item.
  19.  
  20.   Cara penggunaan :
  21.   Pasang di bawah material namun diatas main.
  22.   =============================================================================
  23.   * Script call :
  24.   ----------------------------------------------------------------------------
  25.   MENENTUKAN KONDISI :
  26.   Tulis script call kek gini
  27.   - @item =  [1,2,3,4,5] << id dari item
  28.   - @value = [2,5,2,4,3] << jumlah item
  29.  
  30.   Itu artinya:
  31.   - item dengan id 1 syaratnya berjumlah 2
  32.   - item dengan id 2 syaratnya berjumlah 5
  33.   - item dengan id 3 syaratnya berjumlah 2
  34.   - item dengan id 4 syaratnya berjumlah 4
  35.   - item dengan id 5 syaratnya berjumlah 3
  36.  
  37.   ----------------------------------------------------------------------------
  38.   PANGGIL SCENE SELECT ITEM :
  39.   - select_item << udah tulis gini ajah :v
  40.  
  41.   ----------------------------------------------------------------------------
  42.   PANGGIL KONDISI :
  43.   Dalam conditional branch, page 4, pilih yg script. Lalu tulis kek gini
  44.   (pilih aja salah satu)
  45.   - item?(item_id, value, operand_type)
  46.   - item?(item_id, value)
  47.   - item?(item_id)
  48.   - item?
  49.  
  50.   Pilihan pertama :
  51.   Kamu nentuin kondisinya sendiri dari script call conditional branch. item_id
  52.   adalah id dari item. Value adalah jumlah yg jadi patokannya. operand_type
  53.   adalah tipe pembandingnya. List pembandingnya kek gini.
  54.  
  55.   - 0 >> Sama dengan
  56.   - 1 >> Lebih besar
  57.   - 2 >> Lebih kecil
  58.   - 3 >> Lebih besar sama dengan
  59.   - 4 >> Lebih kecil sama dengan
  60.   - 5 >> Tidak sama dengan
  61.  
  62.   Semisal kamu nulisnya @> Script : item?(1,3,3)
  63.   Maka, jika item yang kamu pilih adalah id 1 dengan jumlah lebih dari atau
  64.   sama dengan 3, maka conditional branchnya bener
  65.  
  66.   Pilihan kedua   :
  67.   Kamu cuman nentuin kondisinya item_id ama valuenya doank. operand_type yang
  68.   dipake secara default adalah nomor 3
  69.  
  70.   Pilihan ketiga  :
  71.   Kamu cuman nentuin kondisi item_idnya doank. Value secara default nilainya 1
  72.  
  73.   Pilihan keempat :
  74.   Kamu ga nentuin kondisi pas manggil ini. Kondisinya otomatis sama kek yg
  75.   diatas. Pake @item ama @value. Cuman jika u pengen operand_typenya laen
  76.   (bukan 3), maka yg harus u tulis kek gini.
  77.  
  78.   @> Script : item?(@item,@value,1)
  79.   ----------------------------------------------------------------------------
  80.   Contoh penggunaan dalam event kek gini
  81.   ----------------------------------------------------------------------------
  82.   @> Script :@item =  [1,2,3,4,5]
  83.    :        :@value = [2,5,2,4,3]
  84.   @> Script :select_item
  85.   @> Conditional Branch: Script :item?
  86.         @> # Jika iya eventnya disini
  87.    :  else
  88.         @> # Jika ngga eventnya disini
  89.    :  Branch end
  90.   @>
  91.   ----------------------------------------------------------------------------
  92.   Terms of use :
  93.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  94.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Dan btw, kalo dipake
  95.   untuk komersial, bagi ane keuntungannya juga. Ama gratisan gamenya juga
  96.  
  97. =end
  98. # =============================================================================
  99. # Konfigurasi :
  100. # =============================================================================
  101. module THEO
  102.   module SELECT
  103.    
  104.     # =======================================================================
  105.       SELECT_VOCAB  = "Select Item"
  106.     # -----------------------------------------------------------------------
  107.     # Kata-kata untuk footer
  108.     # =======================================================================
  109.    
  110.     # =======================================================================
  111.       LOSE_ITEM     = false
  112.     # -----------------------------------------------------------------------
  113.     # Semisal u set true, maka pas u milih barang, barang u otomatis ilang.
  114.     # Semisal u set false, u bisa manual ngilangin barang dari inventory pake
  115.     # script call kek gini.
  116.     #
  117.     # @> Script : lose_item
  118.     # =======================================================================
  119.    
  120.     # =======================================================================
  121.       SHOW_AMOUNT   = true
  122.       AMOUNT_VOCAB  = "Amount :"
  123.       AMOUNT_WIDTH  = 200
  124.     # -----------------------------------------------------------------------
  125.     # Jika show amount u tulis true, maka akan keluar window amount untuk
  126.     # nentuin berapa banyak item yg u select/pilih. Jika false, otomatis
  127.     # jumlah item yg u pilih nilainya 1
  128.     #
  129.     # Amount vocab : kata2 di window amount
  130.     # Amount width : lebar windownya (default: 200)
  131.     # =======================================================================
  132.    
  133.   end
  134. end
  135. # =============================================================================
  136. # Akhir dari konfigurasi
  137. # =============================================================================
  138. class Game_Interpreter
  139.  
  140.   alias pre_select_item_init initialize
  141.   def initialize(depth = 0)
  142.     pre_select_item_init(depth)
  143.     init_selected_item
  144.   end
  145.  
  146.   def init_selected_item
  147.     @item   = [0]
  148.     @value  = [1]
  149.   end
  150.  
  151.   def item?(item_ids = @item,value = @value,operand_type = 3)
  152.     item_ids = [item_ids] unless item_ids.is_a?(Array)
  153.     value = [value] unless value.is_a?(Array)
  154.     hash = {}
  155.     item_ids.each_with_index do |id, index|
  156.       hash[id] = value[index].nil? ? 1 : value[index]
  157.     end
  158.     obj_id = $game_temp.selected_item.object.id rescue obj_id = 0
  159.     return hash.any? do |k,v|
  160.       k == obj_id && compare(v,operand_type)
  161.     end
  162.   end
  163.  
  164.   def compare(v,type)
  165.     amount = $game_temp.item_amount
  166.     case type
  167.     when 0 # sama dengan jumlah
  168.       return amount == v
  169.     when 1 # lebih besar dari jumlah
  170.       return amount > v
  171.     when 2 # lebih kecil dari jumlah
  172.       return amount < v
  173.     when 3 # lebih besar sama dengan jumlah
  174.       return amount >= v
  175.     when 4 # lebih kecil sama dengan jumlah
  176.       return amount <= v
  177.     when 5 # tidak sama dengan jumlah
  178.       return amount != v
  179.     end
  180.   end
  181.  
  182.   def select_item
  183.     new_command = RPG::EventCommand.new(230,@indent,[1])
  184.     @list.insert(@index+1,new_command)
  185.     SceneManager.call(Scene_SelectItem)
  186.   end
  187.  
  188.   def lose_item
  189.     return unless $game_temp.selected_item.object
  190.     item = $game_temp.selected_item.object
  191.     amount = $game_temp.item_amount
  192.     $game_party.lose_item(item,amount)
  193.   end
  194.  
  195.   def gain_item
  196.     return unless $game_temp.selected_item.object
  197.     item = $game_temp.selected_item.object
  198.     amount = $game_temp.item_amount
  199.     $game_party.gain_item(item,amount)
  200.   end
  201.  
  202. end
  203.  
  204. class Game_Temp
  205.   attr_accessor :item_amount
  206.   attr_reader   :selected_item
  207.  
  208.   alias selected_item_init initialize
  209.   def initialize
  210.     selected_item_init
  211.     @selected_item = Game_BaseItem.new
  212.     @item_amount = 0
  213.   end
  214.  
  215.   def clear_selected_item
  216.     @selected_item.object = nil
  217.     @item_amount = 0
  218.   end
  219.  
  220. end
  221.  
  222. class Scene_SelectItem < Scene_MenuBase
  223.  
  224.   include THEO::SELECT
  225.  
  226.   def start
  227.     super
  228.     create_help_window
  229.     create_footer_window
  230.     create_window_itemlist
  231.     create_amount_window
  232.   end
  233.  
  234.   def create_footer_window
  235.     @footer = Window_SelectFooter.new
  236.     @footer.viewport = @viewport
  237.   end
  238.  
  239.   def create_window_itemlist
  240.     x = 0
  241.     y = @help_window.height
  242.     w = Graphics.width
  243.     h = Graphics.height - y - @footer.height
  244.     @item_window = Window_SelectItem.new(x,y,w,h)
  245.     @item_window.viewport = @viewport
  246.     @item_window.help_window = @help_window
  247.     @item_window.set_handler(:ok, method(:select_item))
  248.     @item_window.set_handler(:cancel, method(:return_scene))
  249.     @item_window.category = :item
  250.     @item_window.activate
  251.     @item_window.select(0)
  252.   end
  253.  
  254.   def create_amount_window
  255.     @amount_window = Window_SelectAmount.new
  256.     @amount_window.viewport = @viewport
  257.     @amount_window.item_window = @item_window
  258.   end
  259.  
  260.   def select_item
  261.     if SHOW_AMOUNT
  262.       show_amount
  263.       return
  264.     end
  265.     on_amount_confirm
  266.   end
  267.  
  268.   def show_amount
  269.     @amount_window.open
  270.   end
  271.  
  272.   def update
  273.     super
  274.     if @amount_window.open?
  275.       hide_amount if Input.trigger?(:B)
  276.       on_amount_confirm if Input.trigger?(:C)
  277.     end
  278.   end
  279.  
  280.   def hide_amount
  281.     Sound.play_cancel
  282.     @amount_window.close
  283.     @item_window.activate
  284.   end
  285.  
  286.   def on_amount_confirm
  287.     Sound.play_ok
  288.     process_save_item
  289.     process_lose_item if LOSE_ITEM
  290.     return_scene
  291.   end
  292.  
  293.   def process_save_item
  294.     $game_temp.selected_item.object = @item_window.item
  295.     $game_temp.item_amount = @amount_window.amount
  296.   end
  297.  
  298.   def process_lose_item
  299.     $game_party.lose_item(@item_window.item,@amount_window.amount)
  300.   end
  301.  
  302. end
  303.  
  304. class Window_SelectFooter < Window_Base
  305.  
  306.   def initialize
  307.     w = Graphics.width
  308.     h = fitting_height(1)
  309.     x = 0
  310.     y = Graphics.height - h
  311.     super(x,y,w,h)
  312.     refresh
  313.   end
  314.  
  315.   def refresh
  316.     contents.clear
  317.     draw_text(0,0,contents.width,line_height,THEO::SELECT::SELECT_VOCAB,1)
  318.   end
  319.  
  320. end
  321.  
  322. class Window_SelectItem < Window_ItemList
  323.  
  324.   def enable?(item)
  325.     return true
  326.   end
  327.  
  328.   def item_number(item = @data[index])
  329.     $game_party.item_number(item)
  330.   end
  331.  
  332. end
  333.  
  334. class Window_SelectAmount < Window_Base  
  335.   attr_writer   :item_window
  336.   attr_reader   :amount
  337.  
  338.   include THEO::SELECT
  339.  
  340.   def initialize
  341.     super(0,0,window_width,window_height)
  342.     self.openness = 0
  343.     reset_amount
  344.     update_position
  345.     refresh
  346.   end
  347.  
  348.   def reset_amount
  349.     @amount = 1
  350.   end
  351.  
  352.   def open
  353.     super
  354.     reset_amount
  355.   end
  356.  
  357.   def update_position
  358.     self.x = (Graphics.width / 2) - (self.width / 2)
  359.     self.y = (Graphics.height / 2) - (self.height / 2)
  360.   end
  361.  
  362.   def refresh
  363.     contents.clear
  364.     draw_text(0,0,contents.width,24,AMOUNT_VOCAB)
  365.     draw_text(0,0,contents.width,24,@amount,2)
  366.   end
  367.  
  368.   def window_width
  369.     return AMOUNT_WIDTH
  370.   end
  371.  
  372.   def window_height
  373.     return 24+24
  374.   end
  375.  
  376.   def update
  377.     super
  378.     if open?
  379.       increment if Input.repeat?(:RIGHT)
  380.       decrement if Input.repeat?(:LEFT)
  381.       ten_increment if Input.repeat?(:UP)
  382.       ten_decrement if Input.repeat?(:DOWN)
  383.     end
  384.   end
  385.  
  386.   def increment
  387.     change_amount(1)
  388.   end
  389.  
  390.   def decrement
  391.     change_amount(-1)
  392.   end
  393.  
  394.   def ten_increment
  395.     change_amount(10)
  396.   end
  397.  
  398.   def ten_decrement
  399.     change_amount(-10)
  400.   end
  401.  
  402.   def change_amount(modifier)
  403.     @amount = [[@amount+modifier,1].max,max_amount].min
  404.     refresh
  405.   end
  406.  
  407.   def show
  408.     super
  409.     reset_amount
  410.   end
  411.  
  412.   def max_amount
  413.     @item_window.item_number rescue 0
  414.   end
  415.  
  416. end
Advertisement
Add Comment
Please, Sign In to add comment