-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgame.js
123 lines (98 loc) · 4.27 KB
/
game.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
$(document).ready(function() {
if(!("WebSocket" in window)){
$("#websocket").hide();
$("#weapons").hide();
$("body").append("<p>Your browser does not support HTML5</p>");
}else{
// default values for weapons
$("#size").val("4");
$('#colorpicker').farbtastic('#color');
$("#circle").attr('checked', true);
// socket, global variable if socket are enabled
var socket;
var host = "ws://localhost:12345/websocket/server.php";
try{
socket = new WebSocket(host);
console.log('WebSocket - status '+socket.readyState);
socket.onopen = function(msg){ console.log("Welcome - status "+this.readyState); };
socket.onclose = function(msg){ console.log("Disconnected - status "+this.readyState); };
// handling socket sent message to this client
socket.onmessage = function(msg){
var params = msg.data.split("|");
params[0] = params[0].substring(params[0].indexOf("#")+1, params[0].length);
// événements possibles {in, out, paint}
if (params[1]=="in") {
// création de la souris si elles n'existe pas deja
var $user_pointer = $(".user-pointer[title="+params[0]+"]");
if ($user_pointer.length==0) {
$("#thezone").append("<div class='user-pointer' title='"+params[0]+"'></div>");
var $user_pointer = $(".user-pointer[title="+params[0]+"]");
}
$user_pointer.attr("styles", $user_pointer.attr("style")+params[4]);
$user_pointer.css("left", (params[2]-$user_pointer.width()/2) +"px").css("top", (params[3]-$user_pointer.height()/2)+"px");
}
if (params[1]=="out") {
var $user_pointer = $(".user-pointer[title="+params[0]+"]");
$user_pointer.fadeOut('slow', function() {
$(this).remove();
});
}
if(params[1]=="paint") {
console.log("out!");
var $painted = $("<div class='user-pointer'></div>");
$("#thezone").append($painted);
$painted.attr("style", $painted.attr("style")+params[4]);
$painted.css("left", (params[2]-$painted.width()/2) +"px").css("top", (params[3]-$painted.height()/2)+"px");
}
console.log("Received: "+params);
};
} catch (ex) {
error("Event if websockets are enabled with your web browser, an error while handshaking was thrown.");
}
// when in and out of the zone : register event
var is_mouse_down=false;
$("#thezone").mouseover(function() {
$("#thezone").bind("mousemove", function(event) {
var x = event.pageX-$(this).offset().left;
var y = event.pageY-$(this).offset().top;
var prefix= (is_mouse_down)?"paint":"in";
var style= ";background-color:"+$("#color").val()+";height:"+$("#size").val()+"px;width:"+$("#size").val()+"px;"+ (($("#circle").is(":checked"))?"-moz-border-radius: "+$("#size").val()+"px; -webkit-border-radius: "+$("#size").val()+"px; border-radius: "+$("#size").val()+"px;":"") ;
var msg=prefix+"|"+x+"|"+y +"|"+style;
try{
socket.send(msg);
console.log('Sent: '+msg);
} catch (ex) {
error("Socket may be really busy, because sending your position has not been posible."+ex);
}
});
$("#thezone").bind("mouseup", function(event) { is_mouse_down=false; });
$("#thezone").bind("mousedown", function(event) {
is_mouse_down=true;
var x = event.pageX-$(this).offset().left;
var y = event.pageY-$(this).offset().top;
var style= ";background-color:"+$("#color").val()+";height:"+$("#size").val()+"px;width:"+$("#size").val()+"px;"+ (($("#circle").is(":checked"))?"-moz-border-radius: "+$("#size").val()+"px; -webkit-border-radius: "+$("#size").val()+"px; border-radius: "+$("#size").val()+"px;":"") ;
var msg="paint|"+x+"|"+y +"|"+style;
try{
socket.send(msg);
console.log('Sent: '+msg);
} catch (ex) {
error("Socket may be really busy, because sending your position has not been posible."+ex);
}
});
});
$("#thezone").mouseout(function() {
try{
var msg="out";
socket.send(msg);
console.log('Mouse out ???? Sent: '+msg);
} catch (ex) {
error("Socket may be really busy, because sending your position has not been posible."+ex);
}
$("#thezone").unbind("mousemove");
$("#thezone").unbind("click");
});
//socket.close();
//socket=null;
}
});
function error(msg) { $("#error").html(msg); }