Class: NEXXT::Parser::Map

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

Defined Under Namespace

Classes: Metatile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_bytes, width: nil, height: nil) ⇒ Map

Returns a new instance of Map.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nexxt/parser/map.rb', line 13

def initialize(raw_bytes, width: nil, height: nil)
  @raw_bytes = raw_bytes.dup
  @width, @height = if width && height
                      [width, height]
                    else
                      Map.extract_dimensions(@raw_bytes)
                    end
  @tiles = Map.organize_in_tiles(@raw_bytes, @width, @height)
  @attributes = @raw_bytes[(@width * @height)..]

  @metatiles = Map.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.rb', line 6

def attributes
  @attributes
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#metatilesObject (readonly)

Returns the value of attribute metatiles.



6
7
8
# File 'lib/nexxt/parser/map.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.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.rb', line 6

def tiles
  @tiles
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Class Method Details

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



52
53
54
55
56
57
# File 'lib/nexxt/parser/map.rb', line 52

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



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nexxt/parser/map.rb', line 30

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



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nexxt/parser/map.rb', line 59

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



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nexxt/parser/map.rb', line 72

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 = Map.extract_metatile_tiles(tiles, meta_row, meta_column)
      Metatile.new(
        meta_tiles[0], meta_tiles[1], meta_tiles[2], meta_tiles[3],
        Map.extract_attribute(attributes, width, meta_row, meta_column)
      )
    end
  end.flatten
end

.organize_in_tiles(bytes, width, height) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/nexxt/parser/map.rb', line 44

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



26
27
28
# File 'lib/nexxt/parser/map.rb', line 26

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