level.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. class Level
  2. attr_reader :version, :filename, :name, :author, :theme, :objects, :sectors, :current_sector
  3. attr_writer :version, :filename, :name, :author, :theme, :objects, :sectors, :current_sector
  4. def initialize(*params)
  5. @name = "No Name"
  6. @author = "No Author"
  7. @theme = "antarctica"
  8. @filename = nil
  9. @music = ""
  10. @version = 2
  11. if params.length() == 2 then
  12. # New Level
  13. (width, height) = params
  14. @width = width
  15. @height = height
  16. @current_sector = Sector.new(self)
  17. @current_sector.new_from_size("main", width, height)
  18. @sectors = []
  19. @sectors.push(@current_sector)
  20. elsif params.length() == 1 then
  21. # Load Level from file
  22. (@filename,) = params
  23. tree = load_lisp(@filename, :"supertux-level")
  24. data = tree[1..-1]
  25. @version = get_value_from_tree(["version", "_"], data, 0);
  26. print "VERSION: ", @filename, " ", @version, "\n"
  27. if (@version == 1) then
  28. parse_v1(data)
  29. else
  30. parse_v2(data)
  31. end
  32. else
  33. raise "Wrong arguments for SuperTux::___init__"
  34. end
  35. end
  36. def parse_v2(data)
  37. @name = get_value_from_tree(["name", "_"], data, "no name")
  38. @author = get_value_from_tree(["author", "_"], data, "no author")
  39. @current_sector = nil
  40. @sectors = []
  41. for sec in sexpr_filter(:sector, data)
  42. sector = Sector.new(self)
  43. sector.load_v2(sec)
  44. @sectors.push(sector)
  45. if sector.name == "main"
  46. @current_sector = sector
  47. end
  48. end
  49. if @current_sector == nil
  50. print "Error: No main sector defined: ", sectors, "\n"
  51. @current_sector = @sectors[0]
  52. end
  53. end
  54. def parse_v1(data)
  55. sector = Sector.new(self)
  56. sector.load_v1(data)
  57. @sectors = []
  58. @sectors.push(sector)
  59. @current_sector = sector
  60. @name = get_value_from_tree(["name", "_"], data, "no name")
  61. @author = get_value_from_tree(["author", "_"], data, "no author")
  62. end
  63. def save(filename)
  64. save_v2(filename)
  65. end
  66. def save_v2(filename)
  67. f = File.new(filename, "w")
  68. f.write(";; Generated by Flexlay Editor\n" +
  69. "(supertux-level\n")
  70. f.write(" (version 2)\n")
  71. f.write(" (name (_ \"%s\"))\n" % @name)
  72. f.write(" (author \"%s\")\n" % @author)
  73. for sector in @sectors
  74. f.write(" (sector\n")
  75. sector.save(f)
  76. f.write(" )\n")
  77. end
  78. f.write(" )\n\n;; EOF ;;\n")
  79. f.close()
  80. end
  81. def save_v1(filename)
  82. f = File.new(filename, "w")
  83. f.write(";; Generated by Flexlay Editor\n" +
  84. "(supertux-level\n")
  85. f.write(" (version 1)\n")
  86. f.write(" (name \"%s\")\n" % @name)
  87. f.write(" (author \"%s\")\n" % @author)
  88. f.write(" (width %d)\n" % @width)
  89. f.write(" (height %d)\n" % @height)
  90. f.write(" (music \"%s\")\n" % @music)
  91. f.write(" (gravity %f)\n" % @gravity)
  92. f.write(" (theme \"%s\")\n" % @theme)
  93. f.write(" (interactive-tm\n")
  94. for i in @interactive.get_data()
  95. f.write("%d " % i)
  96. end
  97. f.write(" )\n\n")
  98. f.write(" (background-tm\n")
  99. for i in @background.get_data()
  100. f.write("%d " % i)
  101. end
  102. f.write(" )\n\n")
  103. f.write(" (foreground-tm\n")
  104. for i in @foreground.get_data()
  105. f.write("%d " % i)
  106. end
  107. f.write(" )\n\n")
  108. f.write(" (camera\n")
  109. f.write(" (mode \"autoscroll\")\n")
  110. f.write(" (path\n")
  111. for obj in @objects.get_objects()
  112. pathnode = obj.get_data()
  113. if (pathnode.class() == PathNode)
  114. f.write(" (point (x %d) (y %d) (speed 1))\n" % obj.get_pos().x, obj.get_pos().y)
  115. end
  116. end
  117. f.write(" ))\n\n")
  118. f.write(" (objects\n")
  119. for obj in @objects.get_objects()
  120. badguy = obj.get_data()
  121. if (badguy.class == BadGuy)
  122. pos = obj.get_pos()
  123. if (badguy.type != "resetpoint")
  124. f.write(" (%s (x %d) (y %d))\n" % badguy.type, int(pos.x), int(pos.y))
  125. end
  126. end
  127. end
  128. f.write(" )\n\n")
  129. f.write(" (reset-points\n")
  130. for obj in @objects.get_objects()
  131. badguy = obj.get_data()
  132. if (badguy.class == BadGuy)
  133. pos = obj.get_pos()
  134. if (badguy.type == "resetpoint")
  135. f.write(" (point (x %d) (y %d))\n" % (pos.x.to_i), pos.y.to_i)
  136. end
  137. end
  138. end
  139. f.write(" )\n\n")
  140. f.write(" )\n\n;; EOF ;;\n")
  141. f.close()
  142. end
  143. def activate_sector(sectorname, workspace)
  144. for sec in @sectors
  145. if sec.name == sectorname
  146. sec.activate(workspace)
  147. @current_sector = sec
  148. break
  149. end
  150. end
  151. end
  152. def activate(workspace)
  153. @current_sector.activate(workspace)
  154. end
  155. def add_sector(sector)
  156. @sectors.push(sector)
  157. end
  158. def remove_sector(name)
  159. if @sectors.length() > 1
  160. @sectors.delete_if{|sec| sec.name == name}
  161. else
  162. print "Only one sector left, can't delete it\n"
  163. end
  164. end
  165. def get_sectors()
  166. return @sectors.map {|sec| sec.name}
  167. end
  168. end
  169. # EOF #