Advertisement
AngryPacman

PAC Skills Shop Scene

Jul 29th, 2011
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.75 KB | None | 0 0
  1. #===============================================================================
  2. #
  3. # Pacman Advanced Creatived (PAC) Engine - Skills Shop Scene
  4. # 5/6/2011
  5. # Type: Scene
  6. # Installation: Simple values, event commands, tags.
  7. # Level: Difficult
  8. #
  9. #===============================================================================
  10. #
  11. # Description:
  12. # These seem quite popular, so I decided to make one. It's a fairly simple
  13. # skills shop(pe), functioning through script calls and tags. Trust me, it's
  14. # the simplest way to do it. Players will be able to purchase skills for their
  15. # actors and use them in battle and menus.
  16. #
  17. #===============================================================================
  18. #
  19. # Insctuctions:
  20. # INSTALLATION
  21. # Paste above main, below materials in the script editor. Remember to save.
  22. # Follow configuration, tags and call instructions as below.
  23. # CONFIGURATION
  24. # DEFAULT_BUY_SKIIL_COST is, of course, the default cost for all skills.
  25. # SKILL_LEARNED_ICON is the number of the icon that will display if the current
  26. # skill is already taught to them.
  27. # TAGS
  28. # <buy cost: x>
  29. # Sets the cost for the skill in whose notebox this is placed.
  30. # <buy class: x>
  31. # <buy class: x, x, x>
  32. # The classes that are allowed to buy the skill. If nothing is used, all will
  33. # be able to purchase it.
  34. # <buy actor: x>
  35. # <buy actor: x, x, x>
  36. # The actors able to purchase the skill. If nothing is used, all will be able
  37. # to buy it.
  38. # <buy level requirement: x>
  39. # The level required to purchase the skill.
  40. # SCRIPT CALLS
  41. # In an event, use the script command and enter the following syntax:
  42. # skills = [Skill IDs]
  43. # $scene = Scene_SkillShop.new(skills)
  44. # For example, use:
  45. # skills = [1, 2, 5, 6..10, 15...22, 60, 62]
  46. # $scene = Scene_SkillShop.new(skills)
  47. # To enter the skill shop with the chosen skill IDs. Note, .. means that all
  48. # numbers between the two, inclusive of the last one will be used, ... means
  49. # that all numbers between the two, exclusive of the last will be used.
  50. #
  51. #===============================================================================
  52. #
  53. # EDITING BEGINS AT LINE 65. DO NOT TOUCH LINES 55-63.
  54. #
  55. #===============================================================================
  56.  
  57. $imported = {} if $imported == nil
  58. $imported["PAC_SkillShop"] = true
  59.  
  60. module PAC
  61.   module MENU
  62.    
  63. #===============================================================================
  64. #
  65. # BEGIN EDIING
  66. #
  67. #===============================================================================
  68.    
  69.     # This is the default cost for skills.
  70.     DEFAULT_BUY_SKILL_COST = 500
  71.     # This icon will appear next to the actor's name if skill already learned.
  72.     SKILL_LEARNED_ICON = 141
  73.    
  74. #===============================================================================
  75. #
  76. # This script no longer requires editing. Do not edit anything in this script
  77. # unless you are a compenent scripter. Should you edit without any scripting
  78. # education, it may result in me tutting at you for getting it wrong.
  79. #
  80. #===============================================================================
  81.    
  82.   end
  83. end
  84.  
  85. #==============================================================================
  86. # RPG::Skill
  87. #==============================================================================
  88.  
  89. class RPG::Skill < RPG::UsableItem
  90.   #--------------------------------------------------------------------------
  91.   # * Buy Skill Cost
  92.   #--------------------------------------------------------------------------
  93.   def buy_skill_cost
  94.     return @buy_skill_cost if @buy_skill_cost != nil
  95.     @buy_skill_cost = PAC::MENU::DEFAULT_BUY_SKILL_COST
  96.     self.note.split(/[\r\n]+/).each { |line|
  97.       case line
  98.       when /<(?:BUY_COST|BUY COST):[ ](\d+)>/i
  99.         @buy_skill_cost = $1.to_i
  100.       end
  101.     }
  102.     return @buy_skill_cost
  103.   end
  104.   #--------------------------------------------------------------------------
  105.   # * Buy For Class Skill
  106.   #--------------------------------------------------------------------------
  107.   def buy_for_class_skill
  108.     return @buy_for_class_skill if @buy_for_class_skill != nil
  109.     @buy_for_class_skill = []
  110.     self.note.split(/[\r\n]+/).each { |line|
  111.       case line
  112.       when /<(?:BUY_CLASS|BUY CLASS):[ ](\d+(?:\s*,\s*\d+)*)>/i
  113.         $1.scan(/\d+/).each { |num|
  114.         @buy_for_class_skill.push(num.to_i) if num.to_i > 0 }
  115.       end
  116.     }
  117.     if @buy_for_class_skill == [] and self.buy_for_actor_skill == []
  118.       for class_id in $data_classes
  119.         @buy_for_class_skill.push(class_id.id)
  120.       end
  121.     end
  122.     return @buy_for_class_skill
  123.   end
  124.   #--------------------------------------------------------------------------
  125.   # * Buy For Actor Skill
  126.   #--------------------------------------------------------------------------
  127.   def buy_for_actor_skill
  128.     return @buy_for_actor_skill if @buy_for_actor_skill != nil
  129.     @buy_for_actor_skill = []
  130.     self.note.split(/[\r\n]+/).each { |line|
  131.       case line
  132.       when /<(?:BUY_ACTOR|BUY ACTOR):[ ](\d+(?:\s*,\s*\d+)*)>/i
  133.         $1.scan(/\d+/).each { |num|
  134.         @buy_for_actor_skill.push(num.to_i) if num.to_i > 0 }
  135.       end
  136.     }
  137.     return @buy_for_actor_skill
  138.   end
  139.   #--------------------------------------------------------------------------
  140.   # * Buy Skill Level Requirement
  141.   #--------------------------------------------------------------------------
  142.   def buy_skill_level_requirement
  143.     return @buy_skill_level_requirement if @buy_skill_level_requirement != nil
  144.     @buy_skill_level_requirement = 0
  145.     self.note.split(/[\r\n]+/).each { |line|
  146.       case line
  147.       when /<(?:BUY LEVEL REQUIREMENT|BUY LEVEL REQUIREMENT):[ ](\d+)>/i
  148.         @buy_skill_level_requirement = $1.to_i
  149.       end
  150.     }
  151.     return @buy_skill_level_requirement
  152.   end
  153. end
  154.  
  155. #==============================================================================
  156. # ** Window_SkillBuy
  157. #==============================================================================
  158.  
  159. class Window_SkillBuy < Window_Selectable
  160.   #--------------------------------------------------------------------------
  161.   # * Object Initialization
  162.   #--------------------------------------------------------------------------
  163.   def initialize(gold_window, skill_list)
  164.     @skill_list = skill_list
  165.     window_y = gold_window.y + gold_window.height
  166.     super(0, window_y, Graphics.width - 240, Graphics.height - window_y)
  167.     refresh
  168.     self.index = 0
  169.   end
  170.   #--------------------------------------------------------------------------
  171.   # * Get Skill
  172.   #--------------------------------------------------------------------------
  173.   def skill
  174.     return @data[self.index]
  175.   end
  176.   #--------------------------------------------------------------------------
  177.   # * Refresh
  178.   #--------------------------------------------------------------------------
  179.   def refresh
  180.     @data = []
  181.     for skill in @skill_list
  182.       @data.push(skill) if include?(skill)
  183.     end
  184.     @item_max = @data.size
  185.     create_contents
  186.     for i in 0...@item_max
  187.       draw_item(i)
  188.     end
  189.   end
  190.   #--------------------------------------------------------------------------
  191.   # * Whether or not to include in skill list
  192.   #--------------------------------------------------------------------------
  193.   def include?(skill)
  194.     return false if skill.nil?
  195.     return true
  196.   end
  197.   #--------------------------------------------------------------------------
  198.   # * Draw Item
  199.   #--------------------------------------------------------------------------
  200.   def draw_item(index)
  201.     rect = item_rect(index)
  202.     self.contents.clear_rect(rect)
  203.     skill = @data[index]
  204.     if skill != nil
  205.       rect.width -= 4
  206.       enabled = can_buy_skill?(skill)
  207.       draw_item_name(skill, rect.x, rect.y, enabled)
  208.       self.contents.draw_text(rect, skill.buy_skill_cost, 2)
  209.     end
  210.   end
  211.   #--------------------------------------------------------------------------
  212.   # * Whether or not player can buy the skill
  213.   #--------------------------------------------------------------------------
  214.   def can_buy_skill?(skill)
  215.     return false if skill.nil?
  216.     return false if skill.buy_skill_cost > $game_party.gold
  217.     return true
  218.   end
  219.   #--------------------------------------------------------------------------
  220.   # * Update Help Text
  221.   #--------------------------------------------------------------------------
  222.   def update_help
  223.     @help_window.set_text(skill == nil ? "" : skill.description)
  224.   end
  225. end
  226.  
  227. #==============================================================================
  228. # ** Window_SkillShopStatus
  229. #==============================================================================
  230.  
  231. class Window_SkillShopStatus < Window_Selectable
  232.   #--------------------------------------------------------------------------
  233.   # * Object Initialization
  234.   #--------------------------------------------------------------------------
  235.   def initialize(buy_window)
  236.     @buy_window = buy_window
  237.     super(@buy_window.width, @buy_window.y, 240, @buy_window.height)
  238.     self.active = false
  239.     self.index = -1
  240.     refresh
  241.   end
  242.   #--------------------------------------------------------------------------
  243.   # * Get Actor
  244.   #--------------------------------------------------------------------------
  245.   def actor
  246.     return @data[self.index]
  247.   end
  248.   #--------------------------------------------------------------------------
  249.   # * Refresh
  250.   #--------------------------------------------------------------------------
  251.   def refresh
  252.     @skill = @buy_window.skill
  253.     @data = $game_party.members
  254.     @item_max = @data.size
  255.     create_contents
  256.     for i in 0...@item_max
  257.       draw_item(i)
  258.     end
  259.   end
  260.   #--------------------------------------------------------------------------
  261.   # * Get rectangle for displaying items
  262.   #--------------------------------------------------------------------------
  263.   def item_rect(index)
  264.     rect = Rect.new(0, 0, 0, 0)
  265.     rect.width = (contents.width + @spacing) / @column_max - @spacing
  266.     rect.height = (contents.height / [@item_max, 1].max)
  267.     rect.x = index % @column_max * (rect.width + @spacing)
  268.     rect.y = index / @column_max * rect.height
  269.     return rect
  270.   end
  271.   #--------------------------------------------------------------------------
  272.   # * Draw Item
  273.   #--------------------------------------------------------------------------
  274.   def draw_item(index)
  275.     rect = item_rect(index)
  276.     self.contents.clear_rect(rect)
  277.     actor = @data[index]
  278.     if actor != nil
  279.       rect.width -= 4
  280.       enabled = can_learn_skill?(actor, @skill)
  281.       self.contents.font.color.alpha = enabled ? 255 : 128
  282.       face_width = [92, rect.height-4].min
  283.       draw_actor_face(actor, rect.x+2, rect.y+2, face_width)
  284.       if actor.skill_learn?(@skill)
  285.         draw_icon(PAC::MENU::SKILL_LEARNED_ICON, rect.x+4+face_width, rect.y+2)
  286.         face_width += 24
  287.       end
  288.       self.contents.draw_text(rect.x+4+face_width, rect.y+2, 108, 24, actor.name)
  289.       return if WLH*2 + rect.y+2 > rect.height + rect.y
  290.       level = sprintf("%s %d", Vocab.level, actor.level)
  291.       rect.y += WLH
  292.       if actor.skill_learn?(@skill)
  293.         draw_icon(@skill.icon_index, rect.x+4+face_width-24, rect.y+2)
  294.       end
  295.       self.contents.draw_text(rect.x+4+face_width, rect.y+2, 108, 24, level)
  296.     end
  297.   end
  298.   #--------------------------------------------------------------------------
  299.   # * Can the actor learn the skill?
  300.   #--------------------------------------------------------------------------
  301.   def can_learn_skill?(actor, skill)
  302.     return false if skill.nil?
  303.     return false if actor.skill_learn?(skill)
  304.     return false if skill.buy_skill_level_requirement > actor.level
  305.     return true if skill.buy_for_actor_skill.include?(actor.id)
  306.     return true if skill.buy_for_class_skill.include?(actor.class.id)
  307.     return false
  308.   end
  309. end
  310.  
  311. #==============================================================================
  312. # ** Scene_SkillShop
  313. #==============================================================================
  314.  
  315. class Scene_SkillShop < Scene_Base
  316.   #--------------------------------------------------------------------------
  317.   # * Object Initialization
  318.   #--------------------------------------------------------------------------
  319.   def initialize(skill_list = [])
  320.     id_list = []
  321.     for skill_id in skill_list
  322.       case skill_id
  323.       when Integer
  324.         id_list.push(skill_id)
  325.       when Range
  326.         id_list |= skill_id.to_a
  327.       end
  328.     end
  329.     @skill_list = []
  330.     for skill_id in id_list
  331.       @skill_list.push($data_skills[skill_id])
  332.     end
  333.   end
  334.   #--------------------------------------------------------------------------
  335.   # * Start processing
  336.   #--------------------------------------------------------------------------
  337.   def start
  338.     super
  339.     create_menu_background
  340.     @help_window = Window_Help.new
  341.     @gold_window = Window_Gold.new(Graphics.width - 160, @help_window.height)
  342.     create_command_window
  343.     @buy_window = Window_SkillBuy.new(@gold_window, @skill_list)
  344.     @buy_window.y = @gold_window.y + @gold_window.height
  345.     @buy_window.help_window = @help_window
  346.     @buy_window.active = false
  347.     @status_window = Window_SkillShopStatus.new(@buy_window)
  348.   end
  349.   #--------------------------------------------------------------------------
  350.   # * Termination Processing
  351.   #--------------------------------------------------------------------------
  352.   def terminate
  353.     super
  354.     dispose_menu_background
  355.     @command_window.dispose
  356.     @help_window.dispose
  357.     @gold_window.dispose
  358.     @buy_window.dispose
  359.     @status_window.dispose
  360.   end
  361.   #--------------------------------------------------------------------------
  362.   # * Create Command Window
  363.   #--------------------------------------------------------------------------
  364.   def create_command_window
  365.     s1 = Vocab::ShopBuy
  366.     s2 = Vocab::ShopCancel
  367.     @command_window = Window_Command.new(Graphics.width - 160, [s1, s2], 2)
  368.     @command_window.y = @gold_window.y
  369.   end
  370.   #--------------------------------------------------------------------------
  371.   # * Frame Update
  372.   #--------------------------------------------------------------------------
  373.   def update
  374.     super
  375.     update_menu_background
  376.     @help_window.update
  377.     @gold_window.update
  378.     if @command_window.active
  379.       update_command_selection
  380.     elsif @buy_window.active
  381.       update_buy_selection
  382.     elsif @status_window.active
  383.       update_status_selection
  384.     end
  385.   end
  386.   #--------------------------------------------------------------------------
  387.   # * Update Command Selection
  388.   #--------------------------------------------------------------------------
  389.   def update_command_selection
  390.     @command_window.update
  391.     if Input.trigger?(Input::B)
  392.       Sound.play_cancel
  393.       $scene = Scene_Map.new
  394.     elsif Input.trigger?(Input::C)
  395.       case @command_window.index
  396.       when 0  # buy
  397.         Sound.play_decision
  398.         @command_window.active = false
  399.         @buy_window.active = true
  400.       when 1  # quit
  401.         Sound.play_decision
  402.         $scene = Scene_Map.new
  403.       end
  404.     end
  405.   end
  406.   #--------------------------------------------------------------------------
  407.   # * Update Buy Item Selection
  408.   #--------------------------------------------------------------------------
  409.   def update_buy_selection
  410.     @buy_window.update
  411.     if @last_buy_index != @buy_window.index
  412.       @last_buy_index = @buy_window.index
  413.       @status_window.refresh
  414.     end
  415.     if Input.trigger?(Input::B)
  416.       Sound.play_cancel
  417.       @command_window.active = true
  418.       @buy_window.active = false
  419.       @last_buy_index = -1
  420.     elsif Input.trigger?(Input::C)
  421.       if @buy_window.can_buy_skill?(@buy_window.skill)
  422.         Sound.play_decision
  423.         @status_window.index = 0
  424.         @status_window.active = true
  425.         @buy_window.active = false
  426.       else
  427.         Sound.play_buzzer
  428.       end
  429.     end
  430.   end
  431.   #--------------------------------------------------------------------------
  432.   # * Update Status Selection
  433.   #--------------------------------------------------------------------------
  434.   def update_status_selection
  435.     @status_window.update
  436.     if Input.trigger?(Input::B)
  437.       Sound.play_cancel
  438.       @status_window.index = -1
  439.       @status_window.active = false
  440.       @buy_window.active = true
  441.     elsif Input.trigger?(Input::C)
  442.       if @status_window.can_learn_skill?(@status_window.actor, @buy_window.skill)
  443.         Sound.play_shop
  444.         @status_window.actor.learn_skill(@buy_window.skill.id)
  445.         $game_party.lose_gold(@buy_window.skill.buy_skill_cost)
  446.         @gold_window.refresh
  447.         @buy_window.refresh
  448.         @status_window.refresh
  449.       else
  450.         Sound.play_buzzer
  451.       end
  452.     end
  453.   end
  454. end
  455.  
  456. #===============================================================================
  457. #
  458. # END OF SCRIPT
  459. #
  460. #===============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement