summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/room.js
blob: 6c0e1bb02c6125b20d3265ecab99771c37ef50ca (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
(function(){
	var vec2, Rect, sort
	if ('window' in this) {
		vec2 = window.vec2
		Rect = window.Rect
		sort = window.sort
	}
	else {
		vec2 = require('./vec2')
		Rect = require('./rect')
		UidGenerator = require('../util/uid')
		Rooms = { uid: new UidGenerator({}) }
		sort = require('../util/sort')
		FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
		TOP = CEILING, BOTTOM = FLOOR
		FRONT_BACK = FRONT | BACK
		LEFT_RIGHT = LEFT | RIGHT
		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
		}
	}

	var Room = function(opt){
		this.id = opt.id || Rooms.uid("room_")
		this.rect = opt.rect
		this.regions = []
		this.walls = []
		this.floor = []
		this.ceiling = []

		this.height = opt.height || 200
		this.focused = false
	}
	
	Room.prototype.clone = function(){
	  return new Room ({
	    id: this.id,
	    rect: this.rect.clone(),
	    height: this.height,
	  })
	}
	
	Room.prototype.toString = function(){
		return this.rect.toString()
	}
	
	Room.prototype.serialize = function(){
		return {
			id: this.id,
			rect: this.rect.serialize(),
			height: ~~this.height,
		}
	}
	
	Room.prototype.reset = function(){
		var copy = this.rect.clone()
		copy.id = this.id
		copy.sides = FRONT | BACK | LEFT | RIGHT
		this.regions = [ copy ]

		this.intersects = []
		this.constructed = false
		
		this.walls = []
		
		this.mx_walls = []
		this.mx_floor = []
		this.mx_ceiling = []
	}
	
	Room.prototype.bind = function(){
		var base = this
		base.mx_walls.forEach(function(wall){
			$(wall.el).bind({
				mouseover: function(){
				},
				mousemove: function(e){
					var color = choice(window.palettes.colors)
					base.mx_walls.forEach(function(wall){
						$(wall.el).css("background-color", color)
					})
				},
				mousedown: function(){
				}
			})
		})
	}
	
	Room.prototype.group_mx_walls = function(){
		var base = this
		var side_groups = {}, walls = []

		// group the walls by side
		base.mx_walls.forEach(function(wall){
		
			// ignore half-walls for now
			var side = wall.side || wall.half_side

			if (side_groups[side]) {
				side_groups[side].push(wall)
			}
			else {
				side_groups[side] = [wall]
			}
		})
		
		// sort the subgroups, and then combine mx objects under wall objects
		pairs(side_groups).forEach(function(pair){
			var side = pair[0], els = pair[1]
			
			if (side & LEFT_RIGHT) {
 				els.sort(sort.compare_rect_x)
			}
			else if (side & FRONT_BACK) {
 				els.sort(sort.compare_rect_y)
			}
			
			// wall holds state for the last wall we created/saw..
			// right now we keep walls subdivided where there are half-walls to simplify
			// calculations involving placing media on walls, calculating wall boundaries, etc..
			// one consequence of this is we can't place things on the walls above "doorways".
			// in the future, we should have more robust placement algorithm, which takes
			// this into account, and only use one wall "object" per plane
			var wall

			els.forEach(function(el){
				if (el.half_side) {
					wall = new_wall(el)
					walls.push(wall)
					wall = null
				}
				else if (! wall) {
					wall = new_wall(el)
					walls.push(wall)
				}
				else if (side & FRONT_BACK && wall.rect.x.b == el.rect.x.a) {
					wall.rect.x.b = el.rect.x.b
					wall.mx.push(el)
				}
				else if (side & LEFT_RIGHT && wall.rect.y.b == el.rect.y.a) {
					wall.rect.y.b = el.rect.y.b
					wall.mx.push(el)
				}
				else {
					wall = new_wall(el)
					walls.push(wall)
				}
			})
		})

		function new_wall (el) {
			return new Wall ({
				id: base.id + "_" + (el.side | el.half_side) + "_" + walls.length,
				room: base.id,
				side: el.side | el.half_side,
				half_side: el.half_side,
				rect: el.rect.clone(),
				el: el,
			})
		}
		
		return walls
	}
	
	Room.prototype.clipTo = function(r){
		// for each of this rect's regions split the region if necessary
		var regions = this.regions
		var splits
		for (var i = 0, len = regions.length; i < len; i++) {
			if (regions[i] && regions[i].intersects(r)) {
				splits = regions[i].split(r)
				regions = regions.concat(splits)
				regions[i] = null
			}
		}
		this.regions = regions
	}
	
	Room.prototype.collides = function(x,y){
		var collision = 0, wall_collision, contains_x, contains_y
		this.regions.forEach(function(r){
			if (! r.sides) return
			
			wall_collision = 0

			if ((r.sides & FRONT) && y < r.y.a) {
				wall_collision |= FRONT
			}
			if ((r.sides & BACK) && r.y.b < y) {
				wall_collision |= BACK
			}
			if ((r.sides & LEFT) && x < r.x.a) {
				wall_collision |= LEFT
			}
			if ((r.sides & RIGHT) && r.x.b < x) {
				wall_collision |= RIGHT
			}
			if (! wall_collision) return

			contains_y = r.y.contains(y)
			contains_x = r.x.contains(x)
			
			if (contains_x) {
				collision |= wall_collision & FRONT_BACK
			}
			else if (contains_y) {
				collision |= wall_collision & LEFT_RIGHT
			}
			else if (bitcount(wall_collision) > 1) {
 				collision |= wall_collision
			}
		})
		return collision
	}

	Room.prototype.collidesDisc = function(x,y,radius){
		var collision = 0, wall_collision, contains_x, contains_y
		this.regions.forEach(function(r){
			if (! r.sides) return
			
			wall_collision = 0

			if ((r.sides & FRONT) && y-radius < r.y.a) {
				wall_collision |= FRONT
			}
			if ((r.sides & BACK) && r.y.b < y+radius) {
				wall_collision |= BACK
			}
			if ((r.sides & LEFT) && x-radius < r.x.a) {
				wall_collision |= LEFT
			}
			if ((r.sides & RIGHT) && r.x.b < x+radius) {
				wall_collision |= RIGHT
			}
			if (! wall_collision) return

			contains_x = r.x.contains(x, radius)
			contains_y = r.y.contains(y, radius)

			if (contains_x) {
				collision |= wall_collision & FRONT_BACK
			}
			if (contains_y) {
				collision |= wall_collision & LEFT_RIGHT
			}
//    if (bitcount(wall_collision) > 1) {
//  				collision |= wall_collision
// 			}
		})
		return collision
	}

	if ('window' in this) {
		window.Room = Room
	}
	else {
		module.exports = Room
	}

})()