summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/rooms/grouper.js
blob: 532146d39dc5bfccbe45709cd8c4286011e12706 (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
(function(){

	var Rooms, UidGenerator, Wall, sort
	if ('window' in this) {
		Rooms = window.Rooms
		UidGenerator = window.UidGenerator
		Wall = window.Wall
		sort = window.sort
	}
	else {
		Rooms = require('./_rooms')
		UidGenerator = require('../../util/uid')
		Wall = require('../../models/wall')
		sort = require('../../util/sort')
		FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
		PI = Math.PI
		HALF_PI = PI/2
		TOP = CEILING, BOTTOM = FLOOR
		function sidesToString(sides){
			var s = ""
			if (sides & FRONT) s += "front "
			if (sides & BACK)  s += "back "
			if (sides & LEFT)  s += "left "
			if (sides & RIGHT) s += "right "
			if (sides & TOP)  s += "top "
			if (sides & BOTTOM) s += "bottom "
			return s
		}
	}

	Rooms.grouper = new function(){

		var base = this

		base.list = {}
		
		base.uid = new UidGenerator(base.list)
		
		base.build = function (){
			var walls = []
			var collections = base.collect()
			base.cull(collections)
			base.group(walls, collections, FRONT)
			base.group(walls, collections, BACK)
			base.group(walls, collections, LEFT)
			base.group(walls, collections, RIGHT)
			Rooms.walls = walls
			base.bind()
		}
		base.collect = function(){			
			var collections = {}
			collections[FRONT] = []
			collections[BACK] = []
			collections[LEFT] = []
			collections[RIGHT] = []
			
			Rooms.forEach(function(room){
				room.mx_walls.forEach(function(mx){
					var side = mx.side || mx.half_side
					collections[side].push(mx)
				})
			})
			
			base.cull(collections)
			
			return collections
		}
		base.cull = function(collections){
			[FRONT, BACK, LEFT, RIGHT].forEach(function(side){
				var collection = collections[side]
				var useX = side & FRONT_BACK
				var useA = side & (FRONT | RIGHT)
				var last_mx
				
				collection.sort( useX ? sort.compare_zx : sort.compare_xz )
				collection.forEach(function(mx){
					if (last_mx && last_mx.rect.eq(mx.rect)) {
//							console.log( mx.y - mx.height/2 )
//							console.log( last_mx.y - last_mx.height/2 )
//  							console.log(last_mx.y, mx.y)
// 							console.log(last_mx.height, mx.height)
// 							console.log(Rooms.list[ last_mx.rect.id ].height, Rooms.list[ mx.rect.id ].height)
//							console.log("___")
// 							last_mx.height += mx.height/2
// 							last_mx.y += mx.height/4
						if (last_mx.rect.id == mx.rect.id) {
							last_mx.height += mx.height/2
							last_mx.y += mx.height/4
						}
						last_mx.side = side

						mx.culled = true
						scene.remove(mx)
						return
					}
					last_mx = mx
				})
			})
			return collections
		}
		base.group = function(walls, collections, side){
			var collection = collections[side]
			var wall
			var useX = side & FRONT_BACK
			var useA = side & (FRONT | RIGHT)

			// collection.sort( useX ? sort.compare_zx : sort.compare_xz )
			
			collection.forEach(function(mx){
				if (mx.culled) return
				var coplanar = wall && wall.edge == mx.rect[useX ? 'y': 'x'][useA ? 'a': 'b']
				
				if (wall && coplanar) {
					if (useX && wall.vec.b == mx.rect.x.a) {
						wall.vec.b = mx.rect.x.b
						wall.mx.push(mx)
						return
					}
					else if (! useX && wall.vec.b == mx.rect.y.a) {
						wall.vec.b = mx.rect.y.b
						wall.mx.push(mx)
						return
					}
				}
				wall = new Wall ({
					id: base.uid(),
					side: side,
					vec: mx.rect[ useX ? 'x' : 'y' ].clone(),
					edge: mx.rect[useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ],
					el: mx,
				})
				walls.push(wall)
			})

			return walls
		}
		
		base.bind = function(){
			Rooms.walls.forEach(function(wall){
				wall.bind()
				wall.randomize_colors()
			})
		}

	}

})()