-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.js
138 lines (101 loc) · 3.76 KB
/
GameManager.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
/**
* @author Amber Roy / http://github.com/amberroy
*/
GameManager = function( game, params ) {
this.game = game;
this.init = this.game && this.game.init.bind( this.game ) || this._defaultInit;
this.update = this.game && this.game.update.bind( this.game ) || this._defaultUpdate;
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( this.renderer.domElement );
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.3, 10000 );
this.vrControls = new THREE.VRControls( this.camera );
this.vrEffect = new THREE.VREffect( this.renderer );
this.vrEffect.setSize( window.innerWidth, window.innerHeight );
this.vrManager = new WebVRManager( this.renderer, this.vrEffect, {hideButton: false} );
// Don't move the camera, move the player instead.
this.player = new THREE.Object3D();
this.head = new THREE.Object3D();
this.player.add( this.head );
this.head.add( this.camera );
this.player.add( this.camera );
this.scene.add( this.player );
this._addEventListeners();
};
GameManager.prototype.loadAudio = function(filenameBase) {
// Reference:
// https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats
// http://www.online-convert.com/
// Load sound effect. Chromium doesn't support mp3 so include wav too.
var ext = (new Audio()).canPlayType('audio/mpeg') ? ".mp3" : ".wav";
var audio = new Audio(filenameBase + ext);
return audio;
};
GameManager.prototype._defaultInit = function() {
console.log("GameManager using default init()");
var gridTileImage = "./assets/images/grid-tile.png"
// Create 3D objects.
var geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
var material = new THREE.MeshNormalMaterial();
var cube = new THREE.Mesh(geometry, material);
cube.name = "cube";
cube.userData.isRotating = true;
// Position cube mesh
cube.position.z = -1;
// Add cube mesh to your three.js scene
this.scene.add(cube);
// Also add a repeating grid as a skybox.
var boxWidth = 10;
var texture = THREE.ImageUtils.loadTexture(gridTileImage);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(boxWidth, boxWidth);
var geometry = new THREE.BoxGeometry(boxWidth, boxWidth, boxWidth);
var material = new THREE.MeshBasicMaterial({
map: texture,
color: 0x333333,
side: THREE.BackSide
});
var skybox = new THREE.Mesh(geometry, material);
skybox.name = "skybox";
this.scene.add(skybox);
};
GameManager.prototype._defaultUpdate = function() {
// Apply rotation objects
for (var i=0; i < this.scene.children.length; i++ ) {
var object = this.scene.children[i];
if ( object.userData.isRotating ) {
object.rotation.y += 0.01;
}
}
};
// Request animation frame loop function
GameManager.prototype.animate = function() {
// Update VR headset position and apply to camera.
this.vrControls.update();
// Update game objects
this.update();
// Render the scene through the manager.
this.vrManager.render(this.scene, this.camera);
requestAnimationFrame( this.animate.bind( this ) );
}
GameManager.prototype._addEventListeners = function () {
// Reset the position sensor when 'z' pressed.
function onKey(event) {
if (event.keyCode == 90) { // z
this.vrControls.resetSensor();
}
if (event.keyCode == 13) { // Enter VR Mode
this.vrManager.button.emit("click");
}
};
window.addEventListener('keydown', onKey.bind( this ), true);
// Handle window resizes
function onWindowResize() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.vrEffect.setSize( window.innerWidth, window.innerHeight );
}
window.addEventListener('resize', onWindowResize.bind( this ), false);
};