123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- class TileGroup
- attr_accessor :name, :tiles
-
- def initialize(name, tiles)
- @name = name
- @tiles = tiles
- end
- end
- # Load game tiles from filename into tileset
- class Tileset
- alias old_initialize initialize
- attr_accessor :tilegroups
- def initialize(*params)
- old_initialize(*params)
- @tilegroups = []
- end
- def load(filename)
- puts "Loading Tileset: #{filename}"
- tree = load_lisp(filename, :"supertux-tiles")
- tree = tree[1..-1]
- counter = 0
-
- tree.each do |i|
- case i[0]
- when :tiles
- data = i[1..-1]
- width = get_value_from_tree(['width', '_'], data, 1)
- height = get_value_from_tree(['height', '_'], data, 1)
- ids = get_value_from_tree(['ids'], data, [])
- # attributes = get_value_from_tree(['attributes'], data, [])
- image = get_value_from_tree(['image', '_'], data, nil)
- if not image then
- image = get_value_from_tree(['images', '_'], data, nil)
- end
- if not image then
- image = get_value_from_tree(['editor-images', '_'], data, "tiles/auxiliary/notile.png")
- end
- x = 0
- y = 0
- ids.each{|id|
- pixelbuffer = make_region_pixelbuffer_from_resource($datadir + 'images/' + image,
- x * 32, y * 32, 32, 32)
- add_tile(id, Tile.new(pixelbuffer))
- x += 1
- if (x == width) then
- x = 0
- y += 1
- end
- }
-
- when :tile
- data = i[1..-1]
- id = get_value_from_tree(['id', '_'], data, -1)
- image = get_value_from_tree(['editor-images', '_'], data, false)
- hidden = get_value_from_tree(['hidden', '_'], data, false)
- if not(image) then
- image = get_value_from_tree(['images', '_'], data, "tiles/auxiliary/notile.png")
- end
-
- if image.is_a?(String) then
- pixelbuffer = make_pixelbuffer($datadir + 'images/' + image)
- elsif image.is_a?(Array) then
- if image[0] == :region then
- pixelbuffer = make_region_pixelbuffer_from_resource($datadir + 'images/' + image[1],
- image[2], image[3], image[4], image[5])
- end
- end
-
- if not hidden then
- if id == 0 or not(pixelbuffer) then
- add_tile(id, nil)
- else
- add_tile(id, Tile.new(pixelbuffer))
- end
- end
- when :tilegroup
- data = i[1..-1]
- name = get_value_from_tree(['name', '_'], data, "Unnamed")
- tiles = get_value_from_tree(['tiles'], data, [])
-
- if not @tilegroups then
- @tilegroups = []
- end
- @tilegroups.push(TileGroup.new(name, tiles))
-
- end
- counter += 1
- if counter % 20 == 0 then
- print "Loading tiles: %3.0f%%\r" % [counter.to_f/tree.length.to_f*100.0]
- $stdout.flush()
- end
- end
- puts ""
- end
- def create_ungrouped_tiles_group()
- @tilegroups.push(TileGroup.new("Ungrouped Tiles", get_ungrouped_tiles()))
- end
- def get_ungrouped_tiles()
- # Searches for tiles which are not yet grouped and creates a group
- # for them
- # Potentially quite slow
- ungrouped_tiles = []
- get_tiles().each {|tile|
- catch :tile_is_grouped do
- tilegroups.each {|group|
- if group.tiles.index(tile) then
- throw :tile_is_grouped
- end
- }
- ungrouped_tiles.push(tile)
- end
- }
- return ungrouped_tiles
- end
- end
- # EOF #
|