Class: Table

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

Constant Summary collapse

MAX_ROW_LENGTH =
20

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Table

Returns a new instance of Table.



24
25
26
27
28
# File 'lib/RGSS.rb', line 24

def initialize(bytes)
  @dim, @x, @y, @z, items, *@data = bytes.unpack('L5 S*')
  raise "Size mismatch loading Table from data" unless items == @data.length
  raise "Size mismatch loading Table from data" unless @x * @y * @z == items
end

Class Method Details

._load(bytes) ⇒ Object



69
70
71
# File 'lib/RGSS.rb', line 69

def self._load(bytes)
  Table.new(bytes)
end

Instance Method Details

#_dump(*ignored) ⇒ Object



65
66
67
# File 'lib/RGSS.rb', line 65

def _dump(*ignored)
  return [@dim, @x, @y, @z, @x * @y * @z, *@data].pack('L5 S*')
end

#encode_with(coder) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/RGSS.rb', line 32

def encode_with(coder)
  coder.style = Psych::Nodes::Mapping::BLOCK

  coder['dim'] = @dim
  coder['x'] = @x
  coder['y'] = @y
  coder['z'] = @z

  if @x * @y * @z > 0
    stride = @x < 2 ? (@y < 2 ? @z : @y) : @x
    rows = @data.each_slice(stride).to_a
    if MAX_ROW_LENGTH != -1 && stride > MAX_ROW_LENGTH
      block_length = (stride + MAX_ROW_LENGTH - 1) / MAX_ROW_LENGTH
      row_length = (stride + block_length - 1) / block_length
      rows = rows.collect { |x| x.each_slice(row_length).to_a }.flatten(1)
    end
    rows = rows.collect { |x| x.collect { |y| "%04x" % y }.join(" ") }
    coder['data'] = rows
  else
    coder['data'] = []
  end
end

#init_with(coder) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/RGSS.rb', line 55

def init_with(coder)
  @dim = coder['dim']
  @x = coder['x']
  @y = coder['y']
  @z = coder['z']
  @data = coder['data'].collect { |x| x.split(" ").collect { |y| y.hex } }.flatten
  items = @x * @y * @z
  raise "Size mismatch loading Table from YAML" unless items == @data.length
end