@data = Array.new(width, Array.new(height, default_value))
@data[x][y] = terrain_id
@data[0][1] = 0 # [
[nil, 1, nil], # XY: 0, 1
[nil, 1, nil], # XY: 1, 1
[nil, 1, nil] # XY: 2, 1
]
class Data_Tilemap
attr_reader(:data)
attr_accessor(:default_value)
def initialize(width, height, default_value = 0)
@width = width
@height = height
@default_value = default_value
#@data = Array.new(width, Array.new(height, default_value))
@data = []
width.times { @data.push(Array.new) }
end
def [](x, y)
if x.between?(0, @width - 1) and y.between?(0, @height - 1)
return @data[x][y]
else
return @default_value
end
end
def []=(x, y, value)
@data[x][y] = value if x.between?(0, @width - 1) and y.between?(0, @height - 1)
print(
@data[0].object_id, " ", #=> 21483444
@data[1].object_id, " ", #=> 21483444
@data[2].object_id) #=> 21483444
puts
end
def resize(width, height)
new_data = Array.new(width, Array.new(height, @default_value))
for x in 0...@width
for y in 0...@height
if x.between?(0, width - 1) and y.between?(0, height - 1)
new_data[x][y] = @data[x][y]
end
end
end
@data = new_data
@width = width
@height = height
end
end
file = File.open(filename, "wb")
Marshal.dump(data, file)
file.close
file = File.open(@filename, "rb")
@data = Marshal.load(file)
file.close
@data = Array.new(width, Array.new(height, default_value))
Array#initialize
, it will be evaluated once (like all method arguments) and used as the initial value in the array. If you pass a block, however, that block will be evaluated once to generate the initial value for each cell:@data = Array.new(width) { Array.new(height, default_value) }
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill