Class: Table

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y = 0, z = 0) ⇒ Table

Returns a new instance of Table.



7
8
9
10
# File 'lib/rgss3/table.rb', line 7

def initialize(x, y = 0, z = 0)
  init_attr(x, y, z)
  @data = Array.new(@xsize * @ysize * @zsize, 0)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/rgss3/table.rb', line 4

def data
  @data
end

#xsizeObject (readonly)

Returns the value of attribute xsize.



5
6
7
# File 'lib/rgss3/table.rb', line 5

def xsize
  @xsize
end

#ysizeObject (readonly)

Returns the value of attribute ysize.



5
6
7
# File 'lib/rgss3/table.rb', line 5

def ysize
  @ysize
end

#zsizeObject (readonly)

Returns the value of attribute zsize.



5
6
7
# File 'lib/rgss3/table.rb', line 5

def zsize
  @zsize
end

Class Method Details

._load(s) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rgss3/table.rb', line 54

def self._load(s)
  size, nx, ny, nz, items = *s[0, 20].unpack('LLLLL')
  t = Table.new(*[nx, ny, nz].first(size))
  d = s[20, items * 2].unpack("S#{items}")
  tail_offset = 20 + items * 2
  if s.length > tail_offset
    a = Marshal.load(s[tail_offset..-1])
    d.collect! do |i|
      if i & 0x8000 == 0x8000
        a[i&~0x8000]
      else
        i
      end
    end
  end
  t.data = d
  t
end

Instance Method Details

#[](x, y = 0, z = 0) ⇒ Object



12
13
14
# File 'lib/rgss3/table.rb', line 12

def [](x, y = 0, z = 0)
  @data[x + y * @xsize + z * @xsize * @ysize]
end

#[]=(x, y = 0, z = 0, v) ⇒ Object



30
31
32
# File 'lib/rgss3/table.rb', line 30

def []=(x, y = 0, z = 0, v)
  @data[x + y * @xsize + z * @xsize * @ysize] = v
end

#_dump(d = 0) ⇒ Object



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

def _dump(d = 0)
  s = [@dim, @xsize, @ysize, @zsize, @xsize * @ysize * @zsize].pack('LLLLL')
  a = []
  ta = []
  @data.each do |d|
    if d.is_a?(Fixnum) && (d < 32768 && d >= 0)
      s << [d].pack("S")
    else
      s << [ta].pack("S#{ta.size}")
      ni = a.size
      a << d
      s << [0x8000|ni].pack("S")
    end
  end
  if a.size > 0
    s << Marshal.dump(a)
  end
  s
end

#resize(x, y = 0, z = 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rgss3/table.rb', line 16

def resize(x, y = 0, z = 0)
  new_table = Table.new(x, y, z)
  x.times do |i|
    [y, 1].max.times do |j|
      [z, 1].max.times do |k|
        new_table[i, j, k] = self[i, j, k] || 0
      end
    end
  end
  @data = new_table.data
  init_attr(x, y, z)
end