Advertisement
Loque

Intera regione un evento

Jun 18th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.86 KB | None | 0 0
  1. =begin
  2. #===============================================================================
  3.  Title: Region Events
  4.  Author: Hime
  5.  Date: Dec 29, 2013
  6.  URL: http://himeworks.wordpress.com/2013/12/29/region-events/
  7. --------------------------------------------------------------------------------
  8.  ** Change log
  9.  Dec 29, 2013
  10.    - initial release
  11. --------------------------------------------------------------------------------  
  12.  ** Terms of Use
  13.  * Free to use in non-commercial projects
  14.  * Contact me for commercial use
  15.  * No real support. The script is provided as-is
  16.  * Will do bug fixes, but no compatibility patches
  17.  * Features may be requested but no guarantees, especially if it is non-trivial
  18.  * Credits to Hime Works in your project
  19.  * Preserve this header
  20. --------------------------------------------------------------------------------
  21.  ** Description
  22.  
  23.  This script allows you to create "Region Events". Basically, it allows you to
  24.  turn an entire region into an event based on an existing event on your map.
  25.  
  26.  For example, suppose you have some Region 1 tiles. Using this script, you can
  27.  connect Region 1 to an event on the map. The result is called "Region Event 1",
  28.  and when you activate Region Event 1, it will run the event it is connected to.
  29.  
  30.  Region events obey all event rules. They do not have a graphic, because they
  31.  are simply regions on your map. Region events are useful when you want
  32.  multiple tiles to all run the same event.
  33.  
  34. --------------------------------------------------------------------------------
  35.  ** Installation
  36.  
  37.  In the script editor, install this script below Materisls and above Main
  38.  
  39. --------------------------------------------------------------------------------
  40.  ** Usage
  41.  
  42.  -- Understanding Region Events --
  43.  
  44.  Region events obey event trigger rules. If the region event is activated by
  45.  "action trigger", then players must press the "OK" button in order to activate
  46.  the event. Similarly, if the region event is activated by "player touch", then
  47.  the player can activate the event by walking onto the tile.
  48.  
  49.  Region events obey priority rules. If the region event is "same as character",
  50.  then you can activate it when you stand in front of it. If the region event is
  51.  "below character", then you must stand on it to activate it.
  52.  
  53.  Region events obey page conditions.
  54.  
  55.  -- Creating Region Events --
  56.  
  57.  There are several ways to create a region event. One way is to note-tag the
  58.  map with
  59.  
  60.    <region event: regionID eventID>
  61.    
  62.  One region can only have at most one referenced event. For example, region 1
  63.  might reference event 2.
  64.  
  65.  Multiple regions can reference the same event. For example, both region 1 and
  66.  region 2 might reference event 2.
  67.  
  68.  Simply add more note-tags for each region as required.
  69.  An extended note-tag is also available for maps:
  70.  
  71.    <region events>
  72.      regionID: eventID
  73.      regionID: eventID
  74.    </region events>
  75.    
  76.  It is just an alternative if you prefer that over individual note-tags.
  77.  
  78.  The second way to creating region events is to note-tag events themselves.
  79.  Create a comment, then note-tag it with
  80.  
  81.    <region event: regionID>
  82.    
  83.  Note that the event ID used will be the ID of this event.
  84.  
  85.  -- Changing Region Events --
  86.  
  87.  Region events can be changed during the game using script calls:
  88.  
  89.    change_region_event(regionID, eventID)
  90.    remove_region_event(regionID)
  91.    
  92. --------------------------------------------------------------------------------
  93.  ** Example
  94.  
  95.  If you want to have region 1 reference event 3, you would make the script call
  96.  
  97.    change_region_event(1, 3)
  98.    
  99.  This means that when region event 1 is activated, it will run event 3.
  100.  To remove region event 1, you can use the script call
  101.  
  102.    remove_region_event(1)
  103.  
  104. #===============================================================================
  105. =end
  106. $imported = {} if $imported.nil?
  107. $imported["TH_RegionEvents"] = true
  108. #===============================================================================
  109. # ** Configuration
  110. #===============================================================================
  111. module TH
  112.   module Region_Events
  113.    
  114.     # Specify both region ID and event ID for map note-tag
  115.     Regex = /<region[-_ ]event:\s*(\d+)\s*(\d+)\s*>/i
  116.     Ext_Regex = /<region[-_ ]events>(.*?)<\/region[-_ ]events>/im
  117.    
  118.     # Event ID is implied with event note-tags
  119.     Event_Regex = /<region[-_ ]event:\s*(\d+)\s*>/i
  120.   end
  121. end
  122. #===============================================================================
  123. # ** Rest of Script
  124. #===============================================================================
  125. module RPG
  126.   class Event
  127.    
  128.     def region_event
  129.       parse_region_event unless @region_event
  130.       return @region_event
  131.     end
  132.    
  133.     def parse_region_event
  134.       @region_event = Data_RegionEvent.new
  135.       self.pages[0].list.each do |cmd|
  136.         if cmd.code == 108 && cmd.parameters[0] =~ TH::Region_Events::Event_Regex
  137.           @region_event.region_id = $1.to_i
  138.           break
  139.         end
  140.       end
  141.     end
  142.   end
  143.  
  144.   class Map
  145.     def region_events
  146.       load_notetag_region_events unless @region_events
  147.       return @region_events
  148.     end
  149.    
  150.     def load_notetag_region_events
  151.       @region_events = {}
  152.      
  153.       # compact note-tag
  154.       results = self.note.scan(TH::Region_Events::Regex)
  155.       results.each do |res|
  156.         region_id = res[0].to_i
  157.         event_id = res[1].to_i
  158.         @region_events[region_id] = event_id
  159.       end
  160.      
  161.       # extended note-tag
  162.       results = self.note.scan(TH::Region_Events::Ext_Regex)
  163.       results.each do |res|
  164.         res[0].strip.split("\r\n").each do |line|
  165.           data = line.split(":")
  166.           region_id = data[0].to_i
  167.           event_id = data[1].to_i
  168.           @region_events[region_id] = event_id
  169.         end
  170.       end
  171.     end
  172.   end
  173. end
  174.  
  175. class Data_RegionEvent
  176.  
  177.   attr_accessor :region_id
  178.  
  179.   def initialize
  180.     @region_id = -1
  181.   end
  182. end
  183.  
  184. class Game_Map
  185.  
  186.   alias :th_region_events_setup_events :setup_events
  187.   def setup_events
  188.     setup_region_events
  189.     th_region_events_setup_events
  190.   end
  191.  
  192.   #-----------------------------------------------------------------------------
  193.   # Create a hash of region events. The key is the region ID, and the value is
  194.   # the ID of the event that will be called. The ID can be changed at anytime
  195.   #-----------------------------------------------------------------------------
  196.   def setup_region_events
  197.     @region_events = {}
  198.     # Region events from map note-tag
  199.     @map.region_events.each do |region_id, event_id|
  200.       @region_events[region_id] = event_id
  201.     end
  202.     # Region events from events on the map
  203.     @map.events.each do |id, event|
  204.       data = event.region_event
  205.       if data.region_id != -1
  206.         @region_events[data.region_id] = id
  207.       end
  208.     end
  209.   end
  210.  
  211.   #-----------------------------------------------------------------------------
  212.   # Also need to check if there are any region events at this position.
  213.   # Assumes only the player can trigger this.
  214.   #-----------------------------------------------------------------------------
  215.   alias :th_region_events_events_xy :events_xy
  216.   def events_xy(x, y)
  217.     res = th_region_events_events_xy(x, y)
  218.     region_id = region_id(x, y)
  219.     region_event_id = @region_events[region_id]
  220.     event = @events[region_event_id]
  221.     if event
  222.       res.push(event)
  223.     end
  224.     return res
  225.   end
  226.  
  227.   def set_region_event(region_id, event_id)
  228.     if @events[event_id]
  229.       @region_events[region_id] = event_id
  230.     end
  231.   end
  232.  
  233.   def remove_region_event(region_id)
  234.     @region_events.delete(region_id)
  235.   end
  236. end
  237.  
  238. class Game_Interpreter
  239.  
  240.   def change_region_event(region_id, event_id)
  241.     $game_map.set_region_event(region_id, event_id)
  242.   end
  243.  
  244.   def remove_region_event(region_id)
  245.     $game_map.remove_region_event(region_id)
  246.   end
  247. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement