-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
147 lines (130 loc) · 4.25 KB
/
common.js
File metadata and controls
147 lines (130 loc) · 4.25 KB
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
function playSound(sound){
var audio = new Audio('sounds/' + sound + '.mp3');
audio.play();
}
function writeText(text) {
ctx.font = "30px Monospace";
ctx.textAlign = "center";
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
}
function writePoints(text) {
ctx.font = "12px Monospace";
ctx.textAlign = "left";
ctx.fillText(text, 12, canvas.height);
}
function writeSubText(text) {
ctx.font = "20px Monospace";
ctx.textAlign = "center";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 30);
}
function writeScore(text) {
ctx.font = "20px Monospace";
ctx.textAlign = "center";
ctx.fillText(text, canvas.width / 2, 20);
}
var initialSeconds = Math.floor(Date.now() / 1000)
function writeTime() {
var seconds = Math.floor(Date.now() / 1000) - initialSeconds;
ctx.font = "12px Monospace";
ctx.textAlign = "right";
ctx.fillText(seconds, canvas.width - 12, canvas.height);
}
function clearCanvas(canvas, canvasContext) {
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
}
var controls = {
rightPressed: false,
leftPressed: false,
upPressed: false,
downPressed: false
}
//Listening websocket events
var ws = new WebSocket("ws://localhost:2055");
var datas = "";
//Websocket communication
if ("WebSocket" in window) {
//if(!isConnection)
//console.log("WebSocket is supported by your Browser!");
// Let us open a web socket
ws.onopen = function() {
// Web Socket is connected, send data using send()
ws.send('{ "type": "authentication", "moduleId": "vgames", "moduleSecret": "qwerty" }');
// ws.send('{ "type":"setCapabilities", "kaiId": "default", "pyrData": true }');
console.log("Message is sent...");
};
ws.onmessage = function (evt) {
datas = evt.data;
console.log("Message is received..." + datas);
doMove();
};
ws.onclose = function(event) {
// websocket is closed.
alert("Connection is closed...\nEnsure that the server is running and started the hyperIMU.");
// if (event.wasClean) {
// alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
// } else {
// alert('[close] Connection died');
// }
};
ws.onerror = function(error) {
alert(`[error] ${error.message}`);
};
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
function doMove(){
//For Jumping
if(datas != ""){
var obj = JSON.parse(datas);
// if(obj.type == "incomingData"){
// if(obj.data[0].type == "pyrData"){
if(obj["y"] < -3){ //Moving left
controls.leftPressed = true;
}else if(obj["y"] > 3){ //Moving right
controls.rightPressed = true;
}else if(obj["x"] > 3){ //Moving down
controls.downPressed = true;
}else if(obj["x"] < -3){ //Moving up
controls.upPressed = true;
}else{
controls.rightPressed = false;
controls.leftPressed = false;
controls.downPressed = false;
controls.upPressed = false;
}
// }
// }
}
}
console.log("listening to arrows");
document.addEventListener("keydown", keyDownHandler);
document.addEventListener("keyup", keyUpHandler);
function keyDownHandler(e) {
if (e.keyCode == 39 || e.keyCode == 68) {
controls.rightPressed = true;
}
else if (e.keyCode == 37 || e.keyCode == 65) {
controls.leftPressed = true;
}
else if (e.keyCode == 38 || e.keyCode == 87) {
controls.upPressed = true;
}
else if (e.keyCode == 40 || e.keyCode == 83) {
controls.downPressed = true;
}
}
function keyUpHandler(e) {
if (e.keyCode == 39 || e.keyCode == 68) {
controls.rightPressed = false;
}
else if (e.keyCode == 37 || e.keyCode == 65) {
controls.leftPressed = false;
}
else if (e.keyCode == 38 || e.keyCode == 87) {
controls.upPressed = false;
}
else if (e.keyCode == 40 || e.keyCode == 83) {
controls.downPressed = false;
}
}