gameobj.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. class GameObj
  2. def property_dialog()
  3. print "Object ", self.class, " has no properties\n"
  4. end
  5. def set_obj(obj)
  6. @obj = obj
  7. end
  8. end
  9. class SecretArea<GameObj
  10. attr_accessor :message
  11. def initialize(data, sexpr = [])
  12. @data = data
  13. @message = get_value_from_tree(["message", "_"], sexpr, "")
  14. x = get_value_from_tree(["x", "_"], sexpr, nil)
  15. y = get_value_from_tree(["y", "_"], sexpr, nil)
  16. width = get_value_from_tree(["width", "_"], sexpr, 64)
  17. height = get_value_from_tree(["height", "_"], sexpr, 64)
  18. if x != nil and y != nil then
  19. @data.set_rect(CL_Rect.new(CL_Point.new(x.to_i, y.to_i),
  20. CL_Size.new(width.to_i, height.to_i)))
  21. end
  22. end
  23. def save(f, obj)
  24. rect = @data.get_rect()
  25. f.write(" (secretarea (x #{rect.left})\n" \
  26. " (y #{rect.top})\n" \
  27. " (width #{rect.get_width()})\n" \
  28. " (height #{rect.get_height()})\n" \
  29. " (message #{@message.inspect}))\n")
  30. end
  31. def property_dialog()
  32. puts @message.inspect
  33. dialog = GenericDialog.new("SecretArea Property Dialog", $gui.get_component())
  34. dialog.add_string("Message: ", @message)
  35. dialog.set_callback(proc{|message|
  36. @message = message
  37. })
  38. end
  39. end
  40. class AmbientSound<GameObj
  41. attr_accessor :factor, :bias, :sample, :volume
  42. def initialize(data, sexpr = [])
  43. @data = data
  44. @data.set_color(CL_Color.new(200, 200, 200, 128))
  45. @factor = get_value_from_tree(["distance_factor", "_"], sexpr, 0.1)
  46. @bias = get_value_from_tree(["distance_bias", "_"], sexpr, 200)
  47. @sample = get_value_from_tree(["sample", "_"], sexpr, "waterfall")
  48. @volume = get_value_from_tree(["volume", "_"], sexpr, 1)
  49. x = get_value_from_tree(["x", "_"], sexpr, nil)
  50. y = get_value_from_tree(["y", "_"], sexpr, nil)
  51. width = get_value_from_tree(["width", "_"], sexpr, 64)
  52. height = get_value_from_tree(["height", "_"], sexpr, 64)
  53. if x != nil and y != nil then
  54. @data.set_rect(CL_Rect.new(CL_Point.new(x, y), CL_Size.new(width, height)))
  55. end
  56. end
  57. def save(f, obj)
  58. rect = @data.get_rect()
  59. f.write(" (ambient_sound (x #{rect.left})\n" \
  60. " (y #{rect.top})\n" \
  61. " (width #{rect.get_width()})\n" \
  62. " (height #{rect.get_height()})\n" \
  63. " (sample #{@sample.inspect})\n" \
  64. " (distance_factor #{@factor.inspect})\n" \
  65. " (distance_bias #{@bias.inspect})\n" \
  66. " (volume #{@volume.inspect}))\n")
  67. end
  68. def property_dialog()
  69. puts @factor.inspect
  70. puts @bias.inspect
  71. puts @sample.inspect
  72. puts @volume.inspect
  73. dialog = GenericDialog.new("AmbientSound Property Dialog", $gui.get_component())
  74. dialog.add_float("Distance Factor: ", @factor)
  75. dialog.add_float("Distance Bias: ", @bias)
  76. dialog.add_string("Sample: ", @sample)
  77. dialog.add_int("Max Volume: ",@volume)
  78. dialog.set_callback(proc{|factor, bias, sample, volume|
  79. @factor = factor
  80. @bias = bias
  81. @sample = sample
  82. @volume = volume
  83. })
  84. end
  85. end
  86. class SequenceTrigger<GameObj
  87. attr_accessor :sequence, :data
  88. def initialize(data, sexpr = [])
  89. @data = data
  90. @sequence = get_value_from_tree(["sequence", "_"], sexpr, "")
  91. @data.set_color(CL_Color.new(255, 0, 0, 128))
  92. x = get_value_from_tree(["x", "_"], sexpr, nil)
  93. y = get_value_from_tree(["y", "_"], sexpr, nil)
  94. width = get_value_from_tree(["width", "_"], sexpr, 64)
  95. height = get_value_from_tree(["height", "_"], sexpr, 64)
  96. if x != nil and y != nil then
  97. @data.set_rect(CL_Rect.new(CL_Point.new(x, y), CL_Size.new(width, height)))
  98. end
  99. end
  100. def save(f, obj)
  101. rect = @data.get_rect()
  102. f.write(" (sequencetrigger (x #{rect.left})\n" \
  103. " (y #{rect.top})\n" \
  104. " (width #{rect.get_width()})\n" \
  105. " (height #{rect.get_height()})\n" \
  106. " (sequence #{@sequence.inspect}))\n")
  107. end
  108. def property_dialog()
  109. puts @sequence.inspect
  110. dialog = GenericDialog.new("SequenceTrigger Property Dialog", $gui.get_component())
  111. dialog.add_string("Sequence: ", @sequence)
  112. dialog.set_callback(proc{|sequence|
  113. @sequence = sequence
  114. })
  115. end
  116. end
  117. class BadGuy<GameObj
  118. def initialize(type)
  119. @type = type
  120. @direction = "auto"
  121. end
  122. def save(f, obj)
  123. pos = obj.get_pos()
  124. f.write(" (%s (x %d) (y %d) (direction \"%s\")\n" % [@type, pos.x, pos.y, direction])
  125. end
  126. def property_dialog()
  127. dialog = GenericDialog.new("BadGuy Property Dialog", $gui.get_component())
  128. dialog.add_enum("Direction: ", ["left", "right", "auto"], @direction)
  129. dialog.set_callback(proc{|direction|
  130. if (@direction != "auto" and direction != "auto" and @direction != direction)
  131. @obj.flip_horizontal()
  132. end
  133. @direction = direction
  134. })
  135. end
  136. end
  137. class Dispenser<GameObj
  138. def initialize(data, sexpr = [])
  139. @data = data
  140. @badguy = get_value_from_tree(["badguy", "_"], sexpr, "snowball")
  141. @cycle = get_value_from_tree(["cycle", "_"], sexpr, 2)
  142. end
  143. def save(f, obj)
  144. pos = obj.get_pos()
  145. f.write(" (dispenser (x %d) (y %d) (badguy \"%s\") (cycle %d))\n" % [pos.x, pos.y, @badguy, @cycle])
  146. end
  147. def property_dialog()
  148. dialog = GenericDialog.new("Dispenser Property Dialog", $gui.get_component())
  149. dialog.add_string("Badguy Type: ", @badguy)
  150. dialog.add_int("Cycle Type: ", @cycle)
  151. dialog.set_callback(proc{|badguy, cycle|
  152. @badguy = badguy
  153. @cycle = cycle
  154. })
  155. end
  156. end
  157. class Platform<GameObj
  158. def initialize(data, sexpr = [])
  159. @data = data
  160. @path = get_value_from_tree(["use_path", "_"], sexpr, "path01")
  161. @type = get_value_from_tree(["type", "_"], sexpr, "flying")
  162. end
  163. def save(f, obj)
  164. pos = obj.get_pos()
  165. f.write(" (platform (x %d) (y %d) (use_path \"%s\") (type \"%s\"))\n" % [pos.x, pos.y, @path, @type])
  166. end
  167. def property_dialog()
  168. dialog = GenericDialog.new("Platform Property Dialog", $gui.get_component())
  169. dialog.add_string("Use Path: ", @path)
  170. dialog.add_string("Platform Type: ", @type)
  171. dialog.set_callback(proc{|path, type|
  172. @path = path
  173. @type = type
  174. })
  175. end
  176. end
  177. class SpawnPoint<GameObj
  178. attr_accessor :name
  179. attr_reader :data
  180. def initialize(data, sexpr = [])
  181. @data = data
  182. @name = get_value_from_tree(["name", "_"], sexpr, "main")
  183. connect_v1_ObjMapObject(data.to_object.sig_move(), method(:on_move))
  184. on_move(data)
  185. end
  186. def on_move(data)
  187. pos = @data.to_object.get_pos()
  188. pos.x = (((pos.x+16)/32).to_i)*32
  189. pos.y = (((pos.y+16)/32).to_i)*32
  190. @data.to_object.set_pos(pos)
  191. end
  192. def save(f, obj)
  193. pos = obj.get_pos()
  194. f.write(" (spawnpoint (name \"%s\") (x %d) (y %d))\n" % [@name, pos.x, pos.y])
  195. end
  196. def property_dialog()
  197. dialog = GenericDialog.new("SpawnPoint Property Dialog", $gui.get_component())
  198. dialog.add_string("Name: ", @name)
  199. dialog.set_callback(proc{|name|
  200. @name = name
  201. })
  202. end
  203. end
  204. class SimpleObject<GameObj
  205. def initialize(type)
  206. @type = type
  207. end
  208. def save(f, obj)
  209. pos = obj.get_pos()
  210. f.write(" (%s (x %d) (y %d))\n" % [@type, pos.x, pos.y])
  211. end
  212. end
  213. class SimpleTileObject<GameObj
  214. def initialize(data, type, sexpr = [])
  215. @type = type
  216. @data = data
  217. connect_v1_ObjMapObject(@data.to_object.sig_move(), method(:on_move))
  218. on_move(data)
  219. end
  220. def on_move(data)
  221. pos = @data.to_object.get_pos()
  222. pos.x = (((pos.x+16)/32).to_i)*32
  223. pos.y = (((pos.y+16)/32).to_i)*32
  224. @data.to_object.set_pos(pos)
  225. end
  226. def save(f, obj)
  227. pos = obj.get_pos()
  228. f.write(" (%s (x %d) (y %d))\n" % [@type, pos.x, pos.y])
  229. end
  230. end
  231. class InfoBlock<GameObj
  232. attr_accessor :message
  233. def initialize(data, sexpr = [])
  234. @data = data
  235. @message = get_value_from_tree(["message", "_"], sexpr, "")
  236. connect_v1_ObjMapObject(@data.to_object.sig_move(), method(:on_move))
  237. on_move(data)
  238. end
  239. def on_move(data)
  240. pos = @data.to_object.get_pos()
  241. pos.x = (((pos.x+16)/32).to_i)*32
  242. pos.y = (((pos.y+16)/32).to_i)*32
  243. @data.to_object.set_pos(pos)
  244. end
  245. def save(f, obj)
  246. pos = obj.get_pos()
  247. f.write(" (infoblock (x %d) (y %d)\n" % [pos.x, pos.y]);
  248. f.write(" (message (_ \"%s\"))\n" % [@message]);
  249. f.write(" )\n");
  250. end
  251. def property_dialog()
  252. dialog = GenericDialog.new("InfoBox Property Dialog" % [@type],
  253. $gui.get_component())
  254. dialog.add_string("Message: ", @message)
  255. dialog.set_callback(proc{|message|
  256. @message = message
  257. })
  258. end
  259. end
  260. class Powerup<GameObj
  261. attr_accessor :sprite
  262. def initialize(data, sexpr = [])
  263. @data = data
  264. @sprite = get_value_from_tree(["sprite", "_"], sexpr, "egg")
  265. connect_v1_ObjMapObject(@data.to_object.sig_move(), method(:on_move))
  266. on_move(data)
  267. end
  268. def on_move(data)
  269. pos = @data.to_object.get_pos()
  270. pos.x = (((pos.x+16)/32).to_i)*32
  271. pos.y = (((pos.y+16)/32).to_i)*32
  272. @data.to_object.set_pos(pos)
  273. end
  274. def save(f, obj)
  275. pos = obj.get_pos()
  276. f.write(" (powerup (x %d) (y %d)\n" % [pos.x, pos.y]);
  277. f.write(" (sprite (_ \"%s\"))\n" % [@sprite]);
  278. f.write(" )\n");
  279. end
  280. def property_dialog()
  281. dialog = GenericDialog.new("Powerup Property Dialog" % [@sprite],
  282. $gui.get_component())
  283. dialog.add_string("Sprite: ", @sprite)
  284. dialog.set_callback(proc{|sprite|
  285. @sprite = sprite
  286. })
  287. end
  288. end
  289. class ParticleSystem<GameObj
  290. def initialize(type, sexpr = [])
  291. @type = type
  292. @layer = get_value_from_tree(["layer", "_"], sexpr, -1)
  293. end
  294. def save(f, obj)
  295. f.write(" (particles-%s\n" % [@type])
  296. if(@layer != -1)
  297. f.write(" (layer %d)\n" % [@layer])
  298. end
  299. f.write(" )\n")
  300. end
  301. def property_dialog()
  302. dialog = GenericDialog.new("%s-ParticleSystem Property Dialog" % [@type],
  303. $gui.get_component())
  304. dialog.add_int("Layer: ", @layer)
  305. dialog.set_callback(proc{|layer|
  306. @layer = layer
  307. })
  308. end
  309. end
  310. class Gradient<GameObj
  311. def initialize(object, sexpr = [])
  312. @object = object
  313. @layer = get_value_from_tree(["layer", "_"], sexpr, -1)
  314. @color_top = [0, 0, 0]
  315. @color_bottom = [0, 0, 0]
  316. @type = "image"
  317. if(get_value_from_tree(["top_color"], sexpr, []) != [])
  318. @color_top = parse_color(
  319. get_value_from_tree(["top_color"], sexpr, []))
  320. @color_bottom = parse_color(
  321. get_value_from_tree(["bottom_color"], sexpr, []))
  322. end
  323. end
  324. def parse_color(sexpr = [])
  325. if(sexpr.size() < 3)
  326. return [0, 0, 0]
  327. end
  328. return [sexpr[0], sexpr[1], sexpr[2]]
  329. end
  330. def save(f, obj)
  331. f.write(" (gradient\n")
  332. f.write(" (top_color %f %f %f)\n" % [@color_top[0], @color_top[1], @color_top[2]])
  333. f.write(" (bottom_color %f %f %f)\n" % [@color_bottom[0], @color_bottom[1], @color_bottom[2]])
  334. if(@layer != -1)
  335. f.write(" (layer %d)\n" % [@layer])
  336. end
  337. f.write(" )\n")
  338. end
  339. def property_dialog()
  340. dialog = GenericDialog.new("Background Property Dialog", $gui.get_component())
  341. dialog.add_int("Layer: ", @layer)
  342. dialog.add_int("Top Red: ", @color_top[0])
  343. dialog.add_int("Top Green: ", @color_top[1])
  344. dialog.add_int("Top Blue: ", @color_top[2])
  345. dialog.add_int("Bottom Red: ", @color_bottom[0])
  346. dialog.add_int("Bottom Green: ", @color_bottom[1])
  347. dialog.add_int("Bottom Blue: ", @color_bottom[2])
  348. dialog.set_callback(
  349. proc{|layer, topred, topgreen, topblue, botred, botgreen, botblue|
  350. @layer = layer
  351. @color_top[0] = topred
  352. @color_top[1] = topgreen
  353. @color_top[2] = topblue
  354. @color_bottom[0] = botred
  355. @color_bottom[1] = botgreen
  356. @color_bottom[2] = botblue
  357. })
  358. end
  359. end
  360. class Background<GameObj
  361. def initialize(object, sexpr = [])
  362. @object = object
  363. @image = get_value_from_tree(["image", "_"], sexpr, "")
  364. @speed = get_value_from_tree(["speed", "_"], sexpr, 1.0)
  365. @layer = get_value_from_tree(["layer", "_"], sexpr, -1)
  366. @type = "image"
  367. end
  368. def save(f, obj)
  369. f.write(" (background\n")
  370. f.write(" (image \"%s\")\n" % [@image])
  371. f.write(" (speed %f)\n" % [@speed])
  372. if(@layer != -1)
  373. f.write(" (layer %d)\n" % [@layer])
  374. end
  375. f.write(" )\n")
  376. end
  377. def property_dialog()
  378. dialog = GenericDialog.new("Background Property Dialog", $gui.get_component())
  379. dialog.add_string("Image: ", @image)
  380. dialog.add_float("Speed: ", @speed)
  381. dialog.add_int("Layer: ", @layer)
  382. dialog.set_callback(
  383. proc{|type, image, speed, layer|
  384. @type = type
  385. @image = image
  386. @speed = speed
  387. @layer = layer
  388. })
  389. end
  390. end
  391. class UnimplementedObject<GameObj
  392. def initialize(sexpr = [])
  393. @sexpr = sexpr
  394. end
  395. def save(f)
  396. f.write(" (sexpr %s)\n" % [@sexpr])
  397. # TODO
  398. end
  399. end
  400. class LevelTime<GameObj
  401. def initialize(sexpr = [])
  402. @time = get_value_from_tree(["time", "_"], sexpr, 999)
  403. end
  404. def save(f, obj)
  405. f.write(" (leveltime\n");
  406. f.write(" (time %f)\n" % [@time]);
  407. f.write(" )\n");
  408. end
  409. def property_dialog()
  410. dialog = GenericDialog.new("LevelTime Property Dialog",
  411. $gui.get_component())
  412. dialog.add_float("Time: ", @time)
  413. dialog.set_callback(proc{|time|
  414. @time = time
  415. })
  416. end
  417. end
  418. class Door<GameObj
  419. attr_accessor :sector, :spawnpoint
  420. attr_reader :data
  421. def initialize(type, data, sexpr = [])
  422. @type = type
  423. @data = data
  424. @sector = get_value_from_tree(["sector", "_"], sexpr, "main")
  425. @spawnpoint = get_value_from_tree(["spawnpoint", "_"], sexpr, "main")
  426. connect_v1_ObjMapObject(@data.to_object.sig_move(), method(:on_move))
  427. on_move(data)
  428. end
  429. def on_move(data)
  430. pos = @data.to_object.get_pos()
  431. pos.x = (((pos.x+16)/32).to_i)*32
  432. pos.y = (((pos.y+16)/32).to_i)*32
  433. @data.to_object.set_pos(pos)
  434. end
  435. def save(f, obj)
  436. pos = obj.get_pos()
  437. f.write(" (%s\n" % [@type])
  438. f.write(" (x %d) (y %d)" % [pos.x, pos.y])
  439. f.write(" (sector \"%s\")\n" % @sector)
  440. f.write(" (spawnpoint \"%s\")\n" % @spawnpoint)
  441. f.write(" )\n")
  442. end
  443. def property_dialog()
  444. dialog = GenericDialog.new("Door Property Dialog", $gui.get_component())
  445. dialog.add_string("Sector: ", @sector)
  446. dialog.add_string("Spawnpoint: ", @spawnpoint)
  447. dialog.set_callback(proc{|sector, spawnpoint|
  448. @sector = sector
  449. @spawnpoint = spawnpoint
  450. })
  451. end
  452. end
  453. class PathNode<GameObj
  454. attr_accessor :node
  455. def initialize(node)
  456. @node = node
  457. end
  458. def save(f, obj)
  459. end
  460. end
  461. class ScriptedObject<GameObj
  462. def initialize(object, sexpr = [])
  463. @object = object
  464. @name = get_value_from_tree(["name", "_"], sexpr, "")
  465. @sprite = get_value_from_tree(["sprite", "_"], sexpr, "")
  466. @visible = get_value_from_tree(["visible", "_"], sexpr, true)
  467. @physic_enabled = get_value_from_tree(["physic-enabled", "_"], sexpr, false)
  468. @solid = get_value_from_tree(["solid", "_"], sexpr, false)
  469. @layer = get_value_from_tree(["layer", "_"], sexpr, 100)
  470. load_sprite()
  471. end
  472. def save(f, obj)
  473. pos = obj.get_pos()
  474. f.write(" (scriptedobject\n")
  475. f.write(" (x %d) (y %d)\n" % [pos.x, pos.y])
  476. f.write(" (name \"%s\")\n" % @name)
  477. f.write(" (sprite \"%s\")\n" % @sprite)
  478. f.write(" (layer %d)\n" % @layer)
  479. if(@visible == true)
  480. f.write(" (visible #t)\n")
  481. else
  482. f.write(" (visible #f)\n")
  483. end
  484. if(@physic_enabled == true)
  485. f.write(" (physic-enabled #t)\n")
  486. else
  487. f.write(" (physic-enabled #f)\n")
  488. end
  489. if(@solid == true)
  490. f.write(" (solid #t)\n")
  491. else
  492. f.write(" (solid #f)\n")
  493. end
  494. f.write(" )\n");
  495. end
  496. def load_sprite()
  497. begin
  498. sprite = load_cl_sprite($datadir + @sprite)
  499. @object.set_sprite(sprite)
  500. rescue
  501. sprite = load_cl_sprite($datadir + "images/engine/editor/scriptedobject.png")
  502. end
  503. @object.set_sprite(sprite)
  504. end
  505. def property_dialog()
  506. dialog = GenericDialog.new("Scripted Object Property Dialog",
  507. $gui.get_component())
  508. dialog.add_string("Name: ", @name)
  509. dialog.add_string("Sprite: ", @sprite)
  510. dialog.add_int("Layer: ", @layer)
  511. dialog.add_bool("Visible: ", @visible)
  512. dialog.add_bool("Physics: ", @physic_enabled)
  513. dialog.add_bool("Solid: ", @solid)
  514. dialog.set_callback(proc{|name, sprite, layer, visible, physic_enabled, solid|
  515. @name = name
  516. @sprite = sprite
  517. @layer = layer
  518. @visible = visible
  519. @physic_enabled = physic_enabled
  520. @solid = solid
  521. load_sprite()
  522. })
  523. end
  524. end
  525. # EOF #