/
main.js
206 lines (155 loc) · 3.87 KB
/
main.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
/**
an endless golden thread
initialization and frame pump
@module T
@author cpgauthier
**/
var T = {
DETAIL_DISTANCE: 2,
detailUpdate: SOAR.vector.create(Infinity, Infinity, Infinity),
rng: SOAR.random.create(),
/**
generate a random number for seeding other RNGs
used because seeding by time fails on startup--
all seeds will be the same. you need a "master"
seed RNG to drive all others.
@method seed
@return number, seed integet
**/
seed: function() {
var seed = Math.round(T.rng.get() * T.rng.modu);
console.log(seed);
return seed;
},
/**
create GL context, set up game objects, load resources
@method start
**/
start: function() {
var gl, init, total, id;
// create the GL display
try {
T.display = SOAR.display.create("gl");
} catch (e) {
debugger;
}
// resize display & redraw if window size changes
window.addEventListener("resize", T.resize, false);
// set initial display size
T.display.setSize(
document.body.clientWidth,
document.body.clientHeight
);
// set up debugger element
T.debugit = document.getElementById("debugit");
// set up any webgl stuff that's not likely to change
gl = T.display.gl;
gl.clearDepth(1.0);
gl.depthFunc(gl.LEQUAL);
gl.enable(gl.DEPTH_TEST);
gl.clearColor(0.75, 0.75, 0.75, 1);
// create and spawn worker thread
T.worker = new Worker("thread.js");
T.worker.onmessage = function(e) {
T.handle(e);
};
T.worker.onerror = function(e) {
console.log("worker thread error: " +
e.message + " at line " +
e.lineno + " in file " +
e.filename);
};
// set up an array of all objects to be initialized
inits = [ T.path, T.player ];
total = inits.length;
// set up a function to call for the next several animation frames
// this will perform initialization, with progress bar animations
id = SOAR.schedule(function() {
var il = inits.length;
var np = total - il;
// as long as there are objects to init
if (il > 0) {
// init the next one
inits.shift().init();
} else {
// unschedule the init function
SOAR.unschedule(id);
// schedule animation frame functions
SOAR.schedule(T.update, 0, true);
//SOAR.schedule(T.debug, 100, true);
// set display parameters
T.resize();
}
}, 0, true);
// start capturing control events
SOAR.capture.start();
// start the message pump
SOAR.run();
},
/**
handle browser resizing
@method resize
**/
resize: function() {
T.display.setSize(
document.body.clientWidth,
document.body.clientHeight
);
T.player.camera.projector();
T.draw();
},
/**
update all game objects that require it
@method update
**/
update: function() {
SOAR.capture.update();
T.player.update();
// update all detail objects if we've moved far enough
var ppos = T.player.camera.position;
if (T.detailUpdate.distance(ppos) > T.DETAIL_DISTANCE) {
T.worker.postMessage({
cmd: "generate",
pos: ppos
});
T.detailUpdate.copy(ppos);
}
T.draw();
},
/**
handle message from worker thread
@method handle
@param e object, data from thread
**/
handle: function(e) {
switch(e.data.cmd) {
case "build-meshes":
T.path.build(e.data);
break;
default:
console.log("worker thread sent unknown message: " + e.data.cmd);
break;
}
},
/**
draw all game objects that require it
draw and update are separated so that the
game can redraw the display when the game
is paused (T.e., not updating) and resize
events are being generated
@method draw
**/
draw: function() {
var gl = T.display.gl;
gl.disable(gl.BLEND);
gl.disable(gl.CULL_FACE);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
T.path.draw();
},
dump: function(o) {
T.debugit.innerHTML = JSON.stringify(o);
},
debug: function() {
T.debugit.innerHTML = SOAR.fps;
}
};