Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- #==============================================================================
- * Quack Spawn Events - v1.0
- Author: Quack
- #==============================================================================
- What it is and what does it do?
- -------------------------------------------------------------------------------
- Spawn in events from a specified stock map (map 1 by default).
- You can use self switches and self vars for the spawned events, they are reset
- when the event is erased so it won't affect any events that are spawned later.
- Script call functions to use for spawning events:
- -spawn_event(x, y, id, name_tag = true)
- Spawns event at position given by x and y. Event to spawn is specified with id.
- You can either specify event with event id number, or with a nametag. If you
- want to spawn an event using event id you have to set the name_tag parameter
- to false.
- -spawn_event_region(region_id, id, name_tag)
- Same thing but instead of specifying a position, you give it a region id and the
- event will spawn on a random tile which has that region id.
- Requirements:
- None
- Instructions/Install notes:
- Paste it below Materials like you would any other script.
- Terms of Use:
- Use it however you wish (yes, that does include commercial projects), as long
- as you do not claim it as your own code. I would also prefer you mention me in
- your credits if you do decide to use it.
- Bugs:
- dunno
- History:
- 29/10/2016 -
- =end
- $imported = {} if $imported.nil?
- $imported["Quack-Spawn"] = true
- module QUACK
- module SPAWN
- # The map you keep your stock events that you might wanna spawn
- STOCK_EVENT_MAP_ID = 1
- # Self switches and self variables isn't data stored inside the event itself.
- # This means that although spawned events disappear after you leave the map,
- # their self switches remain. However depending on how you use spawned events
- # you often will not know which id a spawned event will get. So the self
- # switches a spawned event has one time another spawned event might get
- # another time. This can cause a lot of problems and you probably want the
- # self switches for a spawned event to go away just like the spawned event
- # does once you leave the map.
- # If this value is set to true the game will automatically remove these self switch
- # that were used by spawned events when you enter that map.
- CLEAR_SPAWNED_EVENT_DATA_ON_LOAD = true
- end
- end
- #==============================================================================
- # Game_SelfSwitches
- #==============================================================================
- class Game_SelfSwitches
- # new
- def remove_data(map_id, event_id)
- if @data.has_key?([map_id,event_id,"A"]);@data.delete([map_id,event_id,"A"]);end
- if @data.has_key?([map_id,event_id,"B"]);@data.delete([map_id,event_id,"B"]);end
- if @data.has_key?([map_id,event_id,"C"]);@data.delete([map_id,event_id,"C"]);end
- if @data.has_key?([map_id,event_id,"D"]);@data.delete([map_id,event_id,"D"]);end
- for j in (1..10)
- if !$game_selfVars[[map_id, event_id,j]].nil?
- if $game_selfVars[[map_id,event_id,j]] != 0
- $game_selfVars[[map_id, event_id,j]] = 0
- end
- end
- end
- end
- # new
- def remove_bad_data(map_id, events)
- d_keys = []
- for key in @data
- if key[0][0] == map_id
- # if event doesn't exist, this data belongs to a spawned event
- if events[key[0][1]].nil?
- d_keys.push(key) # add data to list of data to be removed
- end
- end
- end
- #print d_keys.length.to_s + " spawned event data removed!\n"
- for key in d_keys
- # delete spawned event data
- @data.delete key
- for j in (1..10)
- if !$game_selfVars[[map_id,key[0][1],j]].nil?
- if $game_selfVars[[map_id,key[0][1],j]] != 0
- $game_selfVars[[map_id, key[0][1], j]] = 0
- end
- end
- end
- end
- end
- end
- #==============================================================================
- # Game_Map
- #==============================================================================
- class Game_Map
- attr_accessor :s_events
- # alias
- alias qs_map_setup setup
- def setup(map_id)
- @s_events = {}
- qs_map_setup(map_id)
- #@spawned_ids = []
- @spawn_count = 0 # keeps track of how many events have been spawned
- @event_spawn_queue = []
- @event_spawn_queue_x = []
- @event_spawn_queue_y = []
- end
- # alias
- alias setup_events_spawn_alias setup_events
- def setup_events
- setup_events_spawn_alias
- if QUACK::SPAWN::CLEAR_SPAWNED_EVENT_DATA_ON_LOAD
- $game_self_switches.remove_bad_data(@map_id, events)
- end
- end
- # alias
- alias qs_update update
- def update(main = false)
- qs_update(main)
- if @event_spawn_queue.length > 0
- for i in (0..(@event_spawn_queue.length - 1))
- key_id = @event_spawn_queue[i].id
- $game_self_switches.remove_data(@map_id, key_id)
- @events[key_id] = Game_Event.new(@map_id, @event_spawn_queue[i])
- @events[key_id].set_as_spawned
- qq_offset = @events[key_id].notetags_page =~ /<START OFFSET:[ ]*(.+),[ ]*(.+)>/i ? true : nil
- if qq_offset
- @events[key_id].moveto(@event_spawn_queue_x[i]+$1.to_f, @event_spawn_queue_y[i]+$2.to_f)
- else
- @events[key_id].moveto(@event_spawn_queue_x[i], @event_spawn_queue_y[i])
- end
- end
- @event_spawn_queue.clear
- @event_spawn_queue_x.clear
- @event_spawn_queue_y.clear
- SceneManager.scene.spriteset.refresh_characters
- end
- end
- # new
- def delete_spawned_event(id)
- #delete the event and it's sprite
- @events.delete(id)
- end
- # new
- def get_stock_event(id, name_tag)
- if name_tag
- # pick event by name tag
- return $data_stock_name_tags[id]
- else
- # pick event by event id
- for key in $data_stock_event_map.events
- event = key[1]
- next if event.nil?
- return event if event.id == id
- end
- end
- return nil
- end
- # new
- def spawn_event(dx, dy, id, name_tag)
- #return 0 if $game_player.collide_with_characters?(dx, dy)
- #return 0 if dx == $game_player.x && dy == $game_player.y
- event1 = get_stock_event(id, name_tag)#.dup
- event = Marshal.load(Marshal.dump(event1))
- return 0 if event.nil?
- @spawn_count += 1
- #event.name = event.name + ":s" + @spawn_count.to_s
- # 1000 added to id so it won't overlap with normal events
- event.id = @spawn_count + 1000
- #Directly adding the event to @events could cause a crash if the game is
- #currently iterating through @events. Which is why we add it to a queue
- #instead so it is added later at a safe location in the code.
- @event_spawn_queue.push(event)
- @event_spawn_queue_x.push(dx)
- @event_spawn_queue_y.push(dy)
- return event.id
- end
- # new
- def spawn_event_region(reg_id, id, name_tag)
- tile = get_random_region_tile(reg_id)
- return 0 if tile.nil?
- return spawn_event(tile[0], tile[1], id, name_tag)
- end
- # new
- def get_random_region_tile(reg_id)
- tiles = []
- for i in 0...width
- for j in 0...height
- next unless region_id(i, j) == reg_id
- next if $game_player.collide_with_characters?(i, j)
- next if i == $game_player.x && j == $game_player.y
- tiles.push([i, j])
- end
- end
- return tiles.sample
- end
- end
- #==============================================================================
- # Game_Event
- #==============================================================================
- class Game_Event < Game_Character
- # alias
- alias qs_initialize initialize
- def initialize(map_id, event)
- qs_initialize(map_id, event)
- @spawned = false
- end
- # new
- def erased?
- @erased
- end
- # new
- def spawned?
- @spawned
- end
- # new
- def set_as_spawned
- @spawned = true
- end
- # alias
- alias qs_erase erase
- def erase
- qs_erase
- $game_map.delete_spawned_event(@id) if spawned?
- $game_map.delete_event_tags(self)
- end
- end
- #==============================================================================
- # Scene_Map
- #==============================================================================
- class Scene_Map < Scene_Base
- attr_accessor :spriteset
- end
- #==============================================================================
- # Game_Interpreter
- #==============================================================================
- class Game_Interpreter
- # new
- def spawn_event(dx, dy, id, name_tag = true)
- return 0 unless SceneManager.scene_is?(Scene_Map)
- return $game_map.spawn_event(dx, dy, id, name_tag)
- end
- # new
- def spawn_event_region(region_id, id, name_tag = true)
- return 0 unless SceneManager.scene_is?(Scene_Map)
- return $game_map.spawn_event_region(region_id, id, name_tag)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement