Created
February 3, 2012 01:13
-
-
Save ruprict/1726891 to your computer and use it in GitHub Desktop.
ROFLBALT BuildingGenerator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment