sector.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. class Sector
  2. @parent = nil
  3. @name = nil
  4. @music = nil
  5. @gravity = 10.0
  6. @width = nil
  7. @height = nil
  8. @background = nil
  9. @interactive = nil
  10. @foreground = nil
  11. @objects = nil
  12. # sketch = nil
  13. @editormap = nil
  14. @cameramode = "normal"
  15. attr_reader :objects, :background, :interactive, :foreground, :parent, :width, :height
  16. attr_accessor :name, :music, :gravity
  17. def initialize(parent)
  18. @parent = parent
  19. @cameramode = "normal"
  20. end
  21. def get_level()
  22. return @parent
  23. end
  24. def resize(size, pos)
  25. @width = size.width
  26. @height = size.height
  27. @background.resize(size, pos)
  28. @interactive.resize(size, pos)
  29. @foreground.resize(size, pos)
  30. end
  31. def new_from_size(name, width, height)
  32. @name = name
  33. @music = ""
  34. @gravity = 10.0
  35. @width = width
  36. @height = height
  37. @foreground = TilemapLayer.new($tileset, @width, @height)
  38. @interactive = TilemapLayer.new($tileset, @width, @height)
  39. @background = TilemapLayer.new($tileset, @width, @height)
  40. @objects = ObjectLayer.new()
  41. # @sketch = SketchLayer.new()
  42. @editormap = EditorMap.new(true)
  43. # @editormap.set_background_color(CL_Color.new(255, 255, 255))
  44. @editormap.add_layer(@background.to_layer())
  45. @editormap.add_layer(@interactive.to_layer())
  46. @editormap.add_layer(@objects.to_layer())
  47. @editormap.add_layer(@foreground.to_layer())
  48. # @editormap.add_layer(@sketch.to_layer())
  49. # FIXME: Data might not get freed since its 'recursively' refcounted
  50. @editormap.set_metadata(self)
  51. return self
  52. end
  53. def load_v1(data)
  54. @name = "main"
  55. @music = get_value_from_tree(["music", "_"], data, "")
  56. @gravity = get_value_from_tree(["gravity", "_"], data, 10.0)
  57. @width = get_value_from_tree(["width", "_"], data, 20)
  58. @height = get_value_from_tree(["height", "_"], data, 15)
  59. @foreground = TilemapLayer.new($tileset, @width, @height)
  60. @foreground.set_data(get_value_from_tree(["foreground-tm"], data, []))
  61. @interactive = TilemapLayer.new($tileset, @width, @height)
  62. @interactive.set_data(get_value_from_tree(["interactive-tm"], data, []))
  63. @background = TilemapLayer.new($tileset, @width, @height)
  64. @background.set_data(get_value_from_tree(["background-tm"], data, []))
  65. @cameramode = "normal"
  66. @editormap = EditorMap.new(true)
  67. @objects = ObjectLayer.new()
  68. for i in get_value_from_tree(["objects"], data, [])
  69. (name, odata) = i[0], i[1..-1]
  70. # fix some old object names
  71. if(name == :money)
  72. name = :jumpy
  73. end
  74. if(name == :laptop)
  75. name = :mriceblock
  76. end
  77. create_gameobject_from_data(@editormap, @objects, name, odata)
  78. end
  79. start_pos_x = get_value_from_tree(["start_pos_x", "_"], data, 0)
  80. start_pos_y = get_value_from_tree(["start_pos_y", "_"], data, 0)
  81. sexpr = [["name", "main"], ["x", start_pos_x], ["y", start_pos_y]]
  82. create_gameobject_from_data(@editormap, @objects, "spawnpoint", sexpr)
  83. background = get_value_from_tree(["background", "_"], data, "")
  84. if(background != "")
  85. sexpr = [["image", background], ["speed", 0.5]]
  86. create_gameobject_from_data(@editormap, @objects, :background, sexpr)
  87. else
  88. sexpr = [["top_color",
  89. get_value_from_tree(["bkgd_red_top", "_"], data, 0),
  90. get_value_from_tree(["bkgd_green_top", "_"], data, 0),
  91. get_value_from_tree(["bkgd_blue_top", "_"], data, 0)],
  92. ["bottom_color",
  93. get_value_from_tree(["bkgd_red_bottom", "_"], data, 0),
  94. get_value_from_tree(["bkgd_green_bottom", "_"], data, 0),
  95. get_value_from_tree(["bkgd_blue_bottom", "_"], data, 0)],
  96. ["speed", 0.5]]
  97. create_gameobject_from_data(@editormap, @objects, :background, sexpr)
  98. end
  99. partsys = get_value_from_tree(["particle_system", "_"], data, "")
  100. if(partsys == "snow")
  101. sexpr = []
  102. create_gameobject_from_data(@editormap, @objects, :'particles-snow', sexpr)
  103. elsif(partsys == "rain")
  104. sexpr = []
  105. create_gameobject_from_data(@editormap, @objects, :'particles-rain', sexpr)
  106. elsif(partsys == "clouds")
  107. sexpr = []
  108. create_gameobject_from_data(@editormap, @objects, :'particles-clouds', sexpr)
  109. elsif(partsys == "")
  110. else
  111. print "Unknown particle system type '", partsys, "'\n"
  112. end
  113. @editormap.add_layer(@background.to_layer())
  114. @editormap.add_layer(@interactive.to_layer())
  115. @editormap.add_layer(@objects.to_layer())
  116. @editormap.add_layer(@foreground.to_layer())
  117. # FIXME: Data might not get freed since its 'recursively' refcounted
  118. @editormap.set_metadata(self)
  119. end
  120. def load_v2(data)
  121. @name = "<No Name>"
  122. @music = ""
  123. @init_script = ""
  124. @gravity = 10.0
  125. @cameramode = "normal"
  126. @width = 0
  127. @height = 0
  128. @background = nil
  129. @interactive = nil
  130. @foreground = nil
  131. @editormap = EditorMap.new(true)
  132. @objects = ObjectLayer.new()
  133. # @sketch = SketchLayer.new()
  134. for i in data
  135. (name,data) = i[0], i[1..-1]
  136. if name == :name
  137. @name = data[0]
  138. elsif name == :gravity
  139. @gravity = data[0]
  140. elsif name == :music
  141. @music = data[0]
  142. elsif name == :"init-script"
  143. @init_script = data[0]
  144. elsif name == :tilemap
  145. layer = get_value_from_tree(["layer", "_"], data, "interactive")
  146. width = get_value_from_tree(["width", "_"], data, 20)
  147. height = get_value_from_tree(["height", "_"], data, 15)
  148. solid = get_value_from_tree(["solid", "_"], data, false)
  149. tilemap = TilemapLayer.new($tileset, width, height)
  150. tilemap.set_data(get_value_from_tree(["tiles"], data, []))
  151. if solid then
  152. @interactive = tilemap
  153. @width = width
  154. @height = height
  155. elsif layer == "background"
  156. @background = tilemap
  157. elsif layer == "foreground"
  158. @foreground = tilemap
  159. else
  160. print "Flexlay doesn't handle tilemap layer '", layer, "'.\n"
  161. end
  162. elsif name == :camera
  163. @cameramode = "normal"
  164. # TODO...
  165. else
  166. create_gameobject_from_data(@editormap, @objects, name, data)
  167. end
  168. end
  169. print "Tileset: ", $tileset, " ", width, " ", height, "\n"
  170. if(@interactive == nil || @width == 0 || @height == 0)
  171. throw "No interactive tilemap in sector '", @name , "'.\n"
  172. end
  173. if (@background == nil)
  174. @background = TilemapLayer.new($tileset, @width, @height)
  175. end
  176. if (@foreground == nil)
  177. @foreground = TilemapLayer.new($tileset, @width, @height)
  178. end
  179. @editormap.add_layer(@background.to_layer()) if @background
  180. @editormap.add_layer(@interactive.to_layer()) if @interactive
  181. @editormap.add_layer(@foreground.to_layer()) if @foreground
  182. @editormap.add_layer(@objects.to_layer())
  183. # @editormap.add_layer(@sketch.to_layer())
  184. @editormap.set_metadata(self)
  185. end
  186. def activate(workspace)
  187. workspace.set_map(@editormap)
  188. TilemapLayer.set_current(@interactive)
  189. ObjectLayer.set_current(@objects)
  190. connect(@editormap.sig_change(), proc{$gui.on_map_change()})
  191. end
  192. def save_tilemap(f, tilemap, name, solid = nil)
  193. f.write(" (tilemap\n")
  194. f.write(" (layer \"%s\")\n" % name)
  195. f.write(" (solid %s)\n" % if solid == :solid then "#t" else "#f" end)
  196. f.write(" (speed %f)\n" % 1.0)
  197. f.write(" (width %d)\n" % tilemap.get_width())
  198. f.write(" (height %d)\n" % tilemap.get_height())
  199. f.write(" (tiles\n")
  200. f.write(" ")
  201. x = 0
  202. for i in tilemap.get_data()
  203. f.write("%d " % i)
  204. x += 1
  205. if x == width then
  206. f.write("\n ")
  207. x = 0
  208. end
  209. end
  210. f.write("))\n")
  211. end
  212. def save(f)
  213. f.write(" (name \"#{@name}\")\n" % @name)
  214. if(@music != "")
  215. f.write(" (music \"#{@music}\")\n" % @music)
  216. end
  217. if(@init_script != "")
  218. f.write(" (init-script \"#{@init_script}\")\n")
  219. end
  220. f.write(" (gravity %f)\n" % @gravity)
  221. save_tilemap(f, @background, "background")
  222. save_tilemap(f, @interactive, "interactive", :solid)
  223. save_tilemap(f, @foreground, "foreground")
  224. # save_strokelayer(f, @sketch)
  225. f.write(" (camera\n")
  226. f.write(" (mode \"%s\")\n" % [@cameramode])
  227. # f.write(" (path\n")
  228. # @objects.get_objects().each {|obj|
  229. # pathnode = obj.get_data()
  230. # if (pathnode.is_a?(PathNode))
  231. # f.write(" (point (x %d) (y %d) (speed 1))\n" % obj.get_pos().x, obj.get_pos().y)
  232. # end
  233. # }
  234. # f.write(" )")
  235. f.write(" )\n\n")
  236. for obj in @objects.get_objects()
  237. object = obj.get_data()
  238. object.save(f, obj)
  239. end
  240. end
  241. end
  242. # EOF #