-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (152 loc) · 5.83 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
/*
* Driver file
*/
$(document).ready(function() {
socket = io.connect();
var entry_coordinates = {x:-1, y:-1};
game = new Game();
game.init();
init_stats();
init_world();
init_client();
render_clouds();
// THREEX utilities init
THREEx.WindowResize(WORLD.renderer, WORLD.camera);
/*******************
* ACTION BINDINGS *
*******************/
// When quitting the game, send this player id to the server
window.onbeforeunload = function() {
socket.emit('hello', {"id": id}); // disconnect signal
};
$(document)
.keydown(function(e) {
e.preventDefault();
var k;
k = (e.keyCode ? e.keyCode : e.which);
keys[k] = true;
})
.keyup(function(e) {
e.preventDefault();
var k;
k = (e.keyCode ? e.keyCode : e.which);
if (k in keys) {
keys[k] = false;
}
})
.click(function(e) {
emit_attack();
});
// closing user
function disable_listeners() {
$(document).unbind('click');
}
/****************
* POINTER LOCK *
****************/
$("#pointerLock").click(function() {
var canvas = $("#pointerLock").get()[0];
canvas.requestPointerLock = canvas.requestPointerLock ||
canvas.mozRequestPointerLock ||
canvas.webkitRequestPointerLock;
canvas.requestPointerLock();
});
function changeCallback(e) {
var canvas = $("#pointerLock").get()[0];
if (document.pointerLockElement === canvas ||
document.mozPointerLockElement === canvas ||
document.webkitPointerLockElement === canvas) {
document.addEventListener("mousemove", moveCallback, false);
} else {
document.removeEventListener("mousemove", moveCallback, false);
}
}
function moveCallback(e) {
move_x = e.movementX || e.mozMovementX || e.webkitMovementX || 0;
move_y = e.movementY || e.mozMovementY || e.webkitMovementY || 0;
}
document.addEventListener('pointerlockchange', changeCallback, false);
document.addEventListener('mozpointerlockchange', changeCallback, false);
document.addEventListener('webkitpointerlockchange', changeCallback, false);
/***********
* SIGNALS *
***********/
socket.on('setupComplete', function(assigned_id) {
game.start();
});
socket.on('assignID', function(assigned_id) {
id = assigned_id;
});
/*
* Loads all the connected players from the central server
*/
socket.on('setupPlayers', function(server_entities) {
for (var p in server_entities) {
game.entities.push(
new TYPE.Player(game,
p,
new THREE.Mesh(WORLD.player_geometry,
WORLD.player_material),
new THREE.Vector3(0,0,0), // direction
0, // velocity
server_entities[p].active));
game.entities[p].mesh.position.x = server_entities[p].mesh.position.x;
game.entities[p].mesh.position.y = server_entities[p].mesh.position.y;
game.entities[p].mesh.position.z = server_entities[p].mesh.position.z;
game.entities[p].mesh.rotation.y = server_entities[p].mesh.rotation.y;
game.entities[p].mesh.castShadow = true;
game.entities[p].mesh.receiveShadow = true;
if (server_entities[p].active === true) {
WORLD.scene.add(game.entities[p].mesh);
}
}
WORLD.player = game.entities[game.entities.length-1];
WORLD.player.mesh.add(WORLD.camera);
});
socket.on('addPlayer', function(new_id) {
game.entities.push(new TYPE.Player(game, new_id,
new THREE.Mesh(WORLD.player_geometry,
WORLD.player_material),
new THREE.Vector3(0,0,0), // direction
0, // velocity
true));
game.entities[game.entities.length-1].mesh.castShadow = true;
game.entities[game.entities.length-1].mesh.receiveShadow = true;
WORLD.scene.add(game.entities[game.entities.length-1].mesh);
});
/*
* Signal when ANOTHER players disconnects
*/
socket.on('player_disconnect', function(id) {
game.entities[id].active = false;
WORLD.scene.remove(game.entities[id].mesh);
//alert(id);
});
/**************************
* Updating OTHER Players *
**************************/
socket.on('updatePlayer', function(data) {
game.entities_updates.push(data);
});
// new bullet from another player
socket.on('new_bullet', function(bullet_data) {
game.bullets.push(new TYPE.Bullet(game,
new THREE.Vector3(bullet_data.dir.x,
bullet_data.dir.y,
bullet_data.dir.z),
new THREE.Mesh(WORLD.bullet_geometry,
WORLD.bullet_material))
);
game.bullets[game.bullets.length-1].mesh.position = bullet_data.pos;
WORLD.scene.add(game.bullets[game.bullets.length-1].mesh);
});
socket.on('hit', function(player_data) {
//if myself
if (player_data.id = id) {
alert("You died! Refresh the page to try again.");
}
game.entities[player_data.id].active = false;
WORLD.scene.remove(game.entities[player_data.id].mesh);
disable_listeners(); // prevent users from doing anything afterwards
});
});