Advertisement
LiTTleDRAgo

[RGSS2/3] Fade Room Effect

Jul 27th, 2013
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.44 KB | None | 0 0
  1. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  2. # DRG Fade Room Effect
  3. # Version: 3.00
  4. # Author : LiTTleDRAgo
  5. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  6. ($imported ||= {})[:drg_fade_effect_room] = 3.00
  7.  
  8. module LiTTleDRAgo
  9.   FADE_ROOM = {
  10.     #-----------------------------------------------------------------
  11.     # Editable Section
  12.     #
  13.     # - Format :
  14.     # MAP_ID => "Command",
  15.     #
  16.     #   -or-
  17.     #
  18.     # MAP_ID => {
  19.     #             'ROOM_1' => "Command",
  20.     #             'ROOM_2' => "Command",
  21.     #             'ROOM_3' => "Command",
  22.     #           },
  23.     #-----------------------------------------------------------------        
  24.              
  25.     2 => {
  26.          # This area will forever hidden in the map
  27.          # Grid at (0,0) to (20,6) will be hidden WITH the exception
  28.          # at <10,2> to <18,6>
  29.          'Dark' => "
  30.                   <Color = 40, 40, 40, 255>
  31.                   (0,0,20,6),<10,2,18,6> #
  32.                   (0,7,20,22),<2,7,18,20>
  33.                   (2,18,5,20)
  34.                   ",
  35.          # This room will be hidden but will showed when player inside the area
  36.          # Room at (10,2) to (18,8) will be hidden and will showed again when
  37.          # player is inside [10,2] to [18,8]
  38.           "Room1" => "
  39.                   <Color = 40, 40, 40, 255>
  40.                   (10,2,18,8),[10,2,18,8] #
  41.                   ",
  42.          # Room at (2,7) to (18,17) will be hidden with the exception
  43.          # <10,7> to <18,8> and will showed again when player is inside
  44.          # [2,7] to [18,17] and not inside {10,7} to {18,8}
  45.           "Room2" => "
  46.                   <Color = 40, 40, 40, 255>
  47.                   (2,7,18,17),[2,7,18,17],<10,7,18,8>,{10,7,18,8} #
  48.                   (6,18,18,20),[2,7,18,17]
  49.                   ",
  50.                    
  51.         },
  52.        
  53.     }
  54.     #-----------------------------------------------------------------
  55.     # End Editable
  56.     #-----------------------------------------------------------------
  57. end
  58.  
  59. RGSS = 1
  60. RGSS = 2 if Graphics.respond_to?(:brightness)
  61. RGSS = 3 if Graphics.methods.include?(:play_movie)
  62.  
  63. raise "This version is not for RMXP" if RGSS == 1
  64.  
  65. #==============================================================================
  66. # ** Spriteset_Map
  67. #------------------------------------------------------------------------------
  68. #  This class brings together map screen sprites, tilemaps, etc.
  69. #  It's used within the Scene_Map class.
  70. #==============================================================================
  71. class Spriteset_Map
  72.   #--------------------------------------------------------------------------
  73.   # * Alias Listing
  74.   #--------------------------------------------------------------------------
  75.   alias_method :drg132_update, :update if !method_defined?(:drg132_update)
  76.   alias_method :drg132_dispos, :dispose if !method_defined?(:drg132_dispos)
  77.   #--------------------------------------------------------------------------
  78.   # * Objects Initialization
  79.   #--------------------------------------------------------------------------
  80.   def init_fadesprite
  81.     @fade_sprite.each {|m| m[0].dispose } if @fade_sprite
  82.     @fade_sprite = []
  83.     @fade_effect = a = ($game_system.fade_room[$game_map.map_id] || '').dup
  84.     a = a.invert.keys.join if a && a.is_a?(Hash)
  85.     a.to_s.split(/\n/).each {|a| create_fade_effect(a)} if a
  86.   end
  87.   #--------------------------------------------------------------------------
  88.   # * check_fade_parameter
  89.   #--------------------------------------------------------------------------
  90.   def create_fade_effect(parameter)
  91.     if parameter =~ /<Color = (\d+), (\d+), (\d+)>/i
  92.       j = [$1.to_i,$2.to_i,$3.to_i,255]
  93.     elsif parameter =~ /<Color = (\d+), (\d+), (\d+), (\d+)>/i
  94.       j = [$1.to_i,$2.to_i,$3.to_i,$4.to_i]
  95.     end
  96.     @fade_time = [$1.to_i,$2.to_i] if parameter =~ /<Fade = (\d+), (\d+)>/i
  97.     @fade_oxy  = [$1.to_i,$2.to_i] if parameter =~ /<Move = (.+?), (.+?)>/i
  98.     if parameter =~ /<Picture = (.+?), (.+?)>/i
  99.       b = ["#{$1}","#{$2}"]
  100.       b = (b == ['0']*2) ? [1,1] : ["Graphics/#{b.join('/')}"]
  101.       @fade_picture = Bitmap.new(*b)
  102.     end
  103.     @fade_time    ||= [16,16]
  104.     @fade_oxy     ||= [0,0]
  105.     @fade_picture ||= Bitmap.new(1,1)
  106.     @fade_color   = Color.new(*j) if !j.nil? && j[0..2] != [0]*3
  107.     if parameter =~ /\((.+?),(.+?),(\d+),(\d+)\)/
  108.       a, e = [$1.to_i,$2.to_i,$3.to_i,$4.to_i], [-1]*4
  109.       if parameter =~ /\[(\d+),(\d+),(\d+),(\d+)\]/
  110.         e = [$1.to_i,$2.to_i,$3.to_i,$4.to_i]
  111.       elsif parameter =~ /\[(\d+),(\d+)\]/
  112.         e = [$1.to_i,$2.to_i,$1.to_i,$2.to_i]
  113.       end
  114.     elsif parameter =~ /\(\[(\d+),(\d+),(\d+),(\d+)\]\)/
  115.       a = e = [$1.to_i,$2.to_i,$3.to_i,$4.to_i]
  116.     elsif parameter =~ /\(\[(\d+),(\d+)\]\)/
  117.       a = e = [$1.to_i,$2.to_i,$1.to_i,$2.to_i]
  118.     elsif parameter =~ /\((\d+),(\d+)\)/
  119.       a, e = [$1.to_i,$2.to_i,$1.to_i,$2.to_i], [0]*4
  120.     end
  121.     return unless !a.nil? && a.flatten[0].is_a?(Numeric) && @fade_color
  122.     sw = [a[2]-a[0].abs, a[3]-a[1].abs]
  123.     sa = [sw[0] > 0 ? 16 : 0 , sw[1] > 0 ? 16 : 0]
  124.     sa = [0,0]
  125.     x,y = [a[0]*32+sa[0],0].max, [a[1]*32+sa[1],0].max
  126.     w,h = [sw[0]+1,1].max*32,[sw[1]+1,1].max*32
  127.     s = Sprite.new(@viewport1)
  128.     s.bitmap = Bitmap.new(w,h)
  129.     s.bitmap.fill_rect(0,0,s.bitmap.width,s.bitmap.height,@fade_color)
  130.     s.bitmap.stretch_blt(s.bitmap.rect, @fade_picture, @fade_picture.rect)
  131.     s.x, s.y, s.z, tc = x, y, 200, Color.new(*[0]*4)
  132.     mask = [s, e, [], fade_coord_player]
  133.     if parameter =~ /<(\d+),(\d+),(\d+),(\d+)>/
  134.       a,b,c,d = $1.to_i,$2.to_i,$3.to_i,$4.to_i
  135.       s.bitmap.fill_rect(a*32-s.x,b*32-s.y,(c-a+1)*32,(d-b+1)*32,tc)
  136.     elsif parameter =~ /<(\d+),(\d+)>/
  137.       a,b,c,d = $1.to_i, $2.to_i, $1.to_i, $2.to_i
  138.       s.bitmap.fill_rect(a*32-s.x,b*32-s.y,(c-a+1)*32,(d-b+1)*32,tc)
  139.     end
  140.     if parameter =~ /{(\d+),(\d+),(\d+),(\d+)}/
  141.       mask[2] = [$1.to_i, $2.to_i, $3.to_i, $4.to_i]
  142.     elsif parameter =~ /{(\d+),(\d+)}/
  143.       mask[2] = [$1.to_i, $2.to_i,$1.to_i, $2.to_i]
  144.     end
  145.     if parameter =~ /<{(\d+),(\d+),(\d+),(\d+)}>/
  146.       a,b,c,d = $1.to_i,$2.to_i,$3.to_i,$4.to_i
  147.       s.bitmap.fill_rect(a*32-s.x,b*32-s.y,(c-a+1)*32,(d-b+1)*32,tc)
  148.       mask[2] = [a,b,c,d]
  149.     elsif parameter =~ /<{(\d+),(\d+)}>/
  150.       a,b,c,d = $1.to_i,$2.to_i,$1.to_i,$2.to_i
  151.       s.bitmap.fill_rect(a*32-s.x,b*32-s.y,(c-a+1)*32,(d-b+1)*32,tc)
  152.       mask[2] = [a,b,c,d]
  153.     end
  154.     mask[0].ox, mask[0].oy = fade_disp_map
  155.     mask[4..6] = (mask[1][0]..mask[1][2]),(mask[1][1]..mask[1][3]),@fade_time
  156.     mask[7..11] = s.bitmap.dup,0,0,@fade_oxy[0],@fade_oxy[1]
  157.     mask[0].opacity =  mask[4] === mask[3][0] && mask[5] === mask[3][1] &&
  158.         (mask[2].empty? || !((mask[2][0]..mask[2][2]) === mask[3][0]&&
  159.         (mask[2][1]..mask[2][3]) === mask[3][1])) ? 0 : 255
  160.     (@fade_sprite ||= []) << mask
  161.   end
  162.   #--------------------------------------------------------------------------
  163.   # * Frame Update
  164.   #--------------------------------------------------------------------------
  165.   def update
  166.     $game_system.init_fade_room if $game_system.fade_room.nil?
  167.     ($game_system.fade_room[$game_map.map_id] || '') != @fade_effect ||
  168.     @fade_sprite.nil? ? init_fadesprite : @fade_sprite.each {|mask|
  169.       mask[0].ox, mask[0].oy = fade_disp_map
  170.       if mask[1] != [-1]*4
  171.         a = fade_coord_player
  172.         a = [mask[4] === a[0] && mask[5] === a[1] &&
  173.             (mask[2].empty? || !((mask[2][0]..mask[2][2]) === a[0] &&
  174.             (mask[2][1]..mask[2][3]) === a[1])), mask[0].opacity,mask[6]].flatten
  175.         mask[0].opacity = a[0] ? [a[1] - a[3],0].max : [a[1] + a[2], 255].min
  176.       end
  177.       next if mask[10] == 0 && mask[11] == 0
  178.       w,h = mask[0].bitmap.width, mask[0].bitmap.height
  179.       mask[0].bitmap.blt(mask[8],mask[9], mask[7],  mask[7].rect )
  180.       mask[0].bitmap.blt(mask[8],h+mask[9],  mask[7],  mask[7].rect )
  181.       mask[0].bitmap.blt(mask[8],-h+mask[9],  mask[7],  mask[7].rect )
  182.       mask[0].bitmap.blt(w+mask[8],mask[9],  mask[7],  mask[7].rect )
  183.       mask[0].bitmap.blt(-w+mask[8],mask[9],  mask[7],  mask[7].rect )
  184.       mask[0].bitmap.blt(w+mask[8],h+mask[9],  mask[7],  mask[7].rect )
  185.       mask[0].bitmap.blt(w+mask[8],-h+mask[9],  mask[7],  mask[7].rect )
  186.       mask[0].bitmap.blt(-w+mask[8],h+mask[9],  mask[7],  mask[7].rect )
  187.       mask[0].bitmap.blt(-w+mask[8],-h+mask[9],  mask[7],  mask[7].rect )
  188.       mask[8] = mask[8] >= w || mask[8] <= -w ? 0 : mask[8] + mask[10]
  189.       mask[9] = mask[9] >= h || mask[9] <= -h ? 0 : mask[9] + mask[11]      }
  190.     drg132_update  
  191.   end
  192.   #--------------------------------------------------------------------------
  193.   # * Frame Dispose
  194.   #--------------------------------------------------------------------------
  195.   def dispose
  196.     (@fade_sprite || []).each {|mask| mask[0].dispose }
  197.     drg132_dispos  
  198.   end
  199.   #--------------------------------------------------------------------------
  200.   # * Fade Coord Player
  201.   #--------------------------------------------------------------------------
  202.   def fade_coord_player
  203.     return [$game_player.real_x/128, $game_player.real_y/128] if RGSS == 1
  204.     return [$game_player.x, $game_player.y]
  205.   end
  206.   #--------------------------------------------------------------------------
  207.   # * Fade Display Map
  208.   #--------------------------------------------------------------------------
  209.   def fade_disp_map
  210.     return [$game_map.display_x/4, $game_map.display_y/4]    if RGSS == 1
  211.     return [$game_map.display_x/8, $game_map.display_y/8]    if RGSS == 2
  212.     return [$game_map.display_x*32, $game_map.display_y*32]  if RGSS == 3
  213.   end
  214. end
  215.  
  216. #==============================================================================
  217. # ** Game_System
  218. #------------------------------------------------------------------------------
  219. #  This class handles data surrounding the system.
  220. #  Refer to "$game_system" for the instance of this class.
  221. #==============================================================================
  222. class Game_System
  223.   #--------------------------------------------------------------------------
  224.   # * init_fade_room
  225.   #--------------------------------------------------------------------------
  226.   def init_fade_room() @fade_room = LiTTleDRAgo::FADE_ROOM end
  227.   #--------------------------------------------------------------------------
  228.   # * Public Instance Variables
  229.   #--------------------------------------------------------------------------
  230.   attr_accessor :fade_room
  231. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement