tmx 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +0,0 @@
1
- module Tmx
2
- module Map::XMLLoader
3
- protected
4
-
5
- def parse_tile_set_def xml
6
- properties = xml.tmx_parse_attributes
7
- image_path = File.absolute_path xml.xpath('image/@source').first.value, File.dirname(xml.document.url)
8
- [ image_path, properties ]
9
- end
10
-
11
- def parse_layer_def xml
12
- properties = xml.tmx_parse_properties.merge! xml.tmx_parse_attributes
13
- Layer.new self, xml.tmx_data, properties
14
- end
15
-
16
- def parse_object_group_def xml
17
- properties = xml.tmx_parse_properties.merge! xml.tmx_parse_attributes
18
- group = ObjectGroup.new self, properties
19
-
20
- xml.xpath('object').each do |child|
21
- obj = parse_object_def(child, group)
22
- group.add obj if obj
23
- end
24
-
25
- group
26
- end # parse_object_group_def
27
-
28
- def parse_object_def xml, group
29
- properties = xml.tmx_parse_properties.merge! xml.tmx_parse_attributes
30
- # name = properties[:name]
31
- name = properties[:name]
32
-
33
- [:x, :y, :width, :height].each do |key|
34
- properties[key] = properties[key] * @scale_units
35
- end if @scale_units
36
-
37
- on_object name, group, properties
38
- end
39
-
40
- end # Map::XMLLoader
41
- end
@@ -1,56 +0,0 @@
1
- # TODO investigate writing Nokogiri::TMX class hierarchy (yeah right)
2
-
3
- class Nokogiri::XML::Node
4
- def to_sym; self.to_s.to_sym end
5
-
6
- def to_i; self.to_s.to_i end
7
- def to_f; self.to_s.to_f end
8
- def to_c; self.to_s.to_c end
9
- def to_r; self.to_s.to_r end
10
-
11
- def tmx_parse
12
- str = to_s
13
- case str
14
- when '' then nil
15
- when %r(^ (?: false | no | off ) $)ix then false
16
- when %r(^ (?: true | yes | on ) $)ix then true
17
- when %r(^ [+-]? \d+ / [+-]? \d+ $)ix then str.to_r
18
- when %r(^ [+-]? (?: \d+ \. \d+ [+-] ) \d+ i $)ix then str.to_c
19
- when %r(^ [+-]? \d+ \. \d+ $)ix then str.to_f
20
- when %r(^ [+-]? \d+ $)ix then str.to_i
21
- when %r(^ \# [0-9a-f]{6} $)ix
22
- 0xff000000 | str[1..6].to_i(16)
23
- when %r(^ \# [0-9a-f]{8} $)ix
24
- str[1..8].to_i(16)
25
- else str
26
- end
27
- end
28
- end
29
-
30
- class Nokogiri::XML::Element
31
- def tmx_parse_attributes
32
- attributes.reduce({}) do |h, pair|
33
- name = pair[0].to_sym
34
- value = pair[1].tmx_parse
35
- h.merge name => value
36
- end
37
- end
38
-
39
- def tmx_parse_properties
40
- properties_element = xpath('properties').first or return {}
41
- properties_element.xpath('property').reduce({}) do |h, property|
42
- name = property.attribute('name').to_sym
43
- value = property.attribute('value').tmx_parse
44
- h.merge! name => value
45
- end
46
- end
47
-
48
- def tmx_data
49
- data = ''
50
- xpath('data').each do |data_element|
51
- attrs = data_element.tmx_parse_attributes
52
- data << Tmx::Coder.decode(data_element.text, attrs[:compression], attrs[:encoding])
53
- end
54
- data
55
- end
56
- end
@@ -1,30 +0,0 @@
1
- module Tmx
2
- class ObjectGroup
3
- include Enumerable
4
-
5
- attr_reader :properties
6
- attr_reader :name
7
-
8
- def initialize map, properties = {}
9
- @map = WeakRef.new map
10
- @properties = properties
11
- @objects = Hash[]
12
-
13
- @name = properties.delete :name
14
- end
15
-
16
- def map; @map.__getobj__ end
17
-
18
- def add obj
19
- @objects[obj.object_id] = obj
20
- end
21
-
22
- def remove obj
23
- @objects.delete obj.object_id
24
- end
25
-
26
- def each &block
27
- @objects.each_value &block
28
- end
29
- end
30
- end
data/spec/coder_spec.rb DELETED
@@ -1,29 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
2
- describe 'Coder' do
3
- it 'can find the default coders' do
4
- Tmx::Coder.find_coder(:base64).should_not == nil
5
- Tmx::Coder.find_coder(:gzip).should_not == nil
6
- end
7
-
8
- CODER_TEST_STRING = <<-EOS
9
- Lorem ipsum dolor sit amet, consecteteur adapiscing elit.
10
- EOS
11
-
12
- def coder_round_trip *encodings
13
- encoded = Tmx::Coder.encode CODER_TEST_STRING, *encodings
14
- decoded = Tmx::Coder.decode encoded, *encodings
15
- decoded.should == CODER_TEST_STRING
16
- end
17
-
18
- it 'can round trip base64' do
19
- coder_round_trip :base64
20
- end
21
-
22
- it 'can round trip gzip' do
23
- coder_round_trip :gzip
24
- end
25
-
26
- it 'can round trip multiple encodings' do
27
- coder_round_trip :gzip, :base64
28
- end
29
- end
data/spec/map_spec.rb DELETED
@@ -1,50 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
2
- describe 'Map' do
3
- it 'can load the test map' do
4
- $map = Tmx::Map.new File.join($data_dir, 'test.tmx'),
5
- :scale_units => false
6
- $map.should.is_a? Tmx::Map
7
- end
8
-
9
- it 'has the correct dimensions' do
10
- $map.columns.should == 16
11
- $map.rows.should == 16
12
-
13
- $map.width.should == 16 * $map.tile_width
14
- $map.height.should == 16 * $map.tile_height
15
- end
16
-
17
- it 'has the correct tile dimensions' do
18
- $map.tile_width.should == 16
19
- $map.tile_height.should == 16
20
- end
21
-
22
- # it 'loads all tile set definitions into one flat set' do
23
- # $map.tile_set.should.is_a? Tmx::TileSet
24
- # $map.tile_set[0].should == nil
25
- # $map.tile_set[1].should.is_a? Gosu::Image
26
- # end
27
-
28
- it 'loads all layers' do
29
- $map.layers.count.should == 3
30
- $map.layers.each do |name, layer|
31
- layer.should.is_a? Tmx::Layer
32
- end
33
- end
34
-
35
- it 'loads all object groups' do
36
- $map.object_groups.count.should == 2
37
- $map.object_groups.each do |name, group|
38
- Tmx::ObjectGroup.should === group
39
- end
40
- end
41
-
42
- # it 'can draw itself' do
43
- # $window.run_test\
44
- # :time => 1.0,
45
- # :before => proc { @x, @y = 0, 0 },
46
- # :update => proc { @y -= 1 },
47
- # :draw => proc { $map.draw @x, @y },
48
- # end
49
-
50
- end
@@ -1,5 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
2
- describe 'nokogiri additions' do
3
- before(:all) { $doc = File.join($data_dir, 'test.tmx') }
4
- after(:all) { $doc = nil }
5
- end
@@ -1,41 +0,0 @@
1
-
2
- require File.join(File.dirname(__FILE__), 'spec_helper')
3
- describe 'object groups' do
4
-
5
- # {:image=>"door.png", :target=>"door2", :name=>"door1", :type=>"Door", :x=>128, :y=>208, :width=>16, :height=>32}
6
- # {:image=>"door.png", :target=>"door1", :name=>"door2", :type=>"Door", :x=>64, :y=>32, :width=>16, :height=>32}
7
-
8
- it 'loads all object group properties' do
9
- map = Tmx::Map.new File.join($data_dir, 'test.tmx'), :scale_units => false
10
- object_group = map.object_groups["doors"]
11
- object_group.should_not be_nil
12
-
13
- captured = []
14
- object_group.each do |obj|
15
- captured << obj
16
- end
17
-
18
- obj = captured.first
19
-
20
- obj[:target].should == "door2"
21
- obj[:image].should == "door.png"
22
- obj[:type].should == "Door"
23
- obj[:x].should == 128
24
- obj[:y].should == 208
25
- obj[:width].should == 16
26
- obj[:height].should == 32
27
- obj[:name].should == "door1"
28
-
29
- obj = captured.last
30
- obj[:target].should == "door1"
31
- obj[:image].should == "door.png"
32
- obj[:type].should == "Door"
33
- obj[:x].should == 64
34
- obj[:y].should == 32
35
- obj[:width].should == 16
36
- obj[:height].should == 32
37
- obj[:name].should == "door2"
38
- end
39
-
40
-
41
- end