blucalm

Event Spawn

Nov 7th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.87 KB | None | 0 0
  1. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  2. # ** Reproduce Events
  3. # Author: Eshra
  4. # First Version: 21 Sept. 2012
  5. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  6. #
  7. # Allows events to be loaded from other maps in your project and placed on
  8. # the current map. Also adds functionality for persistent events which will
  9. # stay on the map even if it is reloaded (switching maps or loading save
  10. # files).
  11. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  12. #
  13. #------------------------------------------------------------------------------
  14. # * How to Use
  15. #------------------------------------------------------------------------------
  16. #
  17. # Heavily modified aliases:
  18. # straighten inside Game_CharacterBase
  19. # events are now "straightened" to correspond to their original pattern, not 1
  20. #
  21. # tileset_bitmap inside Sprite_Character
  22. # now checks if the character has a specific map which the bitmap should be
  23. # loaded from.
  24. #
  25.  
  26. # Original Release Date: 7 Nov. 2012
  27. #
  28. # Update Log
  29. #
  30. # October 28, 2012: Can now remove spawned events.
  31. # October 27, 2012: Finished adding support for persistent events, made code
  32. # more extendible. Added code to allow use of tile graphics.
  33. #
  34. # Sepetember 21, 2012: Code reorganized made into it's own script for portability.
  35. #
  36. # August 2012: Version 0.0 completed
  37.  
  38. ($imported ||= {})["(~^_^)~Event Eng"] = 0.1
  39.  
  40. #==============================================================================
  41. # * EventSpawn
  42. #==============================================================================
  43. module EventSpawn
  44. #----------------------------------------------------------------------------
  45. # * Constants
  46. #----------------------------------------------------------------------------
  47. module Constants
  48. SS = ['A','B','C','D']
  49. Tiles_On = true # can use tilesets from offscreen maps as event graphics
  50. end
  51. #----------------------------------------------------------------------------
  52. # * Spawn Event
  53. #----------------------------------------------------------------------------
  54. def self.spawn_event(spawn_from_map_id, _event_id, _x, _y, persistent, init_dir, opts = {})
  55. return unless SceneManager.scene_is?(Scene_Map)
  56. return if _event_id.nil? || spawn_from_map_id.nil?
  57.  
  58. options = { :A => false, :B => false, :C => false, :D => false}.merge(opts)
  59. map = load_data(sprintf("Data/Map%03d.rvdata2", spawn_from_map_id))
  60. event_id = next_id
  61. preprocessed_event = map.events[_event_id]
  62. preprocessed_event.id = event_id
  63. event = create_event(preprocessed_event, event_id)
  64.  
  65. event.gen_from_map_id = spawn_from_map_id
  66. event.moveto(_x, _y)
  67. add_to_spmap(event)
  68.  
  69. event.persistent = persistent
  70. persistent ? store_pev(event) : $game_map.spawned_event_ids.push(event_id)
  71. $game_map.need_refresh = true
  72.  
  73. EventSpawn::Constants::SS.each{|letter|
  74. is_on = options[letter.to_sym]
  75. $game_self_switches[[$game_map.map_id, event_id, letter]] = is_on
  76. }
  77.  
  78. return event
  79. end # End - spawn_event
  80.  
  81. def self.next_id
  82. max_id = $game_system.max_evn_id
  83. keys = $game_map.events.keys
  84. id = keys==[] ? 1 : keys.max + 1
  85. max_id = id > max_id ? max_id = id : max_id+1
  86.  
  87. $game_system.max_evn_id = max_id # store the largest event id used so far
  88. return max_id
  89. end
  90.  
  91. def self.add_to_spmap(ev)
  92. spm = SceneManager.scene.instance_eval('@spriteset')
  93. sp_map_sps = spm.instance_eval('@character_sprites')
  94. sp_map_sps.push(Sprite_Character.new(spm.instance_eval('@viewport1'), ev))
  95. end
  96.  
  97. #----------------------------------------------------------------------------
  98. # * Create Event
  99. #----------------------------------------------------------------------------
  100. def self.create_event(preprocessed_event, event_id)
  101. $game_map.events[event_id] = Game_Event.new($game_map.map_id, preprocessed_event)
  102. end
  103. #----------------------------------------------------------------------------
  104. # * Process Persistent Events
  105. #----------------------------------------------------------------------------
  106. def self.store_pev(event)
  107. sys = $game_system
  108. $game_map.spawned_event_ids.push(event.id) # note: ** 1
  109. (sys.persistent_events[$game_map.map_id] ||= []).push(event) # Set persistance data
  110.  
  111. # Update the wrapped event's coordinates for reloading
  112. event.event.x = event.x
  113. event.event.y = event.y
  114. end # store_pev
  115. #----------------------------------------------------------------------------
  116. # * Set all selfswitches to false for the event on the current map
  117. #----------------------------------------------------------------------------
  118. def self.clean_self_switches(event_id)
  119. map_id = $game_map.map_id
  120. EventSpawn::Constants::SS.each{ |ch|
  121. $game_self_switches[[map_id,event_id,ch]] = false
  122. }
  123. end
  124.  
  125. end # EventSpawn
  126.  
  127. #==============================================================================
  128. # * Game_System
  129. #==============================================================================
  130. class Game_System
  131.  
  132. attr_accessor :persistent_events #stores persistent events information
  133. attr_accessor :max_evn_id # save the largest id used so far to prevent using
  134. # the save id twice.
  135. #----------------------------------------------------------------------------
  136. # * Alias initialize
  137. #----------------------------------------------------------------------------
  138. alias reprod_evs_initialize_event_hash initialize
  139. def initialize
  140. reprod_evs_initialize_event_hash
  141. @persistent_events = {}
  142. @max_evn_id = 0
  143. end # End - initialize
  144. #----------------------------------------------------------------------------
  145. # * Alias on_after_load
  146. #----------------------------------------------------------------------------
  147. alias rep_evs_on_after_load on_after_load
  148. def on_after_load
  149. $game_map.remove_temporary_spawned_events #remove previously spawned events
  150. rep_evs_on_after_load
  151. end # on_after_load
  152.  
  153. end # Game_System
  154.  
  155. #==============================================================================
  156. # * Game_Map
  157. #==============================================================================
  158. class Game_Map
  159. attr_reader :spawned_event_ids
  160. attr_accessor :spawned_event_ids
  161. attr_accessor :events
  162. #----------------------------------------------------------------------------
  163. # * Alias setup
  164. #----------------------------------------------------------------------------
  165. alias reprod_evs_game_map_setup setup
  166. def setup(map_id)
  167. $game_map.clean_self_switches
  168. @spawned_event_ids = Array.new #the ids of the events that have been spawned
  169. reprod_evs_game_map_setup(map_id)
  170. end # End - setup
  171. #----------------------------------------------------------------------------
  172. # * Alias setup_events
  173. #----------------------------------------------------------------------------
  174. alias ra_dep_setup_evs_al_meth setup_events
  175. def setup_events
  176. ra_dep_setup_evs_al_meth
  177.  
  178. # Correct the self switches if there are persistent events on the map
  179. correct_self_switches if $game_system.persistent_events[@map_id] != nil
  180. end # End - setup_events
  181. #----------------------------------------------------------------------------
  182. # * Correct Self Switches
  183. # Corrects $selfswitches hash so that they correctly correlate to the events'
  184. # ids upon reloading the map (The ids may change b/c of removed events).
  185. #----------------------------------------------------------------------------
  186. def correct_self_switches
  187. p_events = $game_system.persistent_events[@map_id]
  188. new_self_switches = {} # the new global self switch hash
  189. loaded_events = []
  190. @spawned_event_ids = [] # reset spawned events
  191. p_events.each{ |ev|
  192.  
  193. #initialize new id and self_switches
  194. new_event_id = @events.keys.max + 1
  195. new_self_switches[new_event_id] = get_self_switches(ev.id)
  196.  
  197. ev.event.id = new_event_id # update interior events id
  198.  
  199. loaded_event = Game_Event.new(@map_id, ev.event)
  200.  
  201. #update extra unhandled information for the new event
  202. loaded_event.persistent = true
  203.  
  204. #place the new event in the map's event hash
  205. @events[loaded_event.id] = loaded_event
  206.  
  207. loaded_events.push(loaded_event)
  208. @spawned_event_ids.push(loaded_event.id) #update spawned events
  209. } # End - p_events.each
  210.  
  211. #update the persistent_events hash with the new information
  212. $game_system.persistent_events[@map_id] = loaded_events
  213.  
  214. local_ss = $game_self_switches
  215. new_self_switches.keys.each{|id|
  216. count = 0
  217. EventSpawn::Constants::SS.each{ |letter|
  218. $game_self_switches[[@map_id,id,letter]] = new_self_switches[id][count]
  219. count += 1
  220. }
  221. }
  222.  
  223. # Refresh the events so they are placed on the map on the correct page
  224. # also reset the ids of the events inside @spawned_event_ids
  225. @events.keys.each{ |id|
  226. @events[id].refresh
  227. }
  228. refresh_tile_events
  229. end # End - correct_self_switches
  230. #----------------------------------------------------------------------------
  231. # * Get Self Switches
  232. # returns an array of the selfswitches for the given event id and then
  233. # deletes those values from $game_self_switches.
  234. #----------------------------------------------------------------------------
  235. def get_self_switches(event_id)
  236. local_ss = $game_self_switches
  237. vals = []
  238. EventSpawn::Constants::SS.each{ |letter|
  239. vals.push(local_ss[[@map_id, event_id, letter]])
  240. local_ss[[@map_id, event_id, letter]] = false
  241. }
  242. vals
  243. end # End - get_self_switches
  244. #----------------------------------------------------------------------------
  245. # * Clean Self Switches
  246. # self-switches are handled globally, not on an event by event basis
  247. # so the global hash needs to be cleaned out of all the temporarily
  248. # spawned events when changing maps/loading the game, etc.
  249. #----------------------------------------------------------------------------
  250. def clean_self_switches
  251. local_ss = $game_self_switches
  252.  
  253. return if @spawned_event_ids == nil
  254. @spawned_event_ids.each{ |id|
  255. if !@events[id].persistent #don't clean up persistant event self switches
  256. EventSpawn::Constants::SS.each{ |letter|
  257. local_ss[[@map_id, id, letter]] = false #reset self_switches of temp events
  258. }
  259. end # End if
  260. }
  261. end # End - clean_self_switches
  262. #----------------------------------------------------------------------------
  263. # Removes all of the nonpersistent spawned events from the current map
  264. #----------------------------------------------------------------------------
  265. def remove_temporary_spawned_events
  266. return if @spawned_event_ids.nil?
  267. clean_self_switches
  268. clean_temp_evs_helper
  269. need_refresh = true
  270. end # End - remove_temporary_spawned_events
  271. #----------------------------------------------------------------------------
  272. # * Cleans out temporary events
  273. #----------------------------------------------------------------------------
  274. def clean_temp_evs_helper
  275. @spawned_event_ids.each{|id| @events.delete(id) unless @events[id].persistent}
  276. @spawned_event_ids = []
  277. end # End - clean_temp_evs_helper
  278.  
  279. #----------------------------------------------------------------------------
  280. # * Removes an event from the current map, including persistant events
  281. #----------------------------------------------------------------------------
  282. def destroy_event_any(id)
  283. return unless !(ev = @events[id]).nil?
  284. EventSpawn.clean_self_switches(id)
  285. ev.set_intp_repr_ev(0)
  286.  
  287. ev.erase # to hide the graphic
  288.  
  289. if persistants = $game_system.persistent_events[@map_id]
  290. persistants.each_with_index{ |ev, i|
  291. if ev.id == id
  292. persistants.delete_at(i)
  293. break
  294. end
  295. }
  296. end
  297.  
  298. @spawned_event_ids.delete(id) # delete from array
  299. @events.delete(id) # " " hash
  300.  
  301.  
  302. refresh
  303. end
  304. end # End - Game_Map
  305.  
  306. #==============================================================================
  307. # * Game_Character
  308. #==============================================================================
  309. class Game_Character < Game_CharacterBase
  310. attr_accessor :gen_from_map_id # map the event was generated from
  311. end
  312.  
  313. #==============================================================================
  314. # * Game_Event
  315. #==============================================================================
  316. class Game_Event < Game_Character
  317. attr_accessor :event # wrapped event
  318. attr_accessor :id # id of this event
  319. attr_accessor :persistent # does event stay after switching maps
  320.  
  321. #attr_accessor :init_dir # used to specify direction with throw!
  322. #----------------------------------------------------------------------------
  323. # * Alias method initialize
  324. #----------------------------------------------------------------------------
  325. alias ra_extra_fields_dep_init initialize
  326. def initialize(map_id, event)
  327. ra_extra_fields_dep_init(map_id,event)
  328. @persistent = false
  329. #@init_dir = -1
  330. end # End - initialize
  331.  
  332. #--------------------------------------------------------------------------
  333. # * Alias straighten - sets pattern to orig_pattern
  334. #--------------------------------------------------------------------------
  335. alias straight_ev_sp_ed_st_orig straighten
  336. def straighten
  337. straight_ev_sp_ed_st_orig
  338. @pattern = @original_pattern ? @original_pattern : 1 if @walk_anime || @step_anime
  339. end
  340.  
  341. #--------------------------------------------------------------------------
  342. # * Alias move_straight - Store updated postion for persistent events
  343. #--------------------------------------------------------------------------
  344. alias move_str_ed_fr_persis_support move_straight
  345. def move_straight(dir)
  346. move_str_ed_fr_persis_support(dir)
  347. @event.x, @event.y = @x, @y if @persistent
  348. end
  349.  
  350. #--------------------------------------------------------------------------
  351. # * Alias move_diagonal- Store updated postion for persistent events
  352. #--------------------------------------------------------------------------
  353. alias move_diag_ed_fr_pesis_support move_diagonal
  354. def move_diagonal(horz, vert)
  355. move_diag_ed_fr_pesis_support(horz, vert)
  356. @event.x, @event.y = @x, @y if @persistent
  357. end
  358.  
  359. def set_intp_repr_ev(id)
  360. @interpreter.set_ev_id_repr_ev(id) unless @interpreter.nil?
  361. end
  362. end # Game_Event
  363.  
  364. #==============================================================================
  365. # * Sprite_Character
  366. #==============================================================================
  367. class Sprite_Character < Sprite_Base
  368.  
  369. #--------------------------------------------------------------------------
  370. # * Alias tileset_bitmap - load tiles from offscreen maps
  371. #--------------------------------------------------------------------------
  372. alias ts_bm_sprt_fr_offscr_maps tileset_bitmap
  373. def tileset_bitmap(tile_id)
  374.  
  375. if (id = @character.gen_from_map_id) && id > 0
  376. map = load_data(sprintf("Data/Map%03d.rvdata2", id))
  377. return Cache.tileset($data_tilesets[map.tileset_id].tileset_names[5 + tile_id / 256])
  378. else
  379. return ts_bm_sprt_fr_offscr_maps(tile_id)
  380. end
  381.  
  382. end # tileset_bitmap
  383.  
  384. end # Sprite_Character
  385. #==============================================================================
  386. # ** Game_Interpreter
  387. #==============================================================================
  388. class Game_Interpreter
  389. def set_ev_id_repr_ev(id)
  390. @event_id = id
  391. end # set_ev_id_repr_ev
  392. end # Game_Interpreter
  393.  
  394. #EOF
Advertisement
Add Comment
Please, Sign In to add comment