summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/wall.js
blob: 8723c3c911fd43a6b17d277c25c43504231b3bdd (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
291
(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')
	}
	
	var Wall = function(opt){
		this.id = opt.id
		this.vec = opt.vec
		this.edge = opt.edge
		this.side = opt.side
		this.surface = opt.surface
		this.mx = opt.mx
	}
	
	Wall.prototype.toString = function(){
		return this.vec.toString()
	}

	Wall.prototype.reset = function(){
	}
	
	Wall.prototype.destroy = function(){
		this.mx.forEach(function(mx){
			mx.destroy && mx.destroy()
		})
		this.room = this.vec = this.mx = null
	}
	
	Wall.prototype.bind = function(){
		var base = this
		base.$walls = $( this.mx.map(function(mx){ return mx.el }) )
		
		this.mx.forEach(function(mx, index){
			$(mx.el).bind({
				mouseover: function(){
				},
				mouseenter: function(e){
					Scenery.mouse.mouseenter(e, {
						wall: base,
						index: index,
					})
				},
				mousemove: function(e){
				},
				mousedown: function(e){
					if (Scenery.nextMedia) {
            var scenery = Scenery.addNextToWall({
            	wall: base,
            	index: index
            })
            
            // scenery was not placed
            if (! scenery) {	
            	e.stopPropagation()
            	return
            }
					
  					UndoStack.push({
  					  type: 'create-scenery',
  					  undo: { id: scenery.id },
              redo: scenery.serialize(),
  					})

  					// TODO: watch individual scenery object here
  					Minotaur.watch( app.router.editorView.settings )
					}
					else if (Scenery.nextWallpaper) {
						base.wallpaper()
					}
					else {
						app.controller.hideExtras()
					}
				}
			})
		})
		this.outline()
	}


  // wall
  // side: corresponds to the orientation of this wall
  // vec: equivalent to the bounds of the Surface
  // edge: the coordinate of the normal of this surface
  // 			 var useX = side & LEFT_RIGHT
  //			 var useA = side & (FRONT | LEFT)
  //       edge = mx.rect[useX ? 'x': 'y'][ useA ? 'a': 'b']
  // surface: an ordered set of contiguous wall regions, corresponding to mx objects

	Wall.prototype.positionToMx = function(position, dimension) {
		var x, z
		switch (this.side) {
			case FRONT:
				x = position.a + dimension.a / 2
				z = this.edge + painting_distance_from_wall
				break
			case BACK:
				x = position.a + dimension.a / 2
				z = this.edge - painting_distance_from_wall
				break
			case LEFT:
				x = this.edge + painting_distance_from_wall
				z = position.a + dimension.a / 2
				break
			case RIGHT:
				x = this.edge - painting_distance_from_wall
 				z = position.a + dimension.a / 2
				break
		}
		return {
		  x: x,
		  y: position.b + dimension.b / 2,
		  z: z,
		  rotationY: wall_rotation[ this.side ],
		}
	}
	Wall.prototype.mxToPosition = function(mx, dimension) {
	  var position = new vec2(0,0)
	  switch (this.side) {
	    case FRONT:
	    case BACK:
	      position.a = mx.x
	      position.b = mx.y
	      break
	    case LEFT:
	    case RIGHT:
	      position.a = mx.z
	      position.b = mx.y
	      break
	  }
	  if (dimension) {
	  	position.a -= dimension.a / 2
	  	position.b -= dimension.b / 2
	  }
// 	  if (mx.width)  { position.a -= mx.width  / 2 }
// 	  if (mx.height) { position.b -= mx.height / 2 }
	  return position
	}
	
	Wall.prototype.bounds_for = function(img, scale) {
		scale = scale || 1
		var coord = this.side & FRONT_BACK ? this.rect.x : this.rect.y
		var halfWidth = img.width/2 * scale
		var halfHeight = img.height/2 * scale

		return new Rect( new vec2( coord.a + halfWidth, coord.b - halfWidth ),
		                 new vec2( halfHeight, Rooms.list[this.room].height - halfHeight ) )
	}
	
	Wall.prototype.fits = function(img, scale){
		if (this.side & FRONT_BACK && this.rect.x.length() < img.width * scale) {
			return false
		}
		if (this.side & LEFT_RIGHT && this.rect.y.length() < img.width * scale) {
			return false
		}
		return true
	}
	
	Wall.prototype.center = function(offset){
	
		switch (this.side) {
			case FRONT:
				x = this.vec.midpoint()
				z = this.edge + painting_distance_from_wall
				break
			case BACK:
				x = this.vec.midpoint()
				z = this.edge - painting_distance_from_wall
				break
			case LEFT:
				x = this.edge + painting_distance_from_wall
				z = this.vec.midpoint()
				break
			case RIGHT:
				x = this.edge - painting_distance_from_wall
 				z = this.vec.midpoint()
				break
		}

		return new vec2 (x, z)
	}
	
	Wall.prototype.color = function(color){
		this.$walls && this.$walls.css("background-color", color)
	}
	
	Wall.prototype.wallpaper = function(){
		var useX = this.side & FRONT_BACK
		var shouldFlip = this.side & (LEFT | BACK)
		this.mx.forEach(function(mx){
      var partitionOffset = useX ? mx.x : mx.z
      if (shouldFlip) partitionOffset *= -1
      partitionOffset += mx.width/2
      var floorOffset = mx.y + mx.height/2

      mx.el.style.backgroundImage = Scenery.nextWallpaper
      mx.el.style.backgroundPosition = (~~partitionOffset) + "px " + (~~floorOffset) + "px"
    })
	}
	
	Wall.prototype.outline = function(){
		var useX = this.side & FRONT_BACK
		var shouldFlip = this.side & (LEFT | BACK)
		var mx = this.mx

		if (! shouldFlip) {
			mx = mx.reverse()
		}

		var len = this.mx.length
		
    var backgroundColor = "rgba(255,255,255,0.95)"
    var borderColor = "rgba(0,0,0,1.0)"

		zz = window.zz || 0

		mx.forEach(function(mx, i){
			if (mx.outlined) return
			mx.outlined = true
      mx.el.style.backgroundColor = backgroundColor

			// all walls get bottom lines
			mx.el.style.borderBottom = "1px solid " + borderColor
			
			// all walls get top lines
			mx.el.style.borderTop = "1px solid " + borderColor
			
			// walls on initial sides get left lines
			// if their left edge lines up with the rect edge
			if (i == 0) {
        mx.el.style.borderLeft = "1px solid " + borderColor
			}
			
			// walls on terminal sides get right lines....
			// if their right edge lines up with the rect edge
			if (i == len-1) {
        mx.el.style.borderRight = "1px solid " + borderColor
			}
		})
	}
	
	Wall.prototype.siblings = function(){
		return this
// 		var base = this
// 		var match = base.side | base.half_side
// 		var walls = Rooms.list[this.room].walls.filter(function(w){
// 			return (w.side | w.half_side) & match && w.$walls
// 		})
// 		return walls
	}

	Wall.prototype.randomize_colors = function(){
		var color = window.grayColors[ this.side | this.half_side ]
		// this.color(color)
	}

	Wall.prototype.stroke_colors = function(){
		var color = "#fff"
		var siblings = this.siblings()
		siblings.forEach(function(w, i){
			if (! w.$walls) return
			w.color(color)
			if (i == 0) {
				w.$walls.css("border-left", "1px solid #000")
			}
			if (i == siblings.length-1) {
				w.$walls.css("border-right", "1px solid #000")
			}
			w.$walls.css("border-top", "1px solid #000")
			w.$walls.css("border-bottom", "1px solid #000")
		})
	}
	
	if ('window' in this) {
		window.Wall = Wall
	}
	else {
		module.exports = Wall
	}


})()