public
Created

ROFLBALT BuildingGenerator

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
class BuildingGenerator
def initialize world, background
@world = world
@background = background
end
def destroy_if_necessary
while @world.buildings.any? && @world.buildings.first.x < -100
@world.buildings.shift
end
end
def generate_if_necessary
while (b = @world.buildings.last).x < @world.horizon
@world.buildings << build(
b.right_x + minimium_gap + rand(24),
next_y(b),
rand(40) + 40
)
end
end
def minimium_gap; 16 end
def maximum_height_delta; 10 end
def minimum_height_clearance; 12; end
def next_y previous_building
p = previous_building
delta = 0
while delta.abs <= 1
delta = maximum_height_delta * -1 + rand(2 * maximum_height_delta + 1)
end
[25, [previous_building.y - delta, minimum_height_clearance].max].min
end
def build x, y, width
Building.new x, y, width, @background
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.