tileset.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. class TileGroup
  2. attr_accessor :name, :tiles
  3. def initialize(name, tiles)
  4. @name = name
  5. @tiles = tiles
  6. end
  7. end
  8. # Load game tiles from filename into tileset
  9. class Tileset
  10. alias old_initialize initialize
  11. attr_accessor :tilegroups
  12. def initialize(*params)
  13. old_initialize(*params)
  14. @tilegroups = []
  15. end
  16. def load(filename)
  17. puts "Loading Tileset: #{filename}"
  18. tree = load_lisp(filename, :"supertux-tiles")
  19. tree = tree[1..-1]
  20. counter = 0
  21. tree.each do |i|
  22. case i[0]
  23. when :tiles
  24. data = i[1..-1]
  25. width = get_value_from_tree(['width', '_'], data, 1)
  26. height = get_value_from_tree(['height', '_'], data, 1)
  27. ids = get_value_from_tree(['ids'], data, [])
  28. # attributes = get_value_from_tree(['attributes'], data, [])
  29. image = get_value_from_tree(['image', '_'], data, nil)
  30. if not image then
  31. image = get_value_from_tree(['images', '_'], data, nil)
  32. end
  33. if not image then
  34. image = get_value_from_tree(['editor-images', '_'], data, "tiles/auxiliary/notile.png")
  35. end
  36. x = 0
  37. y = 0
  38. ids.each{|id|
  39. pixelbuffer = make_region_pixelbuffer_from_resource($datadir + 'images/' + image,
  40. x * 32, y * 32, 32, 32)
  41. add_tile(id, Tile.new(pixelbuffer))
  42. x += 1
  43. if (x == width) then
  44. x = 0
  45. y += 1
  46. end
  47. }
  48. when :tile
  49. data = i[1..-1]
  50. id = get_value_from_tree(['id', '_'], data, -1)
  51. image = get_value_from_tree(['editor-images', '_'], data, false)
  52. hidden = get_value_from_tree(['hidden', '_'], data, false)
  53. if not(image) then
  54. image = get_value_from_tree(['images', '_'], data, "tiles/auxiliary/notile.png")
  55. end
  56. if image.is_a?(String) then
  57. pixelbuffer = make_pixelbuffer($datadir + 'images/' + image)
  58. elsif image.is_a?(Array) then
  59. if image[0] == :region then
  60. pixelbuffer = make_region_pixelbuffer_from_resource($datadir + 'images/' + image[1],
  61. image[2], image[3], image[4], image[5])
  62. end
  63. end
  64. if not hidden then
  65. if id == 0 or not(pixelbuffer) then
  66. add_tile(id, nil)
  67. else
  68. add_tile(id, Tile.new(pixelbuffer))
  69. end
  70. end
  71. when :tilegroup
  72. data = i[1..-1]
  73. name = get_value_from_tree(['name', '_'], data, "Unnamed")
  74. tiles = get_value_from_tree(['tiles'], data, [])
  75. if not @tilegroups then
  76. @tilegroups = []
  77. end
  78. @tilegroups.push(TileGroup.new(name, tiles))
  79. end
  80. counter += 1
  81. if counter % 20 == 0 then
  82. print "Loading tiles: %3.0f%%\r" % [counter.to_f/tree.length.to_f*100.0]
  83. $stdout.flush()
  84. end
  85. end
  86. puts ""
  87. end
  88. def create_ungrouped_tiles_group()
  89. @tilegroups.push(TileGroup.new("Ungrouped Tiles", get_ungrouped_tiles()))
  90. end
  91. def get_ungrouped_tiles()
  92. # Searches for tiles which are not yet grouped and creates a group
  93. # for them
  94. # Potentially quite slow
  95. ungrouped_tiles = []
  96. get_tiles().each {|tile|
  97. catch :tile_is_grouped do
  98. tilegroups.each {|group|
  99. if group.tiles.index(tile) then
  100. throw :tile_is_grouped
  101. end
  102. }
  103. ungrouped_tiles.push(tile)
  104. end
  105. }
  106. return ungrouped_tiles
  107. end
  108. end
  109. # EOF #