-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursive-division
executable file
·57 lines (42 loc) · 1.92 KB
/
recursive-division
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby
require 'json'
def divide(walls, x_start, z_start, x_end, z_end)
width, height = (x_end - x_start), (z_end - z_start)
return if width < 2 || height < 2
coin_flip = rand(2)
if width < height || (width == height && coin_flip == 0)
# horizontal wall
wall_z = z_start + 1 + rand(height - 1)
gap_x = x_start + 1 + rand(width - 1)
walls << {:begin => {:x => x_start, :z => wall_z}, :end => {:x => gap_x, :z => wall_z}}
walls << {:begin => {:x => gap_x + 1, :z => wall_z}, :end => {:x => x_end, :z => wall_z}}
divide(walls, x_start, z_start, x_end, wall_z)
divide(walls, x_start, wall_z, x_end, z_end)
elsif width > height || (width == height && coin_flip == 1)
# vertical wall
wall_x = x_start + 1 + rand(width - 1)
gap_z = z_start + 1 + rand(height - 1)
walls << {:begin => {:x => wall_x, :z => z_start}, :end => {:x => wall_x, :z => gap_z}}
walls << {:begin => {:x => wall_x, :z => gap_z + 1}, :end => {:x => wall_x, :z => z_end}}
divide(walls, x_start, z_start, wall_x, z_end)
divide(walls, wall_x, z_start, x_end, z_end)
end
end
X_SIZE, Z_SIZE = 13, 9
walls = [{:begin => {:x => 0, :z => 0}, :end => {:x => X_SIZE, :z => 0}},
{:begin => {:x => X_SIZE, :z => 0}, :end => {:x => X_SIZE, :z => Z_SIZE}},
{:begin => {:x => X_SIZE, :z => Z_SIZE}, :end => {:x => 0, :z => Z_SIZE}},
{:begin => {:x => 0, :z => Z_SIZE}, :end => {:x => 0, :z => 0}}]
divide(walls, 0, 0, X_SIZE, Z_SIZE)
# scale the walls up to actual size
walls = walls.map do |w|
{:begin => {:x => w[:begin][:x] * 4, :z => w[:begin][:z] * 4},
:end => {:x => w[:end][:x] * 4, :z => w[:end][:z] * 4}}
end
level = {:name => 'levelx',
:dimensions => {:x => X_SIZE * 4, :z => Z_SIZE * 4},
:camera => {:x => 0, :y => 35, :z => 0.5},
:target => {:x => 1, :z => 1.5},
:ball => {:x => 24, :z => 16},
:walls => walls}
puts level.to_json