Class: Noise

Inherits:
Map
  • Object
show all
Defined in:
lib/delve/generator/noise.rb

Constant Summary collapse

@@grains =
{
  :fine => 0.03,
  :coarse => 0.1
}

Instance Method Summary collapse

Methods inherited from Map

#fill, #height, #width

Constructor Details

#initialize(width, height, grain) ⇒ Noise

Returns a new instance of Noise.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/delve/generator/noise.rb', line 11

def initialize width, height, grain
  raise 'Cannot initialize noise generator when width is less than zero' if width < 0
  raise 'Cannot initialize noise generator when height is less than zero' if height < 0
  raise 'Cannot initialize noise generator when grain is not defined' unless grain
  raise 'Cannot initialize noise generator with unknown grain' unless @@grains.include?(grain)

  super width, height

  @grain = @@grains[grain]
  @inverse = 1/@grain
end

Instance Method Details

#generateObject



23
24
25
26
27
28
29
30
31
# File 'lib/delve/generator/noise.rb', line 23

def generate
  noise = Perlin::Noise.new 2

  0.step((@width-1) * @grain, @grain) do |x|
    0.step((@height-1) * @grain, @grain).each do |y|
      yield(x*@inverse, y*@inverse, noise[x, y])
    end
  end
end