TheoAllen

Game Continue development

May 30th, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.46 KB | None | 0 0
  1. =begin
  2.  
  3.   Game Continuation script in development
  4.   To use this script, both project must have this script
  5.  
  6.   Overview :
  7.   I stored switches and variables in save header.
  8.   It because if I store it in main contents, it somehow throws an error if you
  9.   use many and complicated script in previous project.
  10.  
  11.   Also to prevent other game save file to be loaded using this script :P
  12.  
  13.  
  14.   Please do not claims as yours!
  15.  
  16. =end
  17. # ============================================================================
  18. # Configuration
  19. # ============================================================================
  20. module Theo
  21.   module GamePlus
  22.    
  23.     Transfer_Switches   = [1,2,3,4] # Switches to be transfered
  24.     Transfer_Variables  = [1,2,3,4] # Variables to be transfered
  25.    
  26.     FileName = "SavePlus" # Name for previous saved files
  27.    
  28.   end
  29. end
  30. # ============================================================================
  31. # End config
  32. # ============================================================================
  33. module Theo
  34.   module GamePlus
  35.     @gameplus = false
  36.     @index = nil
  37.    
  38.     def self.activate
  39.       @gameplus = true
  40.     end
  41.    
  42.     def self.deactivate
  43.       @gameplus = false
  44.     end
  45.    
  46.     def self.active
  47.       @gameplus
  48.     end
  49.    
  50.     def self.index=(index)
  51.       @index = index
  52.     end
  53.    
  54.     def self.index
  55.       @index
  56.     end
  57.     # ========================================================================
  58.     # Call save file from previous game and transfer the data.
  59.     # Have to be called when gameplus flag is active and index is set
  60.     # ========================================================================
  61.     def self.load
  62.       header = DataManager.load_header(index)
  63.       Transfer_Switches.each do |sid|
  64.         $game_switches[sid] = header[:switches][sid]
  65.       end
  66.       Transfer_Variables.each do |vid|
  67.         $game_variables[vid] = header[:variables][vid]
  68.       end
  69.     end
  70.    
  71.   end
  72. end
  73.  
  74. class << DataManager
  75.   # ===========================================================================
  76.   # If gameplus flag is return true, then change the filename
  77.   # ===========================================================================
  78.   alias theo_gameplus_file_exist? save_file_exists?
  79.   def save_file_exists?
  80.     return theo_gameplus_file_exist? unless Theo::GamePlus.active
  81.     !Dir.glob(Theo::GamePlus::Filename + '*.rvdata2').empty?
  82.   end
  83.  
  84.   alias theo_gameplus_make_filename make_filename
  85.   def make_filename(index)
  86.     return theo_gameplus_make_filename(index) unless Theo::GamePlus.active
  87.     sprintf("#{Theo::GamePlus::FileName}%02d.rvdata2", index + 1)
  88.   end
  89.  
  90.   alias theo_gameplus_make_header make_save_header
  91.   def make_save_header
  92.     header = theo_gameplus_make_header
  93.     header[:switches] = $game_switches
  94.     header[:variables] = $game_variables
  95.     header
  96.   end
  97.  
  98.   alias theo_gameplus_new_game setup_new_game
  99.   def setup_new_game
  100.     theo_gameplus_new_game
  101.     # Load extra data if gameplus flag is true
  102.     Theo::GamePlus.load if Theo::GamePlus.active
  103.   end
  104.  
  105. end
  106. # ============================================================================
  107. # Windows for the list of saved files from previous game.
  108. # ============================================================================
  109. class Window_GameplusList < Window_Selectable
  110.  
  111.   def initialize(y)
  112.     super(0, y, window_width, window_height)
  113.     refresh
  114.     to_center
  115.     activate
  116.     select(0)
  117.   end
  118.  
  119.   def to_center
  120.     self.x = (Graphics.width - width)/2
  121.   end
  122.  
  123.   def window_width
  124.     return 150
  125.   end
  126.  
  127.   def window_height
  128.     fitting_height(visible_line_number)
  129.   end
  130.  
  131.   def visible_line_number
  132.     return 7
  133.   end
  134.  
  135.   def make_item_list
  136.     @data = []
  137.     DataManager.savefile_max.times do |i|
  138.       next unless FileTest.exist?(DataManager.make_filename(i))
  139.       @data.push(i)
  140.     end
  141.   end
  142.  
  143.   def refresh
  144.     make_item_list
  145.     create_contents
  146.     draw_all_items
  147.   end
  148.  
  149.   def draw_item(index)
  150.     return if @data[index].nil?
  151.     text = sprintf("%02d: File", @data[index])
  152.     r = item_rect_for_text(index)
  153.     draw_text(r, text)
  154.   end
  155.  
  156.   def item_max
  157.     @data ? @data.size : 1
  158.   end
  159.  
  160. end
  161.  
  162. class Window_TitleCommand
  163.  
  164.   alias theo_gameplus_init initialize
  165.   def initialize
  166.     theo_gameplus_init
  167.     set_handler(:gameplus, method(:on_gameplus))
  168.   end
  169.  
  170.   alias theo_gameplus_make_command make_command_list
  171.   def make_command_list
  172.    
  173.     # Placeholder command. Adds New Game+ for game continuation
  174.     add_command("New Game+", :gameplus)
  175.     theo_gameplus_make_command
  176.   end
  177.  
  178.   def on_gameplus
  179.     SceneManager.scene.close_command_window
  180.     SceneManager.call(Scene_GamePlus)
  181.   end
  182.  
  183. end
  184. # ============================================================================
  185. # This is just a placeholder scene. I don't have any idea how the layout should
  186. # be like. Any suggestion?
  187. # ============================================================================
  188. class Scene_GamePlus < Scene_MenuBase
  189.  
  190.   def start
  191.     super
  192.     # Activate flag
  193.     Theo::GamePlus.activate
  194.     @window = Window_GameplusList.new(0)
  195.     @window.set_handler(:ok, method(:on_load_ok))
  196.   end
  197.  
  198.   def on_load_ok
  199.     Theo::GamePlus.index = @window.index
  200.     DataManager.setup_new_game
  201.     fadeout_all
  202.     $game_map.autoplay
  203.     SceneManager.goto(Scene_Map)
  204.   end
  205.  
  206.   def terminate
  207.     super
  208.     # Deactivate flag
  209.     Theo::GamePlus.deactivate
  210.   end
  211.  
  212. end
Advertisement
Add Comment
Please, Sign In to add comment