-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplayer.js
712 lines (625 loc) · 24.5 KB
/
player.js
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// Copyright 2011-2012 Kevin Reid under the terms of the MIT License as detailed
// in the accompanying file README.md or <http://opensource.org/licenses/MIT>.
(function () {
"use strict";
var AAB = cubes.util.AAB;
var Blockset = cubes.Blockset;
var Body = cubes.Body;
var CubeRotation = cubes.util.CubeRotation;
var dynamicText = cubes.util.dynamicText;
var exponentialStep = cubes.util.exponentialStep;
var max = Math.max;
var min = Math.min;
var mkelement = cubes.util.mkelement;
var mod = cubes.util.mod;
var Notifier = cubes.util.Notifier;
var Selection = cubes.Selection;
var World = cubes.World;
var WorldRenderer = cubes.WorldRenderer;
var ZEROVEC = cubes.util.ZEROVEC;
function noop() {}
// physics constants
var WALKING_SPEED = 4; // cubes/s
var FLYING_SPEED = 10; // cubes/s
var JUMP_SPEED = 8; // cubes/s
var CONTROL_STIFFNESS = 0.18;
var AIRSTEER_STIFFNESS = 0.03;
var playerAABB = new AAB(
-0.35, 0.35, // x
-1.75, 0.15, // y
-0.35, 0.35 // z
);
function Player(config, initialWorld, renderer, audio, scheduleDraw, objectUI) {
var player = this;
var gl = renderer.context;
// a Place stores a world and state in it; used for push/pop
function Place(world, bodyInitializer) {
this.world = world;
this.cursor = null;
this.tool = 2; // first non-bogus block id
this.selection = null;
// find or make body
var body = world.getPlayerBody();
if (body) {
this.bodyIsWorldly = true;
//console.log("Found existing body at " + vec3.str(body.pos));
} else {
body = new Body(world, playerAABB);
Object.defineProperty(body, "noclip", { enumerable: true, get: function () { // TODO kludge
return config.noclip.get();
}});
bodyInitializer(body);
//console.log("Created unworldly body with " + vec3.str(body.pos));
this.bodyIsWorldly = false;
}
this.body = body;
// must happen late
this.wrend = new WorldRenderer(world, function () { return body.pos; }, renderer, audio, scheduleDraw, true);
}
Place.prototype.delete = function () {
this.wrend.deleteResources();
};
// Worlds we've been in
var placeStack = [];
var currentPlace;
// kludge: Since UI sets pitch absolutely, it's not a place variable
var pitch = 0;
var exposure = 1.0;
var movement = vec3.create();
var mousePos = null; // or an [screen x, screen y] vector. Immutable value.
// Generate vectors [p, a, b] such that the corners of the face designated
// by the cursor are p, p+a, p+b, and p+a+b.
function cursorFaceVectors(cursor) {
var p = vec3.create(cursor.cube);
// This works, but don't ask me to justify it.
var qp = vec3.subtract(vec3.create(), cursor.face);
var qr = vec3.createFrom(-qp[1], -qp[2], -qp[0]); // first perpendicular vector
var qs = vec3.cross(qp, qr, vec3.create()); // second perpendicular vector
if (qp[0]+qp[1]+qp[2] > 0) {
vec3.subtract(p, qr);
} else {
vec3.subtract(p, qp);
}
return [p, qr, qs];
}
var cursorR = new renderer.RenderBundle(gl.LINE_LOOP, null, function (vertices, normals, colors) {
var sel = currentPlace ? currentPlace.cursor : null;
if (sel !== null) {
var cv = cursorFaceVectors(sel);
var p = cv[0];
var a = cv[1];
var b = cv[2];
colors.push(1,1,1,1); normals.push(0,0,0); vertices.push(p[0],p[1],p[2]);
colors.push(1,1,1,1); normals.push(0,0,0); vertices.push(p[0]+a[0],p[1]+a[1],p[2]+a[2]);
colors.push(1,1,1,1); normals.push(0,0,0); vertices.push(p[0]+a[0]+b[0],p[1]+a[1]+b[1],p[2]+a[2]+b[2]);
colors.push(1,1,1,1); normals.push(0,0,0); vertices.push(p[0]+b[0],p[1]+b[1],p[2]+b[2]);
}
}, {
aroundDraw: function (draw) {
renderer.setLineWidth(1);
gl.disable(gl.DEPTH_TEST);
draw();
gl.enable(gl.DEPTH_TEST);
}
});
var aabbR = renderer.aabRenderer(function (draw) {
if (currentPlace) {
draw(currentPlace.body.pos, playerAABB, [0,0,1]);
currentPlace.body.debugHitAABBs.forEach(function (aabb) {
draw(ZEROVEC, aabb, [0,1,0]);
});
currentPlace.body.world.forEachBody(function (body) {
draw(body.pos, body.aabb, [1,0,0]);
});
}
});
function _transformPointToSubworld(cube,world,rot,point) {
var buf = vec3.create(point);
vec3.subtract(buf, cube);
rot.transformPoint(buf, buf);
vec3.scale(buf, world.wx); // cubical assumption
return buf;
}
function aimChanged() {
scheduleDraw(); // because this routine is also 'view direction changed'
var foundCube = null, foundFace = null;
if (mousePos !== null) {
var w = currentPlace.world;
var ray = renderer.getAimRay(mousePos, player.render);
w.raycast(ray.origin, ray.direction, 20, function (x,y,z,value,face) {
if (w.selectable(x,y,z)) {
var cube = Object.freeze([x,y,z]);
var type = w.blockset.get(value);
var subfound = false;
if (!type.opaque && type.world) {
// test against shape of noncubical block
var w1 = type.world;
var rot = CubeRotation.byCode[w.gRot(x,y,z)].inverse;
w1.raycast(_transformPointToSubworld(cube,w1,rot,ray.origin),
rot.transformVector(ray.direction),
Infinity,
function (x1,y1,z1,v1,f1) {
if (w1.selectable(x1,y1,z1)) {
subfound = true;
return true;
}
});
if (!subfound) return;
}
foundCube = cube;
foundFace = Object.freeze(Array.prototype.slice.call(face));
return true;
}
});
// Note: If we ever want to enable selection of the edges of the world,
// then that can be done by noting the last cube the raycast hit.
}
var newSel;
if (foundCube !== null) {
newSel = Object.freeze({
cube: foundCube,
face: foundFace,
toString: function () { return this.cube + ";" + this.face; }
});
} else {
newSel = null;
}
if (String(currentPlace.cursor) !== String(newSel)) {
currentPlace.cursor = newSel;
cursorR.recompute();
scheduleDraw();
}
if (isDraggingSelection) {
reshapeSelection();
}
}
function updateAudioListener() {
audio.setListener.apply(audio, currentPlace.body.getListenerParameters());
}
function computeExposure() {
var world = currentPlace.world;
// TODO inefficient to rebuild this matrix — cache! This is the inverse of applyViewRot
var matrix = mat4.identity();
mat4.rotate(matrix, currentPlace.body.yaw, [0, 1, 0]);
mat4.rotate(matrix, pitch, [1, 0, 0]);
var pos = currentPlace.body.pos;
var light = 0;
var hits = 0;
function ray(look /* overwritten */) {
mat4.multiplyVec3(matrix, look);
var foundOpenSpace = !world.inBoundsv(pos);
world.raycast(pos, look, 20/*TODO magic number */, function (x,y,z,value,face) {
if (world.opaque(x,y,z)) { // TODO use appropriate test; what we actually want here is "is this a block which has a valid light value
if (foundOpenSpace) {
x += face[0];
y += face[1];
z += face[2];
light += world.gLight(x,y,z);
hits++;
return true;
}
} else {
// This flag keeps the ray from stopping immediately if we are inside opaque blocks.
foundOpenSpace = true;
}
});
}
for (var x = -1; x <= 1; x++)
for (var y = -1; y <= 1; y++) {
ray([x/2, y/2, -1]);
}
var localLightFactor = (hits ? light / hits : world.lightOutside) * world.lightScale;
var compensation = 0.75;
return 1/(((localLightFactor - 1) * compensation) + 1);
}
this.getExposure = function () {
return exposure;
};
var footstepPhase = 0;
var footstepPeriod = 1.4;
var footstepY = 0;
var EPSILON = 1e-3;
function stepPlayer(timestep) {
var body = currentPlace.body;
var floor = body.getFloor();
// determine coordinate system for movement control
var controlOrientation = mat4.identity(mat4.create());
mat4.rotateY(controlOrientation, body.yaw);
if (body.flying && config.pitchRelativeFlight.get() /* && is mouselook mode? */) {
mat4.rotateX(controlOrientation, pitch);
}
// apply movement control to velocity
var movAdj = vec3.create();
mat4.multiplyVec3(controlOrientation, movement, movAdj);
vec3.scale(movAdj, body.flying ? FLYING_SPEED : WALKING_SPEED);
var stiffness = !body.flying && !floor ? AIRSTEER_STIFFNESS : CONTROL_STIFFNESS;
body.addVelocity([
(movAdj[0] - body.vel[0]) * stiffness,
body.flying ? (movAdj[1] - body.vel[1]) * stiffness
: movAdj[1] !== 0 ? (movAdj[1] - body.vel[1]) * stiffness + timestep * Body.GRAVITY : 0,
(movAdj[2] - body.vel[2]) * stiffness]);
if (config.lighting.get()) {
var newExposure = computeExposure();
if (newExposure !== exposure && !isNaN(newExposure)) {
exposure = exponentialStep(exposure, newExposure, timestep, -0.7, 1e-3);
if (isNaN(exposure)) {
exposure = 1.0;
}
scheduleDraw();
}
}
}
function afterPlayerBodyMoved(beforeMoveVel, timestep) {
var body = currentPlace.body;
updateAudioListener();
aimChanged();
var floor = body.getFloor();
if (vec3.length(movement) < EPSILON) {
footstepPhase = 0;
} else {
footstepPhase += vec3.length(body.vel) * timestep;
}
if (footstepPhase > footstepPeriod) {
footstepPhase = mod(footstepPhase, footstepPeriod);
playFootstep();
} else if (floor && beforeMoveVel[1] < -1 || body.pos[1] > footstepY && footstepPhase > 0.4) {
// footstep sooner if just hit a bump or fell down
footstepPhase = 0;
playFootstep();
}
function playFootstep() {
footstepY = body.pos[1];
// TODO play sounds for all blocks below or otherwise be less biased (getFloor gives arbitrary results)
var type = floor && body.world.gtv(floor);
if (type) {
audio.play(floor, type, "footstep", 0.5);
}
}
}
this.stepYourselfAndWorld = function (timestep) {
var body = currentPlace.body;
stepPlayer(timestep);
var beforeMovePos = vec3.set(body.pos, new Float64Array(3));
var beforeMoveVel = vec3.set(body.vel, new Float64Array(3));
currentPlace.world.step(timestep);
if (!currentPlace.bodyIsWorldly) body.step(timestep, noop);
if (vec3.dist(beforeMovePos, body.pos) > 0) {
afterPlayerBodyMoved(beforeMoveVel, timestep);
}
currentPlace.world.polishLightInVicinity(currentPlace.body.pos, config.renderDistance.get(), 1);
if (config.debugPlayerCollision.get()) {
aabbR.recompute();
scheduleDraw();
}
};
// --- The facet for rendering ---
var worldSceneObject = {
draw: function () {
currentPlace.wrend.draw();
if (config.debugPlayerCollision.get()) aabbR.draw();
}
};
var cursorInfoTextElem = document.createElement("pre");
var cursorInfoElem = mkelement("div", "overlay");
cursorInfoElem.appendChild(cursorInfoTextElem);
var cursorInfo = dynamicText(cursorInfoTextElem);
var cursorSceneObject = {
draw: function () {
cursorR.draw();
// Update HTML part
cursorInfo.data = "";
var cur = player.getCursor();
if (cur !== null) {
var cube = cur.cube;
var empty = vec3.add(cur.cube, cur.face, vec3.create());
var world = player.getWorld();
var value = world.gv(cube);
var sub = world.gSubv(cube);
var type = world.gtv(cube);
var light = type.opaque ? world.gLightv(empty)
: world.gLightv(cube);
var text = (
value
+ (sub ? ":" + sub : "")
+ (type.name ? " (" + type.name + ")" : "")
+ "\nat " + cur.cube
+ "\n" + (type.opaque ? "Surface" : "Interior") + " light: " + light
);
var circuit = world.getCircuit(cube);
if (circuit !== null) {
text += "\nCircuit: " + type.behavior.name + " " + circuit.describeBlock(cube);
}
cursorInfo.data = text;
}
},
element: cursorInfoElem,
boundsPoints: function (considerPoint) {
var cur = player.getCursor();
if (cur !== null) {
var cube = cur.cube;
for (var dx = 0; dx <= 1; dx++)
for (var dy = 0; dy <= 1; dy++)
for (var dz = 0; dz <= 1; dz++) {
considerPoint(cube[0] + dx, cube[1] + dy, cube[2] + dz, 1);
}
}
}
};
function reshapeSelection() {
if (currentPlace.cursor === null) return;
var lx = +Infinity;
var ly = +Infinity;
var lz = +Infinity;
var hx = -Infinity;
var hy = -Infinity;
var hz = -Infinity;
function extendSelection(point) {
lx = min(lx, point[0]);
ly = min(ly, point[1]);
lz = min(lz, point[2]);
hx = max(hx, point[0]);
hy = max(hy, point[1]);
hz = max(hz, point[2]);
}
var cv1 = cursorFaceVectors(currentPlace.selectionMark);
var cv2 = cursorFaceVectors(currentPlace.cursor);
var buf = vec3.create();
extendSelection(vec3.add(cv1[0], cv1[1], buf));
extendSelection(vec3.add(cv1[0], cv1[2], buf));
extendSelection(vec3.add(cv2[0], cv2[1], buf));
extendSelection(vec3.add(cv2[0], cv2[2], buf));
currentPlace.selection.setToAAB(new AAB(lx, hx, ly, hy, lz, hz));
rebuildSelectionObj();
}
var isDraggingSelection = false;
var selectionSceneObject = null;
function rebuildSelectionObj() {
var selection = currentPlace.selection;
//if (selectionSceneObject && selectionSceneObject._getTheSelection() == selection) return; // inappropriate because selection may have been modified -- TODO why bother w/ this?
if (selectionSceneObject) selectionSceneObject.deleteResources();
if (selection !== null) {
var selectionR = renderer.aabRenderer(function (draw) {
draw(ZEROVEC, selection.bounds, [0,1,1]);
});
var chip = new objectUI.ObjectChip(objectUI.refObject(selection));
selectionSceneObject = {
draw: function () {
selectionR.draw();
},
element: mkelement("div", "overlay", chip.element),
boundsPoints: function (considerPoint) {
var bounds = selection.bounds;
for (var dx = 0; dx <= 1; dx++)
for (var dy = 0; dy <= 1; dy++)
for (var dz = 0; dz <= 1; dz++) {
considerPoint(bounds[0 + dx], bounds[2 + dy], bounds[4 + dz], 1);
}
},
deleteResources: function () { // not part of scene object protocol
selectionR.deleteResources();
},
_getTheSelection: function () { // not part of scene object protocol
return selection;
}
};
} else {
selectionSceneObject = null;
}
}
this.render = Object.freeze({
applyViewRot: function (matrix) {
mat4.rotate(matrix, -pitch, [1, 0, 0]);
mat4.rotate(matrix, -currentPlace.body.yaw, [0, 1, 0]);
},
applyViewTranslation: function (matrix) {
var positionTrans = vec3.negate(currentPlace.body.pos, vec3.create());
positionTrans[1] += currentPlace.body.cameraYLag;
currentPlace.body.cameraYLag *= 0.75; /*Math.exp(-timestep*10) TODO we should be like this */
mat4.translate(matrix, positionTrans);
},
getPosition: function() {
return vec3.create(currentPlace.body.pos);
},
forEachSceneObject: function (callback) {
callback(worldSceneObject);
callback(cursorSceneObject);
if (selectionSceneObject) callback(selectionSceneObject);
},
getWorldRenderer: function () {
return currentPlace.wrend;
}
});
this.setPosition = function(p) {
vec3.set(p, currentPlace.body.pos);
updateAudioListener();
};
this.getWorld = function() {
return currentPlace.world;
};
this.getBody = function() {
return currentPlace.body;
};
this.getCursor = function() {
return currentPlace.cursor;
};
this.setWorld = function (world) {
if (currentPlace) currentPlace.delete();
while (placeStack.length) placeStack.pop().delete();
currentPlace = new Place(world, function (body) {
// TODO: move this position downward to free space rather than just imparting velocity
body.pos[0] = world.wx/2;
body.pos[1] = world.wy - playerAABB.get(1, 0) + EPSILON;
body.pos[2] = world.wz/2;
body.vel[1] = -120;
});
notifyChangedPlace();
};
// --- The facet for user input ---
function intersect(ignore) {
return Body.intersectAABAndWorld(
currentPlace.body.aabb.translate(currentPlace.body.pos),
currentPlace.world,
ignore);
}
var inputNotifier = new Notifier("player.input");
this.input = Object.freeze({
listen: inputNotifier.listen,
useTool: function () {
if (currentPlace.tool === Blockset.ID_EMPTY) {
this.deleteBlock();
} else {
// create block
if (currentPlace.cursor !== null) {
var cube = currentPlace.cursor.cube;
var face = currentPlace.cursor.face;
var x = cube[0]+face[0], y = cube[1]+face[1], z = cube[2]+face[2];
var type = currentPlace.world.blockset.get(currentPlace.tool);
if (currentPlace.world.g(x,y,z) === 0) {
// TODO: rotation on create should be more programmable.
var ray = renderer.getAimRay(mousePos, player.render); // TODO depend on player orientation instead?
var rotation = CubeRotation.nearestToDirection(
vec3.negate(ray.direction, vec3.create()),
[0,0,1],
type.automaticRotations.map(
function (code) { return CubeRotation.byCode[code]; }));
var alreadyIntersecting = intersect(null);
currentPlace.world.s(x,y,z, currentPlace.tool, rotation.code);
if (intersect(alreadyIntersecting) && !currentPlace.body.noclip) {
// New block intersected the player, reject placement.
// TODO: should not-place rather than revert, as this may have side effects -- requires being able to test against a patched world
currentPlace.world.s(x,y,z,0);
return;
}
currentPlace.world.transientEvent([x,y,z], "create");
aimChanged();
}
}
}
},
deleteBlock: function () {
if (currentPlace.cursor !== null) {
var cube = currentPlace.cursor.cube;
var x = cube[0], y = cube[1], z = cube[2];
if (currentPlace.world.g(x,y,z) !== 0 /* i.e. would destruction do anything */) {
currentPlace.world.transientEvent(cube, "destroy");
currentPlace.world.s(x, y, z, 0);
aimChanged();
}
}
},
selectStart: function () {
if (!currentPlace.selectionMark) {
currentPlace.selection = null;
currentPlace.selectionMark = currentPlace.cursor;
currentPlace.selection = new Selection(currentPlace.world);
}
reshapeSelection();
isDraggingSelection = true;
},
selectEnd: function () {
if (currentPlace.selection && currentPlace.selectionMark) {
reshapeSelection();
currentPlace.selectionMark = null;
}
isDraggingSelection = false;
},
tweakSubdata: function (delta) {
if (currentPlace.cursor !== null) {
var cube = currentPlace.cursor.cube;
var x = cube[0], y = cube[1], z = cube[2];
currentPlace.world.sSub(x, y, z,
mod(currentPlace.world.gSub(x,y,z) + delta, World.subdatumBound));
}
},
get blockset () { return currentPlace.world.blockset; },
set blockset (value) { throw new TypeError("player.input.blockset read-only"); },
get movement () { throw new TypeError("player.input.movement write-only"); },
set movement (value) {
vec3.set(value, movement);
if (movement[1] > 0) {
currentPlace.body.flying = true;
}
},
get mousePos () { throw new TypeError("player.input.mousePos write-only"); },
set mousePos (value) { mousePos = value; aimChanged(); },
get pitch () { return pitch; },
set pitch (value) { pitch = value; aimChanged(); },
get yaw () { return currentPlace.body.yaw; },
set yaw (value) { currentPlace.body.yaw = value; aimChanged(); },
get tool () { return currentPlace.tool; },
set tool (value) {
currentPlace.tool = value;
inputNotifier.notify("changedTool");
},
enterWorld: function (blockID) {
var world = currentPlace.world.blockset.get(blockID).world;
if (!world) { return; } // TODO: UI message about this
var oldPlace = currentPlace;
currentPlace = new Place(world, function (body) {
body.pos[0] = world.wx/2;
body.pos[1] = world.wy - playerAABB.get(1, 0) + EPSILON;
body.pos[2] = world.wz/2;
});
currentPlace.forBlock = blockID;
placeStack.push(oldPlace);
notifyChangedPlace();
},
changeWorld: function (direction) {
switch (direction) {
case 1:
if (currentPlace.cursor === null) return;
var cube = currentPlace.cursor.cube;
var x = cube[0], y = cube[1], z = cube[2];
var oldPlace = currentPlace;
var blockID = currentPlace.world.g(x,y,z);
var world = currentPlace.world.blockset.worldFor(blockID);
if (!world) return; // TODO: UI message about this
var tileSize = world.wx;
currentPlace = new Place(world, function (body) {
// Initial adjustments:
// Make new position same relative to cube
vec3.subtract(oldPlace.body.pos, cube, body.pos);
vec3.scale(body.pos, tileSize);
// ... but not uselessly far away.
vec3.scale(body.pos, Math.min(1.0, (tileSize+40)/vec3.length(body.pos))); // TODO make relative to center of world, not origin
// Same velocity, scaled
vec3.set(oldPlace.body.vel, body.vel);
vec3.scale(body.vel, tileSize);
// Same view direction
body.yaw = oldPlace.body.yaw;
// And not falling.
body.flying = true;
});
// This is needed because the routine in aimChanged assumes currentPlace knows the old state of the cursor. TODO: Kludgy.
cursorR.recompute();
placeStack.push(oldPlace);
break;
case -1:
if (placeStack.length <= 0) return;
currentPlace.delete();
currentPlace = placeStack.pop();
break;
}
notifyChangedPlace();
},
jump: function () {
currentPlace.body.jump([0, JUMP_SPEED, 0]);
}
});
function notifyChangedPlace() {
aimChanged();
aabbR.recompute();
updateAudioListener();
rebuildSelectionObj();
inputNotifier.notify("changedWorld");
}
// --- Initialization ---
config.debugPlayerCollision.listen({
interest: function () { return true; },
changed: scheduleDraw
});
this.setWorld(initialWorld);
}
Player.aabb = playerAABB;
cubes.Player = Object.freeze(Player);
}());