TheoAllen

Item Cost Manager

Nov 28th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.34 KB | None | 0 0
  1. class << DataManager
  2.  
  3.   alias theo_icost_load_db load_database
  4.   def load_database
  5.     theo_icost_load_db
  6.     $data_items.compact.each do |item|
  7.       item.load_item_cost
  8.     end
  9.   end
  10.  
  11. end
  12.  
  13. class RPG::Item < RPG::UsableItem
  14.   attr_accessor :hp_cost
  15.   attr_accessor :mp_cost
  16.   attr_accessor :tp_cost
  17.   attr_accessor :hp_cost_per
  18.   attr_accessor :mp_cost_per
  19.   attr_accessor :tp_cost_per
  20.   attr_accessor :requirement
  21.   attr_accessor :perform
  22.  
  23.   def load_item_cost
  24.     @hp_cost = @mp_cost = @tp_cost = @hp_cost_per = @mp_cost_per =
  25.       @tp_cost_per = 0
  26.     @requirement = "true"
  27.     @perform = ""
  28.     req_add = false
  29.     perform_add = false
  30.     note.split(/[\r\n]+/).each do |line|
  31.       case line
  32.       when /<(?:hp cost|hp_cost):[ ]*(\d+)>/i
  33.         @hp_cost = $1.to_i
  34.       when /<(?:hp cost|hp_cost):[ ]*(\d+)\%>/i
  35.         @hp_cost_per = $1.to_i * 0.01
  36.       when /<(?:mp cost|mp_cost):[ ]*(\d+)>/i
  37.         @mp_cost = $1.to_i
  38.       when /<(?:mp cost|mp_cost):[ ]*(\d+)\%>/i
  39.         @mp_cost_per = $1.to_i * 0.01
  40.       when /<(?:tp cost|tp_cost):[ ]*(\d+)>/i
  41.         @tp_cost = $1.to_i
  42.       when /<(?:tp cost|tp_cost):[ ]*(\d+)\%>/i
  43.         @tp_cost_per = $1.to_i * 0.01
  44.       when /<(?:custom requirement|custom_requirement)>/i
  45.         req_add = true
  46.         @requirement = ""
  47.       when /<\/(?:custom requirement|custom_requirement)>/i
  48.         req_add = false
  49.       when /<(?:custom perform|custom_perform)>/i
  50.         perform_add = true
  51.         @perform = ""
  52.       when /<\/(?:custom perform|custom_perform)>/i
  53.         perform_add = false
  54.       else
  55.         if req_add
  56.           @requirement += line
  57.         elsif perform_add
  58.           @perform += line
  59.         end
  60.       end
  61.     end
  62.   end
  63.  
  64.   def usable?(user)
  65.     eval(requirement)
  66.   end
  67.  
  68. end
  69.  
  70. class Game_Battler < Game_BattlerBase
  71.  
  72.   def item_cost_payable?(item)
  73.     item_cost_hp(item) <= hp &&
  74.     item_cost_mp(item) <= mp &&
  75.     item_cost_tp(item) <= tp &&
  76.     item_cost_hpper(item) <= hp_rate &&
  77.     item_cost_mpper(item) <= mp_rate &&
  78.     item_cost_tpper(item) <= tp_rate
  79.   end
  80.  
  81.   def pay_item_cost(item)
  82.     self.hp -= (mhp * item_cost_hpper(item)).to_i
  83.     self.hp -= item_cost_hp(item)
  84.     self.mp -= (mmp * item_cost_mpper(item)).to_i
  85.     self.mp -= item_cost_mp(item)
  86.     self.tp -= (max_tp * item_cost_tpper(item)).to_i
  87.     self.tp -= item_cost_tp(item)
  88.     begin
  89.       eval(item.perform)
  90.     rescue
  91.       msgbox "there's something wrong with your custom cost perform"
  92.       return
  93.     end
  94.   end
  95.  
  96.   def item_cost_hp(item)
  97.     item.hp_cost
  98.   end
  99.  
  100.   def item_cost_mp(item)
  101.     (item.mp_cost * mcr).to_i
  102.   end
  103.  
  104.   def item_cost_tp(item)
  105.     item.tp_cost
  106.   end
  107.  
  108.   def item_cost_hpper(item)
  109.     item.hp_cost_per
  110.   end
  111.  
  112.   def item_cost_mpper(item)
  113.     item.mp_cost_per
  114.   end
  115.  
  116.   def item_cost_tpper(item)
  117.     item.tp_cost_per
  118.   end
  119.  
  120.   alias theo_icost_consume_item consume_item
  121.   def consume_item(item)
  122.     theo_icost_consume_item(item)
  123.     pay_item_cost(item)
  124.   end
  125.  
  126. end
  127.  
  128. class Window_BattleItem < Window_ItemList
  129.   attr_accessor :subject
  130.   alias theo_icost_enable? enable?
  131.   def enable?(item)
  132.     result = true
  133.     if @subject
  134.       result = @subject.item_cost_payable?(item) && item.usable?(@subject)
  135.     end
  136.     result && theo_icost_enable?(item)
  137.   end
  138. end
  139.  
  140. #~ class Window_Help < Window_Base
  141. #~   alias theo_icost_set_item set_item
  142. #~   def set_item(item)
  143. #~     theo_icost_set_item(item)
  144. #~     return unless SceneManager.scene_is?(Scene_Battle)
  145. #~     draw_item_cost(item)
  146. #~   end
  147. #~  
  148. #~   def draw_item_cost(item)
  149. #~     hp_color = 20
  150. #~     mp_color = 23
  151. #~     tp_color = 29
  152. #~     text = "\\}"
  153. #~     text += "\\C[#{hp_color}]"
  154. #~     text += "#{item.hp_cost} HP" if item.hp_cost > 0
  155. #~     text += "\\C[#{mp_color}]"
  156. #~     text += "#{item.mp_cost} MP" if item.mp_cost > 0
  157. #~     text += "\\C[#{tp_color}]"
  158. #~     text += "#{item.hp_cost} TP" if item.tp_cost > 0
  159. #~     text_to_calculate = text.gsub(/(\\C\[\d+\]|\\})/i) {""}
  160. #~     puts text_to_calculate
  161. #~     wdth = text_size(text_to_calculate).width
  162. #~     draw_text_ex(contents.width - wdth/2, line_height + 8, text)
  163. #~   end
  164. #~  
  165. #~ end
  166.  
  167. class Scene_Battle < Scene_Base
  168.   alias theo_icost_cmd_item command_item
  169.   def command_item
  170.     @item_window.subject = BattleManager.actor
  171.     theo_icost_cmd_item
  172.   end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment