lib/demiurge/tmx.rb
# This is to support TMX files for ManaSource, ManaWorld, Land of
# Fire, Source of Tales and other Mana Project games. It can't be
# perfect since there's some variation between them, but it can
# follow most major conventions.
#
# Load a TMX file and calculate additional information like
# animations from the given properties. Assume this TMX file obeys
# ManaSource conventions such as fields for exits and names for
# layers.
#
# This will assign Z values to the layers to determine stacking
# order.
#
# The returned cache entry has some known fields and may have
# others - this is basically an internal format.
#
# @example ``` ruby
# {
#   "width" => 40,               # The width of the TMX map in tiles
#   "height" => 35,              # The height of the TMX map in tiles
#   "tilewidth" => 32,           # The width of a tile in pixels
#   "tileheight" => 32,          # The height of a tile in pixels
#   "tmx_name" => "main_board",  # The TMX filename, minus the extensions
#   "map" => { ... },  # The whole TMX file converted to TMX JSON format
#   "name" => "main_board",  # The name from the TMX file, falling back to the filename
#   "filename" => "../tmx/main_board.tmx", # The filename including path and extension(s)
#   "dir" => "../tmx"        # Just the directory part of the filename
#   "animations" => { ... }  # TMX tile animations extracted from tile properties in an internal format
#   "objects" => [ ... ]     # Extracted TMX objects
# }
# ```
#
# @since 0.1.0
This is to support TMX files for ManaSource, ManaWorld, Land of
Fire, Source of Tales and other Mana Project games. It can't be
perfect since there's some variation between them, but it can
follow most major conventions.

Load a TMX file and calculate additional information like
animations from the given properties. Assume this TMX file obeys
ManaSource conventions such as fields for exits and names for
layers.

This will assign Z values to the layers to determine stacking
order.

The returned cache entry has some known fields and may have
others - this is basically an internal format.
This is to support TMX files for ManaSource ManaWorld Land of Fire Source of Tales and other Mana Project games . It can ' t be perfect since there ' s some variation between them but it can follow most major conventions . Load a TMX file and calculate additional information like animations from the given properties . Assume this TMX file obeys ManaSource conventions such as fields for exits and names for layers . This will assign Z values to the layers to determine stacking order . The returned cache entry has some known fields and may have others - this is basically an internal format .
def sprites_from_manasource_tmx(filename)
      entry = sprites_from_tmx filename
      entry["format"] = "manasource"
      entry["filename"].sub!(/\.tmx\.json\Z/, ".manasource.json")
      stack_layers = entry["map"]["layers"].select { |layer| layer["type"] == "tilelayer" }
      collision_index = stack_layers.index { |l| l["name"].downcase == "collision" }
      collision_layer = stack_layers.delete_at collision_index if collision_index
      entry["collision"] = collision_layer["data"] if collision_layer
      heights_index = stack_layers.index { |l| ["height", "heights"].include?(l["name"].downcase)  }
      heights_layer = stack_layers.delete_at heights_index if heights_index
      entry["heights"] = heights_layer
      fringe_index = stack_layers.index { |l| l["name"].downcase == "fringe" }
      raise ::Demiurge::Errors::TmxFormatError.new("No Fringe layer found in ManaSource TMX File #{filename.inspect}!", "filename" => filename) unless fringe_index
      stack_layers.each_with_index do |layer, index|
        layer["z"] = (index - fringe_index) * 10.0
      end
      entry["tile_layers"] = stack_layers.select { |layer| layer["type"] == "tilelayer" }
      entry
    end
def sprites_from_manasource_tmx ( filename ) entry = sprites_from_tmx filename entry [ "format" ] = "manasource" entry [ "filename" ] . sub! ( / \. tmx \. json \Z / , ".manasource.json" ) stack_layers = entry [ "map" ] [ "layers" ] . select { | layer | layer [ "type" ] == "tilelayer" } collision_index = stack_layers . index { | l | l [ "name" ] . downcase == "collision" } collision_layer = stack_layers . delete_at collision_index if collision_index entry [ "collision" ] = collision_layer [ "data" ] if collision_layer heights_index = stack_layers . index { | l | [ "height" , "heights" ] . include? ( l [ "name" ] . downcase ) } heights_layer = stack_layers . delete_at heights_index if heights_index entry [ "heights" ] = heights_layer fringe_index = stack_layers . index { | l | l [ "name" ] . downcase == "fringe" } raise :: Demiurge :: Errors :: TmxFormatError . new ( "No Fringe layer found in ManaSource TMX File #{filename.inspect}!" , "filename" => filename ) unless fringe_index stack_layers . each_with_index do | layer , index | layer [ "z" ] = ( index - fringe_index ) * 10.0 end entry [ "tile_layers" ] = stack_layers . select { | layer | layer [ "type" ] == "tilelayer" } entry end
This is to support TMX files for ManaSource, ManaWorld, Land of
Fire, Source of Tales and other Mana Project games.
This is to support TMX files for ManaSource ManaWorld Land of Fire Source of Tales and other Mana Project games .
# Remove the collision layer, add as separate collision top-level entry # Some games make this true/false, others have separate visibility # or swimmability in it. In general, we'll just expose the data. # Remove the heights layer, add as separate heights top-level entry # Assign a Z value based on layer depth, with fringe = 0 as a special case
filename
example
``` ruby { "width" => 40, # The width of the TMX map in tiles "height" => 35, # The height of the TMX map in tiles "tilewidth" => 32, # The width of a tile in pixels "tileheight" => 32, # The height of a tile in pixels "tmx_name" => "main_board", # The TMX filename, minus the extensions "map" => { ... }, # The whole TMX file converted to TMX JSON format "name" => "main_board", # The name from the TMX file, falling back to the filename "filename" => "../tmx/main_board.tmx", # The filename including path and extension(s) "dir" => "../tmx" # Just the directory part of the filename "animations" => { ... } # TMX tile animations extracted from tile properties in an internal format "objects" => [ ... ] # Extracted TMX objects } ```
` ` ` ruby { " width " = > 40 # The width of the TMX map in tiles " height " = > 35 # The height of the TMX map in tiles " tilewidth " = > 32 # The width of a tile in pixels " tileheight " = > 32 # The height of a tile in pixels " tmx_name " = > " main_board " # The TMX filename minus the extensions " map " = > { ... } # The whole TMX file converted to TMX JSON format " name " = > " main_board " # The name from the TMX file falling back to the filename " filename " = > " .. / tmx / main_board . tmx " # The filename including path and extension ( s ) " dir " = > " .. / tmx " # Just the directory part of the filename " animations " = > { ... } # TMX tile animations extracted from tile properties in an internal format " objects " = > [ ... ] # Extracted TMX objects } ` ` `
since
None