blucalm

Event Spawn update place by name

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