summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/mx/extensions/mx.movements.js
blob: cecba7a55f4064acf3446f741e250d76bd5d6b77 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
MX.Movements = function (cam) {

	var moveForward,
			moveLeft,
			moveBackward,
			moveRight,
			moveUp,
			moveDown,
			turnLeft,
			turnRight,
			turnUp,
			turnDown,
			jumping = false,
			creeping = false,
			locked = false,
			gravity = false,
			rotationX_min = PI/-4,
			rotationX_max = PI/6

	var v = 12,
			vr = Math.PI * 0.018,
			jumpV = 23,
			vx = vy = vz = 0,
			creepFactor = 0.3

	var mouseX, mouseY, dx, dy, rotX, rotY, dragging = false

	var trackpad

	var DEFAULT_SCALE = 1.0, scale = DEFAULT_SCALE
	
	var pos = { x: 0, y: 0, z: 0, rotationX: 0, rotationY: 0 }

  $(document).one("keydown", function(){
    $("#keyhint").fadeOut(250);
    $('.reader #minimap').addClass('active');
  })

	function clampRotation( vr ) {
		if (window.Rooms && Rooms.mover.noclip) {
			return clamp(vr, PI/-2, PI/2 )
		}
		else {
			return clamp(vr, PI/-4, PI/6 )
		}
	}

	var exports = {

		init: function () {
		
		  trackpad = new wheel ({
		  	el: scene.el,
		  	update: exports.mousewheel,
		  })
		  
			document.addEventListener('keydown', exports.keydown)
			document.addEventListener('keyup', exports.keyup)
			document.addEventListener('mousedown', exports.mousedown)
			document.addEventListener('mousemove', exports.mousemove)
			document.addEventListener('mouseup', exports.mouseup)
			window.addEventListener('blur', exports.reset)
			window.addEventListener('focus', exports.reset)
		},
		
		keydown: function (e) {
			// console.log(e.keyCode)
			if (locked || e.altKey || e.metaKey || e.ctrlKey) {
				return
			}
			switch ( e.keyCode ) {
			
				case 16: // shift
					creeping = true
					break

				case 38: // up
				case 87: // w
					moveForward = true
					break

				case 65: // a
					moveLeft = true
					break

				case 40: // down
				case 83: // s
					moveBackward = true
					break

				case 68: // d
					moveRight = true
					break

				case 37: // left
				case 81: // q
					turnLeft = true
					break
						
				case 39: // right
				case 69: // e
					turnRight = true
					break
			
				case 82: // r
					turnUp = true
					break

				case 70: // f
					turnDown = true
					break

				case 32: // space
					if (gravity) {
						jumping = true
						vy = abs(vy) + jumpV * scale
						if (e.shiftKey) {
							vy *= -1
						}
					}
					else {
						if (e.shiftKey) {
							moveDown = true
						}
						else {
							moveUp = true
						}
					}
					break

				case 27:  // esc
					if (Scenery.nextMedia) {
						Scenery.nextMedia = null
						app.tube('cancel-scenery')
					}
					else if (Scenery.nextWallpaper) {
						Scenery.nextWallpaper = null
						app.tube('cancel-wallpaper')
					}
					else if (app.controller.mediaViewer && app.controller.mediaViewer.$el.hasClass("active")) {
						app.controller.mediaViewer.hide()
						$(".inuse").removeClass("inuse")
					}
					else if (app.controller.colorControl && app.controller.colorControl.$el.hasClass('active')) {
						app.controller.colorControl.hide()
						$(".inuse").removeClass("inuse")
					}
					else if (app.controller.wallpaperPicker && app.controller.wallpaperPicker.$el.hasClass('active')) {
						app.controller.wallpaperPicker.hide()
						$(".inuse").removeClass("inuse")
					}
					else if (app.controller.presets && app.controller.presets.$el.hasClass('active')) {
						app.controller.presets.hide()
						$(".inuse").removeClass("inuse")
					}
					else if (! Rooms.shapesMode) {
						app.controller.toolbar.toggleMap()
					}
					break
				
				case 8: // backspace
				  e.preventDefault()
          if (app.controller.sculptureEditor && app.controller.sculptureEditor.sculpture) {
            app.controller.sculptureEditor.sculpture.remove()
          }
          else if (app.controller.mediaEditor && app.controller.mediaEditor.scenery) {
            app.controller.mediaEditor.scenery.remove()
          }
          else if (app.controller.textEditor && app.controller.textEditor.scenery) {
            app.controller.textEditor.scenery.remove()
          }
			}
		},

		keyup: function (e) {
			if (locked) return;
			switch ( e.keyCode ) {
		
				case 16: // shift
					creeping = false
					break

				case 38: // up
				case 87: // w
					moveForward = false
					break

				case 65: // a
					moveLeft = false
					break

				case 40: // down
				case 83: // s
					moveBackward = false
					break

				case 68: // d
					moveRight = false
					break

				case 37: // left
				case 81: // q
					turnLeft = false
					break
						
				case 39: // right
				case 69: // e
					turnRight = false
					break

				case 82: // r
					turnUp = false
					break

				case 70: // f
					turnDown = false
					break

				case 32: // space
					moveUp = moveDown = false
					break

/*					
				case 48: // 0
					movements.center()
					break
*/
			}
		},
		
		center: function(){
      cam.rotationX = 0
      cam.rotationY = 0
      cam.x = 0
      cam.y = viewHeight
      cam.z = 0
		},

		mousedown: function (e) {
			if (locked) return;
			mouseX = e.pageX
			mouseY = e.pageY
			rotX = cam.rotationX
			rotY = cam.rotationY
			dragging = true
		},
			
		mousemove: function (e) {
			if (locked || ! dragging) return
			var dx = (e.pageX - mouseX) / window.innerWidth * Math.PI/3
			var dy = (e.pageY - mouseY) / window.innerHeight * Math.PI/3
			cam.rotationY = rotY + dx
			cam.rotationX = clampRotation( rotX - dy )
		},
		
		mouseup: function (e) {
			dragging = false
		},
		
		reset: function(){
			moveForward = moveLeft = moveBackward = moveRight = moveUp = moveDown = turnLeft = turnRight = jumping = dragging = creeping = false
		},
		
		mousewheel: function (e, deltaY, deltaX) {
		  if (e.shiftKey) {
        cam.rotationY -= deltaY / 150
      }
      else {
        pos.x += deltaY * Math.cos(cam.rotationY + Math.PI / 2) * 10
        pos.z += deltaY * Math.sin(cam.rotationY + Math.PI / 2) * 10
        app.tube("move", pos)
      }
		},

		update: function (dt) {

			if (locked) { return }
			
			var ry = cam.rotationY
			var s = creeping ? scale * creepFactor : scale
			var vrrrr = creeping ? vr * creepFactor * 5 : vr * 0.5
			var moving = moveForward || moveBackward || moveRight || moveLeft || moveUp || moveDown || turnLeft || turnRight || turnUp || turnDown
			vx = vz = 0
			
			var vv = v
// 			vv *= dt / 100  * 8
// 			s *= dt / 100  * 8
// 			console.log(dt / 100  * 8)

			pos.x = cam.x
			pos.z = cam.z

			if (moving) {

				if (moveForward) {
					vx += vv * Math.cos(ry + Math.PI / 2) * s
					vz += vv * Math.sin(ry + Math.PI / 2) * s
				}
				if (moveBackward) {
					vx -= vv * Math.cos(ry + Math.PI / 2) * s
					vz -= vv * Math.sin(ry + Math.PI / 2) * s
				}
				if (moveLeft) {
					vx -= vv * Math.cos(ry) * s
					vz -= vv * Math.sin(ry) * s
				}
				if (moveRight) {
					vx += vv * Math.cos(ry) * s
					vz += vv * Math.sin(ry) * s
				}
				if (moveUp) {
					pos.y += vv * scale
				}
				if (moveDown) {
					pos.y -= vv * scale
				}

				if (turnUp) {
					cam.rotationX = clampRotation( cam.rotationX - vrrrr*s )
				}
				if (turnDown) {
					cam.rotationX = clampRotation( cam.rotationX + vrrrr*s )
				}
				if (turnLeft) {
					cam.rotationY += vrrrr*s
				}
				if (turnRight) {
					cam.rotationY -= vrrrr*s
				}

				pos.x += vx
				pos.z += vz
			}

			if (gravity) {
				vy -= 1 * scale

				pos.y += vy

				if (vy) {
					moving = true
				}
				if (pos.y <= viewHeight) {
					pos.y = viewHeight
					vy = 0
					jumping = false
				}

				var ceiling = ((window.Rooms && Rooms.mover.room) ? Rooms.mover.room.height : 5000)
				
				if (pos.y >= ceiling-5) {
					vy = 0
					pos.y = ceiling-5
				}
			}

			if (moving) {
				app.tube("move", pos)
			}
		},
		
		lock: function(){ locked = true },
		unlock: function(){ locked = false },
		scale: function(n){ if (n) scale = n; return scale },
		resetScale: function(n){ scale = DEFAULT_SCALE },
		gravity: function(g){ return typeof g == "boolean" ? gravity = g : gravity },
		velocity: function(n){ v = clamp(n, 1, 50) },
		jumpVelocity: function(n){ jumpV = clamp(n, 1, 50) },
	}
	
	return exports
}