Class: NEXXT::Parser::MapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/nexxt/parser/map_file.rb

Defined Under Namespace

Classes: Metatile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_bytes) ⇒ MapFile

Returns a new instance of MapFile.



13
14
15
16
17
18
19
20
# File 'lib/nexxt/parser/map_file.rb', line 13

def initialize(raw_bytes)
  @raw_bytes = raw_bytes.dup
  @width, @height = MapFile.extract_dimensions(@raw_bytes)
  @tiles = MapFile.organize_in_tiles(@raw_bytes, @width, @height)
  @attributes = @raw_bytes[(@width * @height)..]

  @metatiles = MapFile.organize_in_metatiles(@tiles, @attributes, @width, @height)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/nexxt/parser/map_file.rb', line 6

def attributes
  @attributes
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/nexxt/parser/map_file.rb', line 6

def height
  @height
end

#metatilesObject (readonly)

Returns the value of attribute metatiles.



6
7
8
# File 'lib/nexxt/parser/map_file.rb', line 6

def metatiles
  @metatiles
end

#raw_bytesObject (readonly)

Returns the value of attribute raw_bytes.



6
7
8
# File 'lib/nexxt/parser/map_file.rb', line 6

def raw_bytes
  @raw_bytes
end

#tilesObject (readonly)

Returns the value of attribute tiles.



6
7
8
# File 'lib/nexxt/parser/map_file.rb', line 6

def tiles
  @tiles
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/nexxt/parser/map_file.rb', line 6

def width
  @width
end

Class Method Details

.extract_attribute(attributes, width, meta_row, meta_column) ⇒ Object



48
49
50
51
52
53
# File 'lib/nexxt/parser/map_file.rb', line 48

def self.extract_attribute(attributes, width, meta_row, meta_column)
  attribute = attributes[((meta_row / 2) * (width / 4)) + (meta_column / 2)]
  attribute >>= 4 if meta_row.odd?
  attribute >>= 2 if meta_column.odd?
  attribute & 0b11
end

.extract_dimensions(bytes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nexxt/parser/map_file.rb', line 26

def self.extract_dimensions(bytes)
  if bytes.size == 1024
    [32, 30]
  else
    width_l, width_h, height_l, height_h = bytes.pop(4)
    raise 'Invalid sizes' if width_l.nil? ||
                             width_h.nil? ||
                             height_l.nil? ||
                             height_h.nil?

    [(width_h * 256) + width_l, (height_h * 256) + height_l]
  end
end

.extract_metatile_tiles(tiles, meta_row, meta_column) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nexxt/parser/map_file.rb', line 55

def self.extract_metatile_tiles(tiles, meta_row, meta_column)
  upper = meta_row * 2
  left = meta_column * 2
  lower = upper + 1
  right = left + 1
  [
    tiles[upper][left],
    tiles[upper][right],
    tiles[lower][left],
    tiles[lower][right]
  ]
end

.organize_in_metatiles(tiles, attributes, width, height) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nexxt/parser/map_file.rb', line 68

def self.organize_in_metatiles(tiles, attributes, width, height)
  Array.new(height / 2) do |meta_row|
    Array.new(width / 2) do |meta_column|
      meta_tiles = MapFile.extract_metatile_tiles(tiles, meta_row, meta_column)
      Metatile.new(
        meta_tiles[0], meta_tiles[1], meta_tiles[2], meta_tiles[3],
        MapFile.extract_attribute(attributes, width, meta_row, meta_column)
      )
    end
  end.flatten
end

.organize_in_tiles(bytes, width, height) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/nexxt/parser/map_file.rb', line 40

def self.organize_in_tiles(bytes, width, height)
  Array.new(height) do |row|
    Array.new(width) do |column|
      bytes[(row * width) + column]
    end
  end
end

.read(file) ⇒ Object



22
23
24
# File 'lib/nexxt/parser/map_file.rb', line 22

def self.read(file)
  new(File.read(file).unpack('C*'))
end