Skip to content

Instantly share code, notes, and snippets.

@sardaukar
Created November 26, 2016 22:50
Show Gist options
  • Save sardaukar/6bda70f2a5dc55aeb66997118f819dfa to your computer and use it in GitHub Desktop.
Save sardaukar/6bda70f2a5dc55aeb66997118f819dfa to your computer and use it in GitHub Desktop.
module Adlez
class TMXParser
class Object
def self.from_xml(node)
new(node)
end
{% for name in %w(x y width height) %}
@{{name.id}} : Int32
{% end %}
@name : String
@type : String
@properties : TMXPropertiesMap
def initialize(xml)
@name, @type, @x, @y, @width, @height, @properties = parse(xml)
end
private def parse(xml)
name = xml["name"].as(String)
type = xml["type"].as(String)
x = xml["x"].as(String).to_i
y = xml["y"].as(String).to_i
width = xml["width"].as(String).to_i
height = xml["height"].as(String).to_i
properties = TMXPropertiesMap.new
xml.children.each do |child|
next if child.text?
if child.name == "properties"
child.children.each do |grandchild|
next if grandchild.text?
prop_name = grandchild["name"].as(String)
prop_type = grandchild["type"].as(String)
prop_value = grandchild["value"].as(String)
case prop_type
when "int"
properties[prop_name] = prop_value.to_i
when "float"
properties[prop_name] = prop_value.to_f
else
raise "wtf"
end
end
end
end
return name, type, x, y, width, height, properties
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment