-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiftEscape.js
468 lines (401 loc) · 14.5 KB
/
shiftEscape.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
/*
* Shift Escape Prototype
* A game developed using the Three.js library.
*
* By John Bentley
*
* Base code provided by Nathan Whitehead
* http://nathansuniversity.com/index10.html
*/
/* Set up our global variables
* that we will use throughout the
* rest of the program.
*/
var shiftMode = false;
var robotPosition = null;
var highlightPosition = null;
var newPosition = null;
var cubeLight = null;
var Highlight = null;
var numShifts = 10;
var barrierArray = [];
var i;
var j;
// This function sets the offset for the board
var board_to_world = function(position) {
// Convert board position into world position
// This controls where the board is on the screen
return {x: 100 * position.x, y: 100 * position.y, z: 0};
};
// This function sets the offset for the robot
var robot_to_world = function(position) {
// Converts robot position to world position
// Controls where the robot is on screen
return {x: 100 * position.x, y: 100 * position.y, z: 0};
};
// This function sets the offset for the highlight
var highlight_to_world = function(position) {
// Converts highlighted cube position to world position
return {x: 100 * position.x, y: 100 * position.y, z: 100};
};
// This function initializes the material used
// to create the barriers in the game
var Barrier = function(position) {
// A Barrier is a game object
// Default position if unspecified is at square 0, 0
this.isEmpty = false;
this.boardPosition = position || {x: -400, y: 400};
this.type = 'barrier';
this.geometry = new THREE.CubeGeometry(90, 90, 90);
// Geometry should always be around origin
this.material = new THREE.MeshLambertMaterial({color: 0x0033CC});
this.object = new THREE.Mesh(this.geometry, this.material);
// A mesh is an Object3D, change its position to move
this.object.position = board_to_world(this.boardPosition);
};
// This function initializes the material used
// to create the robot in the game
var Robot = function(position) {
//// A Robot is a game object
// Default position if unspecified is at square 0, 0
this.boardPosition = position || {x: -400, y: 400};
this.type = 'robot';
this.geometry = new THREE.SphereGeometry(45, 20, 20);
// Geometry should always be around origin
// Make it blue
this.material = new THREE.MeshPhongMaterial({color: 0x0099CC});
this.object = new THREE.Mesh(this.geometry, this.material);
// A mesh is an Object3D, change its position to move
this.object.position = robot_to_world(this.boardPosition);
};
// This function initializes the material used
// to create the highlight in the game
var Highlight = function(position){
// Display spotlight on highlighted cube to shift
// Use color code 0x45e1f5
this.geometry = new THREE.CubeGeometry(90, 90, 10);
this.boardPosition = position || {x: -400, y: 400};
this.type = 'highlight';
this.material = new THREE.MeshPhongMaterial({color: 0x00CC99});
this.object = new THREE.Mesh(this.geometry, this.material);
this.object.position = highlight_to_world(this.boardPosition);
};
Robot.prototype.updateBoardPosition = function() {
this.object.position = robot_to_world(this.boardPosition);
};
Robot.prototype.moveTo = function(position) {
this.boardPosition = position;
position = robotPosition;
this.updateBoardPosition();
return robotPosition;
};
Highlight.prototype.updateBoardPosition = function() {
this.object.position = highlight_to_world(this.boardPosition);
};
Highlight.prototype.moveTo = function(position) {
this.boardPosition = position;
position = highlightPosition;
this.updateBoardPosition();
return highlightPosition;
};
// A Game object is the highest level object representing entire game
var Game = function() {
};
Game.prototype.init = function() {
var that = this;
//this.barriers = [];
// Create a new 2D array to hold the position of the barriers
this.virtualBoard = new Array(8);
for (var i = 0; i < 8; i++){
this.virtualBoard[i] = new Array(8);
}
for(var x = 0; x < 8; x++){
for(var y = 0; y < 8; y++){
this.virtualBoard[x][y] = new Barrier({x: x-3, y: y-3});
}
}
// Initialize the robot
this.robot = new Robot({x: -1, y: 5});
// Initialize the highlight
this.highlight = new Highlight({x: -2, y: 5});
// Create and position the camera
this.camera = new THREE.PerspectiveCamera(68, 2, 1, 10000);
this.camera.position.z = 500;
this.scene = new THREE.Scene();
// Add the barriers to the scene
for(var x = 0; x < 8; x++){
for(var y = 0; y < 8; y++){
that.scene.add(this.virtualBoard[x][y].object);
}
}
this.scene.add(this.robot.object);
this.scene.add(this.highlight.object);
// Spotlight
var spotlight = new THREE.PointLight(0xffffff, 1, 1000);
spotlight.position.set(0, -100, 300);
this.scene.add(spotlight);
// Ambient light
var ambient_light = new THREE.AmbientLight(0x202020);
this.scene.add(ambient_light);
// Add a Background plane to hold the game board
var bgplane = new THREE.Mesh(new THREE.PlaneGeometry(1300, 1300),
new THREE.MeshBasicMaterial({color: 0x7094FF}));
bgplane.translateZ(-100);
bgplane.translateY(200);
bgplane.translateX(75);
this.scene.add(bgplane);
// Add a simple background
var background = new THREE.Mesh(new THREE.PlaneGeometry(10000, 10000), new THREE.MeshBasicMaterial({color: 0x00F5B8}));
background.translateZ(-200);
this.scene.add(background);
// Add some 3D text to display remaining shifts
/*
var textMesh1, textMesh2, textGeo, material, parent;
var firstLetter = true;
var text = "three.js",
height = 20,
size = 70,
hover = 30,
curveSegments = 4,
bevelThickness = 2,
bevelSize = 1.5,
bevelSegments = 3,
bevelEnabled = true,
font = "optimer", // helvetiker, optimer, gentilis, droid sans, droid serif
weight = "bold", // normal bold
style = "normal"; // normal italic
var mirror = true;
var fontMap = {
"helvetiker": 0,
"optimer": 1,
"gentilis": 2,
"droid sans": 3,
"droid serif": 4
};
var weightMap = {
"normal": 0,
"bold": 1
};
var reverseFontMap = {};
var reverseWeightMap = {};
for ( var i in fontMap ) reverseFontMap[ fontMap[i] ] = i;
for ( var i in weightMap ) reverseWeightMap[ weightMap[i] ] = i;
function capitalize( txt ) {
return txt.substring( 0, 1 ).toUpperCase() + txt.substring( 1 );
}
function decimalToHex( d ) {
var hex = Number( d ).toString( 16 );
hex = "000000".substr( 0, 6 - hex.length ) + hex;
return hex.toUpperCase();
}
*/
// Skybox stuff
// Adding the texture images
var urlPrefix = "images/";
var urls = [ urlPrefix + "xpos.jpg", urlPrefix + "xneg.jpg",
urlPrefix + "ypos.jpg", urlPrefix + "yneg.jpg",
urlPrefix + "zpos.jpg", urlPrefix + "zneg.jpg" ];
var textureCube = THREE.ImageUtils.loadTextureCube( urls );
var shader = THREE.ShaderLib[ "cube" ]; // Naming and adding the shader
var uniforms = THREE.UniformsUtils.clone( shader.uniforms ); // Importing the texture images
uniforms['tCube'].value= textureCube;
var material = new THREE.ShaderMaterial( {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms
} ),
skyMesh = new THREE.Mesh( new THREE.CubeGeometry( 100000, 100000, 100000 ), material );
skyMesh.flipSided = true;
this.scene.add(skyMesh);
// Render the skybox
this.renderer = new THREE.WebGLRenderer({antialias: true});
this.renderer.setSize(800, 600);
this.renderer.setClearColor(0xeeeeee, 1.0);
document.body.appendChild(this.renderer.domElement);
// Setup keyboard events
this.keys = {};
$('body').keydown(function(e) {
console.log('keydown ' + e.which);
if (e.which) {
if (that.keys[e.which] !== 'triggered') {
that.keys[e.which] = true;
}
}
console.log(that.keys);
});
$('body').keyup(function(e) {
console.log('keyup ' + e.which);
if (e.which) {
that.keys[e.which] = false;
}
console.log(that.keys);
});
};
Game.prototype.render = function(t) {
// Hold the camera in position
this.camera.position.x = 0; // 700
this.camera.position.y = -300; // 400
this.camera.position.z = 850; //500
this.camera.lookAt(this.scene.position);
this.renderer.render(this.scene, this.camera);
};
// Set up the light that acts as a cursor, following the robot
Game.prototype.cubeLight = function() {
this.type = 'light';
cubeLight = new THREE.SpotLight(0x5DF57B);
this.position = (robotPosition);
this.castShadow = true;
this.target = robotPosition;
this.scene.add(cubeLight);
};
// This function dictates where the robot and the highlight may move
Game.prototype.legalMove = function(position) {
if (position.x < -3 || position.x > 4) {
return false;
}
if (position.y < -5 || position.y > 4) {
return false;
}
return true;
};
/* Below is my initial attempt to shift rows and columns
Game.prototype.shiftRow = function(position) {
barrierArray[i] = barrierArray[i++];
}
Game.prototype.shiftCol = function(position) {
barrierArray[j] = barrierArray[j++];
//if (this.position.x === 3)
}
*/
// All keyboard input functions go here
Game.prototype.handleInput = function() {
// Robot move left
if (this.keys[65] === true) {
this.keys[65] = 'triggered';
var newPosition = {x: this.robot.boardPosition.x - 1,
y: this.robot.boardPosition.y};
if (this.legalMove(newPosition) && this.barriers != newPosition) {
this.robot.moveTo(newPosition);
}
}
// Robot move right
if (this.keys[68] === true) {
this.keys[68] = 'triggered';
var newPosition = {x: this.robot.boardPosition.x + 1,
y: this.robot.boardPosition.y};
if (this.legalMove(newPosition) && this.barriers != newPosition) {
this.robot.moveTo(newPosition);
}
}
// Robot move up
if (this.keys[87] === true) {
this.keys[87] = 'triggered';
var newPosition = {x: this.robot.boardPosition.x,
y: this.robot.boardPosition.y + 1};
if (this.legalMove(newPosition) && this.barriers != newPosition) {
this.robot.moveTo(newPosition);
}
}
// Robot move down
if (this.keys[83] === true) {
this.keys[83] = 'triggered';
var newPosition = {x: this.robot.boardPosition.x,
y: this.robot.boardPosition.y - 1};
if (this.legalMove(newPosition) && this.barriers != newPosition) {
this.robot.moveTo(newPosition);
}
}
// Highlight cube left
if (this.keys[37] === true && shiftMode === false) {
this.keys[37] = 'triggered';
var newPosition = {x: this.highlight.boardPosition.x - 1, y: this.highlight.boardPosition.y};
if (this.legalMove(newPosition)) {
this.highlight.moveTo(newPosition);
}
}
// Highlight cube right
if (this.keys[39] === true && shiftMode === false) {
this.keys[39] = 'triggered';
var newPosition = {x: this.highlight.boardPosition.x + 1, y: this.highlight.boardPosition.y};
if (this.legalMove(newPosition)) {
this.highlight.moveTo(newPosition);
}
}
// Highlight cube up
if (this.keys[38] === true && shiftMode === false) {
this.keys[38] = 'triggered';
var newPosition = {x: this.highlight.boardPosition.x, y: this.highlight.boardPosition.y + 1};
if (this.legalMove(newPosition)) {
this.highlight.moveTo(newPosition);
}
}
// Highlight cube down
if (this.keys[40] === true && shiftMode === false) {
this.keys[40] = 'triggered';
var newPosition = {x: this.highlight.boardPosition.x, y: this.highlight.boardPosition.y - 1};
if (this.legalMove(newPosition)) {
this.highlight.moveTo(newPosition);
}
}
// Enable shift mode
/*
if (this.keys[16] == true){
shiftMode = true;
}
else {
shiftmode = false;
}
*/
// Row shift right
if (this.keys[39] === true && shiftMode === true) {
this.keys[39] = 'triggered';
var b = this.virtualBoard[6][newPosition.y + 3];
for (var i = 0; i < 7; i++){
if (i == 0){
this.virtualBoard[i][newPosition.y + 3] = b;
continue;
}
this.virtualBoard[i][newPosition.y + 3] = this.virtualBoard[i - 1][newPosition.y + 3];
}
// Update object position (equal to array index)
for (var i = 0; i < 7; i++){
var oldPosition = this.virtualBoard[i][newPosition.y + 3].boardPosition;
oldPosition.x = i - 3;
this.virtualBoard[i][newPosition.y + 3].moveTo(oldPosition);
}
numShifts--;
}
// Column shift
if (this.keys[13] === true) {
this.keys[13] = 'triggered';
this.shiftCol(highlightPosition);
numShifts--;
}
};
Game.prototype.start = function() {
var that = this;
var time0 = new Date().getTime(); // milliseconds since 1970
var loop = function() {
var time = new Date().getTime();
// Render visual frame
that.render(time - time0);
// Respond to user input
that.handleInput();
// Initialize the spotlight
that.cubeLight();
// Loop
requestAnimationFrame(loop, that.renderer.domElement);
};
loop();
};
// Display an alert if the robot reaches the end
Game.prototype.won = function(position) {
if (this.robot.position.y == 5){
alert("you won!");
}
}
$(function() {
var game = new Game();
game.init();
game.start();
});