Advertisement
ForeverZer0

[RMXP] Map Shaded Areas

Jun 26th, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.64 KB | None | 0 0
  1. #+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  2. # Map Shaded Areas
  3. # Author: ForeverZer0
  4. # Version: 1.0
  5. # Date: 6.26.2012
  6. #+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  7. #
  8. # Introduction:
  9. #   This is  simple script to display "shaded" areas on the map. Characters that
  10. #   pass underneath will also be shaded.
  11. #
  12. # Instructions:
  13. #   - See configuration below for detailed instructions on setup
  14. #   - Use "$game_map.shade_color = COLOR" to change the color of the shade.
  15. #     COLOR should be instance of a Color object.
  16. #
  17. #     Example:
  18. #     $game_map.shade_color = Color.new(0, 0, 80, 60) # Red, Green, Blue, Alpha
  19. #
  20. #+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  21.  
  22. #===============================================================================
  23. # ** Shade
  24. #-------------------------------------------------------------------------------
  25. # Class that represents a shaded area on the map.
  26. #===============================================================================
  27.  
  28. class Shade < RPG::Sprite
  29.   #-----------------------------------------------------------------------------
  30.   # * Configuration
  31.   #
  32.   # For each map that you want to display a shaded area in, add a new "when"
  33.   # statement with the ID of the map as the condition. Within that block, create
  34.   # an array of Rect objects as in the example below. These Rects represent the
  35.   # display coordinates of the shaded areas:
  36.   #
  37.   #   ex.
  38.   #     when 45 # Map with ID:45
  39.   #       [Rect.new(3, 7, 10, 4), Rect.new(18, 34, 2, 1)]
  40.   #
  41.   # This will display two shaded areas in map 45. The first area will be as map
  42.   # coordinate (3, 7), and will be 10 tiles wide, and 4 tiles high. The second
  43.   # area will be at map coordinate (19, 34), and will be 2 tiles high, and only
  44.   # one tile wide.
  45.   #
  46.   #   Rect.new(MAP_X, MAP_Y, TILE_WIDTH, TILE_HEIGHT)
  47.   #
  48.   # You can add as many areas you like to each map, or none at all. If you you
  49.   # run out of space and need to break to a new line while configuring, do so
  50.   # after a commma to prevent errors. Make sure that each block for the maps
  51.   # begins and ends with an opening and closing bracket: [ ].
  52.   #-----------------------------------------------------------------------------
  53.   def self.areas(map_id)
  54.     return case map_id
  55.     when 1
  56.       [Rect.new(2, 2, 4, 6), Rect.new(5, 5, 3, 3)]
  57.     when 2
  58.       []
  59.     when 3
  60.       []
  61.     else
  62.       []
  63.     end
  64.   end
  65.   #-----------------------------------------------------------------------------
  66.   # * Public Instance Variables
  67.   #-----------------------------------------------------------------------------
  68.   attr_reader :rect
  69.   #-----------------------------------------------------------------------------
  70.   # * Object Initialization
  71.   #-----------------------------------------------------------------------------
  72.   def initialize(viewport = nil, rect = nil, color = nil)
  73.     super(viewport)
  74.     @color = color
  75.     self.rect = rect
  76.   end
  77.   #-----------------------------------------------------------------------------
  78.   # * Set Rectangle
  79.   #   rectangle : The display rectangle. X and Y values are map coordinates and
  80.   #               width and height values are the number of tiles it covers.
  81.   #-----------------------------------------------------------------------------
  82.   def rect=(rectangle)
  83.     @rect = rectangle
  84.     if self.bitmap != nil && !self.bitmap.disposed?
  85.       self.bitmap = self.bitmap.dispose
  86.     end
  87.     self.x = @rect.x * 32
  88.     self.y = @rect.y * 32
  89.     self.bitmap = Bitmap.new(@rect.width * 32, @rect.height * 32)
  90.     unless @color.nil?
  91.       self.bitmap.fill_rect(self.bitmap.rect, @color)
  92.     end
  93.   end
  94.   #-----------------------------------------------------------------------------
  95.   # * Set Display Area
  96.   #   x      : Map x-coordinate the shade is displayed at
  97.   #   y      : Map y-coordinate the shade is displayed at
  98.   #   width  : Width in tiles the shade covers on the horizontal axis
  99.   #   height : Height in tiles the shade covers on the vertical axis
  100.   #-----------------------------------------------------------------------------
  101.   def area=(x, y, width, height)
  102.     self.rect = Rect.new(x, y, width, height)
  103.   end
  104.  
  105.   def color=(color)
  106.     if @color != color
  107.     @color = color
  108.     self.bitmap.fill_rect(self.bitmap.rect, @color)
  109.     end
  110.   end
  111. end
  112.  
  113. #===============================================================================
  114. # ** Spriteset_Map
  115. #===============================================================================
  116.  
  117. class Spriteset_Map
  118.   #-----------------------------------------------------------------------------
  119.   # * Object Initialization (alias)
  120.   #-----------------------------------------------------------------------------
  121.   alias shade_sprite_init initialize
  122.   def initialize
  123.     @shade_sprites = []
  124.     shade_sprite_init
  125.     areas = Shade.areas($game_map.map_id)
  126.     areas.each {|rect|
  127.       shade = Shade.new(@viewport2, rect, $game_map.shade_color)
  128.       shade.ox = $game_map.display_x / 4
  129.       shade.oy = $game_map.display_y / 4
  130.       @shade_sprites.push(shade)
  131.     }
  132.   end
  133.   #-----------------------------------------------------------------------------
  134.   # * Frame Update (alias)
  135.   #-----------------------------------------------------------------------------
  136.   alias shade_sprite_update update
  137.   def update
  138.     shade_sprite_update
  139.     _ox = $game_map.display_x / 4
  140.     _oy = $game_map.display_y / 4
  141.     @shade_sprites.each {|shade|
  142.       shade.color = $game_map.shade_color
  143.       shade.ox, shade.oy = _ox, _oy
  144.     }
  145.   end
  146.   #-----------------------------------------------------------------------------
  147.   # * Dispose Sprites (alias)
  148.   #-----------------------------------------------------------------------------
  149.   alias shade_sprite_dispose dispose
  150.   def dispose
  151.     shade_sprite_dispose
  152.     @shade_sprites.each {|shade| shade.dispose }
  153.   end
  154. end
  155.  
  156. #===============================================================================
  157. # ** Game_Map
  158. #===============================================================================
  159.  
  160. class Game_Map
  161.   #-----------------------------------------------------------------------------
  162.   # * Public Instance Variables
  163.   #-----------------------------------------------------------------------------
  164.   attr_accessor :shade_color
  165.   #-----------------------------------------------------------------------------
  166.   # * Object Initialization
  167.   #-----------------------------------------------------------------------------
  168.   alias shade_color_init initialize
  169.   def initialize
  170.     @shade_color = Color.new(0, 0, 0, 60)
  171.     shade_color_init
  172.   end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement