Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
- # ** Lightweight Map Highlights
- # Author: Eshra
- # First Verison: 4 Oct. 2012
- # Compatibility: RPG Maker VX Ace
- #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
- #
- # Use this to draw bitmaps representing highlights on the current map.
- # Supports Drawing from sprite sheets. Much lighter than using
- # Sprite_Characters but the functionality is greatly reduced.
- #
- # Was made to support generating a large amount of highlights on a
- # large map, otherwise it's probably better to just use a Game_Character.
- #
- #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
- #
- #------------------------------------------------------------------------------
- # * How to Use
- #------------------------------------------------------------------------------
- # Highlights can now be placed on the game_map from inside of Scene_Map.
- #
- # use: @spriteset.add_highlight_tb(x,y)
- # to place a highlight on the map at position x,y
- #
- # use: @spriteset.add_highlight_tb(x,y,true)
- # to place a persistent highlight on the map which will be redrawn each time
- # the map is reloaded.
- #
- # Persistant highlights can be removed with:
- # remove_saved_highlight(x,y,map_id)
- # param: map_id is optional (uses $game_map.map_id if not passed)
- #
- # Sprite sheets can be used as well, see the Sheet_Data class below.
- #
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- #
- # Original Release Date: 7 Nov. 2012
- #
- # Upate Log
- # 10/30/12 - Reorganized updating to match the default engine's organization.
- # That is, $game_map stores the information about the highlights
- # and scene_map refreshes the highlights when @map_id != game_map.map_id
- # 10/29/12 - Version 0.1 finished, added support for persistant highlights
- # 10/4/12 - First Version finished
- #
- #------------------------------------------------------------------------------
- ($imported||={})["(~^_^)~Highlights"] = 0.1
- #==============================================================================
- # * Spriteset_Map
- #==============================================================================
- class Spriteset_Map
- attr_accessor :viewport1
- #----------------------------------------------------------------------------
- # * Initialize
- #----------------------------------------------------------------------------
- alias initialize_highlights_tb initialize
- def initialize
- initialize_highlights_tb
- create_highlights_tb
- end
- #----------------------------------------------------------------------------
- # * Create Highlights
- #----------------------------------------------------------------------------
- def create_highlights_tb
- @path_hightlights_tb = []
- make_hls_visible_for_map
- end
- def make_hls_visible_for_map
- gh = $game_map.map_highlights_era
- gh.keys.each{|xy|
- add_highlight_tb(xy[0],xy[1], gh[xy])
- }
- end
- #----------------------------------------------------------------------------
- # * Alias Update
- #----------------------------------------------------------------------------
- alias highlights_spritesetmap_update_tb update
- def update
- update_highlights
- highlights_spritesetmap_update_tb
- end
- #----------------------------------------------------------------------------
- # * Update Highlights
- #----------------------------------------------------------------------------
- def update_highlights
- refresh_highlights if @map_id != $game_map.map_id
- return if @path_hightlights_tb.nil?
- @path_hightlights_tb.each{ |sprite| sprite.update }
- end
- #----------------------------------------------------------------------------
- # * Add a highlight
- #----------------------------------------------------------------------------
- def add_highlight_tb(x,y,sh = nil, retain = false)
- hls = (@path_hightlights_tb||=[])
- if sh.nil?
- hls.push(s=Sprite_Highlight.new(@viewport1).place(x,y))
- else
- hls.push(s=Sprite_Highlight.new(@viewport1, :hl_sheet, sh).place(x,y))
- end
- $game_system.save_highlight(x,y,sh) if retain
- end
- #----------------------------------------------------------------------------
- # * Remove Highlights
- #----------------------------------------------------------------------------
- def dispose_highlights_tb
- @path_hightlights_tb.each{|sp| sp.dispose}
- @path_hightlights_tb = []
- end
- #----------------------------------------------------------------------------
- # * Alias Dispose
- #----------------------------------------------------------------------------
- alias sp_highl_spmap_mod_dispose dispose
- def dispose
- sp_highl_spmap_mod_dispose
- dispose_highlights_tb
- end
- def refresh_highlights
- dispose_highlights_tb
- create_highlights_tb
- end
- end # Spriteset_Map
- #==============================================================================
- # * Sprite_Highlight_Base
- #------------------------------------------------------------------------------
- # Provies methods for drawing a highlight on the screen.
- #==============================================================================
- class Sprite_Highlight_Base < Sprite_Base
- #----------------------------------------------------------------------------
- # * Initialize
- #----------------------------------------------------------------------------
- def initialize(viewport)
- super(viewport)
- @disp_x, @disp_y = 0, 0
- @val = 1 # Used lazily as a variable for manipulating the bitmap
- # Add creation and update methods to these hashes for custom highlights:
- # The hash maps symbols => method names
- @create_methods = {
- :default_move => :create_default_move_highlight,
- :default_attack => :create_default_attack_highlight,
- :hl_sheet => :create_bitmap_from_sprite_sheet
- }
- @update_methods = {
- :default_move => :update_default,
- :default_attack => :update_default,
- :hl_sheet => :update_sprite_from_sheet
- }
- end
- #--------------------------------------------------------------------------
- # * Create Bitmap
- #--------------------------------------------------------------------------
- def create_highlight_bitmap
- method(@create_methods[@method_symbol]).call
- end
- #---------------------------------------------------------------------------
- # * Create Default Movement Highlight
- #---------------------------------------------------------------------------
- def create_default_move_highlight
- self.bitmap = Bitmap.new(32, 32)
- self.bitmap.fill_rect(0,0,32,32,Color.new(23,52,123))
- @default = true
- end
- #---------------------------------------------------------------------------
- # * Create Default Attack Highlight
- #---------------------------------------------------------------------------
- def create_default_attack_highlight
- self.bitmap = Bitmap.new(32, 32)
- self.bitmap.fill_rect(0,0,32,32,Color.new(231,52,23))
- @default = true
- end
- #---------------------------------------------------------------------------
- # * Create Bitmap from Spritesheet
- #---------------------------------------------------------------------------
- def create_bitmap_from_sprite_sheet
- self.bitmap = Cache.character(@sheet_data.name)
- sign = @sheet_data.name[/^[\!\$]./]
- if sign && sign.include?('$')
- @cw = bitmap.width / 3
- @ch = bitmap.height / 4
- else
- @cw = bitmap.width / 12
- @ch = bitmap.height / 8
- end
- @default = false
- end
- #---------------------------------------------------------------------------
- # * Update Sprite From Sheet
- #---------------------------------------------------------------------------
- def update_sprite_from_sheet
- if @sheet_data.pattern == (@sheet_data.frequency*3)-1
- @val = -1
- @sheet_data.pattern = @sheet_data.frequency*2
- elsif @sheet_data.pattern == 0
- @val = 1
- @sheet_data.pattern = @sheet_data.frequency-1
- end
- @sheet_data.pattern += @val
- sx = (@sheet_data.index % 4 * 3 + @sheet_data.pattern/@sheet_data.frequency) * @cw
- sy = (@sheet_data.index/4 * 4 + @sheet_data.row)*@ch
- self.src_rect.set(sx, sy, @cw, @ch)
- end
- #---------------------------------------------------------------------------
- # * Place
- #---------------------------------------------------------------------------
- def place(x,y)
- self.ox = 0
- self.oy = 0
- @orx = x
- @ory = y
- self.x = (x - (@disp_x = $game_map.display_x)) * 32
- self.y = (y - (@disp_y = $game_map.display_y)) * 32
- self
- end
- #---------------------------------------------------------------------------
- # * Update
- #---------------------------------------------------------------------------
- def update
- return unless self.bitmap
- super
- if @disp_x != $game_map.display_x || @disp_y != $game_map.display_y
- update_position
- end
- method(@update_methods[@method_symbol]).call
- end
- #---------------------------------------------------------------------------
- # * Update Default
- #---------------------------------------------------------------------------
- def update_default
- @val = self.opacity < 40 ? 3 : (self.opacity > 160 ? -3 : @val)
- self.opacity += @val
- end # update_default
- #---------------------------------------------------------------------------
- # * Update Position
- #---------------------------------------------------------------------------
- def update_position
- return unless @orx && @ory
- self.x = $game_map.adjust_x(@orx) * 32
- self.y = $game_map.adjust_y(@ory) * 32
- self.z = 10
- @disp_x = $game_map.display_x
- @disp_y = $game_map.display_y
- end
- end # Sprite_Highlight_Base
- #==============================================================================
- # * Sprite_Highlight
- #------------------------------------------------------------------------------
- # Extra parameters are added in this subclass to prevent crowding the base
- # initialize method to support extendibility.
- #==============================================================================
- class Sprite_Highlight < Sprite_Highlight_Base
- def initialize(viewport, method_sym = :default_move, sheet_data = nil)
- super(viewport)
- @sheet_data = sheet_data
- @method_symbol = method_sym
- create_highlight_bitmap
- update
- end
- end # Sprite_Highlight
- #==============================================================================
- # * Sprite Sheet Data for Custom Highlights
- #------------------------------------------------------------------------------
- # Create a new instance of this class and call the setup method on it. Then
- # pass that instance to a Sprite_Highlight to use it as a sprite sheet.
- # use :hl_sheet as the method symbol. This provides the default method
- # sprite sheet animation method for highlights.
- #==============================================================================
- class Sheet_Data
- attr_reader :index # Index of the sprites to use
- attr_reader :row # Animation row that should be used
- attr_reader :name # Name of the spritesheet
- attr_accessor :pattern # Used to calculate which part of spritesheet to show
- attr_accessor :frequency # Number of frames before modifying bitmap
- #---------------------------------------------------------------------------
- # * Initialize
- #---------------------------------------------------------------------------
- def initialize(name, index, row, freq = 18, pattern = 1)
- @index = index
- @row = row
- @name = name
- @pattern = pattern
- @frequency = freq
- self
- end
- end # Sheet_Data
- #==============================================================================
- # ** Game_System
- #==============================================================================
- class Game_System
- attr_reader :highlights_drawn
- attr_reader :game_highlights
- def save_highlight(x,y,sh)
- ((@game_highlights||={})[$game_map.map_id]||={})[[x,y]] = sh
- end
- def remove_saved_highlight(x,y, map_id = nil)
- map_id = !map_id ? $game_map.map_id : map_id
- ((@game_highlights||={})[map_id]||={}).delete([x,y])
- end
- def curr_highlights(map_id)
- (@game_highlights||={})[map_id]||={}
- end
- end
- #==============================================================================
- # ** Game_Map
- #==============================================================================
- class Game_Map
- attr_accessor :need_to_remove_highlights
- attr_accessor :map_highlights_era
- alias setup_add_support_rem_hl_when_ch setup
- def setup(map_id)
- setup_add_support_rem_hl_when_ch(map_id)
- setup_map_highlights_era(map_id)
- end
- def setup_map_highlights_era(map_id)
- @map_highlights_era = $game_system.curr_highlights(map_id)
- end
- end # Game_Map
- #End of File
Advertisement
Add Comment
Please, Sign In to add comment