Class: Entityjs::ParseTMX

Inherits:
Object
  • Object
show all
Defined in:
lib/entityjs/parsers/parse_tmx.rb

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/entityjs/parsers/parse_tmx.rb', line 5

def self.parse(data)
  if data.nil? || data.empty?
    return '{}'
  end
  
  contents = ParseXML.parse_to_hash(data)
  
  #convert csv map into json array
  if contents['layer'].is_a? Array
    contents['layer'].each do |k|
      self.parse_layer(k)
    end
  elsif contents['layer'].is_a? Hash
    self.parse_layer(contents['layer'])
  end
  
  #transform into strin
  return ParseXML.parse(contents)
end

.parse_layer(k) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/entityjs/parsers/parse_tmx.rb', line 25

def self.parse_layer(k)
  if k.nil? || k.empty?
    return 
  end
  
    map = k['data']
    #remove encoding
    map.delete '@encoding'
    #convert csv to array
    tiles = map['$'].split(",\n")
    k['data']['$'] = tiles.collect{|i| i.split(',').collect{|j| j.to_i }}
  
end