Class: Budgie::StoreMap

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

Instance Method Summary collapse

Constructor Details

#initialize(filename, palette) ⇒ StoreMap

Returns a new instance of StoreMap.



5
6
7
8
# File 'lib/budgie/store_map.rb', line 5

def initialize(filename, palette)
  @filename = filename
  @palette = palette
end

Instance Method Details

#createObject



10
11
12
13
14
# File 'lib/budgie/store_map.rb', line 10

def create
  map = Map.new
  map[0, 0, 0] = @palette.white
  map
end

#loadObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/budgie/store_map.rb', line 16

def load
  return create unless file_exists?

  png = load_png
  size = png.width
  max = size / 2

  map = Map.new

  size.times do |x|
  size.times do |y|
  size.times do |z|
    png_x = x
    png_y = size * (z + 1) - (y + 1)
    color = png[png_x, png_y] rescue nil
    color = color_to_index color
    map[x - max, y - max, z - max] = color if color
  end
  end
  end

  return map
end

#save(map) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/budgie/store_map.rb', line 40

def save(map)
  max = map.max
  size = 2 * max + 1 # [-max, max] including boundaries

  width = size
  height = size * size
  png = new_png width, height

  map.each do |x, y, z, index|
    x += max
    y += max
    z += max
    png_x = x
    png_y = width * (z + 1) - (y + 1)
    png[png_x, png_y] = index_to_color index
  end

  png.save @filename
end