sprite.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. class Sprite
  2. def initialize(filename)
  3. @actions = Hash.new()
  4. tree = sexpr_read_from_file(filename)
  5. if tree == nil then
  6. raise "Error: Couldn't load: '#{filename}'"
  7. end
  8. @basedir = File.dirname(filename) + "/"
  9. tree[1..-1].each do |i|
  10. case i[0]
  11. when :action
  12. action = Action.new()
  13. action.parse(i[1..-1])
  14. @actions[action.name] = action
  15. if @actions.default == nil || action.name == "default"
  16. @actions.default = action
  17. end
  18. else
  19. print "Unknown symbol '#{i[0]}' in sprite '#{filename}'"
  20. end
  21. end
  22. end
  23. class Action
  24. attr_accessor :name, :image, :x_offset, :y_offset
  25. def parse(sexpr)
  26. @name = get_value_from_tree(["name", "_"], sexpr, "default")
  27. @x_offset = get_value_from_tree(["x-offset", "_"], sexpr, 0)
  28. @y_offset = get_value_from_tree(["y-offset", "_"], sexpr, 0)
  29. # we only parse the first image for now as we don't have support for
  30. # animation in flexlay anyway
  31. @image = get_value_from_tree(["images", "_"], sexpr, 0)
  32. end
  33. end
  34. def get_cl_sprite(action = "default")
  35. action = @actions[action]
  36. sprite = make_sprite(@basedir + action.image)
  37. # FIXME:
  38. # sprite.set_frame_offset(0, CL_Point.new(action.x_offset, action.y_offset))
  39. return sprite
  40. end
  41. end