Advertisement
PencilCase27

CardShop Script v1.01

Oct 5th, 2014
2,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.92 KB | None | 0 0
  1. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  2. #                               CARDGAME CARDSHOP
  3. #                               Author: pencilcase27
  4. #                               Version:       v1.01
  5. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  6. # v1.00 - Script released
  7. # v1.01 - Changed the way inventory is handled
  8. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  9. #
  10. #                                  Description
  11. #
  12. # This script is an addon to my CardGame Skill/Battle System. It allows to set
  13. # up card shops that have a limited amount of cards for sale.
  14. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  15. #
  16. #                                  Script Calls
  17. #
  18. # open_card_shop(shop_ID, purchase_only)
  19. # Opens the given shop. purchase_only can be either true or false.
  20. #
  21. # reset_shop(id)
  22. # Resets the given shops inventory.
  23. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  24. #
  25. #                                   Note Tags
  26. #
  27. # <card price: x>
  28. # Add this tag to skills to set up their buing price.
  29. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  30. module PC27
  31.   module CardShop
  32. #-------------------------------------------------------------------------------
  33. #
  34. #                                   Options
  35. #
  36. #-------------------------------------------------------------------------------
  37.    
  38.     # For how much of the original value do you want to sell cards?
  39.     SELL_PRICE       = 0.5
  40.    
  41.     # When you sold a card, do you want to add it to the shops inventory?
  42.     STORE_SOLD_CARDS = true
  43.    
  44.     # Each shop can hava a short text displayed.
  45.     SHOP_TEXT        = {
  46.     1 => "RED Shop",
  47.     2 => "BLUE Shop",
  48. }    
  49.     # Options for this text.
  50.     SHOP_TEXT_SIZE   = 48
  51.     SHOP_TEXT_BOLD   = false
  52.     SHOP_TEXT_ITALIC = true
  53.  
  54.     # Initial inventory of each shop. -1 means that the card can be bought as
  55.     # many times as you want.
  56.     SHOPS_CONTENT    = {
  57.     1 => { 3 =>  4,
  58.            4 =>  6,
  59.           10 =>  2,
  60.            5 => -1,
  61.            9 => -1, },
  62.    
  63.     2 => { 6 =>  6,
  64.            7 =>  7,
  65.            8 =>  8,
  66.            9 =>  9,
  67.           10 => 10, },
  68.    
  69. }
  70. #-------------------------------------------------------------------------------
  71. #
  72. #                                End Options
  73. #
  74. #-------------------------------------------------------------------------------
  75.   end #CG
  76. end #PC27
  77.  
  78. ################################################################################
  79. #                          Start of script                                     #
  80. ################################################################################
  81.  
  82. $imported = {} if $imported.nil?
  83. $imported["PC27: CardShop"] = true
  84.  
  85. class RPG::UsableItem < RPG::BaseItem
  86.  
  87.   attr_accessor :card_price
  88.  
  89.   #-----------------------------------------------------------------------------
  90.   # Add card attributes
  91.   #-----------------------------------------------------------------------------
  92.   alias load_notetags_pc27_cardgame_skill_shop load_notetags_pc27_cardgame_skill
  93.   def load_notetags_pc27_cardgame_skill
  94.     load_notetags_pc27_cardgame_skill_shop
  95.     @card_price = 0
  96.     self.note.split(/[\r\n]+/).each { |line|
  97.       if /<card[ -_]price: +(?<pnr>\d+)>/i =~ line
  98.         @card_price = pnr.to_i
  99.       end
  100.     }
  101.   end
  102. end
  103.  
  104.  
  105. module DataManager  
  106.   class << self
  107.     alias pc27_cardshop_data_manager_create_game_objects     create_game_objects
  108.     alias pc27_cardshop_data_manager_make_save_contents       make_save_contents
  109.     alias pc27_cardshop_data_manager_extract_save_contents extract_save_contents
  110.   end
  111.  
  112.   def self.create_game_objects
  113.     pc27_cardshop_data_manager_create_game_objects
  114.     dummy = Marshal.load(Marshal.dump(PC27::CardShop::SHOPS_CONTENT))
  115.     $game_card_shops = dummy
  116.   end
  117.  
  118.   def self.make_save_contents
  119.     contents = pc27_cardshop_data_manager_make_save_contents
  120.     contents[:card_shops] = $game_card_shops
  121.     contents
  122.   end
  123.  
  124.   def self.extract_save_contents(contents)
  125.     pc27_cardshop_data_manager_extract_save_contents(contents)
  126.     if contents[:card_shops]
  127.       $game_card_shops = contents[:card_shops]
  128.     else
  129.       dummy = Marshal.load(Marshal.dump(PC27::CardShop::SHOPS_CONTENT))
  130.       $game_card_shops = dummy
  131.     end
  132.   end
  133. end
  134.  
  135. class Game_Interpreter
  136.   #--------------------------------------------------------------------------
  137.   # * CardShop Processing
  138.   #--------------------------------------------------------------------------
  139.   def open_card_shop(shop_ID, purchase_only)
  140.     return if $game_party.in_battle
  141.     SceneManager.call(Scene_CardShop)
  142.     SceneManager.scene.prepare(shop_ID, purchase_only)
  143.     Fiber.yield
  144.   end
  145.  
  146.   def reset_shop(id)
  147.     $game_card_shops[id] = {}
  148.     PC27::CardShop::SHOPS_CONTENT[id].each_key { |key|
  149.       $game_card_shops[id][key] = PC27::CardShop::SHOPS_CONTENT[id][key]
  150.     }
  151.   end
  152. end
  153.  
  154. class Scene_CardShop < Scene_MenuBase
  155.   include PC27::CardShop
  156.   #--------------------------------------------------------------------------
  157.   # * Prepare
  158.   #--------------------------------------------------------------------------
  159.   def prepare(id, purchase_only)
  160.     @id = id
  161.     @purchase_only = purchase_only
  162.   end
  163.   #--------------------------------------------------------------------------
  164.   # * Start Processing
  165.   #--------------------------------------------------------------------------
  166.   def start
  167.     super
  168.     create_help_window
  169.     create_gold_window
  170.     create_command_window
  171.     create_buy_window
  172.     create_sell_window
  173.   end
  174.  
  175.   def create_background
  176.     super
  177.     return unless SHOP_TEXT[@id]
  178.     text = SHOP_TEXT[@id]
  179.     width = Graphics.width - 210
  180.     height = Graphics.height * 0.5
  181.     @background_sprite.bitmap.font.size = SHOP_TEXT_SIZE
  182.     @background_sprite.bitmap.font.italic = SHOP_TEXT_ITALIC
  183.     @background_sprite.bitmap.font.bold = SHOP_TEXT_BOLD
  184.     @background_sprite.bitmap.draw_text(0, 0, width, height, text, 1)
  185.   end
  186.   #--------------------------------------------------------------------------
  187.   # * Create CardHelp Window
  188.   #--------------------------------------------------------------------------
  189.   def create_help_window
  190.     @help_window = Window_CardHelp.new
  191.     @help_window.viewport = @viewport
  192.   end
  193.   #--------------------------------------------------------------------------
  194.   # * Create Gold Window
  195.   #--------------------------------------------------------------------------
  196.   def create_gold_window
  197.     @gold_window = Window_Gold.new
  198.     @gold_window.viewport = @viewport
  199.     @gold_window.x = Graphics.width - @gold_window.width
  200.     @gold_window.y = Graphics.height - @gold_window.height
  201.   end
  202.   #--------------------------------------------------------------------------
  203.   # * Create Command Window
  204.   #--------------------------------------------------------------------------
  205.   def create_command_window
  206.     @command_window = Window_ShopCommand.new(@gold_window.x, @purchase_only)
  207.     @command_window.viewport = @viewport
  208.     @command_window.y = Graphics.height - @command_window.height
  209.     @command_window.set_handler(:buy,    method(:command_buy))
  210.     @command_window.set_handler(:sell,   method(:command_sell))
  211.     @command_window.set_handler(:cancel, method(:return_scene))
  212.   end
  213.   #--------------------------------------------------------------------------
  214.   # * Create Purchase Window
  215.   #--------------------------------------------------------------------------
  216.   def create_buy_window
  217.     wh = Graphics.height - @command_window.height
  218.     @buy_window = Window_CardShopBuy.new(0, 0, wh, @id)
  219.     @buy_window.viewport = @viewport
  220.     @buy_window.help_window = @help_window
  221.     @buy_window.hide
  222.     @buy_window.set_handler(:ok,     method(:on_buy_ok))
  223.     @buy_window.set_handler(:cancel, method(:on_buy_cancel))
  224.   end
  225.   #--------------------------------------------------------------------------
  226.   # * Create Sell Window
  227.   #--------------------------------------------------------------------------
  228.   def create_sell_window
  229.     h = Graphics.height - @command_window.height
  230.     @sell_window = Window_CardShopSell.new(0, 0, Graphics.width * 0.5, h)
  231.     @sell_window.viewport = @viewport
  232.     @sell_window.help_window = @help_window
  233.     @sell_window.hide
  234.     @sell_window.set_handler(:ok,     method(:on_sell_ok))
  235.     @sell_window.set_handler(:cancel, method(:on_sell_cancel))
  236.   end
  237.   #--------------------------------------------------------------------------
  238.   # * Activate Purchase Window
  239.   #--------------------------------------------------------------------------
  240.   def activate_buy_window
  241.     @help_window.draw_card_pic(@buy_window.item)
  242.     @buy_window.refresh
  243.     @buy_window.show.activate
  244.   end
  245.   #--------------------------------------------------------------------------
  246.   # * Activate Sell Window
  247.   #--------------------------------------------------------------------------
  248.   def activate_sell_window
  249.     @help_window.draw_card_pic(@sell_window.item)
  250.     @sell_window.refresh
  251.     @sell_window.show.activate
  252.   end
  253.   #--------------------------------------------------------------------------
  254.   # * [Buy] Command
  255.   #--------------------------------------------------------------------------
  256.   def command_buy
  257.     activate_buy_window
  258.   end
  259.   #--------------------------------------------------------------------------
  260.   # * [Sell] Command
  261.   #--------------------------------------------------------------------------
  262.   def command_sell
  263.     activate_sell_window
  264.   end
  265.   #--------------------------------------------------------------------------
  266.   # * Buy [OK]
  267.   #--------------------------------------------------------------------------
  268.   def on_buy_ok
  269.     @item = @buy_window.item
  270.     $game_party.lose_gold(@item.card_price)
  271.     $game_party.coll.push(@item)
  272.     @buy_window.remove_card(@item.id)
  273.     @buy_window.activate
  274.     @gold_window.refresh
  275.   end
  276.   #--------------------------------------------------------------------------
  277.   # * Buy [Cancel]
  278.   #--------------------------------------------------------------------------
  279.   def on_buy_cancel
  280.     @command_window.activate
  281.     @buy_window.hide
  282.     @help_window.draw_card_pic(nil)
  283.   end
  284.   #--------------------------------------------------------------------------
  285.   # * Sell [OK]
  286.   #--------------------------------------------------------------------------
  287.   def on_sell_ok
  288.     @item = @sell_window.item
  289.     for i in (0...$game_party.coll.length)
  290.       if $game_party.coll[i].id == @item.id
  291.         $game_party.coll.delete_at(i)
  292.         break
  293.       end
  294.     end
  295.     add_card(@item.id) if STORE_SOLD_CARDS
  296.     $game_party.gain_gold((@item.card_price * SELL_PRICE).round)
  297.     @sell_window.refresh
  298.     @sell_window.activate
  299.     @gold_window.refresh
  300.   end
  301.  
  302.   def add_card(card_id)
  303.     if $game_card_shops[@id].has_key?(card_id)
  304.       $game_card_shops[@id][card_id] += 1 unless $game_card_shops[@id][card_id] == -1
  305.     else
  306.       $game_card_shops[@id][card_id] = 1
  307.     end
  308.   end
  309.   #--------------------------------------------------------------------------
  310.   # * Sell [Cancel]
  311.   #--------------------------------------------------------------------------
  312.   def on_sell_cancel
  313.     @command_window.activate
  314.     @sell_window.hide
  315.     @help_window.draw_card_pic(nil)
  316.   end
  317. end
  318.  
  319. class Window_CardShopBuy < Window_Selectable
  320.   #--------------------------------------------------------------------------
  321.   # * Object Initialization
  322.   #--------------------------------------------------------------------------
  323.   def initialize(x, y, height, shop_ID)
  324.     super(x, y, window_width, height)
  325.     @shop_ID = shop_ID
  326.     refresh
  327.     select(0)
  328.   end
  329.   #--------------------------------------------------------------------------
  330.   # * Get Window Width
  331.   #--------------------------------------------------------------------------
  332.   def window_width
  333.     return Graphics.width - 210
  334.   end
  335.   #--------------------------------------------------------------------------
  336.   # * Get Number of Items
  337.   #--------------------------------------------------------------------------
  338.   def item_max
  339.     @data ? @data.size : 1
  340.   end
  341.   #--------------------------------------------------------------------------
  342.   # * Get Item
  343.   #--------------------------------------------------------------------------
  344.   def item
  345.     @data[index]
  346.   end
  347.   #--------------------------------------------------------------------------
  348.   # * Get Activation State of Selection Item
  349.   #--------------------------------------------------------------------------
  350.   def current_item_enabled?
  351.     enable?(@data[index])
  352.   end
  353.   #--------------------------------------------------------------------------
  354.   # * Display in Enabled State?
  355.   #--------------------------------------------------------------------------
  356.   def enable?(item)
  357.     item && item.card_price <= $game_party.gold
  358.   end
  359.   #--------------------------------------------------------------------------
  360.   # * Refresh
  361.   #--------------------------------------------------------------------------
  362.   def refresh
  363.     make_item_list
  364.     create_contents
  365.     draw_all_items
  366.   end
  367.   #--------------------------------------------------------------------------
  368.   # * Create Item List
  369.   #--------------------------------------------------------------------------
  370.   def make_item_list
  371.     @stock = {}
  372.     @data = []
  373.     $game_card_shops[@shop_ID].each_key do |key|
  374.       next if $game_card_shops[@shop_ID][key] == 0
  375.       @data.push($data_skills[key])
  376.       @stock[key] = $game_card_shops[@shop_ID][key]
  377.     end
  378.   end
  379.   #--------------------------------------------------------------------------
  380.   # * Draw Item
  381.   #--------------------------------------------------------------------------
  382.   def draw_item(index)
  383.     item = @data[index]
  384.     rect = item_rect(index)
  385.     draw_item_name(item, rect.x, rect.y, enable?(item))
  386.     rect.width -= 4
  387.     text = item.card_price.to_s
  388.     draw_text(rect, text, 2)
  389.     rect.x += rect.width * 0.7
  390.     amount = (@stock[item.id] == -1 ? "" : "x" + @stock[item.id].to_s)
  391.     draw_text(rect, amount, 0)
  392.   end
  393.   #--------------------------------------------------------------------------
  394.   # * Update Cards
  395.   #--------------------------------------------------------------------------
  396.   def remove_card(id)
  397.     $game_card_shops[@shop_ID][id] -= 1 if $game_card_shops[@shop_ID][id] > 0
  398.     refresh
  399.   end
  400.   #--------------------------------------------------------------------------
  401.   # * Update Help Text
  402.   #--------------------------------------------------------------------------
  403.   def update_help
  404.     @help_window.set_item(item) if @help_window
  405.   end
  406. end
  407.  
  408. class Window_CardShopSell < Window_Selectable
  409.   #--------------------------------------------------------------------------
  410.   # * Object Initialization
  411.   #--------------------------------------------------------------------------
  412.   def initialize(x, y, width, height)
  413.     super(x, y, window_width, height)
  414.     refresh
  415.     select(0)
  416.   end
  417.   #--------------------------------------------------------------------------
  418.   # * Get Window Width
  419.   #--------------------------------------------------------------------------
  420.   def window_width
  421.     return Graphics.width - 210
  422.   end
  423.   #--------------------------------------------------------------------------
  424.   # * Get Number of Items
  425.   #--------------------------------------------------------------------------
  426.   def item_max
  427.     @data ? @data.size : 1
  428.   end
  429.   #--------------------------------------------------------------------------
  430.   # * Get Item
  431.   #--------------------------------------------------------------------------
  432.   def item
  433.     @data[index]
  434.   end
  435.   #--------------------------------------------------------------------------
  436.   # * Get Activation State of Selection Item
  437.   #--------------------------------------------------------------------------
  438.   def current_item_enabled?
  439.     enable?(@data[index])
  440.   end
  441.   #--------------------------------------------------------------------------
  442.   # * Display in Enabled State?
  443.   #--------------------------------------------------------------------------
  444.   def enable?(item)
  445.     item && item.card_price > 0
  446.   end
  447.   #--------------------------------------------------------------------------
  448.   # * Refresh
  449.   #--------------------------------------------------------------------------
  450.   def refresh
  451.     make_item_list
  452.     create_contents
  453.     draw_all_items
  454.   end
  455.   #--------------------------------------------------------------------------
  456.   # * Create Item List
  457.   #--------------------------------------------------------------------------
  458.   def make_item_list
  459.     $game_party.coll.sort! { |a,b| a.card_id <=> b.card_id }
  460.     @stock = {}
  461.     @data = []
  462.     @price = {}
  463.     $game_party.coll.each do |card|
  464.       if @data.include?(card)
  465.         @stock[card] += 1
  466.       else
  467.         @data.push(card)
  468.         @stock[card] = 1
  469.       end
  470.     end
  471.   end
  472.   #--------------------------------------------------------------------------
  473.   # * Draw Item
  474.   #--------------------------------------------------------------------------
  475.   def draw_item(index)
  476.     item = @data[index]
  477.     rect = item_rect(index)
  478.     draw_item_name(item, rect.x, rect.y, enable?(item))
  479.     rect.width -= 4
  480.     text = (item.card_price * PC27::CardShop::SELL_PRICE).round.to_s
  481.     draw_text(rect, text, 2)
  482.     rect.x += rect.width * 0.7
  483.     text = "x" + @stock[item].to_s
  484.     draw_text(rect, text, 0)
  485.   end
  486.  
  487.   #--------------------------------------------------------------------------
  488.   # * Update Help Text
  489.   #--------------------------------------------------------------------------
  490.   def update_help
  491.     @help_window.set_item(item) if @help_window
  492.   end
  493. end
  494. ################################################################################
  495. #                            End of script                                     #
  496. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement