Advertisement
quack_rgss

Spawn Events

Jun 15th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.86 KB | None | 0 0
  1. =begin
  2. #==============================================================================
  3.  * Quack Spawn Events - v1.0
  4.   Author: Quack
  5. #==============================================================================
  6.  
  7. What it is and what does it do?
  8. -------------------------------------------------------------------------------
  9. Spawn in events from a specified stock map (map 1 by default).
  10. You can use self switches and self vars for the spawned events, they are reset
  11. when the event is erased so it won't affect any events that are spawned later.
  12.  
  13. Script call functions to use for spawning events:
  14. -spawn_event(x, y, id, name_tag = true)
  15.   Spawns event at position given by x and y. Event to spawn is specified with id.
  16.   You can either specify event with event id number, or with a nametag. If you
  17.   want to spawn an event using event id you have to set the name_tag parameter
  18.   to false.
  19.  
  20. -spawn_event_region(region_id, id, name_tag)
  21.   Same thing but instead of specifying a position, you give it a region id and the
  22.   event will spawn on a random tile which has that region id.
  23.  
  24. Requirements:
  25. None
  26.  
  27. Instructions/Install notes:
  28. Paste it below Materials like you would any other script.
  29.  
  30. Terms of Use:
  31. Use it however you wish (yes, that does include commercial projects), as long
  32. as you do not claim it as your own code. I would also prefer you mention me in
  33. your credits if you do decide to use it.
  34.  
  35. Bugs:
  36. dunno
  37.  
  38. History:
  39. 29/10/2016 -
  40.  
  41. =end
  42. $imported = {} if $imported.nil?
  43. $imported["Quack-Spawn"] = true
  44.  
  45. module QUACK
  46.   module SPAWN
  47.     # The map you keep your stock events that you might wanna spawn
  48.     STOCK_EVENT_MAP_ID = 1
  49.    
  50.     # Self switches and self variables isn't data stored inside the event itself.
  51.     # This means that although spawned events disappear after you leave the map,
  52.     # their self switches remain. However depending on how you use spawned events
  53.     # you often will not know which id a spawned event will get. So the self
  54.     # switches a spawned event has one time another spawned event might get
  55.     # another time. This can cause a lot of problems and you probably want the
  56.     # self switches for a spawned event to go away just like the spawned event
  57.     # does once you leave the map.
  58.     # If this value is set to true the game will automatically remove these self switch
  59.     # that were used by spawned events when you enter that map.
  60.     CLEAR_SPAWNED_EVENT_DATA_ON_LOAD = true
  61.   end
  62. end
  63.  
  64. #==============================================================================
  65. # Game_SelfSwitches
  66. #==============================================================================
  67. class Game_SelfSwitches
  68.   # new
  69.   def remove_data(map_id, event_id)
  70.     if @data.has_key?([map_id,event_id,"A"]);@data.delete([map_id,event_id,"A"]);end
  71.     if @data.has_key?([map_id,event_id,"B"]);@data.delete([map_id,event_id,"B"]);end
  72.     if @data.has_key?([map_id,event_id,"C"]);@data.delete([map_id,event_id,"C"]);end
  73.     if @data.has_key?([map_id,event_id,"D"]);@data.delete([map_id,event_id,"D"]);end
  74.     for j in (1..10)
  75.       if !$game_selfVars[[map_id, event_id,j]].nil?
  76.         if $game_selfVars[[map_id,event_id,j]] != 0
  77.           $game_selfVars[[map_id, event_id,j]] = 0
  78.         end
  79.       end
  80.     end
  81.   end
  82.   # new
  83.   def remove_bad_data(map_id, events)
  84.     d_keys = []
  85.     for key in @data
  86.       if key[0][0] == map_id
  87.         # if event doesn't exist, this data belongs to a spawned event
  88.         if events[key[0][1]].nil?
  89.           d_keys.push(key) # add data to list of data to be removed
  90.         end
  91.       end
  92.     end
  93.     #print d_keys.length.to_s + " spawned event data removed!\n"
  94.     for key in d_keys
  95.       # delete spawned event data
  96.       @data.delete key
  97.       for j in (1..10)
  98.         if !$game_selfVars[[map_id,key[0][1],j]].nil?
  99.           if $game_selfVars[[map_id,key[0][1],j]] != 0
  100.             $game_selfVars[[map_id, key[0][1], j]] = 0
  101.           end
  102.         end
  103.       end
  104.     end
  105.   end
  106. end
  107.  
  108. #==============================================================================
  109. # Game_Map
  110. #==============================================================================
  111. class Game_Map
  112.   attr_accessor :s_events
  113.  
  114.   # alias
  115.   alias qs_map_setup setup
  116.   def setup(map_id)
  117.     @s_events = {}
  118.     qs_map_setup(map_id)
  119.     #@spawned_ids = []
  120.     @spawn_count = 0 # keeps track of how many events have been spawned
  121.     @event_spawn_queue = []
  122.     @event_spawn_queue_x = []
  123.     @event_spawn_queue_y = []
  124.   end
  125.   # alias
  126.   alias setup_events_spawn_alias setup_events
  127.   def setup_events
  128.     setup_events_spawn_alias
  129.     if QUACK::SPAWN::CLEAR_SPAWNED_EVENT_DATA_ON_LOAD
  130.       $game_self_switches.remove_bad_data(@map_id, events)
  131.     end
  132.   end
  133.   # alias
  134.   alias qs_update update
  135.   def update(main = false)
  136.     qs_update(main)
  137.       if @event_spawn_queue.length > 0
  138.       for i in (0..(@event_spawn_queue.length - 1))
  139.         key_id = @event_spawn_queue[i].id
  140.         $game_self_switches.remove_data(@map_id, key_id)
  141.           @events[key_id] = Game_Event.new(@map_id, @event_spawn_queue[i])
  142.         @events[key_id].set_as_spawned
  143.         qq_offset = @events[key_id].notetags_page =~ /<START OFFSET:[ ]*(.+),[ ]*(.+)>/i ? true : nil
  144.         if qq_offset
  145.           @events[key_id].moveto(@event_spawn_queue_x[i]+$1.to_f, @event_spawn_queue_y[i]+$2.to_f)
  146.         else
  147.           @events[key_id].moveto(@event_spawn_queue_x[i], @event_spawn_queue_y[i])
  148.         end
  149.         end
  150.         @event_spawn_queue.clear
  151.       @event_spawn_queue_x.clear
  152.       @event_spawn_queue_y.clear
  153.       SceneManager.scene.spriteset.refresh_characters
  154.       end
  155.   end
  156.   # new
  157.   def delete_spawned_event(id)
  158.     #delete the event and it's sprite
  159.     @events.delete(id)
  160.   end
  161.   # new
  162.   def get_stock_event(id, name_tag)
  163.     if name_tag
  164.       # pick event by name tag
  165.       return $data_stock_name_tags[id]
  166.     else
  167.       # pick event by event id
  168.       for key in $data_stock_event_map.events
  169.         event = key[1]
  170.         next if event.nil?
  171.         return event if event.id == id
  172.       end
  173.     end
  174.     return nil
  175.   end
  176.   # new
  177.   def spawn_event(dx, dy, id, name_tag)
  178.       #return 0 if $game_player.collide_with_characters?(dx, dy)
  179.       #return 0 if dx == $game_player.x && dy == $game_player.y
  180.       event1 = get_stock_event(id, name_tag)#.dup
  181.     event = Marshal.load(Marshal.dump(event1))
  182.       return 0 if event.nil?
  183.     @spawn_count += 1
  184.     #event.name = event.name + ":s" + @spawn_count.to_s
  185.     # 1000 added to id so it won't overlap with normal events
  186.     event.id = @spawn_count + 1000
  187.      
  188.     #Directly adding the event to @events could cause a crash if the game is
  189.     #currently iterating through @events. Which is why we add it to a queue
  190.       #instead so it is added later at a safe location in the code.
  191.       @event_spawn_queue.push(event)
  192.     @event_spawn_queue_x.push(dx)
  193.     @event_spawn_queue_y.push(dy)
  194.       return event.id
  195.   end
  196.   # new
  197.   def spawn_event_region(reg_id, id, name_tag)
  198.     tile = get_random_region_tile(reg_id)
  199.     return 0 if tile.nil?
  200.     return spawn_event(tile[0], tile[1], id, name_tag)
  201.   end
  202.   # new
  203.   def get_random_region_tile(reg_id)
  204.     tiles = []
  205.     for i in 0...width
  206.       for j in 0...height
  207.         next unless region_id(i, j) == reg_id
  208.         next if $game_player.collide_with_characters?(i, j)
  209.         next if i == $game_player.x && j == $game_player.y
  210.         tiles.push([i, j])
  211.       end
  212.     end
  213.     return tiles.sample
  214.   end
  215. end
  216.  
  217. #==============================================================================
  218. # Game_Event
  219. #==============================================================================
  220. class Game_Event < Game_Character
  221.   # alias
  222.   alias qs_initialize initialize
  223.   def initialize(map_id, event)
  224.       qs_initialize(map_id, event)
  225.       @spawned = false
  226.   end
  227.   # new
  228.   def erased?
  229.     @erased
  230.   end
  231.   # new
  232.   def spawned?
  233.     @spawned
  234.   end
  235.   # new
  236.   def set_as_spawned
  237.     @spawned = true
  238.   end
  239.   # alias
  240.   alias qs_erase erase
  241.   def erase
  242.     qs_erase
  243.     $game_map.delete_spawned_event(@id) if spawned?
  244.     $game_map.delete_event_tags(self)
  245.   end
  246. end
  247.  
  248. #==============================================================================
  249. # Scene_Map
  250. #==============================================================================
  251. class Scene_Map < Scene_Base
  252.   attr_accessor :spriteset
  253. end
  254.  
  255. #==============================================================================
  256. # Game_Interpreter
  257. #==============================================================================
  258. class Game_Interpreter
  259.   # new
  260.   def spawn_event(dx, dy, id, name_tag = true)
  261.     return 0 unless SceneManager.scene_is?(Scene_Map)
  262.       return $game_map.spawn_event(dx, dy, id, name_tag)
  263.   end
  264.   # new
  265.   def spawn_event_region(region_id, id, name_tag = true)
  266.     return 0 unless SceneManager.scene_is?(Scene_Map)
  267.       return $game_map.spawn_event_region(region_id, id, name_tag)
  268.   end
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement