modern_algebra

SSS Skill Shop

Apr 25th, 2011
1,086
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #===============================================================================
  2. #
  3. # Shanghai Simple Script - Skill Shop
  4. # Last Date Updated: 2010.06.06
  5. # Level: Normal
  6. #
  7. # This script lets actors buy skills from a shop and use them in battle.
  8. #===============================================================================
  9. # Instructions
  10. # -----------------------------------------------------------------------------
  11. # To install this script, open up your script editor and copy/paste this script
  12. # to an open slot below ▼ Materials but above ▼ Main. Remember to save.
  13. #
  14. # <buy cost: x>
  15. # Sets the price for how much the skill sells for.
  16. #
  17. # <buy class: x>
  18. # <buy class: x, x, x>
  19. # The classes that are allowed to buy this skill. If nothing is used, then all
  20. # all classes are allowed to buy it.
  21. #
  22. # <buy actor: x>
  23. # <buy actor: x, x, x>
  24. # The actors that are allowed to buy this skill. No changes if nothing is used.
  25. #
  26. # <buy level requirement: x>
  27. # The required level in order to buy this skill.
  28. #
  29. # To launch the skill shop, type something like the following in a script call
  30. # event on the map.
  31. #
  32. # skills = [1, 2, 3, 4, 33..40, 41..80]
  33. # $scene = Scene_SkillShop.new(skills)
  34. #
  35. # And you will automatically be taken to the skill shop screen.
  36. #===============================================================================
  37.  
  38. $imported = {} if $imported == nil
  39. $imported["SkillShop"] = true
  40.  
  41. module SSS
  42.   # This is the default cost for skills.
  43.   DEFAULT_BUY_SKILL_COST = 1000
  44.   # This icon will appear next to the actor's name if already learned the skill.
  45.   SKILL_LEARNED_ICON = 141
  46. end
  47.  
  48. #==============================================================================
  49. # RPG::Skill
  50. #==============================================================================
  51.  
  52. class RPG::Skill < RPG::UsableItem
  53.   #--------------------------------------------------------------------------
  54.   # # Buy Skill Cost
  55.   #--------------------------------------------------------------------------
  56.   def buy_skill_cost
  57.     return @buy_skill_cost if @buy_skill_cost != nil
  58.     @buy_skill_cost = SSS::DEFAULT_BUY_SKILL_COST
  59.     self.note.split(/[\r\n]+/).each { |line|
  60.       case line
  61.       when /<(?:BUY_COST|BUY COST):[ ](\d+)>/i
  62.         @buy_skill_cost = $1.to_i
  63.       end
  64.     }
  65.     return @buy_skill_cost
  66.   end
  67.   #--------------------------------------------------------------------------
  68.   # # Buy For Class Skill
  69.   #--------------------------------------------------------------------------
  70.   def buy_for_class_skill
  71.     return @buy_for_class_skill if @buy_for_class_skill != nil
  72.     @buy_for_class_skill = []
  73.     self.note.split(/[\r\n]+/).each { |line|
  74.       case line
  75.       when /<(?:BUY_CLASS|BUY CLASS):[ ](\d+(?:\s*,\s*\d+)*)>/i
  76.         $1.scan(/\d+/).each { |num|
  77.         @buy_for_class_skill.push(num.to_i) if num.to_i > 0 }
  78.       end
  79.     }
  80.     if @buy_for_class_skill == [] and self.buy_for_actor_skill == []
  81.       for class_id in $data_classes
  82.         @buy_for_class_skill.push(class_id.id)
  83.       end
  84.     end
  85.     return @buy_for_class_skill
  86.   end
  87.   #--------------------------------------------------------------------------
  88.   # # Buy For Actor Skill
  89.   #--------------------------------------------------------------------------
  90.   def buy_for_actor_skill
  91.     return @buy_for_actor_skill if @buy_for_actor_skill != nil
  92.     @buy_for_actor_skill = []
  93.     self.note.split(/[\r\n]+/).each { |line|
  94.       case line
  95.       when /<(?:BUY_ACTOR|BUY ACTOR):[ ](\d+(?:\s*,\s*\d+)*)>/i
  96.         $1.scan(/\d+/).each { |num|
  97.         @buy_for_actor_skill.push(num.to_i) if num.to_i > 0 }
  98.       end
  99.     }
  100.     return @buy_for_actor_skill
  101.   end
  102.   #--------------------------------------------------------------------------
  103.   # # Buy Skill Level Requirement
  104.   #--------------------------------------------------------------------------
  105.   def buy_skill_level_requirement
  106.     return @buy_skill_level_requirement if @buy_skill_level_requirement != nil
  107.     @buy_skill_level_requirement = 0
  108.     self.note.split(/[\r\n]+/).each { |line|
  109.       case line
  110.       when /<(?:BUY LEVEL REQUIREMENT|BUY LEVEL REQUIREMENT):[ ](\d+)>/i
  111.         @buy_skill_level_requirement = $1.to_i
  112.       end
  113.     }
  114.     return @buy_skill_level_requirement
  115.   end
  116. end
  117.  
  118. #==============================================================================
  119. # ** Window_SkillBuy
  120. #==============================================================================
  121.  
  122. class Window_SkillBuy < Window_Selectable
  123.   #--------------------------------------------------------------------------
  124.   # * Object Initialization
  125.   #--------------------------------------------------------------------------
  126.   def initialize(gold_window, skill_list)
  127.     @skill_list = skill_list
  128.     window_y = gold_window.y + gold_window.height
  129.     super(0, window_y, Graphics.width - 240, Graphics.height - window_y)
  130.     refresh
  131.     self.index = 0
  132.   end
  133.   #--------------------------------------------------------------------------
  134.   # * Get Skill
  135.   #--------------------------------------------------------------------------
  136.   def skill
  137.     return @data[self.index]
  138.   end
  139.   #--------------------------------------------------------------------------
  140.   # * Refresh
  141.   #--------------------------------------------------------------------------
  142.   def refresh
  143.     @data = []
  144.     for skill in @skill_list
  145.       @data.push(skill) if include?(skill)
  146.     end
  147.     @item_max = @data.size
  148.     create_contents
  149.     for i in 0...@item_max
  150.       draw_item(i)
  151.     end
  152.   end
  153.   #--------------------------------------------------------------------------
  154.   # * Whether or not to include in skill list
  155.   #--------------------------------------------------------------------------
  156.   def include?(skill)
  157.     return false if skill.nil?
  158.     return true
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # * Draw Item
  162.   #--------------------------------------------------------------------------
  163.   def draw_item(index)
  164.     rect = item_rect(index)
  165.     self.contents.clear_rect(rect)
  166.     skill = @data[index]
  167.     if skill != nil
  168.       rect.width -= 4
  169.       enabled = can_buy_skill?(skill)
  170.       draw_item_name(skill, rect.x, rect.y, enabled)
  171.       self.contents.draw_text(rect, skill.buy_skill_cost, 2)
  172.     end
  173.   end
  174.   #--------------------------------------------------------------------------
  175.   # * Whether or not player can buy the skill
  176.   #--------------------------------------------------------------------------
  177.   def can_buy_skill?(skill)
  178.     return false if skill.nil?
  179.     return false if skill.buy_skill_cost > $game_party.gold
  180.     return true
  181.   end
  182.   #--------------------------------------------------------------------------
  183.   # * Update Help Text
  184.   #--------------------------------------------------------------------------
  185.   def update_help
  186.     @help_window.set_text(skill == nil ? "" : skill.description)
  187.   end
  188. end
  189.  
  190. #==============================================================================
  191. # ** Window_SkillShopStatus
  192. #==============================================================================
  193.  
  194. class Window_SkillShopStatus < Window_Selectable
  195.   #--------------------------------------------------------------------------
  196.   # * Object Initialization
  197.   #--------------------------------------------------------------------------
  198.   def initialize(buy_window)
  199.     @buy_window = buy_window
  200.     super(@buy_window.width, @buy_window.y, 240, @buy_window.height)
  201.     self.active = false
  202.     self.index = -1
  203.     refresh
  204.   end
  205.   #--------------------------------------------------------------------------
  206.   # * Get Actor
  207.   #--------------------------------------------------------------------------
  208.   def actor
  209.     return @data[self.index]
  210.   end
  211.   #--------------------------------------------------------------------------
  212.   # * Refresh
  213.   #--------------------------------------------------------------------------
  214.   def refresh
  215.     @skill = @buy_window.skill
  216.     @data = $game_party.members
  217.     @item_max = @data.size
  218.     create_contents
  219.     for i in 0...@item_max
  220.       draw_item(i)
  221.     end
  222.   end
  223.   #--------------------------------------------------------------------------
  224.   # * Get rectangle for displaying items
  225.   #--------------------------------------------------------------------------
  226.   def item_rect(index)
  227.     rect = Rect.new(0, 0, 0, 0)
  228.     rect.width = (contents.width + @spacing) / @column_max - @spacing
  229.     rect.height = (contents.height / [@item_max, 1].max)
  230.     rect.x = index % @column_max * (rect.width + @spacing)
  231.     rect.y = index / @column_max * rect.height
  232.     return rect
  233.   end
  234.   #--------------------------------------------------------------------------
  235.   # * Draw Item
  236.   #--------------------------------------------------------------------------
  237.   def draw_item(index)
  238.     rect = item_rect(index)
  239.     self.contents.clear_rect(rect)
  240.     actor = @data[index]
  241.     if actor != nil
  242.       rect.width -= 4
  243.       enabled = can_learn_skill?(actor, @skill)
  244.       self.contents.font.color.alpha = enabled ? 255 : 128
  245.       face_width = [92, rect.height-4].min
  246.       draw_actor_face(actor, rect.x+2, rect.y+2, face_width)
  247.       if actor.skill_learn?(@skill)
  248.         draw_icon(SSS::SKILL_LEARNED_ICON, rect.x+4+face_width, rect.y+2)
  249.         face_width += 24
  250.       end
  251.       self.contents.draw_text(rect.x+4+face_width, rect.y+2, 108, 24, actor.name)
  252.       return if WLH*2 + rect.y+2 > rect.height + rect.y
  253.       level = sprintf("%s %d", Vocab.level, actor.level)
  254.       rect.y += WLH
  255.       if actor.skill_learn?(@skill)
  256.         draw_icon(@skill.icon_index, rect.x+4+face_width-24, rect.y+2)
  257.       end
  258.       self.contents.draw_text(rect.x+4+face_width, rect.y+2, 108, 24, level)
  259.     end
  260.   end
  261.   #--------------------------------------------------------------------------
  262.   # * Can the actor learn the skill?
  263.   #--------------------------------------------------------------------------
  264.   def can_learn_skill?(actor, skill)
  265.     return false if skill.nil?
  266.     return false if actor.skill_learn?(skill)
  267.     return false if skill.buy_skill_level_requirement > actor.level
  268.     return true if skill.buy_for_actor_skill.include?(actor.id)
  269.     return true if skill.buy_for_class_skill.include?(actor.class.id)
  270.     return false
  271.   end
  272. end
  273.  
  274. #==============================================================================
  275. # ** Scene_SkillShop
  276. #==============================================================================
  277.  
  278. class Scene_SkillShop < Scene_Base
  279.   #--------------------------------------------------------------------------
  280.   # * Object Initialization
  281.   #--------------------------------------------------------------------------
  282.   def initialize(skill_list = [])
  283.     id_list = []
  284.     for skill_id in skill_list
  285.       case skill_id
  286.       when Integer
  287.         id_list.push(skill_id)
  288.       when Range
  289.         id_list |= skill_id.to_a
  290.       end
  291.     end
  292.     @skill_list = []
  293.     for skill_id in id_list
  294.       @skill_list.push($data_skills[skill_id])
  295.     end
  296.   end
  297.   #--------------------------------------------------------------------------
  298.   # * Start processing
  299.   #--------------------------------------------------------------------------
  300.   def start
  301.     super
  302.     create_menu_background
  303.     @help_window = Window_Help.new
  304.     @gold_window = Window_Gold.new(Graphics.width - 160, @help_window.height)
  305.     create_command_window
  306.     @buy_window = Window_SkillBuy.new(@gold_window, @skill_list)
  307.     @buy_window.y = @gold_window.y + @gold_window.height
  308.     @buy_window.help_window = @help_window
  309.     @buy_window.active = false
  310.     @status_window = Window_SkillShopStatus.new(@buy_window)
  311.   end
  312.   #--------------------------------------------------------------------------
  313.   # * Termination Processing
  314.   #--------------------------------------------------------------------------
  315.   def terminate
  316.     super
  317.     dispose_menu_background
  318.     @command_window.dispose
  319.     @help_window.dispose
  320.     @gold_window.dispose
  321.     @buy_window.dispose
  322.     @status_window.dispose
  323.   end
  324.   #--------------------------------------------------------------------------
  325.   # * Create Command Window
  326.   #--------------------------------------------------------------------------
  327.   def create_command_window
  328.     s1 = Vocab::ShopBuy
  329.     s2 = Vocab::ShopCancel
  330.     @command_window = Window_Command.new(Graphics.width - 160, [s1, s2], 2)
  331.     @command_window.y = @gold_window.y
  332.   end
  333.   #--------------------------------------------------------------------------
  334.   # * Frame Update
  335.   #--------------------------------------------------------------------------
  336.   def update
  337.     super
  338.     update_menu_background
  339.     @help_window.update
  340.     @gold_window.update
  341.     if @command_window.active
  342.       update_command_selection
  343.     elsif @buy_window.active
  344.       update_buy_selection
  345.     elsif @status_window.active
  346.       update_status_selection
  347.     end
  348.   end
  349.   #--------------------------------------------------------------------------
  350.   # * Update Command Selection
  351.   #--------------------------------------------------------------------------
  352.   def update_command_selection
  353.     @command_window.update
  354.     if Input.trigger?(Input::B)
  355.       Sound.play_cancel
  356.       $scene = Scene_Map.new
  357.     elsif Input.trigger?(Input::C)
  358.       case @command_window.index
  359.       when 0  # buy
  360.         Sound.play_decision
  361.         @command_window.active = false
  362.         @buy_window.active = true
  363.       when 1  # quit
  364.         Sound.play_decision
  365.         $scene = Scene_Map.new
  366.       end
  367.     end
  368.   end
  369.   #--------------------------------------------------------------------------
  370.   # * Update Buy Item Selection
  371.   #--------------------------------------------------------------------------
  372.   def update_buy_selection
  373.     @buy_window.update
  374.     if @last_buy_index != @buy_window.index
  375.       @last_buy_index = @buy_window.index
  376.       @status_window.refresh
  377.     end
  378.     if Input.trigger?(Input::B)
  379.       Sound.play_cancel
  380.       @command_window.active = true
  381.       @buy_window.active = false
  382.       @last_buy_index = -1
  383.     elsif Input.trigger?(Input::C)
  384.       if @buy_window.can_buy_skill?(@buy_window.skill)
  385.         Sound.play_decision
  386.         @status_window.index = 0
  387.         @status_window.active = true
  388.         @buy_window.active = false
  389.       else
  390.         Sound.play_buzzer
  391.       end
  392.     end
  393.   end
  394.   #--------------------------------------------------------------------------
  395.   # * Update Status Selection
  396.   #--------------------------------------------------------------------------
  397.   def update_status_selection
  398.     @status_window.update
  399.     if Input.trigger?(Input::B)
  400.       Sound.play_cancel
  401.       @status_window.index = -1
  402.       @status_window.active = false
  403.       @buy_window.active = true
  404.     elsif Input.trigger?(Input::C)
  405.       if @status_window.can_learn_skill?(@status_window.actor, @buy_window.skill)
  406.         Sound.play_shop
  407.         @status_window.actor.learn_skill(@buy_window.skill.id)
  408.         $game_party.lose_gold(@buy_window.skill.buy_skill_cost)
  409.         @gold_window.refresh
  410.         @buy_window.refresh
  411.         @status_window.refresh
  412.       else
  413.         Sound.play_buzzer
  414.       end
  415.     end
  416.   end
  417. end
  418.  
  419. #===============================================================================
  420. #
  421. # END OF FILE
  422. #
  423. #===============================================================================
RAW Paste Data