blucalm

Tile Highlights 3913

Mar 9th, 2013
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.98 KB | None | 0 0
  1. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  2. # ** Lightweight Map Highlights
  3. #    Author: Eshra
  4. #    First Verison: 4 Oct. 2012
  5. #    Compatibility: RPG Maker VX Ace
  6. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  7. #
  8. #    Use this to draw bitmaps representing highlights on the current map.
  9. #    Supports Drawing from sprite sheets. Much lighter than using
  10. #    Sprite_Characters but the functionality is greatly reduced.
  11. #
  12. #    Was made to support generating a large amount of highlights on a
  13. #    large map, otherwise it's probably better to just use a Game_Character.
  14. #
  15. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  16. #
  17. #------------------------------------------------------------------------------
  18. # * How to Use
  19. #------------------------------------------------------------------------------
  20. #   Highlights can now be placed on the game_map from inside of Scene_Map.
  21. #
  22. #   use: @spriteset.add_highlight_tb(x,y)
  23. #   to place a highlight on the map at position x,y
  24. #
  25. #   use: @spriteset.add_highlight_tb(x,y,nil,true)
  26. #   to place a persistent highlight on the map which will be redrawn each time
  27. #   the map is reloaded.
  28. #
  29. #   Persistant highlights can be removed with:
  30. #     remove_saved_highlight(x,y,map_id)
  31. #     param: map_id is optional (uses $game_map.map_id if not passed)
  32. #
  33. #   Sprite sheets can be used as well, see the Sheet_Data class below.
  34. #  
  35. #   To organize the highlights on the map, use param: hloc.
  36. #   this should be a symbol. The highlights added to the spriteset map will be
  37. #   grouped according to symbol (in a hash). This makes it easier to remove
  38. #   specific groups of highlights.
  39. #   See method header below.
  40. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  41. #
  42. # Original Release Date: 7 Nov. 2012
  43. #
  44. # Upate Log
  45. # 8 Mar. 2013 - Can now remove saved highlights based on symbol.
  46. #               $game_system.remove_highlight(x,y,map_id = nil, sym = :def)
  47. #
  48. # 7 Dec. 2012 - Added nil check to remove_highlight
  49. # 5 Dec. 2012 - save_highlight inside Game_System now saves all of the optional
  50. #               data about each retained highlight instead of just the sprite sheet.
  51. #
  52. # 3 Dec. 2012 - Added helper method for adding highlights which takes in an options
  53. #              hash. The method is called add_highlight.
  54. #              add_highlights can take options:
  55. #             :x => x placement location
  56. #             :y => y placement location
  57. #             :sh => sprite sheet
  58. #             :retain => true or false, is the highlight remembered after leaving map and returning?
  59. #             :hloc => "hash location", can be used to group certain highlights
  60. #                       together
  61. #             :sym => symbol of the method used to create and update the highlight
  62. #             :opacity => the opacity value the highlight will be placed on the map with.
  63. #
  64. # v0.2 Updates 11/17, 11/18:
  65. # 18 Nov. 2012 - Highlights can now be organized: header for add_highlight_tb changed:
  66. #             ...(x,y,sh = nil, retain = false, sym = :default_move, hloc = :def)
  67. #             param: hloc should is the symbol defining where the highlight will
  68. #             be added.
  69. #             This makes it easier to remove specific highlights. Just pass in
  70. #             the same symbol to the remove method to remove specific highlights.
  71. # 17 Nov. 2012 - Support for removing highlights. Iterates through an array so slow.
  72. #            changed @path_hightlights_tb to @path_highlights_tb
  73. #            Can force all default highlights to fade at same rate.
  74. # 30 Oct. 2012 - Reorganized updating to match the default engine's organization.
  75. #            That is, $game_map stores the information about the highlights
  76. #            and scene_map refreshes the highlights when @map_id != game_map.map_id
  77. # 29 Oct. 2012 - Version 0.1 finished, added support for persistant highlights
  78. # 4 Oct. 2012 - First Version finished
  79. #
  80. #------------------------------------------------------------------------------
  81.  
  82. ($imported||={})["Ra Highlights"] = 0.3
  83. #==============================================================================
  84. # ** Era Module
  85. #==============================================================================
  86. module Era
  87.  
  88.   module Fade
  89.    
  90.     Max = 160             # Largest opacity for default highlights
  91.     Min = 52              # Smallest opacity for default highlights
  92.     Rate = 3              # Rate Opacity changes each update
  93.     SameRate = true       # All default highlights flicker at the same rate
  94.     @@opacity = 160       # Current opacity
  95.     #--------------------------------------------------------------------------
  96.     # * Get Opacity
  97.     #--------------------------------------------------------------------------
  98.     def self.opacity
  99.       @@opacity
  100.     end
  101.     #--------------------------------------------------------------------------
  102.     # * Setter
  103.     #--------------------------------------------------------------------------
  104.     def self.opacity=(val)
  105.       @@opacity = val
  106.     end
  107.  
  108.   end # Fade
  109.  
  110. end # Era
  111. #==============================================================================
  112. # * Spriteset_Map
  113. #==============================================================================
  114. class Spriteset_Map
  115.   attr_accessor :viewport1
  116.   #----------------------------------------------------------------------------
  117.   # * Initialize
  118.   #----------------------------------------------------------------------------
  119.   alias initialize_highlights_tb initialize
  120.   def initialize
  121.     initialize_highlights_tb
  122.     create_highlights_tb
  123.   end
  124.   #----------------------------------------------------------------------------
  125.   # * Create Highlights
  126.   #----------------------------------------------------------------------------
  127.   def create_highlights_tb
  128.     @path_highlights_tb = {}
  129.     @hl_mod = -Era::Fade::Rate
  130.     make_hls_visible_for_map
  131.   end
  132.   #----------------------------------------------------------------------------
  133.   # * make_hls_visible_for_map
  134.   #----------------------------------------------------------------------------
  135.   def make_hls_visible_for_map
  136.     gh = $game_map.map_highlights_era
  137.     gh.keys.each do |sym|
  138.       gh[sym].keys.each do |xy|
  139.         add_highlight(gh[sym][xy])
  140.       end
  141.     end
  142.   end
  143.   #----------------------------------------------------------------------------
  144.   # * Alias Update
  145.   #----------------------------------------------------------------------------
  146.   alias highlights_spritesetmap_update_tb update
  147.   def update
  148.     update_highlights
  149.     highlights_spritesetmap_update_tb
  150.   end
  151.   #----------------------------------------------------------------------------
  152.   # * Update Highlights
  153.   #----------------------------------------------------------------------------
  154.   def update_highlights
  155.     refresh_highlights if @map_id != $game_map.map_id
  156.     return if @path_highlights_tb.nil?
  157.     @path_highlights_tb.values.each do |arr|
  158.       arr.each{ |sp| sp.update }
  159.     end
  160.     update_hl_global
  161.   end
  162.   #----------------------------------------------------------------------------
  163.   # * Update global highlight opacity value
  164.   #----------------------------------------------------------------------------
  165.   def update_hl_global
  166.     min, max, rate = Era::Fade::Min, Era::Fade::Max, Era::Fade::Rate
  167.     opacity = Era::Fade.opacity
  168.     @hl_mod = opacity <= min ? rate : (opacity >= max ? -rate : @hl_mod)
  169.     Era::Fade.opacity = opacity + @hl_mod
  170.   end
  171.   #----------------------------------------------------------------------------
  172.   # * Add a highlight
  173.   #----------------------------------------------------------------------------
  174.   def add_highlight_tb(x,y,sh = nil, retain = false, sym = :default_move, hloc = :def, opacity = Era::Fade.opacity)
  175.     hls = (@path_highlights_tb||={})
  176.     hls[hloc] ||= []
  177.     print "adding a highlights at (#{x},#{y})\n"
  178.     if sh.nil?
  179.       hls[hloc].push(s=Sprite_Highlight.new(@viewport1, sym).place(x,y, opacity))
  180.     else
  181.       hls[hloc].push(s=Sprite_Highlight.new(@viewport1, :hl_sheet, sh).place(x,y, opacity))
  182.     end
  183.    
  184.   end
  185.  
  186.   def add_highlight(opts = {})
  187.     options = {
  188.       :x => 0, :y => 0, :sh => nil, :retain => false, :sym => :default_move,
  189.       :hloc => :def, :opacity => Era::Fade.opacity
  190.     }.merge(opts)
  191.     add_highlight_tb(options[:x], options[:y], options[:sh], options[:retain],
  192.       options[:sym], options[:hloc], options[:opacity])
  193.      
  194.     $game_system.save_highlight(options[:x], options[:y], options) if options[:retain]
  195.   end
  196.   #----------------------------------------------------------------------------
  197.   # * Remove Highlights
  198.   #----------------------------------------------------------------------------
  199.   def dispose_highlights_tb
  200.     @path_highlights_tb.values.each do |arr|
  201.       arr.each{ |sp| sp.dispose }
  202.     end
  203.     @path_highlights_tb = {}
  204.   end
  205.   #----------------------------------------------------------------------------
  206.   # * Alias Dispose
  207.   #----------------------------------------------------------------------------
  208.   alias sp_highl_spmap_mod_dispose dispose
  209.   def dispose
  210.     sp_highl_spmap_mod_dispose
  211.     dispose_highlights_tb
  212.   end
  213.   #----------------------------------------------------------------------------
  214.   # * Refresh Highlights
  215.   #----------------------------------------------------------------------------
  216.   def refresh_highlights
  217.     dispose_highlights_tb
  218.     create_highlights_tb
  219.   end
  220.   #----------------------------------------------------------------------------
  221.   # * Removes highlight at pos x,y in the group specified by sym.
  222.   #----------------------------------------------------------------------------
  223.   def remove_highlight(x,y, sym = :def)
  224.     sz, i = (@path_highlights_tb[sym]||=[]).size, 1
  225.     @path_highlights_tb[sym].reverse_each do |hl|
  226.      
  227.       if hl.sx == x && hl.sy == y
  228.         del = @path_highlights_tb[sym].delete_at(sz - i)
  229.         hl.dispose
  230.         break
  231.       end # end if
  232.      
  233.       i+=1
  234.     end
  235.   end
  236.   #----------------------------------------------------------------------------
  237.   # * Get a group of highlights
  238.   #----------------------------------------------------------------------------
  239.   def get_hl_group(sym)
  240.     return if @path_highlights_tb.nil?
  241.     @path_highlights_tb[sym]
  242.   end
  243. end # Spriteset_Map
  244.  
  245. #==============================================================================
  246. # * Sprite_Highlight_Base
  247. #------------------------------------------------------------------------------
  248. #   Provies methods for drawing a highlight on the screen.
  249. #==============================================================================
  250. class Sprite_Highlight_Base < Sprite_Base
  251.   attr_reader :sx, :sy
  252.   #----------------------------------------------------------------------------
  253.   # * Initialize
  254.   #----------------------------------------------------------------------------
  255.   def initialize(viewport)
  256.     super(viewport)
  257.     @disp_x, @disp_y = 0, 0
  258.     @val = 1 # Used lazily as a variable for manipulating the bitmap
  259.    
  260.     # Add creation and update methods to these hashes for custom highlights:
  261.     # The hash maps symbols => method names
  262.     @create_methods = {
  263.                         :default_move => :create_default_move_highlight,
  264.                         :default_attack => :create_default_attack_highlight,
  265.                         :hl_sheet => :create_bitmap_from_sprite_sheet,
  266.                         :attack_2 => :create_attack_highlight2
  267.                       }
  268.     @update_methods = {
  269.                         :default_move => :update_default,
  270.                         :default_attack => :update_default,
  271.                         :attack_2 => :update_default,
  272.                         :hl_sheet => :update_sprite_from_sheet
  273.                       }
  274.   end
  275.   #--------------------------------------------------------------------------
  276.   # * Create Bitmap
  277.   #--------------------------------------------------------------------------
  278.   def create_highlight_bitmap
  279.     method(@create_methods[@method_symbol]).call
  280.   end
  281.   #---------------------------------------------------------------------------
  282.   # * Create Default Movement Highlight
  283.   #---------------------------------------------------------------------------
  284.   def create_default_move_highlight
  285.     self.bitmap = Bitmap.new(32, 32)
  286.     self.bitmap.fill_rect(0,0,32,32,Color.new(23,52,123))
  287.     color_border(Color.new(28,91,145))
  288.     @default = true
  289.   end
  290.   #---------------------------------------------------------------------------
  291.   # * Create Default Attack Highlight
  292.   #---------------------------------------------------------------------------
  293.   def create_default_attack_highlight
  294.     self.bitmap = Bitmap.new(32, 32)
  295.     self.bitmap.fill_rect(0,0,32,32,Color.new(250,82,77))
  296.     color_border(Color.new(255,216,107))
  297.     @default = true
  298.   end
  299.   #---------------------------------------------------------------------------
  300.   # * Create Attack Highlight2
  301.   #---------------------------------------------------------------------------
  302.   def create_attack_highlight2
  303.     self.bitmap = Bitmap.new(32, 32)
  304.     self.bitmap.fill_rect(0,0,32,32,Color.new(250,193,18))
  305.     color_border(Color.new(255,246,77))
  306.     @default = true
  307.   end
  308.   def color_border(color)
  309.     self.bitmap.fill_rect(0,0,1,32, color)
  310.     self.bitmap.fill_rect(0,0,32,1, color)
  311.     self.bitmap.fill_rect(31,0,32,32, color)
  312.     self.bitmap.fill_rect(0,31,32,32, color)
  313.   end
  314.   #---------------------------------------------------------------------------
  315.   # * Create Bitmap from Spritesheet
  316.   #---------------------------------------------------------------------------
  317.   def create_bitmap_from_sprite_sheet
  318.     self.bitmap = Cache.character(@sheet_data.name)
  319.     sign = @sheet_data.name[/^[\!\$]./]
  320.     if sign && sign.include?('$')
  321.       @cw = bitmap.width / 3
  322.       @ch = bitmap.height / 4
  323.     else
  324.       @cw = bitmap.width / 12
  325.       @ch = bitmap.height / 8
  326.     end
  327.     @default = false
  328.   end
  329.   #---------------------------------------------------------------------------
  330.   # * Update Sprite From Sheet
  331.   #---------------------------------------------------------------------------
  332.   def update_sprite_from_sheet
  333.       if @sheet_data.pattern == (@sheet_data.frequency*3)-1
  334.         @val = -1
  335.         @sheet_data.pattern = @sheet_data.frequency*2
  336.       elsif @sheet_data.pattern == 0
  337.         @val = 1
  338.         @sheet_data.pattern = @sheet_data.frequency-1
  339.       end
  340.       @sheet_data.pattern += @val
  341.      
  342.       sx = (@sheet_data.index % 4 * 3 + @sheet_data.pattern/@sheet_data.frequency) * @cw
  343.       sy = (@sheet_data.index/4 * 4 + @sheet_data.row)*@ch
  344.       self.src_rect.set(sx, sy, @cw, @ch)
  345.   end
  346.   #---------------------------------------------------------------------------
  347.   # * Place
  348.   #---------------------------------------------------------------------------
  349.   def place(x,y, opacity = Era::Fade.opacity)
  350.     @sx, @sy = x,y
  351.     self.ox = 0
  352.     self.oy = 0
  353.     @orx = x
  354.     @ory = y
  355.     self.x = (x - (@disp_x = $game_map.display_x)) * 32
  356.     self.y = (y - (@disp_y = $game_map.display_y)) * 32
  357.     self.opacity = opacity
  358.     self
  359.   end
  360.   #---------------------------------------------------------------------------
  361.   # * Update
  362.   #---------------------------------------------------------------------------
  363.   def update
  364.     return unless !self.bitmap.nil?
  365.     super
  366.     if @disp_x != $game_map.display_x || @disp_y != $game_map.display_y
  367.       update_position
  368.     end
  369.     method(@update_methods[@method_symbol]).call
  370.   end
  371.   #---------------------------------------------------------------------------
  372.   # * Update Default
  373.   #---------------------------------------------------------------------------
  374.   def update_default
  375.    
  376.     if Era::Fade::SameRate
  377.       self.opacity = Era::Fade.opacity
  378.     else
  379.       @val = self.opacity < Era::Fade::Min ? Era::Fade::Rate : (self.opacity > Era::Fade::Max ? -Era::Fade::Rate : @val)
  380.       self.opacity += @val
  381.     end
  382.    
  383.   end # update_default
  384.   #---------------------------------------------------------------------------
  385.   # * Update Position
  386.   #---------------------------------------------------------------------------
  387.   def update_position
  388.     return unless @orx && @ory
  389.     self.x = $game_map.adjust_x(@orx) * 32
  390.     self.y = $game_map.adjust_y(@ory) * 32
  391.     self.z = 10
  392.     @disp_x = $game_map.display_x
  393.     @disp_y = $game_map.display_y
  394.   end
  395.   #---------------------------------------------------------------------------
  396.   # * Clear contents of highlight
  397.   #---------------------------------------------------------------------------
  398.   def clear
  399.     self.bitmap.clear
  400.   end
  401. end # Sprite_Highlight_Base
  402. #==============================================================================
  403. # * Sprite_Highlight
  404. #------------------------------------------------------------------------------
  405. #     Extra parameters are added in this subclass to prevent crowding the base
  406. #     initialize method to support extendibility.
  407. #==============================================================================
  408. class Sprite_Highlight < Sprite_Highlight_Base
  409.   def initialize(viewport, method_sym = :default_move, sheet_data = nil)
  410.     super(viewport)
  411.     @sheet_data = sheet_data
  412.     @method_symbol = method_sym
  413.     create_highlight_bitmap
  414.     update
  415.   end
  416. end # Sprite_Highlight
  417. #==============================================================================
  418. # * Sprite Sheet Data for Custom Highlights
  419. #------------------------------------------------------------------------------
  420. #     Create a new instance of this class and call the setup method on it. Then
  421. #     pass that instance to a Sprite_Highlight to use it as a sprite sheet.
  422. #     use :hl_sheet as the method symbol. This provides the default method
  423. #     sprite sheet animation method for highlights.
  424. #==============================================================================
  425. class Sheet_Data
  426.   attr_reader :index       # Index of the sprites to use
  427.   attr_reader :row         # Animation row that should be used
  428.   attr_reader :name        # Name of the spritesheet
  429.   attr_accessor :pattern   # Used to calculate which part of spritesheet to show
  430.   attr_accessor :frequency # Number of frames before modifying bitmap
  431.   #---------------------------------------------------------------------------
  432.   # * Initialize
  433.   #---------------------------------------------------------------------------
  434.   def initialize(name, index, row, freq = 18, pattern = 1)
  435.     @index = index
  436.     @row = row
  437.     @name = name
  438.     @pattern = pattern
  439.     @frequency = freq
  440.     self
  441.   end
  442. end # Sheet_Data
  443.  
  444. #==============================================================================
  445. # ** Game_System
  446. #==============================================================================
  447. class Game_System
  448.   attr_reader :highlights_drawn
  449.   attr_reader :game_highlights
  450.  
  451.   def save_highlight(x,y, opts = {})
  452.     sv_highls = ((@game_highlights||={})[$game_map.map_id]||={})
  453.    
  454.     (sv_highls[opts[:sym]] ||= {})[[x,y]] = opts
  455.   end
  456.  
  457.   def remove_saved_highlight(x,y, map_id = nil, sym = :def)
  458.     map_id = !map_id ? $game_map.map_id : map_id
  459.     game_highls = (@game_highlights||={})[map_id]||={}
  460.     saved_opts = game_highls[[x,y]]
  461.    
  462.     (game_highls[sym]||={}).delete([x,y])
  463.    
  464.     # game_highls.delete([x,y]) if saved_opts && saved_opts[:sym] == sym
  465.    
  466.     # if saved_opts
  467.      
  468.   end
  469.  
  470.   def curr_highlights(map_id)
  471.     (@game_highlights||={})[map_id]||={}
  472.   end
  473. end
  474.  
  475. #==============================================================================
  476. # ** Game_Map
  477. #==============================================================================
  478. class Game_Map
  479.   attr_accessor :need_to_remove_highlights
  480.   attr_accessor :map_highlights_era
  481.  
  482.   alias setup_add_support_rem_hl_when_ch setup
  483.   def setup(map_id)
  484.     setup_add_support_rem_hl_when_ch(map_id)
  485.     setup_map_highlights_era(map_id)
  486.   end
  487.  
  488.   def setup_map_highlights_era(map_id)
  489.     @map_highlights_era = $game_system.curr_highlights(map_id)
  490.   end
  491. end # Game_Map
  492. #End of File
Advertisement
Add Comment
Please, Sign In to add comment