Advertisement
HalestormXV

Cooking Script

Jul 6th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 22.17 KB | None | 0 0
  1. #===============================================================================
  2. # Cooking Script v1.0 by GubiD 5/16/2011
  3. #===============================================================================
  4. module Cooking
  5.   #-----------------------------------------------------------------------------
  6.   # Cooking Time Max - Max time that the cooking bar uses to determine high value
  7.   #-----------------------------------------------------------------------------
  8.   Cooking_Time_Max = 1000
  9.   #-----------------------------------------------------------------------------
  10.   # Cooking Buffer - The time in which prior to and after the 'claim time' that you
  11.   # can still claim your item without having it burnt
  12.   #-----------------------------------------------------------------------------
  13.   Cooking_Buffer = 30
  14.   #-----------------------------------------------------------------------------
  15.   # Result Message - message displayed when you successfully completed a recipe
  16.   #-----------------------------------------------------------------------------
  17.   Result_Msg = "You have cooked a:"
  18.   #-----------------------------------------------------------------------------
  19.   # Failed Message - message displayed when you failure to complete a recipe
  20.   #-----------------------------------------------------------------------------
  21.   Failed_Msg = "You have failed to complete these recipe"
  22.   #-----------------------------------------------------------------------------
  23.   # Level Gained Message - Message to be displayed when you gain a cooking lv
  24.   #-----------------------------------------------------------------------------
  25.   Lv_Gained = "You have gained a cooking lv"
  26.  
  27.   #-----------------------------------------------------------------------------
  28.   # Recipes
  29.   #-----------------------------------------------------------------------------
  30.   # ID => [ITEM_ID, BURNT_ID, REQ_LV, EXP]
  31.   #-----------------------------------------------------------------------------
  32.   Recipes = {
  33.   1 => [1,21,1,100],
  34.   2 => [2,1,2,100]
  35.   }
  36.  
  37.   #-----------------------------------------------------------------------------
  38.   # Inregredients
  39.   #   * Recipe_ID - Must be valid Recipe ID from "Recipes"
  40.   # Returns [ [INGREDIENT_ID(item), Number Req], [INGREDIENT_ID(item2), Number Req] etc]
  41.   #-----------------------------------------------------------------------------
  42.   def self.ingredients(id)
  43.     case id
  44.     when 1
  45.       return [[5,1], [6,1],[7,1],[8,1]]
  46.     when 2
  47.       return [[2,1], [3,1],[5,1],[2,1]]
  48.     end
  49.   end
  50.   #-----------------------------------------------------------------------------
  51.   # Cooking Time - Time Required to cook item, in frames
  52.   #-----------------------------------------------------------------------------
  53.   def self.cooking_time(id)
  54.     case id
  55.     when 1; return 200
  56.     when 2; return 300
  57.     end
  58.   end
  59. end
  60. #--------------------------------------------------------------------
  61. # Game Party - New Methods
  62. #--------------------------------------------------------------------
  63. class Game_Party < Game_Unit
  64.   attr_reader :cooking_lv
  65.   attr_reader :learned_recipes
  66.   #--------------------------------------------------------------------
  67.   # Initialize
  68.   #--------------------------------------------------------------------
  69.   alias init_gm_prty_cooking_script initialize unless $@
  70.   def initialize(*args)
  71.     init_gm_prty_cooking_script(*args)
  72.     @cooking_lv = 1
  73.     @cooking_exp = 0
  74.     @cooking_exp_list = [0,500,1000,2000]
  75.     @learned_recipes = []
  76.   end
  77.   #--------------------------------------------------------------------
  78.   # Gain Cooking EXP
  79.   #   *EXP Value
  80.   #--------------------------------------------------------------------
  81.   def gain_cooking_exp(value)
  82.     @cooking_exp += value
  83.     @cooking_exp = [@cooking_exp,9999].min
  84.     if @cooking_exp_list[@cooking_lv] < @cooking_exp
  85.       gain_cooking_lv
  86.     end
  87.   end
  88.   #--------------------------------------------------------------------
  89.   # Gain Cooking LV
  90.   #--------------------------------------------------------------------
  91.   def gain_cooking_lv
  92.     @cooking_lv += 1
  93.   end
  94.   #--------------------------------------------------------------------
  95.   # Learn Recipe
  96.   #  *RecipeID
  97.   #--------------------------------------------------------------------
  98.   def learn_recipe(number)
  99.     @learned_recipes << number if !@learned_recipes.include?(number)
  100.     @learned_recipes.sort!
  101.   end
  102.   #--------------------------------------------------------------------
  103.   # Forget Recipe
  104.   #   *RecipeID
  105.   #--------------------------------------------------------------------
  106.   def forget_recipe(number)
  107.     @learned_recipes.delete(number)
  108.   end
  109. end
  110. #--------------------------------------------------------------------
  111. # Cooking Title - Top window of cooking scene - Displays the Party Cooking Lv
  112. #--------------------------------------------------------------------
  113. class Cooking_Title < Window_Base
  114.   def initialize
  115.     super(0,0,544,64)
  116.     refresh
  117.   end
  118.   #-----------------------------------------------------------------------------
  119.   # Refresh - Redraws window contents with current data.
  120.   #-----------------------------------------------------------------------------
  121.   def refresh
  122.     create_contents
  123.     fsize = self.contents.font.size
  124.     self.contents.font.size = 24
  125.     self.contents.draw_text(0,0,544-32,WLH+8, "Cooking")
  126.     self.contents.font.size = fsize
  127.     self.contents.draw_text(0,0,544-32,WLH+8, "Level: #{$game_party.cooking_lv}",2)
  128.   end
  129. end
  130. #--------------------------------------------------------------------
  131. # Cooking Available List
  132. #--------------------------------------------------------------------
  133. class Cooking_Availabe_List < Window_Selectable
  134.   def initialize
  135.     super(0,64,544/3, 416-64)
  136.     refresh
  137.   end
  138.   #-----------------------------------------------------------------------------
  139.   # Refresh - Redraws Available List of Recipes with current known and available
  140.   #      recipes
  141.   #-----------------------------------------------------------------------------
  142.   def refresh
  143.     recipes = $game_party.learned_recipes
  144.     @item_max = recipes.size
  145.     create_contents
  146.     for i in 0...@item_max
  147.       recipe_data = Cooking::Recipes[recipes[i]]
  148.       cur_lv = $game_party.cooking_lv
  149.       if recipe_data[2] > cur_lv
  150.         draw_recipe_disabled(i)
  151.       else
  152.         draw_recipe_enabled(i)
  153.       end
  154.     end
  155.   end  
  156.   #-----------------------------------------------------------------------------
  157.   # Draw Recipe Disabled - Draws index recipe as disabled color
  158.   #   *Index of party recipe ID
  159.   #-----------------------------------------------------------------------------
  160.   def draw_recipe_disabled(index)
  161.     fcolor = self.contents.font.color
  162.     self.contents.font.color = Color.new(50,50,50,255)
  163.     rect = item_rect(index)
  164.     r_id = $game_party.learned_recipes[index]
  165.     recipe_data = Cooking::Recipes[r_id]
  166.     item_id = recipe_data[0]
  167.     item = $data_items[item_id]
  168.     self.contents.draw_text(rect, item.name)
  169.     self.contents.font.color = fcolor
  170.   end
  171.   #-----------------------------------------------------------------------------
  172.   # Draw Recipe Enabled - Draws index recipe as enabled color
  173.   #    *Index of party recipe ID
  174.   #-----------------------------------------------------------------------------
  175.   def draw_recipe_enabled(index)
  176.     self.contents.font.color = normal_color
  177.     rect = item_rect(index)
  178.     r_id = $game_party.learned_recipes[index]
  179.     recipe_data = Cooking::Recipes[r_id]
  180.     item_id = recipe_data[0]
  181.     item = $data_items[item_id]
  182.     self.contents.draw_text(rect, item.name)
  183.   end
  184. end
  185.  
  186.  
  187. #-------------------------------------------------------------------------------
  188. # Cooking Details - Used to display the details of the currently selected recipe
  189. #-------------------------------------------------------------------------------
  190. class Cooking_Details < Window_Selectable
  191.   attr_reader :can_cook
  192.   attr_reader :can_claim
  193.   attr_reader :recipe_id
  194.   attr_reader :started
  195.   attr_reader :is_burnt
  196.   def initialize
  197.     super(544/3, 64, 544-544/3, 416-64)
  198.     refresh
  199.     @can_cook = false
  200.     @started = false
  201.     @max = 100
  202.     @elapsed = 0
  203.     @claim_time = 0
  204.     @can_claim = false
  205.     @is_burnt = false
  206.   end
  207.   #-----------------------------------------------------------------------------
  208.   # Refresh - Redraws window contents with the current data
  209.   #-----------------------------------------------------------------------------
  210.   def refresh(recipe_id = nil)
  211.     @recipe_id = recipe_id
  212.     @can_cook = false
  213.     create_contents
  214.     return if recipe_id == nil
  215.     @recipe_data = Cooking::Recipes[@recipe_id]
  216.     @item = $data_items[@recipe_data[0]]
  217.     fsize = self.contents.font.size
  218.     self.contents.font.size = 24
  219.     self.contents.font.bold = true
  220.     self.contents.draw_text(0,0,width-32,WLH+8,@item.name,1)
  221.     self.contents.font.bold = false
  222.    
  223.     draw_icon(@item.icon_index, ((width-32)/2)-12, 40)
  224.     draw_item_description
  225.     draw_ingredients
  226.     draw_options
  227.   end
  228.   #-----------------------------------------------------------------------------
  229.   # Draw Item(to be earned by recipe) Description
  230.   #-----------------------------------------------------------------------------
  231.   def draw_item_description
  232.     self.contents.draw_text(0,64,width-32,WLH, @item.description,1)
  233.   end
  234.   #-----------------------------------------------------------------------------
  235.   # Draw Ingredients - Method to draw all ingredients required for the current
  236.   #    recipe and display how many items you have in your possession as to how
  237.   #    many are required for the recipe
  238.   #-----------------------------------------------------------------------------
  239.   def draw_ingredients
  240.     ingredient_list = Cooking.ingredients(@recipe_id)
  241.     self.contents.draw_text(0,136, width-32, WLH, 'Ingredients:', 0)
  242.     required_ingredients = ingredient_list.size
  243.     current_meet = 0
  244.     for i in 0...ingredient_list.size
  245.       y = 160 + WLH*i
  246.       ingredient = ingredient_list[i]
  247.       item = $data_items[ingredient[0]]
  248.       in_possession = $game_party.item_number(item)      
  249.       if in_possession < ingredient[1]
  250.         self.contents.font.color = Color.new(50,50,50,255)
  251.       else
  252.         current_meet += 1
  253.       end
  254.       self.contents.draw_text(40,y, width-32-40, WLH, "#{item.name} #{in_possession}/#{ingredient[1]}",0)
  255.       normal_color
  256.     end
  257.     if current_meet == required_ingredients
  258.       @can_cook = true
  259.     end
  260.   end
  261.   #-----------------------------------------------------------------------------
  262.   # Start Cooking - Setup Flags to trigger 'Cooking'
  263.   #-----------------------------------------------------------------------------
  264.   def start_cooking
  265.     @started = true
  266.     @max = Cooking::Cooking_Time_Max
  267.     @elapsed = 0
  268.     @claim_time = Cooking.cooking_time(@recipe_id)
  269.   end
  270.   #-----------------------------------------------------------------------------
  271.   # Update - Added upon to show a progress bar while 'cooking' as well as expose
  272.   #    how far done and how well cooked the item is.
  273.   #-----------------------------------------------------------------------------
  274.   def update
  275.     super unless @started
  276.     if @started == true  
  277.       w = width-32
  278.       gw = w * @elapsed / @max
  279.       buffer = Cooking::Cooking_Buffer
  280.       if @claim_time + buffer > @elapsed and @elapsed > @claim_time - buffer
  281.         @can_claim = true
  282.         gc1 = Color.new(0,255,50,255)
  283.         gc2 = Color.new(0,255,50,255)
  284.       else
  285.         if @claim_time + buffer < @elapsed
  286.           @is_burnt = true
  287.         end
  288.         @can_claim = false
  289.         gc1 = hp_gauge_color1
  290.         gc2 = hp_gauge_color2
  291.       end
  292.       x = 0
  293.       y = height-32-24-24
  294.       self.contents.fill_rect(x, y + WLH - 8, w, 6, gauge_back_color)
  295.       self.contents.gradient_fill_rect(x, y + WLH - 8, gw, 6, gc1, gc2)
  296.      
  297.       @elapsed += 1
  298.     end
  299.   end
  300.   #-----------------------------------------------------------------------------
  301.   # Stop Cooking - Clears cooking info
  302.   #-----------------------------------------------------------------------------
  303.   def stop_cooking
  304.     @started = false
  305.     @is_burnt = false
  306.     x = 0
  307.     y = height-32-24-12
  308.     w = width-32
  309.     h = 10
  310.     rect = Rect.new(x,y,w,h)
  311.     self.contents.fill_rect(rect, Color.new(0,0,0,0))
  312.   end
  313.   #-----------------------------------------------------------------------------
  314.   # Draw Options - Draws the Cook and Dont Cook options at the bottom of the window
  315.   #-----------------------------------------------------------------------------
  316.   def draw_options
  317.     w = (width-32)/2
  318.     if @recipe_data[2] > $game_party.cooking_lv
  319.       self.contents.font.color = Color.new(50,50,50,255)
  320.     else
  321.       normal_color
  322.     end
  323.     self.contents.draw_text(0,height-32-WLH, w, WLH, "Cook", 1)
  324.     normal_color
  325.     self.contents.draw_text(w,height-32-WLH, w, WLH, "Don't Cook", 1)
  326.    
  327.     @item_max = 2
  328.     @column_max = 2
  329.   end
  330.   #-----------------------------------------------------------------------------
  331.   # Item Rect - Updated for displacement of the rect.y position
  332.   #-----------------------------------------------------------------------------
  333.   alias item_rect_cook_detail item_rect unless $@
  334.   def item_rect(index)
  335.     rect = item_rect_cook_detail(index)
  336.     rect.y = height-32-WLH
  337.     return rect
  338.   end
  339. end
  340. #-------------------------------------------------------------------------------
  341. # Cooking Results - Window to display in center of screen until user input.. self disposing
  342. #-------------------------------------------------------------------------------
  343. class Cooking_Results < Window_Base
  344.   def initialize(message)
  345.     super(0,416/2-28, 544, 24+32)
  346.     set_message(message)
  347.   end
  348.   #-----------------------------------------------------------------------------
  349.   # Set Message - Draws message then waits for user input before continuing
  350.   #-----------------------------------------------------------------------------
  351.   def set_message(message)
  352.     create_contents
  353.     self.contents.draw_text(0,0,width-32, height-32, message,1)
  354.     loop do
  355.       Graphics.update
  356.       Input.update
  357.       self.update
  358.       break if Input.trigger?(Input::C)
  359.     end
  360.     dispose # dispose self
  361.   end
  362. end
  363.  
  364. #-------------------------------------------------------------------------------
  365. # Scene Cooking - Scene in which all cooking is performed.
  366. #-----------------------------------------------------------------------------
  367. class Scene_Cooking < Scene_Base
  368.   def initialize(force_burn = false)
  369.     @tutorial_burn = force_burn
  370.   end
  371.   def start
  372.     create_windows
  373.   end
  374.   #-----------------------------------------------------------------------------
  375.   # Create Windows
  376.   #-----------------------------------------------------------------------------
  377.   def create_windows
  378.     @title = Cooking_Title.new
  379.     @list = Cooking_Availabe_List.new
  380.     @details = Cooking_Details.new
  381.     @list.active = true
  382.     @list.index = 0
  383.     @last_index = -1
  384.     @details.active = false
  385.     pre_animate_in
  386.     if @tutorial_burn == true
  387.       @list.active = false
  388.       @list.index = 0
  389.       refresh_details
  390.       @details.active = true
  391.       @details.index = 0
  392.     end
  393.    
  394.   end
  395.   #-----------------------------------------------------------------------------
  396.   # Pre Animate In - Prepares all windows for the animation process
  397.   #-----------------------------------------------------------------------------
  398.   def pre_animate_in
  399.     @title.y -= @title.height
  400.     @list.x -= @list.width
  401.     @details.x += @details.width
  402.   end
  403.   def post_start
  404.     animate_in
  405.   end
  406.   #-----------------------------------------------------------------------------
  407.   # Animate In - Method that brings the windows into the scene
  408.   #-----------------------------------------------------------------------------
  409.   def animate_in
  410.     timer = 10
  411.     while timer > 0
  412.       Graphics.update
  413.       @title.y += @title.height/10
  414.       @list.x += @list.width/10
  415.       @details.x -= @details.width/10
  416.       timer -= 1
  417.     end
  418.   end
  419.   #-----------------------------------------------------------------------------
  420.   # Update method
  421.   #-----------------------------------------------------------------------------
  422.   def update
  423.     update_windows
  424.     if @list.active == true
  425.       update_list
  426.       return
  427.     end
  428.     if @details.active == true
  429.       update_details
  430.       return
  431.     end
  432.   end
  433.   #-----------------------------------------------------------------------------
  434.   # Update Windows - method to update and refresh window contents when required
  435.   #-----------------------------------------------------------------------------
  436.   def update_windows
  437.     if @last_index != @list.index
  438.       @last_index = @list.index
  439.       refresh_details
  440.     end
  441.     @title.update
  442.     @list.update
  443.     @details.update
  444.   end
  445.   #-----------------------------------------------------------------------------
  446.   # Refresh Details - used to refresh the contents of the Details pane
  447.   #-----------------------------------------------------------------------------
  448.   def refresh_details
  449.     recipe_id = $game_party.learned_recipes[@list.index]
  450.     @details.refresh(recipe_id)
  451.   end
  452.   #-----------------------------------------------------------------------------
  453.   # Update Details - method to monitor/manage user input when the detail pane is active
  454.   #-----------------------------------------------------------------------------
  455.   def update_details
  456.     if Input.trigger?(Input::C) or @tutorial_burn
  457.       if @details.index == 0  #When Cook
  458.         if @details.can_cook == false # if cannot book, playe buzzer and return
  459.           Sound.play_buzzer
  460.           return
  461.         end
  462.         #if can.. continue
  463.         Sound.play_decision
  464.         # get ingredient list
  465.         ingredient_list = Cooking.ingredients(@details.recipe_id)
  466.         for i in 0...ingredient_list.size
  467.           ingredient = ingredient_list[i]
  468.           item = $data_items[ingredient[0]]
  469.           number = ingredient[1]
  470.           $game_party.lose_item(item, number) #take items from party
  471.         end
  472.         #set cooking flag
  473.         @details.start_cooking
  474.         # wait for cooking
  475.         wait_for_cooking
  476.         # disable pane, and return control to 'list' window.
  477.         @list.active = true
  478.         @details.active = false
  479.         @details.index = -1
  480.       else # When Dont Cook
  481.         Sound.play_cancel
  482.         @details.active = false
  483.         @list.active = true
  484.         @details.index = -1
  485.       end # /details.index == 0
  486.       #Refresh Window contents
  487.       @title.refresh
  488.       @list.refresh
  489.       @details.refresh(@details.recipe_id)
  490.       if @tutorial_burn == true
  491.         $scene = Scene_Map.new
  492.       end
  493.     end
  494.     #-----------------------------------------------------------------------------
  495.     # If cancel button is selected
  496.     #-----------------------------------------------------------------------------
  497.     if Input.trigger?(Input::B)
  498.       Sound.play_cancel
  499.       @details.active = false
  500.       @details.index = -1
  501.       @list.active = true
  502.     end
  503.   end
  504.   #-----------------------------------------------------------------------------
  505.   # Wait for Cooking - method in which waits for the Detail window while it is
  506.   #   cooking and dictates what should happen when a item is successfully or un-
  507.   #   successfully cooked within the scene.
  508.   #-----------------------------------------------------------------------------
  509.   def wait_for_cooking
  510.     while @details.started
  511.       Graphics.update
  512.       Input.update
  513.       update_windows
  514.       if ((@tutorial_burn == true and @details.is_burnt == true) or @tutorial_burn == false)
  515.         if Input.trigger?(Input::C)
  516.           # get recipe data
  517.           data = Cooking::Recipes[@details.recipe_id]
  518.           # If recipe is not without 'correct cooking timeframe'
  519.           if @details.can_claim == false
  520.             Sound.play_enemy_damage # Played desired sound effect
  521.             if @details.is_burnt  #if item is BURNT but not garbage.. then earn that item.
  522.               item = $data_items[data[1]]
  523.               $game_party.gain_item(item, 1)
  524.               Cooking_Results.new(Cooking::Result_Msg + " #{item.name}") #display message
  525.             else
  526.               Cooking_Results.new(Cooking::Failed_Msg) #if garbage.. display and return
  527.             end
  528.           else #can claim correctly
  529.             Sound.play_recovery  #play desired sound
  530.             item = $data_items[data[0]]
  531.             $game_party.gain_item(item, 1) #gain item
  532.             Cooking_Results.new(Cooking::Result_Msg + " #{item.name}") #display success message
  533.           end
  534.           # check current level
  535.           cur_lv = $game_party.cooking_lv
  536.           # gain cooking exp
  537.           $game_party.gain_cooking_exp(data[3])
  538.           # confirm level has not changed.. if it has.. show message
  539.           if cur_lv != $game_party.cooking_lv
  540.           # You could play a sound here as well if you want to.
  541.           Cooking_Results.new(Cooking::Lv_Gained)
  542.         end
  543.         # Stop Cooking
  544.           @details.stop_cooking
  545.         end
  546.       end
  547.     end
  548.   end
  549.   #-----------------------------------------------------------------------------
  550.   # Update List - Method in which to update the LIST window when active.
  551.   #-----------------------------------------------------------------------------
  552.   def update_list
  553.     if Input.trigger?(Input::C)
  554.       recipe_id = $game_party.learned_recipes[@list.index]
  555.       data = Cooking::Recipes[recipe_id]
  556.       #if cooking level required for specified item is too low.. return.
  557.       if data[2] > $game_party.cooking_lv
  558.         Sound.play_buzzer
  559.         return
  560.       end
  561.       Sound.play_decision
  562.       @list.active = false
  563.       @details.active = true
  564.       @details.index = 0
  565.     end  
  566.     if Input.trigger?(Input::B)
  567.       Sound.play_cancel
  568.       $scene = Scene_Map.new
  569.     end
  570.   end
  571.   def pre_terminate
  572.     animate_out
  573.   end
  574.   #-----------------------------------------------------------------------------
  575.   # Animate Out - Method to move windows off the scene in prep for disposal
  576.   #-----------------------------------------------------------------------------
  577.   def animate_out
  578.     timer = 10
  579.     while timer > 0
  580.       Graphics.update
  581.       @title.y -= @title.height/10
  582.       @list.x -= @list.width/10
  583.       @details.x += @details.width/10
  584.       timer -= 1
  585.     end
  586.   end
  587.   def terminate
  588.     @title.dispose
  589.     @list.dispose
  590.     @details.dispose
  591.   end
  592. end
  593.  
  594. #-----------------------------------------------------------------------------
  595. # Game Interpreter - Additional methods in order to make learning/forgetting
  596. #   recipes easier
  597. #-----------------------------------------------------------------------------
  598. class Game_Interpreter
  599.   def learn_recipe(number)
  600.     $game_party.learn_recipe(number)
  601.   end
  602.   def forget_recipe(number)
  603.     $game_party.forget_recipe(number)
  604.   end
  605. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement