Advertisement
KK20

Resolution v97a

Feb 7th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 53.88 KB | None | 0 0
  1. #===============================================================================
  2. # Custom Resolution (DLL-less)
  3. # Authors: ForeverZer0, KK20, LiTTleDRAgo
  4. # Version: 0.97a
  5. # Date: 3.2.2014
  6. #===============================================================================
  7. # LiTTleDRAgo's Notes
  8. #===============================================================================
  9. #  Note that X in the notes below means that it's no longer true
  10. #
  11. #===============================================================================
  12. # KK20's Notes
  13. #===============================================================================
  14. # Introduction:
  15. #
  16. #   This script is intended to create screen resolutions other than 640 x 480.
  17. #   The script comes with its own Tilemap rewrite in order to combat larger
  18. #   screen resolutions (because anything beyond 640 x 480 is not drawn).
  19. #
  20. # Instructions:
  21. #
  22. #   Place script above 'Main'. Probably best to put it below all your other
  23. #   custom scripts.
  24. #   This version does not require the 'screenshot.dll'.
  25. #
  26. # Things to Consider:
  27. #
  28. #  X Fullscreen will change the resolution back to 640 x 480. A solution is in
  29. #    the works.
  30. #  - Transitions do not work properly on larger resolutions. You can use a
  31. #    Transitions Add-Ons script if you want better transitions (otherwise, all
  32. #    you will get is the default fade in/out). Links listed below.
  33. #  - Custom scripts that draw windows to the screen will most likely need edits.
  34. #  - Larger resolutions = more processing power = more lag
  35. #
  36. #
  37. #  ***************************************************************************  
  38. #  * THIS IS STILL A WORK IN PROGRESS; IF YOU FIND ANYTHING PLEASE REPORT IT *
  39. #  ***************************************************************************  
  40. #
  41. #  Links:
  42. #  - Fantasist's Transitions Pack
  43. #    http://forum.chaos-project.com/index.php/topic,1390.0.html
  44. #  - ForeverZer0's Add-ons
  45. #    http://forum.chaos-project.com/index.php/topic,7862.0.html
  46. #  - ThallionDarkshine's Add-ons
  47. #    http://forum.chaos-project.com/index.php/topic,12655.0.html
  48. #  - Drago Transition Pack
  49. #    http://forum.chaos-project.com/index.php/topic,13488.0.html
  50. #
  51. #===============================================================================
  52. # ForeverZer0's Notes from v0.93                   (outdated information)
  53. #===============================================================================
  54. # Introduction:
  55. #
  56. #   My goal in creating this script was to create a system that allowed the user
  57. #   to set the screen size to something other than 640 x 480, but not have make
  58. #   huge sacrifices in compatibility and performance. Although the script is
  59. #   not simply Plug-and-Play, it is about as close as one can achieve with a
  60. #   script of this nature.
  61. #
  62. # Instructions:
  63. #
  64. #  X Place the "screenshot.dll" from Fantasist's Transition Pack script, which
  65. #    can be found here: http://www.sendspace.com/file/yjd54h in your game folder
  66. #  - Place this script above main, below default scripts.
  67. #  - In my experience, unchecking "Reduce Screen Flickering" actually helps the
  68. #    screen not to flicker. Open menu with F1 while playing and set this to what
  69. #    you get the best results with.
  70. #
  71. # Features:
  72. #  
  73. #  X Totally re-written Tilemap and Plane class. Both classes were written to
  74. #    display the map across any screen size automatically. The Tilemap class
  75. #    is probably even more efficient than the original, which will help offset
  76. #    any increased lag due to using a larger screen size with more sprites
  77. #    being displayed.
  78. #  - Every possible autotile graphic (48 per autotile) will be cached for the
  79. #    next time that tile is used.
  80. #  - Autotile animation has been made as efficient as possible, with a system
  81. #    that stores their coodinates, but only if they change. This greatly reduces
  82. #    the number of iterations at each update.
  83. #  X System creates an external file to save pre-cached data priorities and
  84. #    autotiles. This will decrease any loading times even more, and only takes a
  85. #    second, depending on the number of maps you have.
  86. #  - User defined autotile animation speed. Can change with script calls.
  87. #  - Automatic re-sizing of Bitmaps and Viewports that are 640 x 480 to the
  88. #    defined resolution, unless explicitely over-ridden in the method call.
  89. #    The graphics themselves will not be resized, but any existing scripts that
  90. #    use the normal screen size will already be configured to display different
  91. #    sizes of graphics for transitions, battlebacks, pictures, fogs, etc.
  92. #  X Option to have a log file ouput each time the game is ran, which can alert
  93. #    you to possible errors with map sizes, etc.
  94. #
  95. # Issues/Bugs/Possible Bugs:
  96. #
  97. #   - Graphic related scripts and your graphics will need to be customized to
  98. #     fit the new screen size, so this script is not for everyone.
  99. #   X The Z-axis for the Plane class, which is used for Fogs and Panoramas has
  100. #     been altered. It is now multiplied by 1000. This will likely be a minor
  101. #     issue for most, since this class is very rarely used except for Fogs and
  102. #     Panoramas, which are already far above and below respectfully.
  103. #   - Normal transitions using graphics cannot be used. With the exception of
  104. #     a standard fade, like that used when no graphic is defined will be used.
  105. #     Aside from that, only special transitions from Transition Pack can be
  106. #     used.
  107. #===============================================================================
  108. #  Credits/Thanks:
  109. #    - ForeverZer0, for script.
  110. #    - Creators of the Transition Pack and Screenshot.dll
  111. #    - Selwyn, for base resolution script
  112. #    - KK20, for Version 0.94 and above and the Tilemap class
  113. #    - LiTTleDRAgo, for his Core Engine script to remove screenshot.dll dependency
  114. #===============================================================================
  115. #                             CONFIGURATION
  116. #===============================================================================
  117.  
  118.   SCREEN = [1024,576]
  119.   # Define the resolution of the game screen. These values can be anything
  120.   # within reason. Centering, viewports, etc. will all be taken care of, but it
  121.   # is recommended that you use values divisible by 32 for best results.
  122.  
  123.   UPDATE_COUNT = 8
  124.   # Define the number of frames between autotile updates. The lower the number,
  125.   # the faster the animations cycle. This can be changed in-game with the
  126.   # following script call: $game_map.autotile_speed = SPEED
  127.  
  128. #===============================================================================
  129. # ** Resolution
  130. #===============================================================================
  131.  
  132. class Resolution
  133.  
  134.   attr_reader :version, :width, :height
  135.  
  136.   def initialize
  137.     # Define version.
  138.     @version = 0.971
  139.     # Set instance variables for calling basic Win32 functions.
  140.     ini = Win32API.new('kernel32', 'GetPrivateProfileString','PPPPLP', 'L')
  141.     ini.call('Game', 'Title', '', (title = "\0" * 256), 256, '.\\Game.ini')
  142.     title.delete!("\0")
  143.     @window   = LiTTleDRAgo.hwnd
  144.     @metrics  = Win32API.new('user32', 'GetSystemMetrics', 'I', 'I')
  145.     # Set default size, displaying error if size is larger than the hardware.
  146.     default_size = self.size
  147.     if default_size[0] < SCREEN[0] || default_size[1] < SCREEN[1]
  148.       print("\"#{title}\" requires a minimum screen resolution of [#{SCREEN[0]} x #{SCREEN[1]}]\r\n\r\n" +
  149.             "\tYour Resolution: [#{default_size[0]} x #{default_size[1]}]")
  150.       exit
  151.     end
  152.     # Apply resolution change.
  153.     enable_tilemap(true)
  154.     change_resolution(SCREEN[0], SCREEN[1])    
  155.   end
  156.   #--------------------------------------------------------------------------
  157.   def size
  158.     # Returns the screen size of the machine.
  159.     return [@metrics.call(0), @metrics.call(1)]
  160.   end
  161.   #--------------------------------------------------------------------------
  162.   def enable_tilemap(value = true)
  163.     @enable = value
  164.   end
  165.   #--------------------------------------------------------------------------
  166.   def enabled?
  167.     @enable
  168.   end
  169.   #--------------------------------------------------------------------------
  170.   def fullscreen?
  171.     size.at(0) == Graphics.width
  172.   end
  173.   #--------------------------------------------------------------------------
  174.   # * New method: change_resolution
  175.   #--------------------------------------------------------------------------
  176.   unless method_defined?(:change_resolution)
  177.     def change_resolution(w = @width, h = @height)
  178.       @set_window_long ||= Win32API.new('user32', 'SetWindowLong', 'LIL', 'L')
  179.       @set_window_pos  ||= Win32API.new('user32', 'SetWindowPos', 'LLIIIII', 'I')
  180.       w, h = (w / 32.0).ceil * 32, (h / 32.0).ceil * 32
  181.       x, y = (size[0] - w) / 2, (size[1] - h) / 2
  182.       @set_window_long.call(@window, -16, 0x14CA0000)
  183.       @set_window_pos.call(@window, 0, x, y, w + 6, h + 26, 0)
  184.       if @width != w || @height != h
  185.         @width, @height = w, h
  186.       end
  187.     end
  188.   end
  189.   #--------------------------------------------------------------------------
  190. end
  191.  
  192.  
  193. #==============================================================================
  194. # ** Drago - Core Engine
  195. # Version : 1.39
  196. # Contact : littledrago.blogspot.com / forum.chaos-project.com
  197. #==============================================================================
  198. # =============================================================================
  199. # NOTE:
  200. # -----------------------------------------------------------------------------
  201. # This is a devtool for me to make me easier for scripting
  202. # If you want to use this, put above all custom script & below Scene_Debug
  203. #
  204. # Note that I'm changing a bit RGSS language in this script, so probably some
  205. # syntax in this script won't work if you use them somewhere else
  206. #
  207. # =============================================================================
  208. module LiTTleDRAgo
  209.   #-------------------------------------------------------------------------
  210.   # * Constant
  211.   #-------------------------------------------------------------------------
  212.   VX           = defined?(Window_ActorCommand)
  213.   VXA          = defined?(Window_BattleActor)
  214.   RGSS1        = defined?(Hangup)
  215.   RGSS2        = RUBY_VERSION == '1.8.1' && !RGSS1
  216.   RGSS3        = RUBY_VERSION == '1.9.2'
  217.   APPPATHDRAGO = "#{ENV['APPDATA']}/Drago/"
  218. end
  219.  
  220. #==============================================================================
  221. # ** CoreDLL
  222. #------------------------------------------------------------------------------
  223. #  
  224. #==============================================================================
  225. module CoreDLL
  226.   #-------------------------------------------------------------------------
  227.   # * Constant
  228.   #-------------------------------------------------------------------------
  229.   Rtlmemory_pi = Win32API.new('kernel32','RtlMoveMemory','pii','i')
  230.   Rtlmemory_ip = Win32API.new('kernel32','RtlMoveMemory','ipi','i')
  231.   #-------------------------------------------------------------------------
  232.   # * Get the game window handle (specific to game)
  233.   #-------------------------------------------------------------------------
  234.   unless method_defined?(:hwnd)
  235.     def hwnd
  236.       @window_find ||= Win32API.new('user32', 'FindWindowEx', %w(l l p p), 'i')
  237.       @game_window ||= @window_find.call(0,0,"RGSS Player",0)
  238.       return @game_window
  239.     end  
  240.   end
  241.   #-------------------------------------------------------------------------
  242.   # * Get the Game Window's width and height
  243.   #-------------------------------------------------------------------------
  244.   unless method_defined?(:client_size)
  245.     def client_size
  246.       @window_c_rect ||= Win32API.new('user32', 'GetClientRect', %w(l p), 'i')
  247.       @window_c_rect.call(self.hwnd, (rect = [0, 0, 0, 0].pack('l4')))
  248.       right, bottom = rect.unpack('l4')[2..3]
  249.       return right, bottom
  250.     end  
  251.   end
  252.   #--------------------------------------------------------------------------
  253.   # * snap_to_bitmap
  254.   #--------------------------------------------------------------------------
  255.   unless method_defined?(:snap_to_bitmap)
  256.     def snap_to_bitmap
  257.       @getdc         ||= Win32API.new('user32','GetDC','i','i')
  258.       @ccdc          ||= Win32API.new('gdi32','CreateCompatibleDC','i','i')
  259.       @ccbitmap      ||= Win32API.new('gdi32','CreateCompatibleBitmap','iii','i')
  260.       @deleteobject  ||= Win32API.new('gdi32','DeleteObject','i','i')
  261.       @bitblt        ||= Win32API.new('gdi32','BitBlt','iiiiiiiii','i')
  262.       @setdibits     ||= Win32API.new('gdi32','SetDIBits','iiiiipi','i')
  263.       @getdibits     ||= Win32API.new('gdi32','GetDIBits','iiiiipi','i')
  264.       @selectobject  ||= Win32API.new('gdi32','SelectObject','ii','i')
  265.       bitmap = Bitmap.new((width = Graphics.width), (height = Graphics.height))
  266.       info   = [40,width,height,1,32,0,0,0,0,0,0].pack('LllSSLLllLL')
  267.       hDC    = @ccdc.call((dc = @getdc.call(hwnd)))
  268.       hBM    = @ccbitmap.call(dc, width, height)
  269.       @deleteobject.call(@selectobject.call(hDC, hBM))
  270.       @setdibits.call(hDC, hBM, 0, height, (address = bitmap.address), info, 0)
  271.       @bitblt.call(hDC, 0, 0, width, height, dc, 0, 0, 0xCC0020)
  272.       @getdibits.call(hDC, hBM, 0, height, address, info, 0)
  273.       @deleteobject.call(hBM)
  274.       @deleteobject.call(hDC)
  275.       bitmap
  276.     end    
  277.   end
  278. end                        
  279. LiTTleDRAgo.extend(CoreDLL)
  280.  
  281. #==============================================================================
  282. # ** Graphics
  283. #------------------------------------------------------------------------------
  284. #  This module handles all Graphics
  285. #==============================================================================
  286. class << Graphics
  287.   #--------------------------------------------------------------------------
  288.   # ● Redefined method: width, height, snap_to_bitmap
  289.   #--------------------------------------------------------------------------
  290.   unless method_defined?(:width)
  291.     def width
  292.       LiTTleDRAgo.client_size.at(0)
  293.     end
  294.   end
  295.    
  296.   unless method_defined?(:height)
  297.     def height
  298.       LiTTleDRAgo.client_size.at(1)
  299.     end
  300.   end
  301.    
  302.   unless method_defined?(:snap_to_bitmap)
  303.     def snap_to_bitmap
  304.       LiTTleDRAgo.snap_to_bitmap
  305.     end
  306.   end
  307. end
  308. #==============================================================================
  309. # ■ Bitmap
  310. #------------------------------------------------------------------------------
  311. #
  312. #==============================================================================
  313. class Bitmap  
  314.   #----------------------------------------------------------------------------
  315.   # ● Constant
  316.   #----------------------------------------------------------------------------
  317.   RtlMoveMemory_pi = CoreDLL::Rtlmemory_pi
  318.   RtlMoveMemory_ip = CoreDLL::Rtlmemory_ip
  319.   #----------------------------------------------------------------------------
  320.   # ● New method: address
  321.   #----------------------------------------------------------------------------
  322.   unless method_defined?(:address)
  323.     def address
  324.       @address ||= (
  325.         RtlMoveMemory_pi.call(a="\0"*4, __id__*2+16, 4)
  326.         RtlMoveMemory_pi.call(a, a.unpack('L')[0]+8, 4)
  327.         RtlMoveMemory_pi.call(a, a.unpack('L')[0]+16, 4)
  328.         a.unpack('L')[0]
  329.       )
  330.     end
  331.   end
  332. end
  333. #===============================================================================
  334. # **                                               END OF Drago - Core Engine
  335. #===============================================================================
  336.  
  337. #===============================================================================
  338. # ** Graphics
  339. #===============================================================================
  340.  
  341. class << Graphics
  342.  
  343.   unless method_defined?(:zer0_graphics_transition)
  344.     alias_method(:zer0_graphics_transition, :transition)
  345.     def transition(duration = 8, *args)
  346.       # Call default transition if no instance of the resolution is defined.
  347.       if $resolution == nil
  348.         zer0_graphics_transition(duration, *args)
  349.       else
  350.         # Skip this section and instantly transition graphics if duration is 0.
  351.         if duration > 0
  352.           # Take a snapshot of the the screen, overlaying screen with graphic.
  353.           #$resolution.snapshot
  354.           zer0_graphics_transition(0)
  355.           # Create screen instance
  356.           viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  357.           sprite = Sprite.new(viewport)
  358.           sprite.bitmap = Graphics.snap_to_bitmap
  359.           # Use a simple fade if transition is not defined.
  360.           fade = 255 / duration
  361.           duration.times { (sprite.opacity -= fade) && update }
  362.           # Dispose sprite and delete snapshot file.
  363.           [sprite, sprite.bitmap, viewport].each {|obj| obj.dispose }
  364.         end
  365.        zer0_graphics_transition(0)
  366.      end
  367.    end
  368.  end
  369. end  
  370.  
  371. #===============================================================================
  372. # ** RPG::Cache
  373. #===============================================================================
  374.  
  375. class << RPG::Cache
  376.  
  377.   AUTO_INDEX = [
  378.  
  379.   [27,28,33,34],  [5,28,33,34],  [27,6,33,34],  [5,6,33,34],
  380.   [27,28,33,12],  [5,28,33,12],  [27,6,33,12],  [5,6,33,12],
  381.   [27,28,11,34],  [5,28,11,34],  [27,6,11,34],  [5,6,11,34],
  382.   [27,28,11,12],  [5,28,11,12],  [27,6,11,12],  [5,6,11,12],
  383.   [25,26,31,32],  [25,6,31,32],  [25,26,31,12], [25,6,31,12],
  384.   [15,16,21,22],  [15,16,21,12], [15,16,11,22], [15,16,11,12],
  385.   [29,30,35,36],  [29,30,11,36], [5,30,35,36],  [5,30,11,36],
  386.   [39,40,45,46],  [5,40,45,46],  [39,6,45,46],  [5,6,45,46],
  387.   [25,30,31,36],  [15,16,45,46], [13,14,19,20], [13,14,19,12],
  388.   [17,18,23,24],  [17,18,11,24], [41,42,47,48], [5,42,47,48],
  389.   [37,38,43,44],  [37,6,43,44],  [13,18,19,24], [13,14,43,44],
  390.   [37,42,43,48],  [17,18,47,48], [13,18,43,48], [1,2,7,8]
  391.    
  392.   ].freeze
  393.  
  394.   alias_method :autotile_cr_tilemap, :autotile
  395.   def autotile(filename)
  396.     return autotile_cr_tilemap(filename) unless $resolution.enabled?
  397.     key = "Graphics/Autotiles/#{filename}"
  398.     if !@cache.include?(key) || @cache[key].disposed?
  399.       # Cache the autotile graphic.
  400.       @cache[key] = (filename == '') ? Bitmap.new(128, 96) : Bitmap.new(key)
  401.       # Cache each configuration of this autotile.
  402.       new_bm = self.format_autotiles(@cache[key], filename)
  403.       @cache[key].dispose
  404.       @cache[key] = new_bm
  405.     end
  406.     return @cache[key]
  407.   end
  408.  
  409.   def format_autotiles(bitmap, filename)
  410.     if bitmap.height > 32
  411.       frames = bitmap.width / 96
  412.       template = Bitmap.new(256*frames,192)
  413.       # Create a bitmap to use as a template for creation.
  414.       (0..frames-1).each{|frame|
  415.       (0...6).each {|i| (0...8).each {|j| AUTO_INDEX[8*i+j].each {|number|
  416.         number -= 1
  417.         x, y = 16 * (number % 6), 16 * (number / 6)
  418.         rect = Rect.new(x + (frame * 96), y, 16, 16)
  419.         template.blt((32 * j + x % 32) + (frame * 256), 32 * i + y % 32, bitmap, rect)
  420.       }}}}
  421.       return template
  422.     else
  423.       return bitmap
  424.     end
  425.   end
  426.  
  427. end
  428. #===============================================================================
  429. # ** Tilemap_DataTable
  430. #===============================================================================
  431. class Tilemap_DataTable
  432.  
  433.   attr_accessor :table, :updates
  434.  
  435.   def [](x,y=nil,z=nil)
  436.     return @table[x,y,z] unless z.nil?
  437.     return @table[x,y]   unless y.nil?
  438.     return @table[x]
  439.   end
  440.  
  441.   def []=(x,y,z=nil,t_id=nil)
  442.     t_id && updates.push([x,y,z,t_id])
  443.     t_id ? (@table[x,y,z] = t_id) : (z.nil? ? @table[x] = y : @table[x,y] = z)
  444.   end
  445.  
  446.   define_method(:initialize) { |table| @table = table }
  447.   define_method(:updates)    { @updates ||= []        }
  448.   define_method(:updated?)    { updates.size >= 1      }
  449.   def method_missing(sym, *a, &blk)  table.send(sym, *a, &blk) end
  450. end
  451. #===============================================================================
  452. # ** CRTilemap
  453. #===============================================================================
  454.  
  455. class CRTilemap
  456.   #---------------------------------------------------------------------------
  457.   # * Public Instance Variables
  458.   #---------------------------------------------------------------------------
  459.   attr_reader   :map_data, :ox, :oy, :viewport, :tile_sprites
  460.   attr_accessor :tileset, :autotiles, :priorities
  461.   define_method(:width)  { $resolution.width  / 32 + 2 }
  462.   define_method(:height) { $resolution.height / 32 + 2 }
  463.  
  464.   def initialize(viewport)
  465.     # Initialize instance variables to store required data.
  466.     @viewport, @autotiles, @tile_sprites, @ox, @oy = viewport, [], [], 0, 0
  467.     @current_frame, @total_frames = [], []
  468.     @tilemap_drawn = false
  469.     @ox_oy_set = [false, false]
  470.     # Get priority data for this tileset from instance of Game_Map.
  471.     @priorities = $game_map.priorities
  472.     # Holds all the Sprite instances of animating tiles (keys based on tile's ID)
  473.     @animating_tiles = {}
  474.     # Game map's x/y location of the top left corner tile
  475.     @corner_tile_loc = [-1,-1]
  476.   end
  477.   #-----------------------------------------------------------------------------
  478.   # Initialize all tile sprites. Draws three sprites per (x,y).
  479.   #-----------------------------------------------------------------------------
  480.   def init_tiles
  481.     # Determine how many frames of animation this autotile has
  482.     for i in 0..6
  483.       bm = @autotiles[i]
  484.       if bm.nil?
  485.         @total_frames = 1
  486.       elsif bm.height > 32
  487.         @total_frames[i] = bm.width / 256
  488.       else
  489.         @total_frames[i] = bm.width / 32
  490.       end
  491.       @current_frame[i] = 0
  492.     end
  493.     # Turn on flag that the tilemap sprites have been initialized
  494.     @tilemap_drawn = true
  495.    
  496.     @animating_tiles.clear
  497.     @zoom_x = $game_map.respond_to?(:zoom_x) ? $game_map.zoom_x : 1.0
  498.     @zoom_y = $game_map.respond_to?(:zoom_y) ? $game_map.zoom_y : 1.0
  499.    
  500.     # Create a sprite and viewport to use for each priority level.
  501.     (0...(width * height)*3).each do |i|
  502.       @tile_sprites[i/3] ||= []
  503.       unless @tile_sprites[i/3][i%3].is_a?(Sprite) && !@tile_sprites[i/3][i%3].disposed?
  504.         @tile_sprites[i/3][i%3] = Sprite.new(@viewport)
  505.       end
  506.       # Rename to something shorter and easier to work with for below
  507.       tile = @tile_sprites[i/3][i%3]
  508.       # Assign tile's respective ID value
  509.       tile.instance_variable_set(:@tile_sprite_id, i)
  510.       # Draw sprite at index location (ex. ID 0 should always be the top-left sprite)
  511.       tile.x = (i % (width*3) / 3 * 32) - 32 + (@ox % 32)
  512.       tile.y = (i / (width*3) * 32) - 32 + (@oy % 32)
  513.       tile.zoom_x = @zoom_x
  514.       tile.zoom_y = @zoom_y
  515.       tile.x = tile.x * tile.zoom_x
  516.       tile.y = tile.y * tile.zoom_y
  517.       map_x, map_y = (tile.x/tile.zoom_x+@ox)/32, (tile.y/tile.zoom_y+@oy)/32
  518.       @corner_tile_loc = [map_x, map_y] if i == 0
  519.       # If the tile happens to be drawn along the outside borders of the map
  520.       if map_x < 0 || map_x >= $game_map.width || map_y < 0 || map_y >= $game_map.height
  521.         tile.z = 0
  522.         tile.bitmap = RPG::Cache.picture('')
  523.         tile.src_rect.set(0,0,0,0)
  524.       else # Tile is actually on the map
  525.         tile_update(tile,i%3)
  526.       end
  527.     end
  528.     # Sprite ID located at top left corner (ranges from 0..map_width * map_height
  529.     @corner_index = 0
  530.   end
  531.   #-----------------------------------------------------------------------------
  532.   # Makes update to ox and oy. Sprites out of range will be moved based on these
  533.   # two values.
  534.   #-----------------------------------------------------------------------------
  535.   def ox=(ox)
  536.     time = Time.now
  537.     return (@ox = ox) && @ox_oy_set[0] = true unless @tilemap_drawn
  538.     return if @ox == ox
  539.     # Shift all tiles left or right by the difference
  540.     shift = (@ox - ox) * @zoom_x
  541.  
  542.     @tile_sprites.flatten.each{|tile| tile.x += shift }
  543.     @ox = ox
  544.    
  545.     # Determine if columns need to be shifted
  546.     col_num = @corner_index
  547.  
  548.     while (t = @tile_sprites[col_num]) && (t[0].x/t[0].zoom_x <= -49 || t[0].x/t[0].zoom_x >= -17)
  549.       @corner_tile_loc[0] += (shift < 0 ? 1 : -1)
  550.       modTileId = (($resolution.width + 64) * ($resolution.height + 64)) /1024    
  551.       # If new ox is greater than old ox
  552.       if shift < 0
  553.         # Move all sprites in left column to the right side and change bitmaps
  554.         # and z-values
  555.         (0...height).each do |n|
  556.           j = (width * n + col_num) % modTileId
  557.           @tile_sprites[j].each_index do |i|
  558.             tile = @tile_sprites[j][i]
  559.             tile.x += (64 + $resolution.width) * tile.zoom_x
  560.             tile_update(tile,i)
  561.           end
  562.         end
  563.         # New corner should be the tile immediately right of the previous tile
  564.         col_num /= width
  565.         col_num *= width
  566.         @corner_index = (@corner_index + 1) % width + col_num
  567.       else
  568.         # Shift right column to the left
  569.         # Gets the right column
  570.         row_index = col_num / width
  571.         row_index *= width
  572.         col_num = (@corner_index - 1) % width + row_index
  573.        
  574.         (0...height).each do |n|
  575.           j = (width * n + col_num) % modTileId
  576.           @tile_sprites[j].each_index do |i|
  577.             tile = @tile_sprites[j][i]
  578.             tile.x -= (64 + $resolution.width) * tile.zoom_x
  579.             tile_update(tile,i)
  580.           end
  581.         end
  582.         col_num /= width
  583.         col_num *= width
  584.         @corner_index = (@corner_index - 1) % width + col_num
  585.       end
  586.       col_num = @corner_index
  587.     end #end of while
  588.     puts Time.now - time
  589.   end
  590.   #-----------------------------------------------------------------------------
  591.   # Makes update to ox and oy. Sprites out of range will be moved based on these
  592.   # two values.
  593.   #-----------------------------------------------------------------------------
  594.   def oy=(oy)
  595.     time = Time.now
  596.     return (@oy = oy) && @ox_oy_set[1] = true unless @tilemap_drawn
  597.     return if @oy == oy
  598.     # Shift all tiles up or down by the difference, and change z-value
  599.     shift = (@oy - oy)
  600.  
  601.     @tile_sprites.each_index do |j|
  602.       @tile_sprites[j].each_index do |i|
  603.         # Get each individual tile on each layer
  604.         tile = @tile_sprites[j][i]
  605.         tile.y += shift * @zoom_y
  606.         priority = tile.instance_variable_get(:@priorities)
  607.         next unless priority.is_a?(Integer) && priority > 0
  608.         tile.z = 32 + (tile.y.floor / 32) * 32 + priority * 32 * tile.zoom_y
  609.       end
  610.     end
  611.    
  612.     @oy = oy
  613.    
  614.     # Determine if rows need to be shifted
  615.     row_num = @corner_index
  616.  
  617.     while (t = @tile_sprites[row_num]) && (t[0].y/t[0].zoom_y <= -49 || t[0].y/t[0].zoom_y >= -17)
  618.       # Needed for resetting the new corner index much later.
  619.       modTileId = (($resolution.width+64)*($resolution.height+64))/1024
  620.       @corner_tile_loc[1] += (shift < 0 ? 1 : -1)
  621.       # If new oy is greater than old oy
  622.       if shift < 0
  623.         row_num /= width
  624.         row_num *= width
  625.         # Move all sprites in top row to the bottom side and change bitmaps
  626.         # and z-values
  627.         (0...width).each do |n|
  628.           # Run through each triad of sprites from left to right
  629.           j = n + row_num
  630.           @tile_sprites[j].each_index do |i|
  631.             # Get each individual tile on each layer
  632.             tile = @tile_sprites[j][i]
  633.             tile.y += (64 + $resolution.height)  * tile.zoom_y
  634.             tile_update(tile,i)
  635.           end
  636.         end
  637.         @corner_index = (@corner_index + width) % modTileId
  638.       else
  639.         row_num = (@corner_index - width) % modTileId
  640.         row_num /= width
  641.         row_num *= width
  642.         (0...width).each do|n|
  643.           # Run through each triad of sprites from left to right
  644.           j = n + row_num
  645.           @tile_sprites[j] && @tile_sprites[j].each_index do |i|
  646.             # Get each individual tile on each layer
  647.             tile = @tile_sprites[j][i]
  648.             tile.y -= (64 + $resolution.height)  * tile.zoom_y
  649.             tile_update(tile,i)
  650.           end
  651.         end
  652.         @corner_index = (@corner_index - width) % modTileId
  653.       end
  654.       row_num = @corner_index
  655.     end # end of while
  656.     puts Time.now - time
  657.   end
  658.   #-----------------------------------------------------------------------------
  659.   # tile_update
  660.   #-----------------------------------------------------------------------------
  661.   def tile_update(tile,i,z = true)
  662.     tile_sprite_id = tile.instance_variable_get(:@tile_sprite_id)
  663.     @animating_tiles.delete(tile_sprite_id)
  664.     # Determine what map coordinate this tile now resides at...
  665.     map_x, map_y = (tile.x/tile.zoom_x+@ox).round/32, (tile.y/tile.zoom_y+@oy).round/32
  666.     # ...and get its tile_id
  667.     tile_id = i.is_a?(String) ? i.to_i : @map_data[map_x,map_y,i]
  668.     tile.instance_variable_set(:@tile_id, tile_id)
  669.     tile.zoom_x = @zoom_x
  670.     tile.zoom_y = @zoom_y
  671.     # If no tile exists here (effectively out of array bounds)
  672.     if tile_id.nil?
  673.       tile.z = [map_y * 32, 1].max
  674.       tile.z *= tile.zoom_y
  675.       tile.bitmap = RPG::Cache.picture('')
  676.       tile.src_rect.set(0,0,0,0)
  677.       tile.instance_variable_set(:@priorities, nil)
  678.       return
  679.     else # Tile exists. Figure out its z-coordinate based on priority
  680.       tile.instance_variable_set(:@priorities, @priorities[tile_id])
  681.       if @priorities[tile_id] == 0
  682.         tile.z = 0
  683.       else
  684.         tile.z = 32  + (tile.y  / 32).floor * 32
  685.         tile.z += @priorities[tile_id] * 32
  686.         tile.z *= tile.zoom_y
  687.       end
  688.     end
  689.     # If empty tile
  690.     if tile_id == 0
  691.       tile.bitmap = RPG::Cache.picture('')
  692.       tile.src_rect.set(0,0,0,0)
  693.       # If not an autotile
  694.     elsif tile_id >= 384
  695.       tile.bitmap = @tileset
  696.       tile.src_rect.set(((tile_id - 384) % 8) * 32,((tile_id - 384) / 8) *32, 32, 32)
  697.     else # Autotile
  698.       auto_id = tile_id/48-1
  699.       tile.bitmap = @autotiles[auto_id]
  700.       tile.src_rect.set(((tile_id % 48) % 8)*32 + @current_frame[auto_id] * 256,((tile_id % 48) / 8)*32, 32, 32)
  701.       @animating_tiles[tile_sprite_id] = tile if @total_frames[auto_id] > 1
  702.     end
  703.   end
  704.   #-----------------------------------------------------------------------------
  705.   # Dispose all the tile sprites
  706.   #-----------------------------------------------------------------------------
  707.   def dispose
  708.     # Dispose all of the sprites
  709.     @tile_sprites.flatten.each {|tile| tile.dispose }
  710.     @tile_sprites.clear
  711.     @animating_tiles.clear
  712.   end
  713.   #-----------------------------------------------------------------------------
  714.   # Set map data
  715.   #-----------------------------------------------------------------------------
  716.   def map_data=(data)
  717.     # Set the map data to new class
  718.     @map_data = data.is_a?(Tilemap_DataTable) ? data : Tilemap_DataTable.new(data)
  719.     @map_data.table = @map_data.table.clone
  720.     @map_data.updates = []
  721.    
  722.     @animating_tiles.clear
  723.     @tilemap_drawn = false
  724.   end
  725.   #---------------------------------------------------------------------------
  726.   # * New method: method_missing
  727.   #---------------------------------------------------------------------------
  728.   def method_missing(val,*a,&b)
  729.     en = tile_sprites.flatten.find_all {|s|s.respond_to?(val.to_sym)}
  730.     if en.empty?
  731.       text = "Undefined method #{val} at #{self.inspect}"
  732.       raise(NoMethodError,text,caller(1))
  733.     end
  734.     return en.map {|s| s.send(val.to_sym,*a,&b)}
  735.   end
  736.   #-----------------------------------------------------------------------------
  737.   # Update the tile sprites; make changes to the map_data and update autotiles
  738.   #-----------------------------------------------------------------------------
  739.   def update
  740.     # Can't update anything if the ox and oy have not yet been set
  741.     return unless @ox_oy_set.at(0) && @ox_oy_set.at(1)
  742.     # If the tilemap sprites have not been initialized, GO DO IT
  743.     draw_tilemap = !@tilemap_drawn
  744.     # If zoom variable has been changed
  745.     draw_tilemap ||= @zoom_x != $game_map.zoom_x || @zoom_y != $game_map.zoom_y
  746.    
  747.     init_tiles if draw_tilemap
  748.    
  749.    
  750.     # If made any changes to $game_map.data, the proper graphics will be drawn
  751.     if @map_data.updated?
  752.       @map_data.updates.each do |item|
  753.         x,y,z,tile_id = item
  754.         # If this changed tile is visible on screen
  755.         if x.between?(@corner_tile_loc.at(0), @corner_tile_loc.at(0)+(width - 1)) &&
  756.            y.between?(@corner_tile_loc.at(1), @corner_tile_loc.at(1)+(height - 1))
  757.          
  758.           x_dif = x - @corner_tile_loc.at(0)
  759.           y_dif = y - @corner_tile_loc.at(1)
  760.          
  761.           id = @corner_index + x_dif
  762.           id -= width          if id / width > @corner_index / width
  763.          
  764.           id += y_dif * width
  765.           id -= width * height if id >= width * height
  766.          
  767.           tile = @tile_sprites[id][z]
  768.           tile_update(tile,tile_id.to_s)
  769.         end
  770.       end
  771.       @map_data.updates = []
  772.     end
  773.     # Update the sprites.
  774.     if Graphics.frame_count % $game_map.autotile_speed == 0
  775.       # Increase current frame of tile by one, looping by width.
  776.       for i in 0..6
  777.         @current_frame[i] = (@current_frame[i] + 1) % @total_frames[i]
  778.       end
  779.       @animating_tiles.each_value do |tile|
  780.         frames = tile.bitmap.width
  781.         tile.src_rect.set((tile.src_rect.x + 256) % frames, tile.src_rect.y, 32, 32)
  782.       end
  783.     end
  784.  
  785.   end
  786. end
  787.  
  788. #===============================================================================
  789. # Game_Map
  790. #===============================================================================
  791.  
  792. class Game_Map
  793.  
  794.   attr_writer :tile_size, :map_edge
  795.   attr_reader :cr_map_data
  796.    
  797.   $@ || alias_method(:zer0_map_edge_setup,     :setup)
  798.   $@ || alias_method(:drg_scroll_right_adjust, :scroll_right)
  799.   $@ || alias_method(:drg_scroll_down_adjust,  :scroll_down)
  800.  
  801.   def setup(map_id)
  802.     # Call original method.
  803.     zer0_map_edge_setup(map_id)
  804.     # Change Map's data into a special Table class
  805.     @cr_map_data = Tilemap_DataTable.new(@map.data)
  806.   end
  807.  
  808.   def scroll_right(distance)
  809.     # Find point that the map edge meets the screen edge, using custom size.
  810.     result = [@display_x + distance, map_edge.at(0) / zoom_x].min
  811.     drg_scroll_right_adjust(distance)
  812.     @display_x = result
  813.   end
  814.  
  815.   def scroll_down(distance)
  816.     # Find point that the map edge meets the screen edge, using custom size.
  817.     result = [@display_y + distance, map_edge.at(1) / zoom_y].min
  818.     drg_scroll_down_adjust(distance)
  819.     @display_y = result
  820.   end
  821.  
  822.   def tile_size
  823.     [_w = ($resolution.width  / 32.0).ceil,
  824.      _h = ($resolution.height / 32.0).ceil]
  825.   end
  826.  
  827.   def map_edge
  828.     [_w = [(width  * zoom_x - tile_size.at(0)) * 128, 0].max,
  829.      _h = [(height * zoom_y - tile_size.at(1)) * 128, 0].max]
  830.   end
  831.                    
  832.   def autotile_speed=(speed)
  833.     # Keep the speed above 0 to prevent the ZeroDivision Error.
  834.     @autotile_speed = [speed, 1].max
  835.   end
  836.  
  837.   define_method(:autotile_speed) { @autotile_speed ||= UPDATE_COUNT }
  838.   define_method(:zoom_x)         { @zoom_x ||= 1.0 }
  839.   define_method(:zoom_y)         { @zoom_y ||= 1.0 }
  840. end
  841.  
  842. #===============================================================================
  843. # ** Game_Player
  844. #===============================================================================
  845.  
  846. class Game_Player
  847.   #--------------------------------------------------------------------------
  848.   # * Alias Listing
  849.   #--------------------------------------------------------------------------
  850.   $@ || alias_method(:drg_adjust_viewport_center, :center)
  851.   #--------------------------------------------------------------------------
  852.   # * Aliased method: center
  853.   #--------------------------------------------------------------------------
  854.   def center(x, y)
  855.     drg_adjust_viewport_center(x, y)
  856.     # Recalculate the screen center based on the new resolution.  
  857.     max_x = $game_map.map_edge.at(0) / $game_map.zoom_x
  858.     max_y = $game_map.map_edge.at(1) / $game_map.zoom_y
  859.     $game_map.display_x = [0, [(x * 32*4) - CENTER_X, max_x].min].max
  860.     $game_map.display_y = [0, [(y * 32*4) - CENTER_Y, max_y].min].max
  861.   end  
  862. end
  863.  
  864. #==============================================================================
  865. # ** Viewport
  866. #------------------------------------------------------------------------------
  867. #  
  868. #==============================================================================
  869. class Viewport
  870.   #--------------------------------------------------------------------------
  871.   # * Alias Listing
  872.   #--------------------------------------------------------------------------
  873.   $@ || alias_method(:zer0_viewport_resize_init, :initialize)
  874.   #--------------------------------------------------------------------------
  875.   # * Aliased method: initialize
  876.   #--------------------------------------------------------------------------
  877.   def initialize(*args)
  878.     default = Rect.new(0, 0, $resolution.width, $resolution.height)
  879.     if args.size == 0 || args == [0, 0, 640, 480]
  880.       # If argument is nil, use default resolution.
  881.       zer0_viewport_resize_init(default)
  882.     else
  883.       # Call method normally.
  884.       zer0_viewport_resize_init(*args)
  885.     end
  886.   end
  887.   #--------------------------------------------------------------------------
  888.   # * New method: resize
  889.   #--------------------------------------------------------------------------
  890.   unless method_defined?(:resize)
  891.     def resize(*args)
  892.       # Resize the viewport. Can call with (X, Y, WIDTH, HEIGHT) or (RECT).
  893.       self.rect = args[0].is_a?(Rect) ? args[0] : Rect.new(*args)
  894.     end
  895.   end
  896.   #--------------------------------------------------------------------------
  897.   # * New method: update_viewport_sizes
  898.   #--------------------------------------------------------------------------
  899.   unless method_defined?(:update_viewport_sizes)
  900.     def update_viewport_sizes
  901.       map = $game_map
  902.       w, h = Graphics.width, Graphics.height
  903.       hor = map.respond_to?(:loop_horizontal?) && map.loop_horizontal?
  904.       ver = map.respond_to?(:loop_vertical?) && map.loop_vertical?
  905.       dx = w > map.width * 32 && !hor ? (w - map.width * 32) / 2 : 0
  906.       dw = hor ? w : [w, map.width * 32].min
  907.       dy = h > map.height * 32 && !ver ? (h - map.height * 32) / 2 : 0
  908.       dh = ver ? h : [h, map.height * 32].min
  909.       resize(Rect.new(dx, dy, dw, dh))
  910.     end
  911.   end
  912. end
  913. #===============================================================================
  914. # ** Plane
  915. #===============================================================================
  916.  
  917. class CRPlane < Sprite
  918.  
  919.   def z=(z)
  920.     # Change the Z value of the viewport, not the sprite.
  921.     super(z * 1000)
  922.   end
  923.  
  924.   def ox=(ox)
  925.     return if @bitmap == nil
  926.     # Have viewport stay in loop on X-axis.
  927.     super(ox % @bitmap.width)
  928.   end
  929.  
  930.   def oy=(oy)
  931.     return if @bitmap == nil
  932.     # Have viewport stay in loop on Y-axis.
  933.     super(oy % @bitmap.height)
  934.   end
  935.  
  936.   def bitmap
  937.     # Return the single bitmap, before it was tiled.
  938.     return @bitmap
  939.   end
  940.  
  941.   def bitmap=(tile)
  942.     @bitmap = tile
  943.     # Calculate the number of tiles it takes to span screen in both directions.
  944.     xx = 1 + ($resolution.width.to_f  / tile.width).ceil
  945.     yy = 1 + ($resolution.height.to_f / tile.height).ceil
  946.     # Create appropriately sized bitmap, then tile across it with source image.
  947.     plane = Bitmap.new(@bitmap.width * xx, @bitmap.height * yy)
  948.     (0..xx).each {|x| (0..yy).each {|y|
  949.       plane.blt(x * @bitmap.width, y * @bitmap.height, @bitmap, @bitmap.rect)
  950.     }}
  951.     # Set the bitmap to the sprite through its super class (Sprite).
  952.     super(plane)
  953.   end
  954.  
  955.   # Redefine methods dealing with coordinates (defined in super) to do nothing.
  956.   define_method(:x)  {|*x|}
  957.   define_method(:y)  {|*y|}
  958.   define_method(:x=) {|*x|}
  959.   define_method(:y=) {|*y|}
  960. end
  961.  
  962. #==============================================================================
  963. # ** Spriteset_Map
  964. #------------------------------------------------------------------------------
  965. #  This class brings together map screen sprites, tilemaps, etc.
  966. #  It's used within the Scene_Map class.
  967. #==============================================================================
  968. class Spriteset_Map
  969.   #--------------------------------------------------------------------------
  970.   # * Alias Listing
  971.   #--------------------------------------------------------------------------
  972.   # Checked whether :drg_spriteset_viewport_adjust is already exist
  973.   # Drago - Core Engine already has this method
  974.   unless method_defined?(:drg_spriteset_viewport_adjust)
  975.     # Method Aliasing
  976.     alias_method :drg_spriteset_viewport_adjust, :update
  977.     #--------------------------------------------------------------------------
  978.     # * Aliased method: update
  979.     #--------------------------------------------------------------------------
  980.     def update(*args)
  981.       # If resolution is changing
  982.       if viewport_size_change?
  983.         @viewport_map_width     = $game_map.width
  984.         @viewport_map_height    = $game_map.height
  985.         @viewport_screen_width  = Graphics.width
  986.         @viewport_screen_height = Graphics.height
  987.         # Change all viewport resolution
  988.         [@viewport1,@viewport2,@viewport3].each { |v| v.update_viewport_sizes }
  989.       end
  990.       drg_spriteset_viewport_adjust(*args)
  991.     end
  992.   end
  993.   #--------------------------------------------------------------------------
  994.   # * New method: viewport_size_change?
  995.   #--------------------------------------------------------------------------
  996.   def viewport_size_change?
  997.     return true if @viewport_map_width     != $game_map.width
  998.     return true if @viewport_map_height    != $game_map.height
  999.     return true if @viewport_screen_width  != Graphics.width
  1000.     return true if @viewport_screen_height != Graphics.height
  1001.   end
  1002.   #--------------------------------------------------------------------------
  1003.   # * New method: reload_tilemap
  1004.   #--------------------------------------------------------------------------
  1005.   def reload_tilemap
  1006.     @tilemap.respond_to?(:dispose) && @tilemap.dispose
  1007.     @tilemap = $resolution.enabled? ? CRTilemap.new(@viewport1) : Tilemap.new(@viewport1)
  1008.     @tilemap.tileset = RPG::Cache.tileset($game_map.tileset_name)
  1009.     for i in 0..6
  1010.       autotile_name = $game_map.autotile_names[i]
  1011.       @tilemap.autotiles[i] = RPG::Cache.autotile(autotile_name)
  1012.     end
  1013.     @tilemap.map_data = $resolution.enabled? ? $game_map.cr_map_data : $game_map.data
  1014.     @tilemap.priorities = $game_map.priorities
  1015.   end    
  1016. end
  1017.  
  1018. #==============================================================================
  1019. # ** Graphics
  1020. #------------------------------------------------------------------------------
  1021. #  This module handles all Graphics
  1022. #==============================================================================
  1023. class << Graphics
  1024.   #--------------------------------------------------------------------------
  1025.   # * Alias Listing
  1026.   #--------------------------------------------------------------------------
  1027.   unless method_defined?(:drg_spriteset_viewport_adjust)
  1028.     alias_method :drg_spriteset_viewport_adjust, :update
  1029.     #--------------------------------------------------------------------------
  1030.     # * Aliased method: update
  1031.     #--------------------------------------------------------------------------
  1032.     def update(*args)
  1033.       check_center_player
  1034.       drg_spriteset_viewport_adjust(*args)
  1035.     end
  1036.     #--------------------------------------------------------------------------
  1037.     # * New method: check_center_player
  1038.     #--------------------------------------------------------------------------
  1039.     def check_center_player
  1040.       return unless $resolution
  1041.       zoom_x = $game_map.respond_to?(:zoom_x) ? $game_map.zoom_x : 1.0
  1042.       zoom_y = $game_map.respond_to?(:zoom_y) ? $game_map.zoom_y : 1.0
  1043.       if $resolution.fullscreen? || !$resolution.enabled?
  1044.         unless Game_Player.const_get(:CENTER_X) == ((640 / 2.0) / zoom_x - 16).ceil * 4
  1045.           Game_Player.const_set(:CENTER_X,  ((640 / 2.0) / zoom_x - 16).ceil  * 4)
  1046.           Game_Player.const_set(:CENTER_Y,  ((480 / 2.0) / zoom_y - 16).ceil  * 4)
  1047.           $game_player && $game_player.center($game_player.x,$game_player.y)
  1048.         end
  1049.       else
  1050.         $resolution.change_resolution if viewport_size_change?
  1051.         unless Game_Player.const_get(:CENTER_X) == (($resolution.width / 2.0) / zoom_x - 16).ceil * 4
  1052.           Game_Player.const_set(:CENTER_X,  (($resolution.width / 2.0)  / zoom_x - 16).ceil * 4)
  1053.           Game_Player.const_set(:CENTER_Y,  (($resolution.height / 2.0) / zoom_y - 16).ceil * 4)
  1054.           $game_player && $game_player.center($game_player.x,$game_player.y)
  1055.         end
  1056.       end
  1057.     end
  1058.     #--------------------------------------------------------------------------
  1059.     # * New method: viewport_size_change?
  1060.     #--------------------------------------------------------------------------
  1061.     def viewport_size_change?
  1062.       return false unless $resolution
  1063.       return false if $resolution.fullscreen?
  1064.       return true if (Graphics.width  - $resolution.width).abs  > 30
  1065.       return true if (Graphics.height - $resolution.height).abs > 30
  1066.     end
  1067.   end
  1068. end
  1069.  
  1070. #==============================================================================
  1071. # ** Spriteset_Map
  1072. #------------------------------------------------------------------------------
  1073. #  This class brings together map screen sprites, tilemaps, etc.
  1074. #  It's used within the Scene_Map class.
  1075. #==============================================================================
  1076. class Spriteset_Map
  1077.   #--------------------------------------------------------------------------
  1078.   # * Public Instance Variable
  1079.   #--------------------------------------------------------------------------
  1080.   attr_reader :tilemap
  1081.   #--------------------------------------------------------------------------
  1082.   # * Alias Listing
  1083.   #--------------------------------------------------------------------------
  1084.   alias_method :drg_weather_fix_update, :update
  1085.   #--------------------------------------------------------------------------
  1086.   # * Aliased method: update
  1087.   #--------------------------------------------------------------------------
  1088.   def update(*args)
  1089.     weather_fix_custom_resolution
  1090.     drg_weather_fix_update(*args)
  1091.   end
  1092.   #--------------------------------------------------------------------------
  1093.   # * New method: weather_fix_custom_resolution
  1094.   #--------------------------------------------------------------------------
  1095.   def weather_fix_custom_resolution
  1096.     if ($resolution.enabled? && @tilemap.is_a?(Tilemap)) ||
  1097.       (!$resolution.enabled? && @tilemap.is_a?(CRTilemap))
  1098.       reload_tilemap
  1099.     end
  1100.     if @panorama.is_a?(Plane)
  1101.       z = @panorama.z
  1102.       @panorama.dispose if @panorama.respond_to?(:dispose)
  1103.       @panorama = CRPlane.new(@viewport1)
  1104.       @panorama.z = z
  1105.     end
  1106.     if @fog.is_a?(Plane)
  1107.       z = @fog.z
  1108.       @fog.dispose if @fog.respond_to?(:dispose)
  1109.       @fog = CRPlane.new(@viewport1)
  1110.       @fog.z = z
  1111.     end
  1112.     if @weather.is_a?(RPG::Weather)
  1113.       @weather.dispose if @weather.respond_to?(:dispose)
  1114.       @weather = Spriteset_Weather.new(@viewport1)
  1115.     end
  1116.   end
  1117. end
  1118.  
  1119. #==============================================================================
  1120. # ** Spriteset_Weather
  1121. #------------------------------------------------------------------------------
  1122. #  A class for weather effects (rain, storm, and snow). It is used within the
  1123. # Spriteset_Map class.
  1124. #==============================================================================
  1125.  
  1126. class Spriteset_Weather
  1127.   #--------------------------------------------------------------------------
  1128.   # * Public Instance Variables
  1129.   #--------------------------------------------------------------------------
  1130.   attr_accessor :type                     # Weather type
  1131.   attr_accessor :ox                       # X coordinate of origin
  1132.   attr_accessor :oy                       # Y coordinate of orgin
  1133.   attr_reader   :power                    # Intensity
  1134.   #--------------------------------------------------------------------------
  1135.   # * Object Initialization
  1136.   #--------------------------------------------------------------------------
  1137.   def initialize(viewport = nil)
  1138.     @viewport = viewport
  1139.     init_members
  1140.     create_rain_bitmap
  1141.     create_storm_bitmap
  1142.     create_snow_bitmap
  1143.   end
  1144.   #--------------------------------------------------------------------------
  1145.   # * Initialize Member Variables
  1146.   #--------------------------------------------------------------------------
  1147.   def init_members
  1148.     @type = :none
  1149.     @ox = 0
  1150.     @oy = 0
  1151.     @power = 0
  1152.     @sprites = []
  1153.   end
  1154.   #--------------------------------------------------------------------------
  1155.   # * Free
  1156.   #--------------------------------------------------------------------------
  1157.   def dispose
  1158.     @sprites.each {|sprite| sprite.dispose }
  1159.     @rain_bitmap.dispose
  1160.     @storm_bitmap.dispose
  1161.     @snow_bitmap.dispose
  1162.   end
  1163.   #--------------------------------------------------------------------------
  1164.   # * Particle Color 1
  1165.   #--------------------------------------------------------------------------
  1166.   def particle_color1
  1167.     Color.new(255, 255, 255, 192)
  1168.   end
  1169.   #--------------------------------------------------------------------------
  1170.   # * Particle Color 2
  1171.   #--------------------------------------------------------------------------
  1172.   def particle_color2
  1173.     Color.new(255, 255, 255, 96)
  1174.   end
  1175.   #--------------------------------------------------------------------------
  1176.   # * Create [Rain] Weather Bitmap
  1177.   #--------------------------------------------------------------------------
  1178.   def create_rain_bitmap
  1179.     @rain_bitmap = Bitmap.new(7, 42)
  1180.     7.times {|i| @rain_bitmap.fill_rect(6-i, i*6, 1, 6, particle_color1) }
  1181.   end
  1182.   #--------------------------------------------------------------------------
  1183.   # * Create [Storm] Weather Bitmap
  1184.   #--------------------------------------------------------------------------
  1185.   def create_storm_bitmap
  1186.     @storm_bitmap = Bitmap.new(34, 64)
  1187.     32.times do |i|
  1188.       @storm_bitmap.fill_rect(33-i, i*2, 1, 2, particle_color2)
  1189.       @storm_bitmap.fill_rect(32-i, i*2, 1, 2, particle_color1)
  1190.       @storm_bitmap.fill_rect(31-i, i*2, 1, 2, particle_color2)
  1191.     end
  1192.   end
  1193.   #--------------------------------------------------------------------------
  1194.   # * Create [Snow] Weather Bitmap
  1195.   #--------------------------------------------------------------------------
  1196.   def create_snow_bitmap
  1197.     @snow_bitmap = Bitmap.new(6, 6)
  1198.     @snow_bitmap.fill_rect(0, 1, 6, 4, particle_color2)
  1199.     @snow_bitmap.fill_rect(1, 0, 4, 6, particle_color2)
  1200.     @snow_bitmap.fill_rect(1, 2, 4, 2, particle_color1)
  1201.     @snow_bitmap.fill_rect(2, 1, 2, 4, particle_color1)
  1202.   end
  1203.   #--------------------------------------------------------------------------
  1204.   # * Set Weather Intensity
  1205.   #--------------------------------------------------------------------------
  1206.   def power=(power)
  1207.     @power = power
  1208.     (sprite_max - @sprites.size).times { add_sprite }
  1209.     (@sprites.size - sprite_max).times { remove_sprite }
  1210.   end
  1211.   alias_method :max,  :power
  1212.   alias_method :max=, :power=
  1213.   #--------------------------------------------------------------------------
  1214.   # * Get Maximum Number of Sprites
  1215.   #--------------------------------------------------------------------------
  1216.   def sprite_max
  1217.     (@power * 10).to_i
  1218.   end
  1219.   #--------------------------------------------------------------------------
  1220.   # * Add Sprite
  1221.   #--------------------------------------------------------------------------
  1222.   def add_sprite
  1223.     sprite = Sprite.new(@viewport)
  1224.     sprite.opacity = 0
  1225.     @sprites.push(sprite)
  1226.   end
  1227.   #--------------------------------------------------------------------------
  1228.   # * Delete Sprite
  1229.   #--------------------------------------------------------------------------
  1230.   def remove_sprite
  1231.     sprite = @sprites.pop
  1232.     sprite.dispose if sprite
  1233.   end
  1234.   #--------------------------------------------------------------------------
  1235.   # * Frame Update
  1236.   #--------------------------------------------------------------------------
  1237.   def update
  1238.     update_screen
  1239.     @sprites.each {|sprite| update_sprite(sprite) }
  1240.   end
  1241.   #--------------------------------------------------------------------------
  1242.   # * Update Screen
  1243.   #--------------------------------------------------------------------------
  1244.   def update_screen
  1245.     @viewport.tone.set(-dimness, -dimness, -dimness)
  1246.   end
  1247.   #--------------------------------------------------------------------------
  1248.   # * Get Dimness
  1249.   #--------------------------------------------------------------------------
  1250.   def dimness
  1251.     (@power * 6).to_i
  1252.   end
  1253.   #--------------------------------------------------------------------------
  1254.   # * Update Sprite
  1255.   #--------------------------------------------------------------------------
  1256.   def update_sprite(sprite)
  1257.     sprite.ox = @ox
  1258.     sprite.oy = @oy
  1259.     case @type
  1260.     when 1, :rain
  1261.       update_sprite_rain(sprite)
  1262.     when 2, :storm
  1263.       update_sprite_storm(sprite)
  1264.     when 3, :snow
  1265.       update_sprite_snow(sprite)
  1266.     end
  1267.     create_new_particle(sprite) if sprite.opacity < 64
  1268.   end
  1269.   #--------------------------------------------------------------------------
  1270.   # * Update Sprite [Rain]
  1271.   #--------------------------------------------------------------------------
  1272.   def update_sprite_rain(sprite)
  1273.     sprite.bitmap = @rain_bitmap
  1274.     sprite.x -= 1
  1275.     sprite.y += 6
  1276.     sprite.opacity -= 12
  1277.   end
  1278.   #--------------------------------------------------------------------------
  1279.   # * Update Sprite [Storm]
  1280.   #--------------------------------------------------------------------------
  1281.   def update_sprite_storm(sprite)
  1282.     sprite.bitmap = @storm_bitmap
  1283.     sprite.x -= 3
  1284.     sprite.y += 6
  1285.     sprite.opacity -= 12
  1286.   end
  1287.   #--------------------------------------------------------------------------
  1288.   # * Update Sprite [Snow]
  1289.   #--------------------------------------------------------------------------
  1290.   def update_sprite_snow(sprite)
  1291.     sprite.bitmap = @snow_bitmap
  1292.     sprite.x -= 1
  1293.     sprite.y += 3
  1294.     sprite.opacity -= 12
  1295.   end
  1296.   #--------------------------------------------------------------------------
  1297.   # * Create New Particle
  1298.   #--------------------------------------------------------------------------
  1299.   def create_new_particle(sprite)
  1300.     sprite.x = rand(Graphics.width + 100) - 100 + @ox
  1301.     sprite.y = rand(Graphics.height + 200) - 200 + @oy
  1302.     sprite.opacity = 160 + rand(96)
  1303.   end
  1304. end
  1305.  
  1306. # Call the resolution, setting it to a global variable for plug-ins.
  1307. $resolution = Resolution.new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement