Advertisement
Loque

Fog regione

Jun 18th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.08 KB | None | 0 0
  1. =begin
  2. #===============================================================================
  3.  Title: Region Fog
  4.  Author: Hime
  5.  Date: Apr 18, 2014
  6. --------------------------------------------------------------------------------
  7.  ** Change log
  8.  Apr 18, 2014
  9.    - fixed crash issue when region fog was not created
  10.  Sep 3, 2013
  11.    - properly disposes region fog
  12.    - added support for custom fog tiles
  13.    - introduced extended note-tag
  14.  Sep 1, 2013
  15.    - Initial release
  16. --------------------------------------------------------------------------------  
  17.  ** Terms of Use
  18.  * Free to use in non-commercial projects
  19.  * Contact me for commercial use
  20.  * No real support. The script is provided as-is
  21.  * Will do bug fixes, but no compatibility patches
  22.  * Features may be requested but no guarantees, especially if it is non-trivial
  23.  * Credits to Hime Works in your project
  24.  * Preserve this header
  25. --------------------------------------------------------------------------------
  26.  ** Description
  27.  
  28.  This script allows you to control fog of war using region tiles.
  29.  Each map can assign multiple different regions of fog. Different fog regions
  30.  are separate from one another.
  31.  
  32.  Script calls are used to show or hide region fog.
  33.  
  34. --------------------------------------------------------------------------------
  35.  ** Required
  36.  
  37.  Script: Bit Switches
  38.  (http://himeworks.wordpress.com/2013/06/07/bit-switches/)
  39.  
  40.  Image: region_fog_tileset
  41.  
  42. --------------------------------------------------------------------------------
  43.  ** Installation
  44.  
  45.  Place this script below Bit Switches and above Main.
  46.  Place the "region_fog_tileset" image into your Graphics/System folder
  47.  
  48. --------------------------------------------------------------------------------
  49.  ** Usage
  50.  
  51.  The simple note-tag allows you to specify all of the region fogs and their
  52.  switches in the same tag:
  53.  
  54.    <region fog>
  55.    region: x1, switch: y1
  56.    region: x2, switch: y2
  57.    </region fog>
  58.    
  59.  Where x and y are numbers. You can assign the same switch to multiple regions,
  60.  across different maps as well.
  61.  
  62.  If you require more advanced options, or don't want to use all of the options
  63.  available, use the extended note-tag as follows:
  64.  
  65.    <region fog: 2>
  66.      switch: 2
  67.      tile: 1
  68.    </region fog>
  69.    
  70.  Which will designate region 2 as a region fog, whose visibility is based on
  71.  bit-switch 2, and uses tile 1.
  72.  
  73.  -- Using custom tiles --
  74.  
  75.  By default, the fog tile you get is black. If you look at the
  76.  region_fog_tileset image, you will see a 32x32 black square near the top-left.
  77.  
  78.  Each tile has an ID. The top-left tile is tile 0, which is no fog.
  79.  The one next to it is tile 1, which is the default black fog. You can add your
  80.  own fog tiles as well, and reference in the extended note-tag if you want
  81.  specific regions to use specific fog tiles.
  82.  
  83.  -- Changing fog visibility --
  84.  
  85.  To show or hide the a region fog, you will need to change the value of the
  86.  switch. These are bit switches, so you won't be able to use the "Control
  87.  Switches" event command to change them.
  88.  
  89.  To hide the fog, make the script call
  90.  
  91.    hide_region_fog(switch_id)
  92.    
  93.  To show the fog, make the script call
  94.  
  95.    show_region_fog(switch_id)
  96.    
  97. --------------------------------------------------------------------------------
  98.  ** Example
  99.  
  100.  Here's a sample region fog setup:
  101.  
  102.    <region fog>
  103.      region: 1, switch: 1
  104.      region: 2, switch: 2
  105.      region: 3, switch: 1
  106.    </region fog>
  107.    
  108.  This means that the fog for regions 1 and 3 use the same bitswitch.
  109.  If you called
  110.  
  111.    hide_region_fog(1)
  112.    
  113.  This would hide the fog for both region 1 and region 3.
  114.  
  115. #===============================================================================
  116. =end
  117. $imported = {} if $imported.nil?
  118. $imported["TH_RegionFog"] = true
  119. #===============================================================================
  120. # ** Configuration
  121. #===============================================================================
  122. module TH
  123.   module Region_Fog
  124.    
  125.     # Variable that will store the region fog settings
  126.     Var_ID = 1
  127. #===============================================================================
  128. # ** Rest of Script
  129. #===============================================================================    
  130.     Regex = /<region[-_ ]fog>(.*?)<\/region[-_ ]fog>/im
  131.     Data_Regex = /region:\s*(\d+)\s*,\s*switch:\s*(\d+)/
  132.    
  133.     Ext_Regex = /<region[-_ ]fog:\s*(\d+)\s*>(.*?)<\/region[-_ ]fog>/im
  134.   end
  135. end
  136.  
  137. module RPG
  138.   class Map
  139.    
  140.     def fog_regions
  141.       load_notetag_region_fog unless @fog_regions
  142.       return @fog_regions
  143.     end
  144.    
  145.     def load_notetag_region_fog
  146.       @fog_regions = {}
  147.       res = self.note.match(TH::Region_Fog::Regex)
  148.       if res
  149.         res[1].strip.split("\r\n").each do |data|
  150.           data =~ TH::Region_Fog::Data_Regex
  151.           region_id = $1.to_i
  152.           switch_id = $2.to_i
  153.          
  154.           fogData = Data_RegionFog.new(region_id)
  155.           fogData.switch_id = switch_id
  156.           @fog_regions[region_id] = fogData
  157.         end
  158.       end
  159.        
  160.       res = self.note.scan(TH::Region_Fog::Ext_Regex)
  161.       res.each do |data|
  162.         region_id = data[0].to_i
  163.         switch_id = 0
  164.         tile_id = 1
  165.         data[1].split("\r\n").each do |option|
  166.           if option =~ /switch:\s*(\d+)/i
  167.             switch_id = $1.to_i
  168.           elsif option =~ /tile:\s*(\d+)/i
  169.             tile_id = $1.to_i
  170.           end
  171.         end
  172.         fogData = Data_RegionFog.new(region_id)
  173.         fogData.switch_id = switch_id
  174.         fogData.tile_id = tile_id
  175.         @fog_regions[region_id] = fogData
  176.       end
  177.     end
  178.   end
  179. end
  180.  
  181. class Data_RegionFog
  182.  
  183.   attr_accessor :region_id
  184.   attr_accessor :switch_id
  185.   attr_accessor :tile_id
  186.  
  187.   def initialize(region_id)
  188.     @region_id = region_id
  189.     @switch_id = 0
  190.     @tile_id = 1
  191.   end
  192. end
  193.  
  194. class Game_Interpreter
  195.  
  196.   def show_region_fog(switch_id)
  197.     $game_map.change_region_fog(switch_id, false)
  198.   end
  199.  
  200.   def hide_region_fog(switch_id)
  201.     $game_map.change_region_fog(switch_id, true)
  202.   end
  203. end
  204.  
  205. class Game_Map
  206.   include TH::Bit_Switches
  207.  
  208.   attr_reader :region_fog
  209.  
  210.   alias :th_region_fog_setup :setup
  211.   def setup(map_id)
  212.     @region_fog_cache = {}
  213.     th_region_fog_setup(map_id)
  214.     setup_region_fog
  215.   end
  216.  
  217.   alias :th_region_fog_refresh :refresh
  218.   def refresh
  219.     update_region_fogs
  220.     th_region_fog_refresh
  221.   end
  222.  
  223.   def setup_region_fog
  224.     @region_fog = Table.new(data.xsize, data.ysize, data.zsize)
  225.     build_region_cache
  226.     update_region_fogs
  227.   end
  228.  
  229.   #-----------------------------------------------------------------------------
  230.   # Pre-compute all of the tiles that need to be updated for a given region
  231.   # so we don't need to do this everytime
  232.   #-----------------------------------------------------------------------------
  233.   def build_region_cache
  234.     for x in 0...@region_fog.xsize
  235.       for y in 0...@region_fog.ysize
  236.         rid = region_id(x, y)
  237.         if rid > 0
  238.           @region_fog_cache[rid] = [] unless @region_fog_cache[rid]
  239.           @region_fog_cache[rid] << [x,y]
  240.         end
  241.       end
  242.     end
  243.   end
  244.  
  245.   def update_region_fogs
  246.     @map.fog_regions.each do |region_id, fogData|
  247.       update_region_fog(region_id, fogData)
  248.     end
  249.   end
  250.  
  251.   def update_region_fog(region_id, fogData)
  252.     val = bit_switch?(TH::Region_Fog::Var_ID, fogData.switch_id)
  253.     tile_id = val ? 0 : fogData.tile_id
  254.     return unless @region_fog_cache[region_id]
  255.     @region_fog_cache[region_id].each do |(x,y)|
  256.       @region_fog[x, y, 2] = tile_id
  257.     end
  258.   end
  259.  
  260.   #-----------------------------------------------------------------------------
  261.   # Get the bitswitch assigned to this region
  262.   #-----------------------------------------------------------------------------
  263.   def get_fog_bitswitch(region_id)
  264.     @map.fog_regions[region_id]
  265.   end
  266.  
  267.   #-----------------------------------------------------------------------------
  268.   # Show or hide the fog for the given region
  269.   #-----------------------------------------------------------------------------
  270.   def change_region_fog(switch_id, val)
  271.     set_bit_switch(TH::Region_Fog::Var_ID, switch_id, val)
  272.   end
  273. end
  274.  
  275. class Spriteset_Map
  276.  
  277.   alias :th_region_fog_create_tilemap :create_tilemap
  278.   def create_tilemap
  279.     th_region_fog_create_tilemap
  280.     create_region_fog
  281.   end
  282.  
  283.   def create_region_fog
  284.     @region_fog = Tilemap.new(@viewport2)
  285.     @region_fog.map_data = $game_map.region_fog
  286.     load_region_tileset
  287.   end
  288.  
  289.   def load_region_tileset
  290.     @region_fog.bitmaps[5] = Cache.system("region_fog_tileset")
  291.   end
  292.  
  293.   alias :th_region_fog_update :update
  294.   def update
  295.     th_region_fog_update
  296.     update_region_fog if @region_fog
  297.   end
  298.  
  299.   def update_region_fog
  300.     @region_fog.map_data = $game_map.region_fog
  301.     @region_fog.ox = $game_map.display_x * 32
  302.     @region_fog.oy = $game_map.display_y * 32
  303.     @region_fog.update
  304.   end
  305.  
  306.   alias :th_region_fog_dispose :dispose
  307.   def dispose
  308.     th_region_fog_dispose
  309.     dispose_region_fog
  310.   end
  311.  
  312.   def dispose_region_fog
  313.     @region_fog.dispose if @region_fog
  314.   end
  315. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement