BDR Layer v1.3

BDR Layer Version: 1.3 for RMVX
By: Fustel

Introduction
This script allow the display of multiple maps in a single map scene. The extra map are considered layers shown over the base map.
The best usage I found for it, and for naw, is the show/hide building roofs, while preserving what is inside the building. This is not a redrawing of the building interior. This is 'really' a multi-layered map.

Features
  • Multi-layered maps
  • Show / Hide each layer individually or as a whole
  • Select the layer management method
  • Make all the changes now, or ready them for the next map to load

Screenshots
Roof Removal Show
Image
with a roof

Image
without a roof


Demo
Roof removal Show
http://www.mediafire.com/?z8rs8ocl6uuhgom

Look at the cool & quick roof removal when you enter a hut.
And now, don't hesitate to go down the cellar...


Script
  1. #==============================================================================
  2. # ** BDR Layer
  3. #------------------------------------------------------------------------------
  4. #  © Fustel, 2010
  5. #  26/05/10
  6. #  Version 1.2
  7. #------------------------------------------------------------------------------
  8. #  VERSION HISTORY:
  9. #   - 1.0 (26/05/10): Initial release
  10. #   - 1.1 (15/06/10): allows usage of small / big layers and their positioning
  11. #   - 1.2 (05/11/10): corrected bug when reloading a previos map
  12. #                     allows hide_layer & show_layer method without parametes
  13. #                       to hide/show all layers
  14. #                     allows the setting of the layer management method
  15. #   - 1.3 (24/01/11): dynamically (script) select the displayrd layers for the next map
  16. #                     dynamically (script) select the display method
  17. #                     method can alos be set to take effect only on next map
  18. #                     bugs fixed
  19. #                       - save / load visibility flag
  20. #                       - bad display at first graphic update when
  21. #                         returning from a scene (menu & others...)
  22. #--------------------------------------------------------------------------------
  23. #  INSTRUCTIONS:
  24. #   - Place this script in the 'Materials' section
  25. #   - Create layers as sub-maps. They may be of any size
  26. #   - Link layers to the main map (see CONFIGURATION below)
  27. #-------------------------------------------------------------------------------
  28. # CONFIGURATION
  29. #  - Update the BDR::Layer::Layer constant
  30. #    - key  = base map ID
  31. #    - data = [ [ layer ID (MUST be unique)
  32. #                 layer map ID
  33. #                 layer viewport z value (height)
  34. #                 layer visible at start (true/false)
  35. #                 (optional, default=0) X position of layer in main map
  36. #                 (optional, default=0) Y position of layer in main map
  37. #               ]
  38. #               , ...
  39. #             ]
  40. #
  41. #  - If necessary, update the BDR::Layer::Method constant for the default method
  42. #    - 1 for viewport repositionning
  43. #    - 2 for viewport offset
  44. #    - 3 for tilemep  offset
  45. #-------------------------------------------------------------------------------
  46. # USAGE (provinding $scene is a Scene_Map)
  47. #  - show a layer :
  48. #      $scene.show_layer(<layer ID>)
  49. #  - show all layers :
  50. #      $scene.show_layer
  51. #  - hide a layer :
  52. #      $scene.hide_layer(<layer ID>)
  53. #  - hide all layers :
  54. #      $scene.hide_layer
  55. #  - change the layer being displayed when entering next map
  56. #    (only listed layer are shown) :
  57. #      $scene.show_layers_on_next_map([<layer_ids>])
  58. #  - change the display method
  59. #      $game_system.bdr_layer_method = <n>
  60. #  - set the display method for next map
  61. #      $game_sustem.bdr_layer_method_on_next_map = <n>
  62. #-------------------------------------------------------------------------------
  63. # NOTE
  64. #  - about the z value of the layer viewport, remomber
  65. #    - special effects(weather, pictures) are at z = 50
  66. #    - the scene brightness is at z = 100
  67. #  - For still unknown reasons, the original viowport repositioning layer
  68. #      management method didn't seem to work for some projects. After multiple
  69. #      experiments , I decided to provide the 3 managements methods that I
  70. #      imagined. If one of them doesn't go well with your project, please try
  71. #      the other ones, and report.
  72. #-------------------------------------------------------------------------------
  73.  
  74. module BDR
  75.   module Layer
  76.     # 1 = viewport reposionning
  77.     # 2 = viewport offset
  78.     # 3 = tilemap  offset
  79.     #--------------------------
  80.     Method = 1
  81.  
  82.     # key  = map ID
  83.     # data = [ [ layer ID,
  84.     #            layer map ID,
  85.     #            viewport z value,
  86.     #            visible at start (true/false),
  87.     #            (optional) layer X position in main map (default = 0),
  88.     #            (optional) layer Y position in main map (default = 0)
  89.     #          ],
  90.     #          ...
  91.     #        ]
  92.     #---------------------------------------
  93.     Layer = {}
  94.     Layer[1] = [ [ 1, 2, 1, true,  4, 2],   # left hut roof
  95.                  [ 2, 2, 1, true, 14, 2]]   # right hut roof
  96.  
  97.     Layer[3] = [ [ 1, 4, 2, false],
  98.                  [ 2, 5, 2, false],
  99.                  [ 3, 6, 2, false]]
  100.   end
  101. end
  102.  
  103. class Game_Temp
  104.   attr_accessor :bdr_layer_show_on_next_map
  105. end
  106.  
  107. class Game_System
  108.   attr_accessor :bdr_layer_method_on_next_map
  109.  
  110.   def bdr_layer_method
  111.     return BDR::Layer::Method if @bdr_layer_method == nil
  112.     return @bdr_layer_method
  113.   end
  114.  
  115.   def bdr_layer_method=(value)
  116.     @bdr_layer_method = value if [1,2,3].include?(value)
  117.   end
  118.  
  119.   def bdr_layer_visible
  120.     @bdr_layer_visible = {} if @bdr_layer_visible == nil
  121.     return @bdr_layer_visible if @bdr_layer_map_id == $game_map.map_id
  122.     @bdr_layer_map_id = $game_map.map_id
  123.     @bdr_layer_visible = {}
  124.     layers = BDR::Layer::Layer[ @bdr_layer_map_id]
  125.     return @bdr_layer_visible if layers == nil
  126.     for layer in layers
  127.       @bdr_layer_visible[ layer[ 0]] = layer[ 3]
  128.     end
  129.     return @bdr_layer_visible if $game_temp.bdr_layer_show_on_next_map == nil
  130.     for id in @bdr_layer_visible.keys
  131.       @bdr_layer_visible[ id] = $game_temp.bdr_layer_show_on_next_map.include?( id)
  132.     end
  133.     $game_temp.bdr_layer_show_on_next_map = nil
  134.     return @bdr_layer_visible
  135.   end
  136. end
  137.  
  138. class Game_Map
  139.   attr_reader :bdr_layers
  140.  
  141.   alias bdr_layer_initialize initialize
  142.   def initialize
  143.     @bdr_layers = {}
  144.     bdr_layer_initialize
  145.   end
  146.  
  147.   alias bdr_layer_setup setup
  148.   def setup(map_id)
  149.     @bdr_layers = {}
  150.     bdr_layer_setup( map_id)
  151.     $game_system.bdr_layer_method = $game_system.bdr_layer_method_on_next_map if $game_system.bdr_layer_method_on_next_map != nil
  152.     $game_system.bdr_layer_method_on_next_map = nil
  153.     layers = BDR::Layer::Layer[ map_id]
  154.     layers = [] if layers == nil
  155.     for layer in layers
  156.       lid = layer[ 0]
  157.       @bdr_layers[ lid] = layer.clone
  158.       @bdr_layers[ lid][1] = load_data(sprintf("Data/Map%03d.rvdata", layer[ 1]))
  159.       @bdr_layers[ lid][4] = 0 if layer[4] == nil
  160.       @bdr_layers[ lid][5] = 0 if layer[5] == nil
  161.     end
  162.   end
  163. end
  164.  
  165. class Spriteset_BDR_Layer
  166.   def initialize( layer)
  167.     @id = layer[ 0]
  168.     @pos_x = layer[ 4] * 32
  169.     @pos_y = layer[ 5] * 32
  170.     create_viewport( layer)
  171.     create_tilemap( layer)
  172.     refresh
  173.   end
  174.  
  175.   def create_viewport( layer)
  176.     @viewport = Viewport.new( @pos_x, @pos_y, layer[1].width * 32, layer[1].height * 32)
  177.     @viewport.z = layer[ 2]
  178.   end
  179.  
  180.   def create_tilemap( layer)
  181.     @tilemap = Tilemap.new(@viewport)
  182.     @tilemap.bitmaps[0] = Cache.system("TileA1")
  183.     @tilemap.bitmaps[1] = Cache.system("TileA2")
  184.     @tilemap.bitmaps[2] = Cache.system("TileA3")
  185.     @tilemap.bitmaps[3] = Cache.system("TileA4")
  186.     @tilemap.bitmaps[4] = Cache.system("TileA5")
  187.     @tilemap.bitmaps[5] = Cache.system("TileB")
  188.     @tilemap.bitmaps[6] = Cache.system("TileC")
  189.     @tilemap.bitmaps[7] = Cache.system("TileD")
  190.     @tilemap.bitmaps[8] = Cache.system("TileE")
  191.     @tilemap.map_data = layer[ 1].data
  192.     @tilemap.passages = $data_system.passages
  193.   end
  194.  
  195.   def dispose
  196.     dispose_tilemap
  197.     dispose_viewport
  198.   end
  199.  
  200.   def dispose_tilemap
  201.     @tilemap.dispose
  202.   end
  203.  
  204.   def dispose_viewport
  205.     @viewport.dispose
  206.   end
  207.  
  208.   def refresh
  209.     update_viewport
  210.     update_tilemap
  211.   end
  212.  
  213.   def update
  214.     refresh
  215.   end
  216.  
  217.   def update_tilemap
  218.     return unless self.visible
  219.     @tilemap.ox = $game_map.display_x / 8 + $game_map.screen.shake if $game_system.bdr_layer_method == 3
  220.     @tilemap.oy = $game_map.display_y / 8 if $game_system.bdr_layer_method == 3
  221.     @tilemap.update
  222.   end
  223.  
  224.   def update_viewport
  225.     @viewport.visible = $game_system.bdr_layer_visible[ @id]
  226.     if !self.visible
  227.       @viewport.update
  228.       return
  229.     end
  230.     @viewport.tone = $game_map.screen.tone
  231.     @viewport.rect.x = @pos_x - $game_map.display_x / 8 + $game_map.screen.shake if $game_system.bdr_layer_method == 1
  232.     @viewport.rect.y = @pos_y - $game_map.display_y / 8 if $game_system.bdr_layer_method == 1
  233.     @viewport.ox = $game_map.display_x / 8 + $game_map.screen.shake if $game_system.bdr_layer_method == 2
  234.     @viewport.oy = $game_map.display_y / 8 if $game_system.bdr_layer_method == 2
  235.     @viewport.update
  236.   end
  237.  
  238.   def visible
  239.     return @viewport.visible
  240.   end
  241.  
  242.   def visible=( value)
  243.     $game_system.bdr_layer_visible[ @id] = value
  244.     refresh
  245.   end
  246.  
  247.   def show
  248.     self.visible = true
  249.   end
  250.  
  251.   def hide
  252.     self.visible = false
  253.   end
  254. end
  255.  
  256. class Spriteset_Map
  257.   alias bdr_layer_initialize initialize
  258.   def initialize
  259.     @bdr_layers = {}
  260.     bdr_layer_initialize
  261.     for layer in $game_map.bdr_layers.values
  262.       @bdr_layers[ layer[ 0]] = Spriteset_BDR_Layer.new( layer)
  263.     end
  264.   end
  265.  
  266.   alias bdr_layer_dispose dispose
  267.   def dispose
  268.     for layer in @bdr_layers.values
  269.       layer.dispose
  270.     end
  271.     bdr_layer_dispose
  272.   end
  273.  
  274.   alias bdr_layer_update update
  275.   def update
  276.     bdr_layer_update
  277.     for layer in @bdr_layers.values
  278.       layer.update
  279.     end
  280.   end
  281.  
  282.   def show_layer( id=nil)
  283.     if id == nil
  284.       for layer in @bdr_layers.values
  285.         layer.show
  286.       end
  287.     else
  288.       layer = @bdr_layers[ id]
  289.       layer.show if layer != nil
  290.    end
  291.   end
  292.  
  293.   def hide_layer( id=nil)
  294.     if id == nil
  295.       for layer in @bdr_layers.values
  296.         layer.hide
  297.       end
  298.     else
  299.       layer = @bdr_layers[ id]
  300.       layer.hide if layer != nil
  301.    end
  302.   end
  303. end
  304.  
  305. class Scene_Map < Scene_Base
  306.   def show_layer( id=nil)
  307.     @spriteset.show_layer( id)
  308.   end
  309.  
  310.   def hide_layer( id=nil)
  311.     @spriteset.hide_layer( id)
  312.   end
  313.  
  314.   def show_layers_on_next_map( layers_id)
  315.     $game_temp.bdr_layer_show_on_next_map = layers_id
  316.   end
  317. end
  318.  
Expand to see the code.


Instructions
  • Copy the script in the 'Materials' section, as it fully overwrite the main methods
  • Create layers for your base maps. Each layer may be any size, and generally includes many transparent tiles
  • Link base map and layer map through the BDR::Layer::Layer constant. This constant is a hash with the following structure
    • key = base map ID
    • data = array of arrays. each sub-array defines a layer with the following structure
      • [0] = layer ID (used for show/hide and future features). MUST be unique
      • [1] = layer map ID.
      • [2] = layer z value. The higher this value, the closer to the player the layer is. Remeber the following:
        • Base map is at z=0
        • Special effects (weathe, pictures, etc...) are at z=50
        • Map brightness is at z*100
      • [3] = visibility of the layer at map loading time (true / false)
      • [4] (optional, default=0) X position of layer map in main map
      • [5] (optional, default=0) Y position of layer map in main map
  • Set the default layer management method, if necessary through the BDR::Layer::Method constant
    • 1 = Viewport repositionning (the default one in the script)
    • 2 = Viewport offset
    • 3 = Tilemap offset
  • Show a layer by invoking: '$scene.show_layer(<layer ID>)
  • Show all layers by invoking: '$scene.show_layer
  • Hide a layer by invoking: '$scene.hide_layer(<layer ID>)
  • Hide all layers by invoking: '$scene.hide_layer
  • Prepare layers to be displayed on next map by invoking (only listed layers will be visible): '$scene.show_layers_on_next_map([<layer_ids>])'
  • Change the display method with: '$game_system.bdr_layer_method = <n>'
  • Set the display method for the next map with: '$game_sustem.bdr_layer_method_on_next_map = <n>'


FAQ

Compatibility
No issue known.

Credits and Thanks
All done by myself
Also, if I remember well, Dargor was working on something similar called 'Map Layers'; but I couldn't dind the reference.

Author's Notes
This is the first step to a full layered map, with player transition from layer to layer, each beeing a 'real map', not just a display thing.
It is intended as a test; so use & abuse it, and tell me if yoy nitice any stress (CPU, meory, internal ressource).

v1.1
The layer maps need not anymore to be the same size as the base map.
Technically, the viewport is now re-positioned instead of the tilemap.

v1.2
  • Corrected a bug when reentering a map with layers
  • Allow the showing/hiding of all layers in a single call
  • Introduced the management method

v1.3
  • dynamically (script) select the displayrd layers for the next map
  • dynamically (script) select the display method
  • method can alos be set to take effect only on next map
  • bugs fixed
    • save / load visibility flag
    • bad display at first graphic update when returning from a scene (menu & others...)

Terms and Conditions
You may not use this in a commercial game without my explicit permission.
You may not post this script anywhere without my explict permission.
You must give me credit.
This works nicely, but a fade-in fade-out roof would be even nicer.
The roof example reminded me about a solution for that very same problem I tried to find once... I ended up having pictures that dispose themselves when the person walks underneath them, essentially creating an almost entirely automated (obviously, you need to initialize the images) handler of self-hiding roofs. I think it's resource-friendlier than your method on the system side... of course you get relatively more filesize (I thought about having kind of an auto-roof creation from tiled pictures, which would downright minimize the filesize to not a bit more), however, I never got around to do it.
EDIT: It's also transparency-compatible ^^

But yeah... I don't want to create the impression that this script is for roofs only... I especially see the purpose in circumventing VX' donkey default mapping possibilities. I doubt it'll get more people to use VX though, sadly...

So, let's skip to the suggestion part... your scripting is comparatively outstanding, even though there's the totally undistinguishable 'BDR' in there everywhere... other than that, practical suggestions:
- handling the map layers via map names would be more confident in my opinion... it should be perfectly possible and would ease working with layers greatly.
- supporting smaller maps to overlay bigger maps would be pretty handy... especially for stuff like that hut, where a 17x13 map is perfectly sufficient in theory. Now I don't know for sure how much trouble that means, but in theory, it should be as easy as shifting the layers by x*32 and y*32 pixels, while x and y are either in your array, if you keep it, or in the map name.

I thought Ihad some more, but I either forgot them while writing this or overestimated my improvement suggestions XD

Keep up the good work!
BlueScope wrote:
....even though there's the totally undistinguishable 'BDR' in there everywhere..

I don't know why.... but I was expecting this one.... 😉

BlueScope wrote:
- handling the map layers via map names would be more confident in my opinion... it should be perfectly possible and would ease working with layers greatly.

Map name are more often changed than map ID. Think about maintainance.

BlueScope wrote:
- supporting smaller maps to overlay bigger maps would be pretty handy... especially for stuff like that hut, where a 17x13 map is perfectly sufficient in theory. Now I don't know for sure how much trouble that means, but in theory, it should be as easy as shifting the layers by x*32 and y*32 pixels, while x and y are either in your array, if you keep it, or in the map name.

Already tried it.... but a smaller map repeats drawing itself over and over the bigger one. It is hard-voded in the Tilemap class for, I think, map looping management.
Unless I also do something about the viewport....mmm...no, that mean we also have to manage the viewport position during update... forget it. Layer map HAVE to be the same size as the base map... for now...

As I said, roof management is only one the things we can do with layers. And this is only the first step to something else....
Well, you put the BDR there, so take it! :haha:

As for the map name, I think you misunderstood me... I don't mean store the maps by name instead of ID in the script (that indeed would be pretty stupid to do, agreed) - I mean put the layering management in the map name. Something like this:
  1. Super-Town [34|2]
Expand to see the code.

That translates to "Super-Town is the second layer for the 34th map set" - assuming you don't plan on using the seperate maps as, well, seperate maps, but only in the corresponding packages (but I think your current script assumes that as well).
After writing the following paragraph (yay time travelling), I realize it's probably better to include the map IDs of the map to layer in the base map's name. I'd elaborate further, but you're a pretty smart guy, so you should be able to figure it out... and also, work is over for today... yay freetime! ^^

And yeah, the repeating issue you talked about sounds a bit like in-the-way... however, a Tilemap rewrite should solve it, no? I remember looking for some, but couldn't find any... maybe that's changed in the meantime, but if not, it'd be a good reason (for you XD ) to make one, right? ^^ I really think it'd be worth trying to fiddle with it... (think resource weight reduced, reusuability, ...)
Including the layers in the map name sitll doesn't apeal to me.
  • It means the name have a meaning (not semantically acceptable)
  • It means parsing the name... and I don't like parsing name, especially with multiple parameters for each layer
  • Try & imagine a HUD displaying the map name...

As for the smaller map usage. Tilemap, although it have a Ruby interface, is a built-in class; and if you explain me how to rewrite a built-in class, I'd be eager to do it :tongue:
I also imagined (during my 'weight-management-time' :blush: ) a way to use viewport sizing & positioning. But I am not sure of whait it will do with looping map, and especially with the Player navigating from layer to layer (THIS is the big idea...).
So, for now, I keep the 'layer MUST be the same size as the base map' principle.
Yeah, I see where you're coming from in all points...

Regarding the Tilemap rewrite, I know it's a built-in class and therefore written in C. That doesn't mean, however, that you can't simply recreate it in Ruby, as in start with class Tilemap; end and go from there, trying to get all methods you need rewritten. Not knowing what they're called is arguably a difficulty, but as it's been done for RMXP a few times, you should be able to get an idea from there... even though they hardly have anything in common.
In fact, I know at least ONE thing they have not in common. VX Tilemap gors directly to the hardware, while XP used standard Windows canvas. I know this becouse my magnifying software (you know I am near-blind, didn't you) enlarges XP windowed game, but not VX one.
And moreover, I am ABSOLUTELY a C-unlover... I am one of those old-time Pascal & Delphi-er :heart:

Someone ???
Fustel wrote:
This scrit allow the display of multiple maps...


Sorry, but this is the one thing about this post that bugs me. If it's supposed to be a script, then well done. Fantastic. If it's a scrit, then I'm not sure what to think if it. I've never heard of a scirt.

~Its a typo, everyone makes them and in no way warrants that kind of behavior.
Chalupa wrote:
Fustel wrote:
This scrit allow the display of multiple maps...


Sorry, but this is the one thing about this post that bugs me. If it's supposed to be a script, then well done. Fantastic. If it's a scrit, then I'm not sure what to think if it. I've never heard of a scirt.



What kind of choice do I have but to answer..... Ooops... :blush:
Due to unanimous plebicite, as well as to the fact the (soon-to-be-released ???) full multi-layered map won't have exactly the same structure as this restricted layered one, v1.1 is up.

The only upgrade (but not the least) is about the layer size. You can now make layers of ANY size. Not the base map one anymore.

Enjoy.
Back to BDR Layer....
Trying to use it in a new project, a couple of issues appear.

1/ The nned to hide/show all layers in one call. This is done with show_layer and, hide_layer, without any argument.
2/ An ENORMOUS bug when re-entering a map with layers. This is corrected.
3/ An annoying issue for which I still do not have any precise explanation. It just happened the method I used to manage the layers (moving its Viewport around) doen't work in this project. After some experiments, I implement a nex method, using the Viewport offset. Which in turn didn't work with the demo. Thus I implemented the 3 methods I found, allowing everybody to choose which one to use. But if anyone has a clear explanation about Viewport.rect, Viewport.ox (and oy) and Tilemap.ox (and oy), PLEEAAASSSSEEEE enlight us.....
After using and abusing the script myself (in a grant vintage project soon to come...), I noticesd some bugs & flaws that are corected in this version. See the Notes and Instructions on the may post for more details

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=71059