Class: Beefdump::Map::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/beefdump/map/map.rb

Constant Summary collapse

MAP_FORMAT_VERSION =
"1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_name, map_data) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/beefdump/map/map.rb', line 15

def initialize(map_name, map_data)
  @name = map_name

  check_compatibility!(map_data)
    
  load_map_attributes!(map_data)
  load_layers!(map_data)
  load_tilesets!(map_data)
  load_objects!(map_data)

  @raw_data = map_data
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def height
  @height
end

#layersObject (readonly)

Returns the value of attribute layers.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def layers
  @layers
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def name
  @name
end

#objectsObject (readonly)

Returns the value of attribute objects.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def objects
  @objects
end

#tile_heightObject (readonly)

Returns the value of attribute tile_height.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def tile_height
  @tile_height
end

#tile_widthObject (readonly)

Returns the value of attribute tile_width.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def tile_width
  @tile_width
end

#tilesetsObject (readonly)

Returns the value of attribute tilesets.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def tilesets
  @tilesets
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/beefdump/map/map.rb', line 13

def width
  @width
end

Instance Method Details

#background_at(x, y, width = 1, height = 1) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/beefdump/map/map.rb', line 28

def background_at(x, y, width = 1, height = 1)
  tiles = []
  @layers.each do |layer|
    field = Array.new(width).map {|e| Array.new(height).map {|e| nil}}
    for diff_x in 0..(width - 1)
      for diff_y in 0..(height - 1)
        field[diff_x][diff_y] = layer.field[y + diff_y] ? layer.field[y + diff_y][x + diff_x] : nil
      end
    end
    tiles << field
  end

  tiles
end

#get_initial_stateObject



58
59
60
61
62
63
64
# File 'lib/beefdump/map/map.rb', line 58

def get_initial_state
  Logger.info("Loading initial state for map '#{@name}'.")
  initial_state_file = "#{MAP_PATH}/#{@name}.isx"
  raise "Initial state file for map '#{@name}' not found!" unless File.exist?(initial_state_file)

  XmlSimple.xml_in(initial_state_file)
end

#objects_at(x, y) ⇒ Object



43
44
45
# File 'lib/beefdump/map/map.rb', line 43

def objects_at(x, y)
  objects = @objects.select {|o| o.is_at?(x, y)}
end

#tileset_for(gid) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/beefdump/map/map.rb', line 47

def tileset_for(gid)
  return unless gid
  chosen = nil
  @tilesets.each do |tileset|
    break if chosen && gid < tileset.first_gid
    chosen = tileset if gid >= tileset.first_gid
  end

  chosen
end