Class: NEXXT::Parser::MapFile
- Inherits:
-
Object
- Object
- NEXXT::Parser::MapFile
- Defined in:
- lib/nexxt/parser/map_file.rb
Defined Under Namespace
Classes: Metatile
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#metatiles ⇒ Object
readonly
Returns the value of attribute metatiles.
-
#raw_bytes ⇒ Object
readonly
Returns the value of attribute raw_bytes.
-
#tiles ⇒ Object
readonly
Returns the value of attribute tiles.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
- .extract_attribute(attributes, width, meta_row, meta_column) ⇒ Object
- .extract_dimensions(bytes) ⇒ Object
- .extract_metatile_tiles(tiles, meta_row, meta_column) ⇒ Object
- .organize_in_metatiles(tiles, attributes, width, height) ⇒ Object
- .organize_in_tiles(bytes, width, height) ⇒ Object
- .read(file) ⇒ Object
Instance Method Summary collapse
-
#initialize(raw_bytes) ⇒ MapFile
constructor
A new instance of MapFile.
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.(@tiles, @attributes, @width, @height) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/nexxt/parser/map_file.rb', line 6 def attributes @attributes end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
6 7 8 |
# File 'lib/nexxt/parser/map_file.rb', line 6 def height @height end |
#metatiles ⇒ Object (readonly)
Returns the value of attribute metatiles.
6 7 8 |
# File 'lib/nexxt/parser/map_file.rb', line 6 def @metatiles end |
#raw_bytes ⇒ Object (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 |
#tiles ⇒ Object (readonly)
Returns the value of attribute tiles.
6 7 8 |
# File 'lib/nexxt/parser/map_file.rb', line 6 def tiles @tiles end |
#width ⇒ Object (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, , ) attribute = attributes[(( / 2) * (width / 4)) + ( / 2)] attribute >>= 4 if .odd? attribute >>= 2 if .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.(tiles, , ) upper = * 2 left = * 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.(tiles, attributes, width, height) Array.new(height / 2) do || Array.new(width / 2) do || = MapFile.(tiles, , ) Metatile.new( [0], [1], [2], [3], MapFile.extract_attribute(attributes, width, , ) ) 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 |