blucalm

Highlights update 5 Dec 2012

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