Project1

标题: 如何让地图输出脚本,不进行图片压缩。 [打印本页]

作者: 574656549    时间: 2011-8-6 21:59
提示: 作者被禁止或删除 内容自动屏蔽
作者: 英顺的马甲    时间: 2011-8-6 22:04
这个么?http://gameface101.playogame.com/t920-map-screen-shot-rmxp
  1.      
  2.     #==============================================================================
  3.     # ** Map Screenshot
  4.     #==============================================================================
  5.     #
  6.     # - Credits: Cycleby for Bitmap to PNG
  7.     #            SephirothSpawn for Tilemap
  8.     #
  9.     #
  10.     # To take a screenshot simply be on the map and press F6. The file will be
  11.     # created in your game directory under the maps name.
  12.     #
  13.     #==============================================================================


  14.     #==============================================================================
  15.     # ** Spriteset_Map
  16.     #==============================================================================
  17.     class Spriteset_Map
  18.       #--------------------------------------------------------------------------
  19.       # * Update
  20.       #--------------------------------------------------------------------------
  21.       alias :bit_to_png_up :update
  22.       def update
  23.         bit_to_png_up
  24.         if Input.trigger?(Input::F6)
  25.           map_infos = load_data("Data/MapInfos.rxdata")
  26.           bit = @tilemap.bitmap
  27.           exp_time = (bit.height * bit.width) * 0.00000664
  28.           string = "Taking screenshot please wait.... \n" +
  29.                   "Number of pixels: #{bit.height * bit.width} \n" +
  30.                   "Estamated time: #{exp_time} seconds."
  31.           print("#{string}")
  32.           old_time = Time.now
  33.           bit.make_png("#{map_infos[$game_map.map_id].name}")
  34.           string = "#{map_infos[$game_map.map_id].name}.png was created. \n" +
  35.                   "File size: width #{bit.width}, height #{bit.height}. \n" +
  36.                   "Time taken: #{Time.now - old_time} seconds."
  37.           print("#{string}")
  38.         end
  39.       end
  40.     end

  41.     #==============================================================================
  42.     #                        Bitmap to PNG By Cycleby
  43.     #==============================================================================
  44.     #
  45.     # Direct use of the Bitmap object.
  46.     #    bitmap_obj.make_png(name[, path])
  47.     #
  48.     # Name: Save the file name
  49.     # Path: path to save the
  50.     #
  51.     # Thanks 66, Shana, gold Guizi reminder and help!
  52.     #==============================================================================
  53.     module Zlib
  54.       class Png_File < GzipWriter
  55.         #--------------------------------------------------------------------------
  56.         # ● Main
  57.         #--------------------------------------------------------------------------
  58.         def make_png(bitmap_Fx,mode)
  59.           @mode = mode
  60.           @bitmap_Fx = bitmap_Fx
  61.           self.write(make_header)
  62.           self.write(make_ihdr)
  63.           self.write(make_idat)
  64.           self.write(make_iend)
  65.         end
  66.         #--------------------------------------------------------------------------
  67.         # ● PNG file header block
  68.         #--------------------------------------------------------------------------
  69.         def make_header
  70.           return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
  71.         end
  72.         #--------------------------------------------------------------------------
  73.         # ● PNG file data block header information (IHDR)
  74.         #--------------------------------------------------------------------------
  75.         def make_ihdr
  76.           ih_size = [13].pack("N")
  77.           ih_sign = "IHDR"
  78.           ih_width = [@bitmap_Fx.width].pack("N")
  79.           ih_height = [@bitmap_Fx.height].pack("N")
  80.           ih_bit_depth = [8].pack("C")
  81.           ih_color_type = [6].pack("C")
  82.           ih_compression_method = [0].pack("C")
  83.           ih_filter_method = [0].pack("C")
  84.           ih_interlace_method = [0].pack("C")
  85.           string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
  86.                   ih_compression_method + ih_filter_method + ih_interlace_method
  87.           ih_crc = [Zlib.crc32(string)].pack("N")
  88.           return ih_size + string + ih_crc
  89.         end
  90.         #--------------------------------------------------------------------------
  91.         # ● Generated image data (IDAT)
  92.         #--------------------------------------------------------------------------
  93.         def make_idat
  94.           header = "\x49\x44\x41\x54"
  95.           case @mode # please 54 ~
  96.           when 1
  97.             data = make_bitmap_data # 1
  98.           else
  99.             data = make_bitmap_data
  100.           end
  101.           data = Zlib::Deflate.deflate(data, 8)
  102.           crc = [Zlib.crc32(header + data)].pack("N")
  103.           size = [data.length].pack("N")
  104.           return size + header + data + crc
  105.         end
  106.         #--------------------------------------------------------------------------
  107.         # ● Requests from the Bitmap object 54 to generate image data in mode 1
  108.         # (please 54 ~)
  109.         #--------------------------------------------------------------------------
  110.         def make_bitmap_data1
  111.           w = @bitmap_Fx.width
  112.           h = @bitmap_Fx.height
  113.           data = []
  114.           for y in 0...h
  115.             data.push(0)
  116.             for x in 0...w
  117.               color = @bitmap_Fx.get_pixel(x, y)
  118.               red = color.red
  119.               green = color.green
  120.               blue = color.blue
  121.               alpha = color.alpha
  122.               data.push(red)
  123.               data.push(green)
  124.               data.push(blue)
  125.               data.push(alpha)
  126.             end
  127.           end
  128.           return data.pack("C*")
  129.         end
  130.         #--------------------------------------------------------------------------
  131.         # ● Bitmap object from the image data generated in mode 0
  132.         #--------------------------------------------------------------------------
  133.         def make_bitmap_data
  134.           gz = Zlib::GzipWriter.open('hoge.gz')
  135.           t_Fx = 0
  136.           w = @bitmap_Fx.width
  137.           h = @bitmap_Fx.height
  138.           data = []
  139.           for y in 0...h
  140.             data.push(0)
  141.             for x in 0...w
  142.               t_Fx += 1
  143.               if t_Fx % 10000 == 0
  144.                 Graphics.update
  145.               end
  146.               if t_Fx % 100000 == 0
  147.                 s = data.pack("C*")
  148.                 gz.write(s)
  149.                 data.clear
  150.                 #GC.start
  151.               end
  152.               color = @bitmap_Fx.get_pixel(x, y)
  153.               red = color.red
  154.               green = color.green
  155.               blue = color.blue
  156.               alpha = color.alpha
  157.               data.push(red)
  158.               data.push(green)
  159.               data.push(blue)
  160.               data.push(alpha)
  161.             end
  162.           end
  163.           s = data.pack("C*")
  164.           gz.write(s)
  165.           gz.close   
  166.           data.clear
  167.           gz = Zlib::GzipReader.open('hoge.gz')
  168.           data = gz.read
  169.           gz.close
  170.           File.delete('hoge.gz')
  171.           return data
  172.         end
  173.         #--------------------------------------------------------------------------
  174.         # ● PNG end of the file data blocks (IEND)
  175.         #--------------------------------------------------------------------------
  176.         def make_iend
  177.           ie_size = [0].pack("N")
  178.           ie_sign = "IEND"
  179.           ie_crc = [Zlib.crc32(ie_sign)].pack("N")
  180.           return ie_size + ie_sign + ie_crc
  181.         end
  182.       end
  183.     end
  184.     #==============================================================================
  185.     # ■ Bitmap
  186.     #------------------------------------------------------------------------------
  187.     # Related to the Bitmap.
  188.     #==============================================================================
  189.     class Bitmap
  190.       #--------------------------------------------------------------------------
  191.       # ● Related
  192.       #--------------------------------------------------------------------------
  193.       def make_png(name="like", path="",mode=0)
  194.         make_dir(path) if path != ""
  195.         Zlib::Png_File.open("temp.gz") {|gz|
  196.           gz.make_png(self,mode)
  197.         }
  198.         Zlib::GzipReader.open("temp.gz") {|gz|
  199.           $read = gz.read
  200.         }
  201.         f = File.open(path + name + ".png","wb")
  202.         f.write($read)
  203.         f.close
  204.         File.delete('temp.gz')
  205.         end
  206.       #--------------------------------------------------------------------------
  207.       # ● Save the path generated
  208.       #--------------------------------------------------------------------------
  209.       def make_dir(path)
  210.         dir = path.split("/")
  211.         for i in 0...dir.size
  212.           unless dir == "."
  213.             add_dir = dir[0..i].join("/")
  214.             begin
  215.               Dir.mkdir(add_dir)
  216.             rescue
  217.             end
  218.           end
  219.         end
  220.       end
  221.     end

  222.     #==============================================================================
  223.     # ** Tilemap (Basic)
  224.     #------------------------------------------------------------------------------
  225.     # SephirothSpawn
  226.     # Version 1.0
  227.     # 2007-05-30
  228.     # SDK Version 2.2 + : Parts I & II
  229.     #------------------------------------------------------------------------------
  230.     # * Credits :
  231.     #
  232.     #  Thanks to Trickster for conversion formula for Hexidecimal to rgb.
  233.     #  Thanks to trebor777 for helping with the priority bug from the 0.9 version.
  234.     #------------------------------------------------------------------------------
  235.     # * Description :
  236.     #
  237.     #  This script was designed to re-write the default RMXP Hidden Tileset class.
  238.     #  The script has added many features and a new "Tilemap Settings" class,
  239.     #  that can be unique if you create mini-maps using this system.
  240.     #------------------------------------------------------------------------------
  241.     # * Instructions :
  242.     #
  243.     #  Place The Script Below the SDK and Above Main.
  244.     #------------------------------------------------------------------------------
  245.     # * Syntax :
  246.     #
  247.     #  Get Autotile Tile Bitmap
  248.     #    - RPG::Cache.autotile_tile(autotile_filename, tile_id[, hue[, frame_id]])
  249.     #
  250.     #      autotile_filename : Filename of autotile
  251.     #      tile_id : ID of tile (Found from RPG::Map.data)
  252.     #      hue (Optional) : Hue for tile
  253.     #      frame_id (Optional) : Frame of tile (for animated autotiles)
  254.     #
  255.     # * Tilemap Syntax
  256.     #
  257.     #  Readable Attributes :
  258.     #    - layers : Array of Sprites (or Planes)
  259.     #
  260.     #  Readable/Writable Attributes :
  261.     #    - tileset (No long required) : Bitmap for Tileset
  262.     #    - tileset_name : Name of Bitmap
  263.     #    - autotiles (No long required) : Array of Autotile Bitmaps
  264.     #    - autotiles_name : Array of Autotile Filenames
  265.     #    - map_data : 3D Table of Tile ID Data
  266.     #    - flash_data : 3D Table of Tile Flash Data
  267.     #                  (Should match tilemap_settings.flash_data)
  268.     #    - priorities : 3D Table of Tile Priorities
  269.     #    - Visible : Tilemap Visible Flag
  270.     #    - ox, oy : Tilemap layer offsets
  271.     #    - tilemap_settings : Unique Special Settings Object (See Below)
  272.     #    - refresh_autotiles : Refresh Autotiles on frame reset flag
  273.     #
  274.     # * Special Tilemap Settings
  275.     #
  276.     #  To make special settings easier to control for your game map and any other
  277.     #  special tilemap sprites you wish to create, a special class was created
  278.     #  to hold these settings. For your game tilemap, a Tilemap_Settings object
  279.     #  was created in $game_map ($game_map.tilemap_settings). It is advised to
  280.     #  modify $game_map.tilemap_settings.flash_data instead of tilemap.flash_data.
  281.     #
  282.     #  Readable/Writeable Attributes :
  283.     #    - map : RPG::Map (Not required, but for additions)
  284.     #    - is_a_plane : Boolean whether layers are Sprites or Planes
  285.     #    - tone : Tone for all layers
  286.     #    - hue : Hue for all layers
  287.     #    - zoom_x, zoom_y : Zoom factor for all layers
  288.     #    - tilesize : Tilesize displayed on map
  289.     #    - flash_data : 3D Table of flash_data
  290.     #==============================================================================

  291.     #==============================================================================
  292.     # ** Tilemap_Options
  293.     #==============================================================================

  294.     module Tilemap_Options
  295.       #--------------------------------------------------------------------------
  296.       # * Tilemap Options
  297.       #
  298.       #
  299.       #  Print Error Reports when not enough information set to tilemap
  300.       #    - Print_Error_Logs          = true or false
  301.       #
  302.       #  Number of autotiles to refresh at edge of viewport
  303.       #  Number of Frames Before Redrawing Animated Autotiles
  304.       #    - Animated_Autotiles_Frames = 16
  305.       #
  306.       #    - Viewport_Padding          = n
  307.       #
  308.       #  When maps are switch, automatically set
  309.       #  $game_map.tileset_settings.flash_data (Recommended : False unless using
  310.       #  flash_data)
  311.       #    - Autoset_Flash_data        = true or false
  312.       #
  313.       #  Duration Between Flash Data Flashes
  314.       #    - Flash_Duration            = n
  315.       #
  316.       #  Color of bitmap (Recommended to use low opacity value)
  317.       #    - Flash_Bitmap_C            = Color.new(255, 255, 255, 50)
  318.       #
  319.       #  Update Flashtiles Default Setting
  320.       #  Explanation : In the Flash Data Addition, because of lag, you may wish
  321.       #  to toggle whether flash tiles flash or not. This is the default state.
  322.       #    - Default_Update_Flashtiles = false
  323.       #--------------------------------------------------------------------------
  324.       Print_Error_Logs          = true
  325.       Animated_Autotiles_Frames = 16
  326.       Autoset_Flash_data        = true
  327.       Viewport_Padding          = 2
  328.       Flash_Duration            = 40
  329.       Flash_Bitmap_C            = Color.new(255, 255, 255, 50)
  330.       Default_Update_Flashtiles = false
  331.     end

  332.     #==============================================================================
  333.     # ** RPG::Cache
  334.     #==============================================================================
  335.     module RPG::Cache
  336.       #--------------------------------------------------------------------------
  337.       # * Auto-Tiles
  338.       #
  339.       #  Auto-Tile 48 : First Auto-Tile, Constructed of tiles 27, 28, 33, 34
  340.       #--------------------------------------------------------------------------
  341.       Autotiles = [
  342.         [[27, 28, 33, 34], [ 5, 28, 33, 34], [27,  6, 33, 34], [ 5,  6, 33, 34],
  343.         [27, 28, 33, 12], [ 5, 28, 33, 12], [27,  6, 33, 12], [ 5,  6, 33, 12]],
  344.         [[27, 28, 11, 34], [ 5, 28, 11, 34], [27,  6, 11, 34], [ 5,  6, 11, 34],
  345.         [27, 28, 11, 12], [ 5, 28, 11, 12], [27,  6, 11, 12], [ 5,  6, 11, 12]],
  346.         [[25, 26, 31, 32], [25,  6, 31, 32], [25, 26, 31, 12], [25,  6, 31, 12],
  347.         [15, 16, 21, 22], [15, 16, 21, 12], [15, 16, 11, 22], [15, 16, 11, 12]],
  348.         [[29, 30, 35, 36], [29, 30, 11, 36], [ 5, 30, 35, 36], [ 5, 30, 11, 36],
  349.         [39, 40, 45, 46], [ 5, 40, 45, 46], [39,  6, 45, 46], [ 5,  6, 45, 46]],
  350.         [[25, 30, 31, 36], [15, 16, 45, 46], [13, 14, 19, 20], [13, 14, 19, 12],
  351.         [17, 18, 23, 24], [17, 18, 11, 24], [41, 42, 47, 48], [ 5, 42, 47, 48]],
  352.         [[37, 38, 43, 44], [37,  6, 43, 44], [13, 18, 19, 24], [13, 14, 43, 44],
  353.         [37, 42, 43, 48], [17, 18, 47, 48], [13, 18, 43, 48], [ 1,  2,  7,  8]]
  354.       ]
  355.       #--------------------------------------------------------------------------
  356.       # * Autotile Cache
  357.       #
  358.       #  @autotile_cache = {
  359.       #    filename => { [autotile_id, frame_id, hue] => bitmap, ... },
  360.       #    ...
  361.       #    }
  362.       #--------------------------------------------------------------------------
  363.       @autotile_cache = {}
  364.       #--------------------------------------------------------------------------
  365.       # * Autotile Tile
  366.       #--------------------------------------------------------------------------
  367.       def self.autotile_tile(filename, tile_id, hue = 0, frame_id = nil)
  368.         # Gets Autotile Bitmap
  369.         autotile = self.autotile(filename)
  370.         # Configures Frame ID if not specified
  371.         if frame_id.nil?
  372.           # Animated Tiles
  373.           frames = autotile.width / 96
  374.           # Configures Animation Offset
  375.           fc = Graphics.frame_count / Tilemap_Options::Animated_Autotiles_Frames
  376.           frame_id = (fc) % frames * 96
  377.         end
  378.         # Creates list if already not created
  379.         @autotile_cache[filename] = {} unless @autotile_cache.has_key?(filename)
  380.         # Gets Key
  381.         key = [tile_id, frame_id, hue]
  382.         # If Key Not Found
  383.         unless @autotile_cache[filename].has_key?(key)
  384.           # Reconfigure Tile ID
  385.           tile_id %= 48
  386.           # Creates Bitmap
  387.           bitmap = Bitmap.new(32, 32)
  388.           # Collects Auto-Tile Tile Layout
  389.           tiles = Autotiles[tile_id / 8][tile_id % 8]
  390.           # Draws Auto-Tile Rects
  391.           for i in 0...4
  392.             tile_position = tiles[i] - 1
  393.             src_rect = Rect.new(tile_position % 6 * 16 + frame_id,
  394.               tile_position / 6 * 16, 16, 16)
  395.             bitmap.blt(i % 2 * 16, i / 2 * 16, autotile, src_rect)
  396.           end
  397.           # Saves Autotile to Cache
  398.           @autotile_cache[filename][key] = bitmap
  399.           # Change Hue
  400.           @autotile_cache[filename][key].hue_change(hue)
  401.         end
  402.         # Return Autotile
  403.         return @autotile_cache[filename][key]
  404.       end
  405.     end

  406.     #==============================================================================
  407.     # ** Spriteset_Map
  408.     #==============================================================================
  409.     class Spriteset_Map
  410.       #--------------------------------------------------------------------------
  411.       # * Object Initialization
  412.       #--------------------------------------------------------------------------
  413.       def initialize
  414.         # Make viewports
  415.         @viewport1 = Viewport.new(0, 0, 640, 480)
  416.         @viewport2 = Viewport.new(0, 0, 640, 480)
  417.         @viewport3 = Viewport.new(0, 0, 640, 480)
  418.         @viewport2.z = 200
  419.         @viewport3.z = 5000
  420.         # Make tilemap
  421.         # Make tilemap
  422.         @tilemap = Tilemap.new(@viewport1)
  423.         @tilemap.tileset = RPG::Cache.tileset($game_map.tileset_name)
  424.         for i in 0..6
  425.           autotile_name = $game_map.autotile_names[i]
  426.           @tilemap.autotiles[i] = RPG::Cache.autotile(autotile_name)
  427.         end
  428.         @tilemap.map_data = $game_map.data.dup
  429.         @tilemap.priorities = $game_map.priorities
  430.         # Set Tilemap Settings
  431.         @tilemap.tileset_name = $game_map.tileset_name
  432.         for i in 0..6
  433.           @tilemap.autotiles_name[i] = $game_map.autotile_names[i]
  434.         end
  435.         @tilemap.tilemap_settings = $game_map.tilemap_settings
  436.         # Setup Flash Data
  437.         @tilemap.flash_data = $game_map.tilemap_settings.flash_data
  438.         # Run Tilemap Setup
  439.         @tilemap.setup
  440.         # Make panorama plane
  441.         @panorama = Plane.new(@viewport1)
  442.         @panorama.z = -1000
  443.         # Make fog plane
  444.         @fog = Plane.new(@viewport1)
  445.         @fog.z = 3000
  446.         # Make character sprites
  447.         @character_sprites = []
  448.         for i in $game_map.events.keys.sort
  449.           sprite = Sprite_Character.new(@viewport1, $game_map.events[i])
  450.           @character_sprites.push(sprite)
  451.         end
  452.         @character_sprites.push(Sprite_Character.new(@viewport1, $game_player))
  453.         # Make weather
  454.         @weather = RPG::Weather.new(@viewport1)
  455.         # Make picture sprites
  456.         @picture_sprites = []
  457.         for i in 1..50
  458.           @picture_sprites.push(Sprite_Picture.new(@viewport2,
  459.             $game_screen.pictures[i]))
  460.         end
  461.         # Make timer sprite
  462.         @timer_sprite = Sprite_Timer.new
  463.         # Frame update
  464.         update
  465.       end
  466.     end

  467.     #==============================================================================
  468.     # ** Tilemap_Settings
  469.     #==============================================================================
  470.     class Tilemap_Settings
  471.       #--------------------------------------------------------------------------
  472.       # * Public Instance Variables
  473.       #--------------------------------------------------------------------------
  474.       attr_accessor :map
  475.       attr_accessor :is_a_plane
  476.       attr_accessor :tone
  477.       attr_accessor :hue
  478.       attr_accessor :zoom_x
  479.       attr_accessor :zoom_y
  480.       attr_accessor :tile_size
  481.       attr_accessor :flash_data
  482.       #--------------------------------------------------------------------------
  483.       # * Object Initialization
  484.       #--------------------------------------------------------------------------
  485.       def initialize(map = nil)
  486.         # Set Instance Variables
  487.         @map, @is_a_plane, @tone, @hue, @zoom_x, @zoom_y, @tile_size,
  488.           @flash_data = map, false, nil, 0, 1.0, 1.0, 32, nil
  489.       end
  490.     end

  491.     #==============================================================================
  492.     # ** Game_Map
  493.     #==============================================================================
  494.     class Game_Map
  495.       #--------------------------------------------------------------------------
  496.       # * Public Instance Variables
  497.       #--------------------------------------------------------------------------
  498.       attr_accessor :tilemap_settings
  499.       #--------------------------------------------------------------------------
  500.       # * Alias Listings
  501.       #--------------------------------------------------------------------------
  502.       alias_method :seph_tilemap_gmap_init, :initialize
  503.       #--------------------------------------------------------------------------
  504.       # * Object Initialization
  505.       #--------------------------------------------------------------------------
  506.       def initialize
  507.         # Original Initialization
  508.         seph_tilemap_gmap_init
  509.         # Create Tilemap Settings
  510.         @tilemap_settings = Tilemap_Settings.new
  511.       end
  512.       #--------------------------------------------------------------------------
  513.       # * Load Map Data
  514.       #--------------------------------------------------------------------------
  515.       def setup_load
  516.         # Reset Tilemap Flash Data
  517.         if Tilemap_Options::Autoset_Flash_data
  518.           @tilemap_settings.flash_data = Table.new(@map.width, @map.height)
  519.         end
  520.         @tilemap_settings.map = @map
  521.       end
  522.     end

  523.     #==============================================================================
  524.     # ** Tilemap
  525.     #==============================================================================
  526.     class Tilemap
  527.       #--------------------------------------------------------------------------
  528.       # * Public Instance Variables
  529.       #--------------------------------------------------------------------------
  530.       attr_reader  :layers
  531.       attr_accessor :tileset
  532.       attr_accessor :tileset_name
  533.       attr_accessor :autotiles
  534.       attr_accessor :autotiles_name
  535.       attr_accessor :map_data
  536.       attr_accessor :flash_data
  537.       attr_accessor :priorities
  538.       attr_accessor :visible
  539.       attr_accessor :ox
  540.       attr_accessor :oy
  541.       attr_accessor :tilemap_settings
  542.       attr_accessor :refresh_autotiles
  543.       #--------------------------------------------------------------------------
  544.       # * Object Initialization
  545.       #--------------------------------------------------------------------------
  546.       def initialize(viewport)
  547.         # Saves Viewport
  548.         @viewport = viewport
  549.         # Creates Blank Instance Variables
  550.         @layers            = []    # Refers to Array of Sprites or Planes
  551.         @tileset          = nil  # Refers to Tileset Bitmap
  552.         @tileset_name      = ''    # Refers to Tileset Filename
  553.         @autotiles        = []    # Refers to Array of Autotile Bitmaps
  554.         @autotiles_name    = []    # Refers to Array of Autotile Filenames
  555.         @map_data          = nil  # Refers to 3D Array Of Tile Settings
  556.         @flash_data        = nil  # Refers to 3D Array of Tile Flashdata
  557.         @priorities        = nil  # Refers to Tileset Priorities
  558.         @visible          = true  # Refers to Tilest Visibleness
  559.         @ox                = 0    # Bitmap Offsets         
  560.         @oy                = 0    # Bitmap Offsets
  561.         @tilemap_settings  = nil  # Special Tilemap Settings
  562.         @dispose          = false # Disposed Flag
  563.         @refresh_autotiles = true  # Refresh Autotile Flag
  564.       end
  565.       #--------------------------------------------------------------------------
  566.       # * Setup
  567.       #--------------------------------------------------------------------------
  568.       def setup
  569.         # Print Error if Tilemap Settings not Found
  570.         if Tilemap_Options::Print_Error_Logs
  571.           if @tilemap_settings.nil?
  572.             p 'Tilemap Settings have not been set. System will not crash.'
  573.           end
  574.           if @map_data.nil?
  575.             p 'Map Data has not been set. System will crash.'
  576.           end
  577.         end
  578.         # Creates Layers
  579.         @layers = []
  580.         for l in 0...3
  581.           layer = @tilemap_settings.nil? || !@tilemap_settings.is_a_plane ?
  582.             Sprite.new(@viewport) : Plane.new(@viewport)
  583.           layer.bitmap = Bitmap.new(@map_data.xsize * 32, @map_data.ysize * 32)
  584.           layer.z = l * 150
  585.           layer.zoom_x = @tilemap_settings.nil? ? 1.0 : @tilemap_settings.zoom_x
  586.           layer.zoom_y = @tilemap_settings.nil? ? 1.0 : @tilemap_settings.zoom_y
  587.           unless @tilemap_settings.nil? || @tilemap_settings.tone.nil?
  588.             layer.tone = @tilemap_settings.tone
  589.           end
  590.           @layers << layer
  591.         end
  592.         # Update Flags
  593.         @refresh_data = nil
  594.         @zoom_x  = @tilemap_settings.nil? ? 1.0 : @tilemap_settings.zoom_x
  595.         @zoom_y  = @tilemap_settings.nil? ? 1.0 : @tilemap_settings.zoom_y
  596.         @tone    = @tilemap_settings.nil? ? nil : @tilemap_settings.tone
  597.         @hue      = @tilemap_settings.nil? ? 0  : @tilemap_settings.hue
  598.         @tilesize = @tilemap_settings.nil? ? 32  : @tilemap_settings.tile_size
  599.       end
  600.       #--------------------------------------------------------------------------
  601.       # * Dispose
  602.       #--------------------------------------------------------------------------
  603.       def dispose
  604.         # Dispose Layers (Sprites)
  605.         @layers.each { |layer| layer.dispose }
  606.         # Set Disposed Flag to True
  607.         @disposed = true
  608.       end
  609.       #--------------------------------------------------------------------------
  610.       # * Disposed?
  611.       #--------------------------------------------------------------------------
  612.       def disposed?
  613.         return @disposed
  614.       end
  615.       #--------------------------------------------------------------------------
  616.       # * Viewport
  617.       #--------------------------------------------------------------------------
  618.       def viewport
  619.         return @viewport
  620.       end
  621.       #--------------------------------------------------------------------------
  622.       # * Frame Update
  623.       #--------------------------------------------------------------------------
  624.       def update
  625.         # Set Refreshed Flag to On
  626.         needs_refresh = true
  627.         # If Map Data, Tilesize or HueChanges
  628.         if @map_data != @refresh_data || (@tilemap_settings != false &&
  629.           @hue != @tilemap_settings.hue) || (@tilemap_settings != false &&
  630.           @tilesize != @tilemap_settings.tile_size)
  631.           # Refresh Bitmaps
  632.           refresh
  633.           # Turns Refresh Flag to OFF
  634.           needs_refresh = false
  635.         end
  636.         # Zoom X, Zoom Y, and Tone Changes
  637.         unless @tilemap_settings.nil?
  638.           if @zoom_x != @tilemap_settings.zoom_x
  639.             @zoom_x = @tilemap_settings.zoom_x
  640.             @layers.each {|layer| layer.zoom_x = @zoom_x}
  641.           end
  642.           if @zoom_y != @tilemap_settings.zoom_y
  643.             @zoom_y = @tilemap_settings.zoom_y
  644.             @layers.each {|layer| layer.zoom_y = @zoom_y}
  645.           end
  646.           if @tone != @tilemap_settings.tone
  647.             @tone = @tilemap_settings.tone.nil? ?
  648.               Tone.new(0, 0, 0, 0) : @tilemap_settings.tone
  649.             @layers.each {|layer| layer.tone = @tone}
  650.           end
  651.         end
  652.         # Update layer Position offsets
  653.         for layer in @layers
  654.           layer.ox = @ox
  655.           layer.oy = @oy
  656.         end
  657.         # If Refresh Autotiles, Needs Refreshed & Autotile Reset Frame
  658.         if @refresh_autotiles && needs_refresh &&
  659.           Graphics.frame_count % Tilemap_Options::Animated_Autotiles_Frames == 0
  660.           # Refresh Autotiles
  661.           refresh_autotiles
  662.         end
  663.       end
  664.       #--------------------------------------------------------------------------
  665.       # * Refresh
  666.       #--------------------------------------------------------------------------
  667.       def refresh
  668.         # Saves Map Data & Tilesize
  669.         @refresh_data = @map_data
  670.         @hue      = @tilemap_settings.nil? ? 0  : @tilemap_settings.hue
  671.         @tilesize = @tilemap_settings.nil? ? 32 : @tilemap_settings.tile_size
  672.         # Passes Through Layers
  673.         for z in 0...@map_data.zsize
  674.           # Passes Through X Coordinates
  675.           for x in 0...@map_data.xsize
  676.             # Passes Through Z Coordinates
  677.             for y in 0...@map_data.ysize
  678.               # Collects Tile ID
  679.               id = @map_data[x, y, z]
  680.               # Skip if 0 tile
  681.               next if id == 0
  682.               # Passes Through All Priorities
  683.               for p in 0..5
  684.                 # Skip If Priority Doesn't Match
  685.                 next unless p == @priorities[id]
  686.                 # Cap Priority to Layer 3
  687.                 p = 2 if p > 2
  688.                 # Draw Tile
  689.                 id < 384 ? draw_autotile(x, y, p, id) : draw_tile(x, y, p, id)
  690.               end
  691.             end
  692.           end
  693.         end
  694.       end   
  695.       #--------------------------------------------------------------------------
  696.       # * Refresh Auto-Tiles
  697.       #--------------------------------------------------------------------------
  698.       def refresh_autotiles
  699.         # Auto-Tile Locations
  700.         autotile_locations = Table.new(@map_data.xsize, @map_data.ysize,
  701.           @map_data.zsize)
  702.         # Get X Tiles
  703.         x1 = [@ox / @tilesize - Tilemap_Options::Viewport_Padding, 0].max.round
  704.         x2 = [@viewport.rect.width / @tilesize +
  705.               Tilemap_Options::Viewport_Padding, @map_data.xsize].min.round
  706.         # Get Y Tiles
  707.         y1 = [@oy / @tilesize - Tilemap_Options::Viewport_Padding, 0].max.round
  708.         y2 = [@viewport.rect.height / @tilesize +
  709.               Tilemap_Options::Viewport_Padding, @map_data.ysize].min.round
  710.         # Passes Through Layers
  711.         for z in 0...@map_data.zsize
  712.           # Passes Through X Coordinates
  713.           for x in x1...x2
  714.             # Passes Through Y Coordinates
  715.             for y in y1...y2
  716.               # Collects Tile ID
  717.               id = @map_data[x, y, z]
  718.               # Skip if 0 tile
  719.               next if id == 0
  720.               # Skip If Non-Animated Tile
  721.               next unless @autotiles[id / 48 - 1].width / 96 > 1 if id < 384
  722.               # Passes Through All Priorities
  723.               for p in 0..5
  724.                 # Skip If Priority Doesn't Match
  725.                 next unless p == @priorities[id]
  726.                 # Cap Priority to Layer 3
  727.                 p = 2 if p > 2
  728.                 # If Autotile
  729.                 if id < 384
  730.                   # Draw Auto-Tile
  731.                   draw_autotile(x, y, p, id)
  732.                   # Draw Higher Tiles
  733.                   for l in 0...@map_data.zsize
  734.                     id_l = @map_data[x, y, l]
  735.                     draw_tile(x, y, p, id_l)
  736.                   end
  737.                   # Save Autotile Location
  738.                   autotile_locations[x, y, z] = 1
  739.                 # If Normal Tile
  740.                 else
  741.                   # If Autotile Drawn
  742.                   if autotile_locations[x, y, z] == 1
  743.                     # Redraw Normal Tile
  744.                     draw_tile(x, y, p, id)
  745.                     # Draw Higher Tiles
  746.                     for l in 0...@map_data.zsize
  747.                       id_l = @map_data[x, y, l]
  748.                       draw_tile(x, y, p, id_l)
  749.                     end
  750.                   end
  751.                 end
  752.               end
  753.             end
  754.           end
  755.         end
  756.       end     
  757.       #--------------------------------------------------------------------------
  758.       # * Draw Tile
  759.       #--------------------------------------------------------------------------
  760.       def draw_tile(x, y, z, id)
  761.         # Gets Tile Bitmap
  762.         bitmap = RPG::Cache.tile(@tileset_name, id, @hue)
  763.         # Calculates Tile Coordinates
  764.         x *= @tilesize
  765.         y *= @tilesize
  766.         # Draw Tile
  767.         if @tilesize == 32
  768.           @layers[z].bitmap.blt(x, y, bitmap, Rect.new(0, 0, 32, 32))
  769.         else
  770.           rect = Rect.new(x, y, @tilesize, @tilesize)
  771.           @layers[z].bitmap.stretch_blt(rect, bitmap, Rect.new(0, 0, 32, 32))
  772.         end
  773.       end
  774.       #--------------------------------------------------------------------------
  775.       # * Draw Auto-Tile
  776.       #--------------------------------------------------------------------------
  777.       def draw_autotile(x, y, z, tile_id)
  778.         # Gets Autotile Filename
  779.         filename = @autotiles_name[tile_id / 48 - 1]
  780.         # Reconfigure Tile ID
  781.         tile_id %= 48
  782.         # Gets Generated Autotile Bitmap Section
  783.         bitmap = RPG::Cache.autotile_tile(filename, tile_id, @hue)
  784.         # Calculates Tile Coordinates
  785.         x *= @tilesize
  786.         y *= @tilesize
  787.         # If Normal Tile
  788.         if @tilesize == 32
  789.           @layers[z].bitmap.blt(x, y, bitmap, Rect.new(0, 0, 32, 32))
  790.         # If Altered Dimensions
  791.         else
  792.           dest_rect = Rect.new(x, y, @tilesize, @tilesize)
  793.           @layers[z].bitmap.stretch_blt(dest_rect, bitmap, Rect.new(0, 0, 32, 32))
  794.         end
  795.       end
  796.       #--------------------------------------------------------------------------
  797.       # * Collect Bitmap
  798.       #--------------------------------------------------------------------------
  799.       def bitmap
  800.         # Creates New Blank Bitmap
  801.         bitmap = Bitmap.new(@layers[0].bitmap.width, @layers[0].bitmap.height)
  802.         # Passes Through All Layers
  803.         for layer in @layers
  804.           bitmap.blt(0, 0, layer.bitmap,
  805.             Rect.new(0, 0, bitmap.width, bitmap.height))
  806.         end
  807.         # Return Bitmap
  808.         return bitmap
  809.       end
  810.     end
复制代码





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1