TheoAllen

Theo - Event Self Variable

Apr 12th, 2013
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.53 KB | None | 0 0
  1. # ============================================================================
  2. # EVENT SELF VARIABLES
  3. # AUTHOR : TheoAllen ~
  4. # Contact : www.rpgmakerid.com
  5. # ============================================================================
  6. $imported = {} if $imported.nil?
  7. $imported[:THEO_EventVariable] = true
  8. # ============================================================================
  9. =begin
  10.  
  11.   PENGENALAN :
  12.   Script ini memberikan fitur tiap event punya individual variable sendiri
  13.  
  14.   PENGGUNAAN :
  15.   Panggil dalam script call :
  16.   - variables[index] = angka >> contoh pengesetan. index bisa berapa aja
  17.   - variables[index] > angka >> contoh dalam conditional branch
  18.  
  19.   Untuk pemanggilan jarak jauh, gunakan script call kek gini
  20.   array = [map_id,event_id]
  21.   variables(array)[index] >> terusin ndiri
  22.  
  23.   BONUS FEATURE :
  24.   ~ Self-switch
  25.     Panggil >> variables["A"] = true << untuk ganti self_switch A
  26.    
  27.   TERMS OF SERVICES :
  28.   - Kamu boleh edit dan redistribusi dengan versi editanmu sendiri. Hanya saja
  29.     kamu harus tetap tulis credit ke gw "TheoAllen".
  30.   - Penggunaan untuk komersil terserah. Tapi gwa lebih suka kalo dikasi
  31.     gratisan projectnya ~
  32.   - Semisal ada error gara2 u edit2 sendiri. Gw ga tanggung jawab :v
  33.  
  34. =end
  35. # =============================================================================
  36. # ♫ Module DataManager ~ ♫
  37. # =============================================================================
  38. module DataManager
  39.   #--------------------------------------------------------------------------
  40.   # ♫ Aliasing lists
  41.   #--------------------------------------------------------------------------  
  42.   class << self
  43.     alias ama_create_game_obj create_game_objects
  44.     alias ama_save_contents make_save_contents
  45.     alias ama_load_contents extract_save_contents
  46.   end
  47.   #--------------------------------------------------------------------------
  48.   # ♫ Bikin object $game_self_variables
  49.   #--------------------------------------------------------------------------    
  50.   def self.create_game_objects
  51.     ama_create_game_obj
  52.     $game_self_variables = Game_SelfVariables.new
  53.   end
  54.   #--------------------------------------------------------------------------
  55.   # ♫ Bikin save contents. Masukin $game_self_variables
  56.   #--------------------------------------------------------------------------  
  57.   def self.make_save_contents
  58.     contents = ama_save_contents
  59.     contents[:selfvariable] = $game_self_variables
  60.     contents
  61.   end
  62.   #--------------------------------------------------------------------------
  63.   # ♫ Ngeload save contents. Termasuk $game_self_variables
  64.   #--------------------------------------------------------------------------  
  65.   def self.extract_save_contents(contents)
  66.     ama_load_contents(contents)
  67.     $game_self_variables = contents[:selfvariable]
  68.   end
  69. end  
  70. # =============================================================================
  71. # ♫ Game Variable Hashes ~ ♫
  72. # =-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  73. # NOTE :
  74. # Class ini yang ngurus penyimpanan data-data dan semacemnya beserta indexnya.
  75. # Class ini berada sebagai value dari object Game_SelfVariable
  76. # =-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  77. # - def link_to_self_switch   >> update nilai self switch
  78. # - def [](key)               >> dapetin nilai
  79. # - def []=(key,value)        >> ngubah nilai
  80. # - def on_change(key,value)  >> refresh kalo berubah
  81. # - def self_switch           >> refers ke $game_self_switches
  82. # =============================================================================
  83. class Game_VariableHashes
  84.   attr_reader :event_key
  85.   #--------------------------------------------------------------------------
  86.   # ♫ Inisialisasi
  87.   #--------------------------------------------------------------------------  
  88.   def initialize(event_key)
  89.     @data = {}
  90.     @event_key = event_key
  91.   end
  92.   #--------------------------------------------------------------------------
  93.   # ♫ Refresh nilai self switch
  94.   #--------------------------------------------------------------------------  
  95.   def link_to_self_switches
  96.     @data["A"] = self_switches[event_key + ["A"]]
  97.     @data["B"] = self_switches[event_key + ["B"]]
  98.     @data["C"] = self_switches[event_key + ["C"]]
  99.     @data["D"] = self_switches[event_key + ["D"]]
  100.   end
  101.   #--------------------------------------------------------------------------
  102.   # ♫ Buat dapetin nilai
  103.   #--------------------------------------------------------------------------  
  104.   def [](key)
  105.     link_to_self_switches
  106.     @data[key] = 0 if @data[key].nil?
  107.     @data[key]
  108.   end
  109.   #--------------------------------------------------------------------------
  110.   # ♫ Buat ngeset nilai
  111.   #--------------------------------------------------------------------------  
  112.   def []=(key,value)
  113.     @data[key] = value
  114.     on_change(key)
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # ♫ Refresh kalo berubah
  118.   #--------------------------------------------------------------------------  
  119.   def on_change(key)
  120.     if key.class == String
  121.       value = @data[key]
  122.       check1 = (key == "A" || key == "B" || key == "C" || key == "D")
  123.       check2 = (value.class == TrueClass || value.class == FalseClass)
  124.       return unless check1 && check2
  125.       switch_key = event_key + [key]
  126.       self_switches[switch_key] = value      
  127.     else
  128.       link_to_self_switches
  129.       $game_map.need_refresh = true
  130.     end
  131.   end
  132.   #--------------------------------------------------------------------------
  133.   # ♫ Ngambil self switch
  134.   #--------------------------------------------------------------------------  
  135.   def self_switches
  136.     $game_self_switches
  137.   end
  138.   #--------------------------------------------------------------------------
  139.   # ♫ End
  140.   #--------------------------------------------------------------------------  
  141. end
  142. # =============================================================================
  143. # ♫ Game Self Variable ~ ♫
  144. # =-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  145. # NOTE :
  146. # Class ini gunanya buat nentuin alamat tempat variable-variable disimpen
  147. # Misalnya kek {[map_id,event_id] => {[1] => 3}} (Hash)
  148. # =============================================================================
  149. class Game_SelfVariables
  150.  
  151.   def initialize
  152.     @data = {}
  153.   end
  154.  
  155.   def [](key)
  156.     @data[key] = Game_VariableHashes.new(key) if @data[key].nil?
  157.     @data[key]
  158.   end
  159.  
  160. end
  161. # =============================================================================
  162. # ♫ Game Map ~ ♫
  163. # - def id >> Get map ID
  164. # =============================================================================
  165. class Game_Map
  166.  
  167.   def id
  168.     @map_id
  169.   end
  170.  
  171. end
  172. # =============================================================================
  173. # ♫ Game Interpreter ~ ♫
  174. # - def event_key >> ngambil array yg isinya [map_id, event_id]
  175. # - def variable  >> refers ke $game_self_variables (return Hash)
  176. # =============================================================================
  177. class Game_Interpreter
  178.  
  179.   def event_key
  180.     [$game_map.id,event_id]
  181.   end
  182.  
  183.   def variables(key = event_key)
  184.     return variables[event_key] unless key.class == Array
  185.     return variables[event_key] unless key.size == 2
  186.     $game_self_variables[key]
  187.   end
  188.  
  189. end
  190. # =============================================================================
  191. #
  192. #                           ♫  End of Script ♫
  193. #
  194. # =============================================================================
Advertisement
Add Comment
Please, Sign In to add comment