summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/engine/builder.js
blob: 00bb1c76e62b494badb77d95f0cfec45e3983aa8 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
var builder = new function(){
	var base = this
	
	var els = []

	base.init = function(){
		base.bind()
	}

	base.bind = function(){
		app.on("clip", rebuild)
	}

	function rebuild(){
		if (window.scene) {
			clear()
			build()
			bind()
		}
	}
	function build (){
		clipper.rooms = sort_rooms_by_id(clipper.rooms)

		clipper.regions.forEach(function(room){
			build_walls(room).forEach(function(el){
				els.push(el)
				scene.add(el)
			})
		})

		clipper.rooms = sort_rooms_by_height(clipper.rooms)
		clipper.rooms.forEach(function(room){
			build_floors(room).forEach(function(el){
				els.push(el)
				scene.add(el)
			})
		})

		clipper.rooms = sort_rooms_by_id(clipper.rooms)
	}
	
	function bind (){
		clipper.rooms.forEach(function(room){
			var walls = room.group_mx_walls()
			walls.forEach(function(wall){
				wall.bind()
				wall.randomize_colors()
			})
		})
	}
	
	function clear (){
		els.forEach(function(el){
			scene.remove(el)
			el.destroy && el.destroy()
		})
		els = []
	}

	function build_walls (region){
		var room = clipper.rooms[ region.id ]
		
		var list = [], el = null
		
		var width = region.x.length()
		var depth = region.y.length()
		var height = room.height

		if (region.sides & FRONT) {
			el = make_wall('.front')
			el.width = width
			el.height = height
			el.rotationY = PI
			el.x = region.x.a + width/2
			el.y = height/2
			el.z = region.y.a
			el.rect = region
			el.side = FRONT
			room.mx_walls.push(el)
			list.push(el)
		}
		if (region.sides & BACK) {
			var el = make_wall('.back')
			el.width = width
			el.height = height
			el.rotationY = 0
			el.x = region.x.b - width/2
			el.y = height/2
			el.z = region.y.b
			el.rect = region
			el.side = BACK
			room.mx_walls.push(el)
			list.push(el)
		}
		if (region.sides & LEFT) {
			el = make_wall('.left')
			el.rotationY = HALF_PI
			el.height = height
			el.width = depth
			el.x = region.x.a
			el.y = height/2
			el.z = region.y.a + depth/2
			el.rect = region
			el.side = LEFT
			room.mx_walls.push(el)
			list.push(el)
		}
		if (region.sides & RIGHT) {
			el = make_wall('.right')
			el.rotationY = -HALF_PI
			el.height = height
			el.width = depth
			el.x = region.x.b
			el.y = height/2
			el.z = region.y.b - depth/2
			el.rect = region
			el.side = RIGHT
			room.mx_walls.push(el)
			list.push(el)
		}

		return list
	}

	function build_floors(room){
		var list = [], el = null
		
		var constructed = room.intersects.filter(function(room){ return room.constructed })
		sort_rooms_by_height(constructed)
		
		if (constructed.length > 0) {
			// render the regions that don't intersect with anything we've already rendered
			// if the height is different, calculate the overlapping sides and render half-walls
			room.regions.forEach(function(region){
				var intersected = false
				for (var i = 0; i < constructed.length; i++) {
					if (constructed[i].rect.contains(region)) {
						intersected = true
						// r.sides = 0xf
						// half_sides
					}
					else if (constructed[i].rect.intersects(region)) {
						intersected = true
						if (room.height < constructed[i].height) {
							list = list.concat( make_ceiling_walls( room, constructed[i], region ) )
						}
					}
				}
				if (! intersected) {
					el = make_floor(room, region) 
					list.push( el )
					room.mx_floor.push(el)

					el = make_ceiling(room, region) 
					list.push( el )
					room.mx_ceiling.push(el)
				}
			})

		}
		else {
			// render floor and ceiling for the entire rectangle
			el = make_floor(room, room.rect) 
			list.push( el )
			room.mx_floor.push(el)

			el = make_ceiling(room, room.rect) 
			list.push( el )
			room.mx_ceiling.push(el)
		}
		
		room.constructed = true
		return list
	}
	
	function make_ceiling_walls( lo, hi, region ){
		var list = []
		
		var width = region.x.length()
		var depth = region.y.length()
		var height = hi.height - lo.height
		
		if (! (region.half_sides & LEFT) && region.x.a == hi.rect.x.a) {
			el = make_wall('.left')
			el.rotationY = HALF_PI
			el.height = height
			el.width = depth
			el.x = region.x.a
			el.y = lo.height + height/2
			el.z = region.y.a + depth/2
			el.rect = region
			list.push(el)
			hi.walls.push(el)
			region.half_sides |= LEFT
		}

		if (! (region.half_sides & RIGHT) && region.x.b == hi.rect.x.b) {
			el = make_wall('.right')
			el.rotationY = -HALF_PI
			el.height = height
			el.width = depth
			el.x = region.x.b
			el.y = lo.height + height/2
			el.z = region.y.b - depth/2
			el.rect = region
			list.push(el)
			hi.walls.push(el)
			region.half_sides |= RIGHT
		}

		if (! (region.half_sides & FRONT) && region.y.a == hi.rect.y.a) {
			el = make_wall('.front')
			el.width = width
			el.height = height
			el.rotationY = PI
			el.x = region.x.a + width/2
			el.y = lo.height + height/2
			el.z = region.y.a
			el.rect = region
			list.push(el)
			hi.walls.push(el)
			region.half_sides |= FRONT
		}

		if (! (region.half_sides & BACK) && region.y.b == hi.rect.y.b) {
			el = make_wall('.back')
			el.width = width
			el.height = height
			el.rotationY = 0
			el.x = region.x.b - width/2
			el.y = lo.height + height/2
			el.z = region.y.b
			el.rect = region
			list.push(el)
			hi.walls.push(el)
			region.half_sides |= BACK
		}
		return list
	}

	function make_floor(room, region){
		var width = region.x.length()
		var depth = region.y.length()
		
		var el = make_wall('.floor')
		el.height = depth
		el.width = width
		el.x = region.x.a + width/2
		el.y = 0
		el.z = region.y.a + depth/2
		el.rotationX = PI/2
		el.rect = region
		el.side = FLOOR
		return el
	}
	function make_ceiling(room, region){
		var width = region.x.length()
		var depth = region.y.length()
		var height = room.height

		var el = make_wall('.ceiling')
		el.height = depth
		el.width = width
		el.x = region.x.a + width/2
		el.y = height
		el.z = region.y.a + depth/2
		el.rotationX = -PI/2
		el.rect = region
		el.side = CEILING
		return el
	}

	function make_wall(room, klass){
		var el = new MX.Object3D(".face" + (klass || ""))
		el.width = el.height = el.scaleX = el.scaleY = el.scaleZ = 1
		el.z = el.y = el.x = 0
		el.side = 0
		el.type = "Face"
		el.el.style.opacity = 1.0
		el.side = 0
		el.rect = null
		el.destroy = function(){
			this.el = this.rect = null
		}
		return el
	}

}