tileset.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ## $Id$
  2. ##
  3. ## Flexlay - A Generic 2D Game Editor
  4. ## Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
  5. ##
  6. ## This program is free software: you can redistribute it and/or modify
  7. ## it under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation, either version 3 of the License, or
  9. ## (at your option) any later version.
  10. ##
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ## GNU General Public License for more details.
  15. ##
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. class TileGroup
  19. attr_accessor :name, :tiles
  20. def initialize(name, tiles)
  21. @name = name
  22. @tiles = tiles
  23. end
  24. end
  25. class Tileset
  26. attr_accessor :tilegroups
  27. def load(filename)
  28. puts "Loading Tileset: #{filename}"
  29. tree = sexpr_read_from_file(filename)
  30. if tree == nil then
  31. puts "Error; Couldn't load: ", filename
  32. return
  33. end
  34. tree = tree[1..-1]
  35. counter = 0
  36. tree.each do |i|
  37. case i[0]
  38. when :tiles
  39. data = i[1..-1]
  40. colmap = get_value_from_tree(['colmap'], data, [])
  41. ids = get_value_from_tree(['ids'], data, [])
  42. image = get_value_from_tree(['image', '_'], data, "")
  43. width = get_value_from_tree(['width', '_'], data, 0)
  44. height = get_value_from_tree(['height', '_'], data, 0)
  45. puts "Loading: Size: #{width}x#{height} Filename: #{$datadir + image}"
  46. pixelbuffer = make_pixelbuffer($datadir + image);
  47. (0..height-1).each {|y|
  48. (0..width-1).each {|x|
  49. if (y*width + x < ids.length) then
  50. $tileset.add_tile(ids[y*width + x],
  51. Tile.new(make_region_pixelbuffer(pixelbuffer,
  52. 8*x, 8*y, 8, 8)))
  53. else
  54. puts "Id out of range: #{y*width + x} >= #{ids.length} for image #{$datadir + image}"
  55. end
  56. }
  57. }
  58. end
  59. end
  60. puts ""
  61. end
  62. end
  63. # EOF #