-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.js
151 lines (127 loc) · 4.31 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
requirejs.config({
shim: {
'lib/createjs': {
exports: 'createjs'
}
}
});
require(["lib/gl-matrix", "Player", "util/download", "Map", "GameInfo", "config", "util/PointerLock"], function(glMatrix, Player, download, Map, GameInfo, config, PointerLock) {
//Define namespace
window.cs = window.cs || { };
function initGL(canvas) {
try {
GameInfo.gl = canvas.getContext("experimental-webgl");
GameInfo.gl.viewportWidth = canvas.width;
GameInfo.gl.viewportHeight = canvas.height;
GameInfo.gl.enable(gl.DEPTH_TEST);
GameInfo.gl.enable(gl.CULL_FACE);
GameInfo.gl.cullFace(gl.FRONT);
} catch (e) { }
if (!GameInfo.gl) {
console.log("Could not initialise WebGL");
}
}
function render() {
var player = GameInfo.player;
var gl = GameInfo.gl;
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
glMatrix.mat4.perspective(GameInfo.pMatrix, config.FIELD_OF_VIEW*Math.PI/180,
gl.viewportWidth / gl.viewportHeight,
config.NEAR_CLIPPING, config.FAR_CLIPPING);
glMatrix.mat4.identity(GameInfo.mvMatrix);
glMatrix.mat4.rotateX(GameInfo.mvMatrix, GameInfo.mvMatrix, player.xAngle);
glMatrix.mat4.rotateY(GameInfo.mvMatrix, GameInfo.mvMatrix, player.yAngle);
//Move the player by moving the map in the reverse direction
glMatrix.mat4.translate(GameInfo.mvMatrix, GameInfo.mvMatrix,
[player.y, -player.z - config.PLAYER_HEIGHT, player.x]);
GameInfo.map.render(player.position());
glMatrix.mat4.identity(GameInfo.mvMatrix);
//Draw player
player.render();
}
function mainLoop() {
requestAnimFrame(mainLoop);
GameInfo.player.move();
render();
}
function webGLStart() {
var canvas = document.getElementById("canvas");
initGL(canvas);
var gl = GameInfo.gl;
var mapName = "cs_assault.bsp"/*sessionStorage.getItem("map")*/;
download("data/maps/" + mapName, "arraybuffer", function(data) {
gl.clearColor(0.0, 0.0, 0.0, 1.0);
//Parse map
GameInfo.map = new Map(gl, data);
GameInfo.player = new Player(gl, -496, 2352, 176);
GameInfo.player.switchWeapon(config.PLAYER_DEFAULT_WEAPON);
//Set event handler for resizing the screen every time
//the window changes size
var resizeCallback = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
gl.viewportWidth = window.innerWidth;
gl.viewportHeight = window.innerHeight;
};
resizeCallback();
window.addEventListener("resize", resizeCallback, false);
//Listen for clicks on the canvas
canvas.addEventListener("click", function() {
//is the mouse not currently locked?
if(!PointerLock.pointerLockElement()) {
//Nope. Request locking
PointerLock.requestPointerLock(canvas);
}
}, false);
//Listen for pointer locking
PointerLock.addPointerLockExchangeEventListener(document, function(e) {
//Did the pointer just go from unlocked to locked?
if(!!PointerLock.pointerLockElement()) {
//Yep! Add mousemove listener
PointerLock.addMouseMoveEventListener(document, rotatePlayer, false);
}
else { //Nope. Remove mouse move listener
PointerLock.removeMouseMoveEventListener(document, rotatePlayer);
}
}, false);
mainLoop();
});
}
//Rotate the player when the mouse is moved
function rotatePlayer(e) {
var player = GameInfo.player;
player.rotate(e.movementX, e.movementY);
}
function start() {
var peer = new Peer({key: cs.peerJSApiKey});
var server = sessionStorage.getItem("server");
//Should we connect to a server?
if(server !== null) {
//Yep. Connect to it
var conn = peer.connect(server);
conn.on("open", function() {
conn.send("Hello, I am " + peer.id);
});
conn.on("error", function(err) {
console.log(err);
});
}
else {
//No server specified: We are the server
peer.on("open", function() {
console.log("Game server started with ID: " + peer.id);
});
peer.on("error", function(err) {
console.log("Error starting game server: " + err);
});
peer.on("connection", function(conn) {
conn.on("data", function(data){
console.log(data);
});
});
}
webGLStart();
}
start();
});