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,nil,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.
- #
- # To organize the highlights on the map, use param: hloc.
- # this should be a symbol. The highlights added to the spriteset map will be
- # grouped according to symbol (in a hash). This makes it easier to remove
- # specific groups of highlights.
- # See method header below.
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- #
- # Original Release Date: 7 Nov. 2012
- #
- # Upate Log
- # 8 Mar. 2013 - Can now remove saved highlights based on symbol.
- # $game_system.remove_highlight(x,y,map_id = nil, sym = :def)
- #
- # 7 Dec. 2012 - Added nil check to remove_highlight
- # 5 Dec. 2012 - save_highlight inside Game_System now saves all of the optional
- # data about each retained highlight instead of just the sprite sheet.
- #
- # 3 Dec. 2012 - Added helper method for adding highlights which takes in an options
- # hash. The method is called add_highlight.
- # add_highlights can take options:
- # :x => x placement location
- # :y => y placement location
- # :sh => sprite sheet
- # :retain => true or false, is the highlight remembered after leaving map and returning?
- # :hloc => "hash location", can be used to group certain highlights
- # together
- # :sym => symbol of the method used to create and update the highlight
- # :opacity => the opacity value the highlight will be placed on the map with.
- #
- # v0.2 Updates 11/17, 11/18:
- # 18 Nov. 2012 - Highlights can now be organized: header for add_highlight_tb changed:
- # ...(x,y,sh = nil, retain = false, sym = :default_move, hloc = :def)
- # param: hloc should is the symbol defining where the highlight will
- # be added.
- # This makes it easier to remove specific highlights. Just pass in
- # the same symbol to the remove method to remove specific highlights.
- # 17 Nov. 2012 - Support for removing highlights. Iterates through an array so slow.
- # changed @path_hightlights_tb to @path_highlights_tb
- # Can force all default highlights to fade at same rate.
- # 30 Oct. 2012 - 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
- # 29 Oct. 2012 - Version 0.1 finished, added support for persistant highlights
- # 4 Oct. 2012 - First Version finished
- #
- #------------------------------------------------------------------------------
- ($imported||={})["Ra Highlights"] = 0.3
- #==============================================================================
- # ** Era Module
- #==============================================================================
- module Era
- module Fade
- Max = 160 # Largest opacity for default highlights
- Min = 52 # Smallest opacity for default highlights
- Rate = 3 # Rate Opacity changes each update
- SameRate = true # All default highlights flicker at the same rate
- @@opacity = 160 # Current opacity
- #--------------------------------------------------------------------------
- # * Get Opacity
- #--------------------------------------------------------------------------
- def self.opacity
- @@opacity
- end
- #--------------------------------------------------------------------------
- # * Setter
- #--------------------------------------------------------------------------
- def self.opacity=(val)
- @@opacity = val
- end
- end # Fade
- end # Era
- #==============================================================================
- # * 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_highlights_tb = {}
- @hl_mod = -Era::Fade::Rate
- make_hls_visible_for_map
- end
- #----------------------------------------------------------------------------
- # * make_hls_visible_for_map
- #----------------------------------------------------------------------------
- def make_hls_visible_for_map
- gh = $game_map.map_highlights_era
- gh.keys.each do |sym|
- gh[sym].keys.each do |xy|
- add_highlight(gh[sym][xy])
- end
- end
- 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_highlights_tb.nil?
- @path_highlights_tb.values.each do |arr|
- arr.each{ |sp| sp.update }
- end
- update_hl_global
- end
- #----------------------------------------------------------------------------
- # * Update global highlight opacity value
- #----------------------------------------------------------------------------
- def update_hl_global
- min, max, rate = Era::Fade::Min, Era::Fade::Max, Era::Fade::Rate
- opacity = Era::Fade.opacity
- @hl_mod = opacity <= min ? rate : (opacity >= max ? -rate : @hl_mod)
- Era::Fade.opacity = opacity + @hl_mod
- end
- #----------------------------------------------------------------------------
- # * Add a highlight
- #----------------------------------------------------------------------------
- def add_highlight_tb(x,y,sh = nil, retain = false, sym = :default_move, hloc = :def, opacity = Era::Fade.opacity)
- hls = (@path_highlights_tb||={})
- hls[hloc] ||= []
- print "adding a highlights at (#{x},#{y})\n"
- if sh.nil?
- hls[hloc].push(s=Sprite_Highlight.new(@viewport1, sym).place(x,y, opacity))
- else
- hls[hloc].push(s=Sprite_Highlight.new(@viewport1, :hl_sheet, sh).place(x,y, opacity))
- end
- end
- def add_highlight(opts = {})
- options = {
- :x => 0, :y => 0, :sh => nil, :retain => false, :sym => :default_move,
- :hloc => :def, :opacity => Era::Fade.opacity
- }.merge(opts)
- add_highlight_tb(options[:x], options[:y], options[:sh], options[:retain],
- options[:sym], options[:hloc], options[:opacity])
- $game_system.save_highlight(options[:x], options[:y], options) if options[:retain]
- end
- #----------------------------------------------------------------------------
- # * Remove Highlights
- #----------------------------------------------------------------------------
- def dispose_highlights_tb
- @path_highlights_tb.values.each do |arr|
- arr.each{ |sp| sp.dispose }
- end
- @path_highlights_tb = {}
- end
- #----------------------------------------------------------------------------
- # * Alias Dispose
- #----------------------------------------------------------------------------
- alias sp_highl_spmap_mod_dispose dispose
- def dispose
- sp_highl_spmap_mod_dispose
- dispose_highlights_tb
- end
- #----------------------------------------------------------------------------
- # * Refresh Highlights
- #----------------------------------------------------------------------------
- def refresh_highlights
- dispose_highlights_tb
- create_highlights_tb
- end
- #----------------------------------------------------------------------------
- # * Removes highlight at pos x,y in the group specified by sym.
- #----------------------------------------------------------------------------
- def remove_highlight(x,y, sym = :def)
- sz, i = (@path_highlights_tb[sym]||=[]).size, 1
- @path_highlights_tb[sym].reverse_each do |hl|
- if hl.sx == x && hl.sy == y
- del = @path_highlights_tb[sym].delete_at(sz - i)
- hl.dispose
- break
- end # end if
- i+=1
- end
- end
- #----------------------------------------------------------------------------
- # * Get a group of highlights
- #----------------------------------------------------------------------------
- def get_hl_group(sym)
- return if @path_highlights_tb.nil?
- @path_highlights_tb[sym]
- end
- end # Spriteset_Map
- #==============================================================================
- # * Sprite_Highlight_Base
- #------------------------------------------------------------------------------
- # Provies methods for drawing a highlight on the screen.
- #==============================================================================
- class Sprite_Highlight_Base < Sprite_Base
- attr_reader :sx, :sy
- #----------------------------------------------------------------------------
- # * 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,
- :attack_2 => :create_attack_highlight2
- }
- @update_methods = {
- :default_move => :update_default,
- :default_attack => :update_default,
- :attack_2 => :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))
- color_border(Color.new(28,91,145))
- @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(250,82,77))
- color_border(Color.new(255,216,107))
- @default = true
- end
- #---------------------------------------------------------------------------
- # * Create Attack Highlight2
- #---------------------------------------------------------------------------
- def create_attack_highlight2
- self.bitmap = Bitmap.new(32, 32)
- self.bitmap.fill_rect(0,0,32,32,Color.new(250,193,18))
- color_border(Color.new(255,246,77))
- @default = true
- end
- def color_border(color)
- self.bitmap.fill_rect(0,0,1,32, color)
- self.bitmap.fill_rect(0,0,32,1, color)
- self.bitmap.fill_rect(31,0,32,32, color)
- self.bitmap.fill_rect(0,31,32,32, color)
- 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, opacity = Era::Fade.opacity)
- @sx, @sy = 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.opacity = opacity
- self
- end
- #---------------------------------------------------------------------------
- # * Update
- #---------------------------------------------------------------------------
- def update
- return unless !self.bitmap.nil?
- 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
- if Era::Fade::SameRate
- self.opacity = Era::Fade.opacity
- else
- @val = self.opacity < Era::Fade::Min ? Era::Fade::Rate : (self.opacity > Era::Fade::Max ? -Era::Fade::Rate : @val)
- self.opacity += @val
- end
- 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
- #---------------------------------------------------------------------------
- # * Clear contents of highlight
- #---------------------------------------------------------------------------
- def clear
- self.bitmap.clear
- 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, opts = {})
- sv_highls = ((@game_highlights||={})[$game_map.map_id]||={})
- (sv_highls[opts[:sym]] ||= {})[[x,y]] = opts
- end
- def remove_saved_highlight(x,y, map_id = nil, sym = :def)
- map_id = !map_id ? $game_map.map_id : map_id
- game_highls = (@game_highlights||={})[map_id]||={}
- saved_opts = game_highls[[x,y]]
- (game_highls[sym]||={}).delete([x,y])
- # game_highls.delete([x,y]) if saved_opts && saved_opts[:sym] == sym
- # if saved_opts
- 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