level.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ## $Id$
  2. ##
  3. ## Flexlay - A Generic 2D Game Editor
  4. ## Copyright (C) 2002 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. require_relative "gameobjects.rb"
  19. class Level
  20. attr_accessor :filename, :data, :editormap, :objects, :tilemap
  21. def initialize(*params)
  22. if params.length == 2 then
  23. (width, height) = params
  24. @data = NetPanzerFileStruct.new($tileset, width, height)
  25. @filename = nil
  26. generate_random_grass()
  27. elsif params.length == 1 then
  28. (@filename,) = params
  29. @data = NetPanzerFileStruct.new($tileset, @filename)
  30. end
  31. @editormap = EditorMap.new(true)
  32. @objects = ObjectLayer.new()
  33. @editormap.add_layer(@data.get_tilemap().to_layer())
  34. @editormap.add_layer(@objects.to_layer())
  35. @tilemap = @data.get_tilemap()
  36. if @filename then
  37. load_optfile(@filename[0..-5] + ".opt")
  38. load_spnfile(@filename[0..-5] + ".spn")
  39. end
  40. # FIXME: Data might not get freed since its 'recursively' refcounted
  41. @editormap.set_data(self)
  42. end
  43. def width()
  44. return @data.get_tilemap().get_width()
  45. end
  46. def height()
  47. return @data.get_tilemap().get_height()
  48. end
  49. def generate_random_grass()
  50. @data.get_tilemap().set_data(Array.new(width()*height()) {|i| 8097 + rand(16) })
  51. end
  52. def name()
  53. return @data.get_name()
  54. end
  55. def name=(name)
  56. @data.set_name(name)
  57. end
  58. def load_optfile(filename)
  59. f = File.new(filename)
  60. count = /ObjectiveCount: ([0-9]+)/.match(f.readline().chop)[1].to_i
  61. count.times{|i|
  62. f.readline() # Skip empty line
  63. name = /Name: (.+)/.match(f.readline().chop)
  64. loc = /Location: ([0-9]+) ([0-9]+)/.match(f.readline().chop)
  65. GameObjects::Outpost.create(@objects,
  66. name,
  67. loc[1].to_i, loc[2].to_i)
  68. }
  69. f.close()
  70. end
  71. def load_spnfile(filename)
  72. f = File.new(filename)
  73. count = /SpawnCount: ([0-9]+)/.match(f.readline().chop)[1].to_i
  74. count.times{|i|
  75. loc = /Location: ([0-9]+) ([0-9]+)/.match(f.readline().chop)
  76. GameObjects::SpawnPoint.create(@objects,
  77. loc[1].to_i, loc[2].to_i)
  78. }
  79. f.close()
  80. end
  81. def save_optfile(filename)
  82. outposts = @objects.get_objects().
  83. find_all {|obj| obj.get_data().is_a? GameObjects::Outpost }.
  84. map {|obj| obj.get_data() }
  85. f = open(filename, "w")
  86. f.write("ObjectiveCount: %d\n\n" % outposts.length)
  87. outposts.each {|obj|
  88. f.write("Name: %s\n" % obj.name)
  89. f.write("Location: %d %d\n\n" % [obj.x, obj.y])
  90. }
  91. f.close()
  92. end
  93. def save_spnfile(filename)
  94. spawnpoints = @objects.get_objects().
  95. find_all {|obj| obj.get_data().is_a? GameObjects::SpawnPoint }.
  96. map {|obj| obj.get_data() }
  97. f = open(filename, "w")
  98. f.write("SpawnCount: %d\n" % spawnpoints.length)
  99. spawnpoints.each {|obj|
  100. f.print("Location: %d %d\n" % [obj.x, obj.y])
  101. }
  102. f.close()
  103. end
  104. def save(filename)
  105. flatten()
  106. if filename[-4..-1] == ".npm"
  107. data.save(filename)
  108. save_optfile(filename[0..-5] + ".opt")
  109. save_spnfile(filename[0..-5] + ".spn")
  110. else
  111. raise "Fileextension not valid, must be .npm!"
  112. end
  113. end
  114. def activate(workspace)
  115. puts "Level activate"
  116. workspace.set_map(@editormap)
  117. TilemapLayer.set_current(@data.get_tilemap())
  118. ObjectLayer.set_current(@objects)
  119. end
  120. def flatten()
  121. # Converts all objects to the Tilemap
  122. @objects.get_objects().each { |obj|
  123. obj.get_data().draw_to_tilemap(@tilemap)
  124. }
  125. end
  126. def unflatten()
  127. # Reconstructs objects from the Tilemap
  128. data = @tilemap.get_data()
  129. width = @tilemap.get_width()
  130. height = @tilemap.get_height()
  131. first_tiles = {}
  132. $brushes.each_with_index{|i,index| first_tiles[i[0]] = index }
  133. (0..height-1).each{|y|
  134. (0..width-1).each{|x|
  135. tile = data[width*y + x]
  136. if tile != 0 && first_tiles.has_key?(tile) then
  137. # Insert checking for dups here
  138. GameObjects::TileObject.create(@objects, first_tiles[data[width*y + x]], x, y)
  139. end
  140. }
  141. }
  142. end
  143. end
  144. # EOF #