-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.js
114 lines (103 loc) · 3.19 KB
/
event.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
var CURR_X;
var CURR_Y;
$(function() {
// Local testing
// const protocol = window.location.protocol === 'https:' && 'wss://' || 'ws://';
// const host = 'localhost:8080/ws';
// var connection = new WebSocket(protocol + host);
// Remote server
const host = "wss://gnocchi-graphics.herokuapp.com/ws";
var connection = new WebSocket(host);
connection.onopen = function() {
console.log("Connected.");
};
connection.onmessage = function(m) {
var data = JSON.parse(m.data);
switch (data.type) {
case "RegisterData":
ID = data.id;
break;
case "ChunkData":
var key = JSON.stringify(data.index);
if (key in CHUNKS) break;
var chunk = new Chunk(data.size, data.index, data.blocks);
CHUNKS_NEW.push(key);
CHUNKS[key] = chunk;
break;
case "BlockData":
var block = new Block(data.block.material, data.block.location);
var blockData = new BlockData(data.index, block);
CURRENT_CHUNK.blockDiff(blockData);
break;
case "EntityData":
if (data.id === ID) {
vec3.set(POSITION, data.position[0], data.position[1], data.position[2]);
} else {
const entity = new Player(
data.id,
data.position
);
ENTITIES.set(data.id, entity);
}
break;
}
};
connection.onclose = function() {
console.log("Disconnected.");
connection = null;
};
webglCanvas.onclick = function (event) {
webglCanvas.requestPointerLock();
};
webglCanvas.onmousemove = function (event) {
if (document.pointerLockElement) {
rotate(event.movementX * getXAxis(), event.movementY * getYAxis());
}
};
window.onkeydown = function (event) {
var direction = vec3.create();
switch (event.which) {
case 38: // Up
case 87: // W
event.preventDefault();
vec3.normalize(direction, getDirection());
break;
case 65: // A
event.preventDefault();
var origin = vec3.create();
vec3.rotateY(direction, getDirection(), origin, Math.PI / 2.0);
vec3.set(direction, direction[0], 0.0, direction[2]);
vec3.normalize(direction, direction);
break;
case 40: // Down
case 83: // S
event.preventDefault();
vec3.negate(direction, getDirection());
vec3.normalize(direction, direction);
break;
case 68: // D
event.preventDefault();
origin = vec3.create();
vec3.rotateY(direction, getDirection(), origin, -Math.PI / 2.0);
vec3.set(direction, direction[0], 0.0, direction[2]);
vec3.normalize(direction, direction);
break;
case 9: // Tab
case 81: // Q
event.preventDefault();
vec3.set(direction, 0.0, 1.0, 0.0);
break;
case 16: // Shift
case 69: // E
event.preventDefault();
vec3.set(direction, 0.0, -1.0, 0.0);
break;
default:
return;
}
vec3.scale(direction, direction, getSpeed());
translate(direction);
const move = new MoveData(direction);
connection.send(JSON.stringify(move));
};
});