Class: Tmx::Parser::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/tmx/parsers/json.rb

Overview

Parses the JSON formatted output from Tiled.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Json

Returns a new instance of Json.



12
13
14
# File 'lib/tmx/parsers/json.rb', line 12

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/tmx/parsers/json.rb', line 10

def options
  @options
end

Instance Method Details

#parse(contents) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tmx/parsers/json.rb', line 16

def parse(contents)
  parsed_contents = MultiJson.load(contents)

  object_layers = parsed_contents["layers"].find_all do |layer|
    layer["type"] == "objectgroup"
  end

  parsed_contents["object_groups"] = object_layers

  object_layers.each do |object_layer|
    parse_object_layer(object_layer)
  end

  image_layers = parsed_contents["layers"].find_all do |layer|
    layer["type"] == "imagelayer"
  end

  parsed_contents["image_layers"] = image_layers

  parsed_contents["layers"].reject! {|layer| layer["type"] != "tilelayer" }

  parsed_contents

end