Class: Worldgen::HeightMap

Inherits:
Object
  • Object
show all
Defined in:
lib/worldgen/heightmap.rb,
ext/worldgen/heightmap.c

Overview

A square heightmap

Defined Under Namespace

Classes: HeightmapData

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ HeightMap

Create a new square heightmap. Arguments:

  • size - the width/height of the map



13
14
15
16
# File 'lib/worldgen/heightmap.rb', line 13

def initialize size
  @size = size
  initialize_native(size)
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/worldgen/heightmap.rb', line 8

def size
  @size
end

Instance Method Details

#[]=(vx, vy, vheight) ⇒ Object

Set the value of our heightmap at a specified point.



97
98
99
100
101
102
103
104
# File 'ext/worldgen/heightmap.c', line 97

VALUE get_at(VALUE self, VALUE vx, VALUE vy) {
  int x = FIX2INT(vx);
  int y = FIX2INT(vy);
  heightmap map = get_heights(self);
  int size = map.size;

  return ARR(map.heights, x, y);
}

#each_heightObject

Iterate over all the points in the heightmap. Example: “‘ heightmap.each_height do |x, y, height|

puts "Height at (#{x}, #{y}) is #{height}"

end



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/worldgen/heightmap.c', line 76

VALUE each_height(VALUE self) {
  heightmap map = get_heights(self);
  heightmap_points ptr = map.heights;
  VALUE args = rb_ary_new2(3);
  int size = get_size(self);
  int x, y;

  for (x = 0; x < size; x++) {
    rb_ary_store(args, 0, INT2FIX(x));
    for (y = 0; y < size; y++) {
      rb_ary_store(args, 1, INT2FIX(y));
      rb_ary_store(args, 2, DBL2NUM(*ptr++));
      rb_yield(args);
    }
  }
  return self;
}

#num_pointsObject

Get the number of points within the heightmap. Right now this is a very simple calculation of size * size.



64
65
66
# File 'ext/worldgen/heightmap.c', line 64

VALUE num_points_wrapped(VALUE self) {
  return INT2FIX(num_points(FIX2INT(rb_iv_get(self, "@size"))));
}