blucalm

highlights import change

Nov 8th, 2012
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 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,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. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  36. #
  37. # Original Release Date: 7 Nov. 2012
  38. #
  39. # Upate Log
  40. # 10/30/12 - Reorganized updating to match the default engine's organization.
  41. # That is, $game_map stores the information about the highlights
  42. # and scene_map refreshes the highlights when @map_id != game_map.map_id
  43. # 10/29/12 - Version 0.1 finished, added support for persistant highlights
  44. # 10/4/12 - First Version finished
  45. #
  46. #------------------------------------------------------------------------------
  47.  
  48. ($imported||={})["Ra Highlights"] = 0.1
  49. #==============================================================================
  50. # * Spriteset_Map
  51. #==============================================================================
  52. class Spriteset_Map
  53. attr_accessor :viewport1
  54. #----------------------------------------------------------------------------
  55. # * Initialize
  56. #----------------------------------------------------------------------------
  57. alias initialize_highlights_tb initialize
  58. def initialize
  59. initialize_highlights_tb
  60. create_highlights_tb
  61. end
  62. #----------------------------------------------------------------------------
  63. # * Create Highlights
  64. #----------------------------------------------------------------------------
  65. def create_highlights_tb
  66. @path_hightlights_tb = []
  67. make_hls_visible_for_map
  68. end
  69.  
  70. def make_hls_visible_for_map
  71. gh = $game_map.map_highlights_era
  72. gh.keys.each{|xy|
  73. add_highlight_tb(xy[0],xy[1], gh[xy])
  74. }
  75. end
  76. #----------------------------------------------------------------------------
  77. # * Alias Update
  78. #----------------------------------------------------------------------------
  79. alias highlights_spritesetmap_update_tb update
  80. def update
  81. update_highlights
  82. highlights_spritesetmap_update_tb
  83. end
  84. #----------------------------------------------------------------------------
  85. # * Update Highlights
  86. #----------------------------------------------------------------------------
  87. def update_highlights
  88. refresh_highlights if @map_id != $game_map.map_id
  89. return if @path_hightlights_tb.nil?
  90. @path_hightlights_tb.each{ |sprite| sprite.update }
  91. end
  92. #----------------------------------------------------------------------------
  93. # * Add a highlight
  94. #----------------------------------------------------------------------------
  95. def add_highlight_tb(x,y,sh = nil, retain = false)
  96. hls = (@path_hightlights_tb||=[])
  97. if sh.nil?
  98. hls.push(s=Sprite_Highlight.new(@viewport1).place(x,y))
  99. else
  100. hls.push(s=Sprite_Highlight.new(@viewport1, :hl_sheet, sh).place(x,y))
  101. end
  102. $game_system.save_highlight(x,y,sh) if retain
  103. end
  104. #----------------------------------------------------------------------------
  105. # * Remove Highlights
  106. #----------------------------------------------------------------------------
  107. def dispose_highlights_tb
  108. @path_hightlights_tb.each{|sp| sp.dispose}
  109. @path_hightlights_tb = []
  110. end
  111. #----------------------------------------------------------------------------
  112. # * Alias Dispose
  113. #----------------------------------------------------------------------------
  114. alias sp_highl_spmap_mod_dispose dispose
  115. def dispose
  116. sp_highl_spmap_mod_dispose
  117. dispose_highlights_tb
  118. end
  119.  
  120. def refresh_highlights
  121. dispose_highlights_tb
  122. create_highlights_tb
  123. end
  124. end # Spriteset_Map
  125.  
  126. #==============================================================================
  127. # * Sprite_Highlight_Base
  128. #------------------------------------------------------------------------------
  129. # Provies methods for drawing a highlight on the screen.
  130. #==============================================================================
  131. class Sprite_Highlight_Base < Sprite_Base
  132. #----------------------------------------------------------------------------
  133. # * Initialize
  134. #----------------------------------------------------------------------------
  135. def initialize(viewport)
  136. super(viewport)
  137. @disp_x, @disp_y = 0, 0
  138. @val = 1 # Used lazily as a variable for manipulating the bitmap
  139.  
  140. # Add creation and update methods to these hashes for custom highlights:
  141. # The hash maps symbols => method names
  142. @create_methods = {
  143. :default_move => :create_default_move_highlight,
  144. :default_attack => :create_default_attack_highlight,
  145. :hl_sheet => :create_bitmap_from_sprite_sheet
  146. }
  147. @update_methods = {
  148. :default_move => :update_default,
  149. :default_attack => :update_default,
  150. :hl_sheet => :update_sprite_from_sheet
  151. }
  152. end
  153. #--------------------------------------------------------------------------
  154. # * Create Bitmap
  155. #--------------------------------------------------------------------------
  156. def create_highlight_bitmap
  157. method(@create_methods[@method_symbol]).call
  158. end
  159. #---------------------------------------------------------------------------
  160. # * Create Default Movement Highlight
  161. #---------------------------------------------------------------------------
  162. def create_default_move_highlight
  163. self.bitmap = Bitmap.new(32, 32)
  164. self.bitmap.fill_rect(0,0,32,32,Color.new(23,52,123))
  165. @default = true
  166. end
  167. #---------------------------------------------------------------------------
  168. # * Create Default Attack Highlight
  169. #---------------------------------------------------------------------------
  170. def create_default_attack_highlight
  171. self.bitmap = Bitmap.new(32, 32)
  172. self.bitmap.fill_rect(0,0,32,32,Color.new(231,52,23))
  173. @default = true
  174. end
  175. #---------------------------------------------------------------------------
  176. # * Create Bitmap from Spritesheet
  177. #---------------------------------------------------------------------------
  178. def create_bitmap_from_sprite_sheet
  179. self.bitmap = Cache.character(@sheet_data.name)
  180. sign = @sheet_data.name[/^[\!\$]./]
  181. if sign && sign.include?('$')
  182. @cw = bitmap.width / 3
  183. @ch = bitmap.height / 4
  184. else
  185. @cw = bitmap.width / 12
  186. @ch = bitmap.height / 8
  187. end
  188. @default = false
  189. end
  190. #---------------------------------------------------------------------------
  191. # * Update Sprite From Sheet
  192. #---------------------------------------------------------------------------
  193. def update_sprite_from_sheet
  194. if @sheet_data.pattern == (@sheet_data.frequency*3)-1
  195. @val = -1
  196. @sheet_data.pattern = @sheet_data.frequency*2
  197. elsif @sheet_data.pattern == 0
  198. @val = 1
  199. @sheet_data.pattern = @sheet_data.frequency-1
  200. end
  201. @sheet_data.pattern += @val
  202.  
  203. sx = (@sheet_data.index % 4 * 3 + @sheet_data.pattern/@sheet_data.frequency) * @cw
  204. sy = (@sheet_data.index/4 * 4 + @sheet_data.row)*@ch
  205. self.src_rect.set(sx, sy, @cw, @ch)
  206. end
  207. #---------------------------------------------------------------------------
  208. # * Place
  209. #---------------------------------------------------------------------------
  210. def place(x,y)
  211. self.ox = 0
  212. self.oy = 0
  213. @orx = x
  214. @ory = y
  215. self.x = (x - (@disp_x = $game_map.display_x)) * 32
  216. self.y = (y - (@disp_y = $game_map.display_y)) * 32
  217. self
  218. end
  219. #---------------------------------------------------------------------------
  220. # * Update
  221. #---------------------------------------------------------------------------
  222. def update
  223. return unless self.bitmap
  224. super
  225. if @disp_x != $game_map.display_x || @disp_y != $game_map.display_y
  226. update_position
  227. end
  228. method(@update_methods[@method_symbol]).call
  229. end
  230. #---------------------------------------------------------------------------
  231. # * Update Default
  232. #---------------------------------------------------------------------------
  233. def update_default
  234. @val = self.opacity < 40 ? 3 : (self.opacity > 160 ? -3 : @val)
  235. self.opacity += @val
  236. end # update_default
  237. #---------------------------------------------------------------------------
  238. # * Update Position
  239. #---------------------------------------------------------------------------
  240. def update_position
  241. return unless @orx && @ory
  242. self.x = $game_map.adjust_x(@orx) * 32
  243. self.y = $game_map.adjust_y(@ory) * 32
  244. self.z = 10
  245. @disp_x = $game_map.display_x
  246. @disp_y = $game_map.display_y
  247. end
  248. end # Sprite_Highlight_Base
  249. #==============================================================================
  250. # * Sprite_Highlight
  251. #------------------------------------------------------------------------------
  252. # Extra parameters are added in this subclass to prevent crowding the base
  253. # initialize method to support extendibility.
  254. #==============================================================================
  255. class Sprite_Highlight < Sprite_Highlight_Base
  256. def initialize(viewport, method_sym = :default_move, sheet_data = nil)
  257. super(viewport)
  258. @sheet_data = sheet_data
  259. @method_symbol = method_sym
  260. create_highlight_bitmap
  261. update
  262. end
  263. end # Sprite_Highlight
  264. #==============================================================================
  265. # * Sprite Sheet Data for Custom Highlights
  266. #------------------------------------------------------------------------------
  267. # Create a new instance of this class and call the setup method on it. Then
  268. # pass that instance to a Sprite_Highlight to use it as a sprite sheet.
  269. # use :hl_sheet as the method symbol. This provides the default method
  270. # sprite sheet animation method for highlights.
  271. #==============================================================================
  272. class Sheet_Data
  273. attr_reader :index # Index of the sprites to use
  274. attr_reader :row # Animation row that should be used
  275. attr_reader :name # Name of the spritesheet
  276. attr_accessor :pattern # Used to calculate which part of spritesheet to show
  277. attr_accessor :frequency # Number of frames before modifying bitmap
  278. #---------------------------------------------------------------------------
  279. # * Initialize
  280. #---------------------------------------------------------------------------
  281. def initialize(name, index, row, freq = 18, pattern = 1)
  282. @index = index
  283. @row = row
  284. @name = name
  285. @pattern = pattern
  286. @frequency = freq
  287. self
  288. end
  289. end # Sheet_Data
  290.  
  291. #==============================================================================
  292. # ** Game_System
  293. #==============================================================================
  294. class Game_System
  295. attr_reader :highlights_drawn
  296. attr_reader :game_highlights
  297.  
  298. def save_highlight(x,y,sh)
  299. ((@game_highlights||={})[$game_map.map_id]||={})[[x,y]] = sh
  300. end
  301.  
  302. def remove_saved_highlight(x,y, map_id = nil)
  303. map_id = !map_id ? $game_map.map_id : map_id
  304. ((@game_highlights||={})[map_id]||={}).delete([x,y])
  305. end
  306.  
  307. def curr_highlights(map_id)
  308. (@game_highlights||={})[map_id]||={}
  309. end
  310. end
  311.  
  312. #==============================================================================
  313. # ** Game_Map
  314. #==============================================================================
  315. class Game_Map
  316. attr_accessor :need_to_remove_highlights
  317. attr_accessor :map_highlights_era
  318.  
  319. alias setup_add_support_rem_hl_when_ch setup
  320. def setup(map_id)
  321. setup_add_support_rem_hl_when_ch(map_id)
  322. setup_map_highlights_era(map_id)
  323. end
  324.  
  325. def setup_map_highlights_era(map_id)
  326. @map_highlights_era = $game_system.curr_highlights(map_id)
  327. end
  328. end # Game_Map
  329. #End of File
Advertisement
Add Comment
Please, Sign In to add comment