Advertisement
Zetu

Z07 QuestSystem v1.01

Dec 14th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.90 KB | None | 0 0
  1. #=============================
  2. # Z07 :: Quest System v1.01 by Zetu
  3. # Commands (In call script)
  4. # => QuestList::acceptquest(symbol)
  5. #      - Adds the quest to questlog.
  6. # => QuestList::questcomplete?(symbol)
  7. #      - Checks if conditions of quest have been completed.
  8. # => QuestList::onquest?(symbol)
  9. #      - Checks if quest is in questlog
  10. # => QuestList::turnin(symbol)
  11. #      - Removes quest from questlog and gives the player rewards.
  12. #=============================
  13.  
  14. module Z07 #questsystem
  15.     QUESTS = {
  16.         # ==============
  17.         :quest1 => [[:defeat, 3, 1, :reward, 3000],
  18.                     ["Quest Title01", "Quest Info"]],
  19.         :quest2 => [[:collect, 5, $data_items[3], :reward, 5000],
  20.                     ["Quest Title02", "Quest Info"]],
  21.         :quest3 => [[:event, :event01, "Event01disc", :reward, 1, $data_armors[3], :xp, 40],
  22.                     ["Quest Title03", "Quest Info"]]
  23.         # ==============
  24.     }
  25.     COLLECTQUEST = "  - %s of %s %ss obtained!"
  26.     DEFEATQUEST  = "  - %s of %s %ss defeated!"
  27.     EVENTQUEST   = "  - %s"
  28. end
  29.  
  30. module QuestList
  31.     def self.start
  32.         @questlist = []
  33.     end
  34.    
  35.     def self.acceptquest(symbol)
  36.         @questlist.push(QuestItem.new(symbol))
  37.     end
  38.    
  39.     def self.questcomplete?(symbol)
  40.         for quest in @questlist
  41.             if quest.symbol == symbol
  42.                 return quest.goalscomplete?
  43.             end
  44.         end
  45.     end
  46.    
  47.     def self.onquest?(symbol)
  48.         for quest in @questlist
  49.             return true if quest.symbol == symbol
  50.         end
  51.         return false
  52.     end
  53.    
  54.     def self.questlist
  55.         return @questlist
  56.     end
  57.    
  58.     def self.quest(symbol)
  59.         for quest in @questlist
  60.             return quest if quest.symbol == symbol
  61.         end
  62.     end
  63.    
  64.     def self.turnin(symbol)
  65.         for quest in @questlist
  66.             if quest.symbol == symbol
  67.                 quest.complete
  68.                 return
  69.             end
  70.         end
  71.     end
  72.    
  73. end
  74.  
  75. QuestList.start
  76.  
  77. class QuestItem
  78.     attr_reader :symbol, :objectives
  79.     def initialize(symbol)
  80.         @objectives = []
  81.         @rewards = []
  82.         @symbol=symbol
  83.         index = 0
  84.         array = Z07::QUESTS[symbol][0]
  85.         while (index < array.size)
  86.             case array[index]
  87.             when :defeat, :collect, :event
  88.                 @objectives.push(QuestObjective.new(array[index..index+2]))
  89.                 index += 2
  90.             when :reward
  91.                 index += 1
  92.                 @rewards.push(array[index])
  93.                 next if (index+1 < array.size)
  94.                 if (array[index+1].is_a?(RPG::Item)||array[index+1].is_a?(RPG::Armor)||array[index+1].is_a?(RPG::Weapon))
  95.                     index +=1; @rewards.push(array[index])
  96.                 end
  97.             when :xp
  98.                 @rewards.push(:xp)
  99.                 @rewards.push(array[index+1])
  100.                 index += 1
  101.             end
  102.             index += 1
  103.         end
  104.     end
  105.    
  106.     def goalscomplete?
  107.         for objective in objectives
  108.             return false unless objective.complete
  109.         end
  110.         return true
  111.     end
  112.    
  113.     def complete
  114.         index = 0
  115.         while index < @rewards.size
  116.             if @rewards[index] == :xp
  117.                 index +=1
  118.                 for member in $game_party.all_members
  119.                     member.gain_exp(@rewards[index])
  120.                 end
  121.             elsif @rewards[index].is_a?(Integer)
  122.                 index += 1
  123.                 if (index<@rewards.size)and(@rewards[index].is_a?(RPG::Item)||@rewards[index].is_a?(RPG::Armor)||@rewards[index].is_a?(RPG::Weapon))
  124.                     $game_party.gain_item(@rewards.size, @rewards[index-1])
  125.                 else
  126.                     $game_party.gain_gold(@rewards[index-1])
  127.                 end
  128.             end
  129.         end
  130.         QuestList::questlist.delete(self)
  131.     end
  132.    
  133. end
  134.  
  135. class QuestObjective
  136.     attr_reader :type, :enemy_id, :item, :eventtag, :total, :disc, :current, :complete
  137.     def initialize(array)
  138.         @type = array[0]
  139.         case @type
  140.         when :defeat
  141.             @total = array[1]
  142.             @current = 0
  143.             @enemy_id = array[2]
  144.         when :collect
  145.             @total = array[1]
  146.             @current = 0
  147.             @item = array[2]
  148.         when :event
  149.             @eventtag = array[1]
  150.             @disc = array[2]
  151.         end
  152.     end
  153.    
  154.     def setascomplete
  155.         @complete=true
  156.     end
  157.    
  158.     def current=(k)
  159.         old = @current
  160.         @current = k
  161.         popup if old < k
  162.     end
  163.    
  164.     def update
  165.         @current = [@current, @total].min if @current.is_a?(Integer)
  166.         setascomplete if @current==@total
  167.     end
  168.    
  169.     def popup
  170.         update
  171.         color_index = @complete ? 17 : 0
  172.         case @type
  173.         when :collect
  174.             SceneManager::popup(sprintf(Z07::COLLECTQUEST, @current, @total, @item.name), color_index)
  175.             print "Collet\n"
  176.         when :defeat
  177.             SceneManager::popup(sprintf(Z07::DEFEATQUEST, @current, @total, $data_enemies[@enemy_id].name), color_index)
  178.             print "Defeat\n"
  179.         when :event
  180.             SceneManager::popup(sprintf(Z07::EVENTQUEST, objective.disc), color_index)
  181.             print "Event\n"
  182.         end
  183.     end
  184.    
  185. end
  186.  
  187. class Game_Enemy < Game_Battler
  188.     alias z07pce perform_collapse_effect
  189.     def perform_collapse_effect
  190.         z07pce
  191.         for quest in QuestList.questlist
  192.             for objective in quest.objectives
  193.                 if objective.enemy_id==@enemy_id
  194.                     objective.current += 1
  195.                     objective.update
  196.                 end
  197.             end
  198.         end
  199.     end
  200.    
  201. end
  202.  
  203. class Game_Party < Game_Unit
  204.     alias z07gi gain_item
  205.     def gain_item(item, amount, include_equip = false)
  206.         z07gi(item, amount, include_equip)
  207.         unless item.nil?
  208.             for quest in QuestList.questlist
  209.                 for objective in quest.objectives
  210.                     if objective.item==item
  211.                         objective.current = $game_party.item_number(item)
  212.                         objective.update
  213.                     end
  214.                 end
  215.             end
  216.         end
  217.     end
  218.    
  219. end
  220.  
  221. class Scene_QuestLog < Scene_Base
  222.     def start
  223.         super
  224.         @help_window = Window_Help.new(1)
  225.         @help_window.viewport = @viewport
  226.         @help_window.set_text("Quest Log")
  227.         @commandwindow = Window_QuestCommand.new(0,48)
  228.         @commandwindow.set_handler(:cancel, method(:return_scene))
  229.         @info_window = Window_QuestMenuInfo.new(160, 48, 384, 368).set_symbol(@commandwindow.current_ext)
  230.     end
  231.    
  232. end
  233.  
  234. class Window_QuestCommand < Window_Command
  235.     def make_command_list
  236.         for quest in QuestList.questlist
  237.             add_command(Z07::QUESTS[quest.symbol][1][0], quest.symbol, true, quest.symbol)
  238.         end
  239.     end
  240.    
  241.     def window_height
  242.         return Graphics.height - 48
  243.     end
  244.    
  245. end
  246.  
  247. class Window_QuestMenuInfo < Window_Base
  248.     def set_symbol(symbol)
  249.         @symbol = symbol
  250.         refresh
  251.     end
  252.    
  253.     def refresh
  254.         contents.clear
  255.         return if @symbol.nil?
  256.         draw_text_ex(0, 0,  Z07::QUESTS[@symbol][1][0])
  257.         draw_text_ex(0, 24, Z07::QUESTS[@symbol][1][1])
  258.         y = 48
  259.         for objective in QuestList::quest(@symbol).objectives
  260.             change_color(normal_color)
  261.             case objective.type
  262.             when :collect
  263.                 change_color(text_color(17)) if objective.current == objective.total
  264.                 draw_text_ind(y, sprintf(Z07::COLLECTQUEST, objective.current, objective.total, objective.item.name))
  265.                 y += 24
  266.             when :defeat
  267.                 change_color(text_color(17)) if objective.current == objective.total
  268.                 draw_text_ind(y, sprintf(Z07::DEFEATQUEST, objective.current, objective.total, $data_enemies[objective.enemy_id].name))
  269.                 y += 24
  270.             when :event
  271.                 change_color(text_color(17)) if objective.current == true
  272.                 draw_text_ind(y, sprintf(Z07::EVENTQUEST, objective.disc))
  273.                 y += 24
  274.             end
  275.         end
  276.         self.show
  277.     end
  278.    
  279. end
  280.  
  281. class Window_Base < Window
  282.     def draw_text_ind(y, text)
  283.         draw_text(0, y, self.width-standard_padding * 2, line_height, text)
  284.     end
  285.    
  286. end
  287.  
  288. module SceneManager
  289.     def self.popup(string, color_id = 0)
  290.         if @scene.popup.nil?
  291.             @scene.popup = Window_Popup.new
  292.         end
  293.         @scene.popup.stack(string, 120, color_id)
  294.     end
  295.    
  296. end
  297.  
  298. class Window_Popup < Window_Base
  299.     def initialize
  300.         super(0, 0, 544, 416)
  301.         self.opacity=0
  302.         @stack = []
  303.     end
  304.    
  305.     def stack(text, frames, color_id = 0)
  306.         @stack.push(Window_PopupItem.new(text, frames, text_color(color_id)))
  307.         refresh
  308.     end
  309.    
  310.     def update
  311.         super
  312.         for window in @stack
  313.             window.update
  314.             @stack.delete(window) if window.time<=0
  315.         end
  316.         for i in 0...@stack.size
  317.             @stack[i].y = 184 + 24*i
  318.         end
  319.     end
  320.    
  321.     def refresh
  322.     end
  323.    
  324.     def dispose
  325.         super
  326.         for window in @stack
  327.             window.dispose
  328.         end
  329.     end
  330.  
  331. end
  332.  
  333. class Window_PopupItem < Window_Base
  334.     attr_reader :time
  335.     def initialize(text, maxtime, color = normal_color)
  336.         super(0, 0, 544, 48)
  337.         self.opacity = 0
  338.         @color = color
  339.         @text=text
  340.         @max=@time=maxtime
  341.         refresh
  342.     end
  343.    
  344.     def refresh
  345.         contents.clear
  346.         draw_text(0, 0, 496, 24, @text, 1)
  347.     end
  348.    
  349.     def update
  350.         @time -= 1
  351.         self.contents_opacity = 255*@time / @max
  352.     end
  353.    
  354. end
  355.  
  356. class Scene_Base
  357.     attr_accessor :popup
  358. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement