Class: Moon::Table

Inherits:
Object show all
Includes:
NData, Tabular, Serializable, Serializable::Properties
Defined in:
lib/moon/packages/data_bags/table.rb,
lib/moon/packages/data_painter/ext/table.rb

Overview

A 2 dimensional array used normally for storing Tilemap data or any 2d grid based data such hmaps, image data, passage data.

Defined Under Namespace

Classes: Iterator

Properties collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tabular

#contains?, #map_with_xy, #rect, #set_by_dict, #sizes

Methods included from NData

#write_data

Constructor Details

#initialize(xsize, ysize, options = {}) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Parameters:

Options Hash (options):

  • :default (Integer) — default: 0

    default value also used as the :fill

  • :fill (Integer) — default: :default

    value used to fill the data

  • :unitialized (Boolean) — default: false

    used in place of .alloc (API)

  • :data (Array<Integer>)

    data to use for table

Yields:

  • (_self)

Yield Parameters:

  • _self (Moon::Table)

    the object that the method was called on


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/moon/packages/data_bags/table.rb', line 46

def initialize(xsize, ysize, options = {})
  return if options[:uninitialized]
  @xsize = xsize.to_i
  @ysize = ysize.to_i
  @default = options.fetch(:default, 0)
  if options.key?(:data)
    @data = options.fetch(:data).dup
  else
    create_data(options.fetch(:fill, @default))
  end
  yield self if block_given?
end

Instance Attribute Details

#dataArray<Integer>

Returns:


33
# File 'lib/moon/packages/data_bags/table.rb', line 33

property_accessor :data

#defaultInteger

Returns:


36
# File 'lib/moon/packages/data_bags/table.rb', line 36

property_accessor :default

#sizeInteger

Returns:


29
# File 'lib/moon/packages/data_bags/table.rb', line 29

property_accessor :size

#xsizeInteger

Returns:


23
# File 'lib/moon/packages/data_bags/table.rb', line 23

property_accessor :xsize

#ysizeInteger

Returns:


26
# File 'lib/moon/packages/data_bags/table.rb', line 26

property_accessor :ysize

Class Method Details

.load(data, depth = 0) ⇒ Moon::Table

Serialization

Returns:


175
176
177
178
179
# File 'lib/moon/packages/data_bags/table.rb', line 175

def self.load(data, depth = 0)
  instance = new nil, nil, uninitialized: true
  instance.import data, depth
  instance
end

Instance Method Details

#[](x, y) ⇒ Integer

Parameters:

Returns:


122
123
124
125
126
127
# File 'lib/moon/packages/data_bags/table.rb', line 122

def [](x, y)
  x = x.to_i
  y = y.to_i
  return @default unless contains?(x, y)
  @data[calc_index(x, y)]
end

#[]=(x, y, n) ⇒ Object

Parameters:


141
142
143
144
145
146
147
# File 'lib/moon/packages/data_bags/table.rb', line 141

def []=(x, y, n)
  x = x.to_i
  y = y.to_i
  n = n.to_i
  return unless contains?(x, y)
  @data[calc_index(x, y)] = n
end

#change_data(data_p, xsize, ysize) ⇒ Object

Parameters:


85
86
87
88
89
# File 'lib/moon/packages/data_bags/table.rb', line 85

def change_data(data_p, xsize, ysize)
  @xsize = xsize
  @ysize = ysize
  @data  = data_p
end

#get_by_index(index) ⇒ Integer

Retrieve a value from the internal data at (index)

Parameters:

Returns:

  • (Integer)

    value Value at index


133
134
135
136
# File 'lib/moon/packages/data_bags/table.rb', line 133

def get_by_index(index)
  return @default if index < 0 || index >= size
  @data[index]
end

#initialize_copy(org) ⇒ Object

Parameters:


71
72
73
74
75
# File 'lib/moon/packages/data_bags/table.rb', line 71

def initialize_copy(org)
  super org
  create_data
  map_with_xy { |_, x, y| org.data[x + y * @xsize] }
end

#iterInterator

Initializes and returns an Iterator

Returns:

  • (Interator)

162
163
164
# File 'lib/moon/packages/data_bags/table.rb', line 162

def iter
  @iter ||= Iterator.new(self)
end

#resize(nxsize, nysize) ⇒ self

Resizes the dataset

Parameters:

Returns:

  • (self)

96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/moon/packages/data_bags/table.rb', line 96

def resize(nxsize, nysize)
  oxsize, oysize = xsize, ysize
  @xsize, @ysize = nxsize, nysize
  old_data = @data
  create_data
  map_with_xy do |n, x, y|
    if x < oxsize && y < oysize
      old_data[x + y * oxsize]
    else
      @default
    end
  end
end

#samplerSampler2

Initializes and returns a sampler

Returns:


8
9
10
# File 'lib/moon/packages/data_painter/ext/table.rb', line 8

def sampler
  @sampler ||= Sampler2.new(self)
end

#set_by_index(index, value) ⇒ Object

Because sometimes its too damn troublesome to convert an index to the proper coords

Parameters:


154
155
156
157
# File 'lib/moon/packages/data_bags/table.rb', line 154

def set_by_index(index, value)
  return if index < 0 || index >= size
  @data[index] = value
end

#to_sString

Returns:


167
168
169
170
171
# File 'lib/moon/packages/data_bags/table.rb', line 167

def to_s
  @ysize.times.map do |y|
    @data[y * @xsize, @xsize].join(', ')
  end.join("\n")
end