forked from therauli/WebNaali
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.js
111 lines (90 loc) · 2.39 KB
/
global.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
/*
global.js - Stuff to handle things. Handles general avatar stuff.
*/
var canvas;
var width;
var height;
var myid;
var timerid;
var moves = new Array()
var old_moves = new Array()
var rightMouseDown = false;
var old_mousex = 0.0;
var old_mousey = 0.0;
var handlers = {};
function setId() {
myid = arguments[0]['id'];
}
function addmove(move) {
moves.push(move)
}
function isin(value, list) {
for (i = 0; i < list.length; i++) {
if (value == list[i]) {
return true;
}
}
return false;
}
function checkmove() {
/*
Does not work well except for walking and stopping. Need to
rethink how keyboard mapping should be done.
*/
for (i in moves) {
var move = moves[i];
if (!(isin(move, old_moves))) {
action = move.split(',')[0];
dir = move.split(',')[1];
//console.log('MOVE ' + move);
ws.send(JSON.stringify(["Action", {action: action, params: [dir], id: myid}]));
}
}
for (i in old_moves) {
var move = old_moves[i];
if (!(isin(move, moves))) {
action = move.split(',')[0];
if (action == 'Rotate') {
action = 'StopRotate';
} else if (action == 'Move') {
action = 'Stop';
}
dir = move.split(',')[1];
//console.log('STOP ' + move);
ws.send(JSON.stringify(["Action", {action: action, params: dir, id: myid}]));
}
}
old_moves = moves.slice(0);
moves = [];
}
function sendSignal(signal) {
var action = signal.split(':')[0];
var id = signal.split(':')[1];
//console.log('sending: ' + action + ' to ' + id);
}
function connectHandler(signal, id) {
handlers[signal] = handlers[signal] || [];
handlers[signal].push(id);
}
function chatMessage(params) {
var sender = params['sender'];
var message = params['msg'];
//console.log('GOT MESSAGE ' + sender + ": " + message);
//FIXME use jquery or somthing smart to do this.
var content = "";
content = document.getElementById("chat").innerHTML;
content = content.concat("<b>" + sender + ":</b> " + message + "<br>");
document.getElementById("chat").innerHTML = content;
}
window.onload = function() {
$('#chatinput').submit(function () {
var message = $('#usermsg').val();
$(':text', '#chatinput').val('');
if (message.trim() == "") {
return false;
}
//console.log(message);
ws.send(JSON.stringify(["chatMessage", {sender: "WebSocket", msg: message}]));
return false;
});
}