forked from elbowdonkey/impsock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpsock.js
101 lines (92 loc) · 4.25 KB
/
impsock.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
ig.module(
'plugins.impsock'
)
.requires(
'impact.impact'
)
.defines(function(){
ig.Impsock = ig.Class.extend({
init: function(game) {
this.host = "http://dev.game.captaindashing.com:8080";
this.game = game;
this.sessionId = null;
this.socket = null;
this.clients = null;
this.otherClients = [];
this.disconnectedClients = [];
this.establishSocket();
},
establishSocket: function() {
var game = this.game;
var self = this;
this.socket = io.connect(this.host);
this.socket.on('connect', function(){
console.info("Socket connected");
});
this.socket.on('message', function(messageFromServer){
self.processMessage(messageFromServer);
});
},
/* Expects entire 'character' entity */
broadcast: function(entity) {
var properties = entity.syncableProperties();
var message = {"action": "broadcast", "entity": properties};
this.socket.emit('message', message);
},
processMessage: function(message) {
/* Sample data inside message looks like this:
{
action: 'broadcast',
entity: {
accel: {x: 0, y:0}, //player acceleration - movement calculated locally
vel: {x: 0, y: 0}, //player velocity - movement calculated locally
flip: true, //direction player is facing
name: "CptDashing333", //internal identifier
displayName: "BillyTheKid", //name that is displayed, originally set to .name
pos: {x: 0, y: 0}, //absolute in-game position, used to correct .accel and .vel
sessionid: "dsfd342fjdsf343943$#43fdsf" //server assigned sessionid - eventually will resume lost connections
}
}
*/
//Append new chat message to local user chat textarea
if (message.action == "chatMessage") {
//Simply append to textarea
var FromName = message.entity.name;
if (message.entity.displayName){
FromName = message.entity.displayName
}
//Wildly insecure I know, this is only a demo though, not meant for production use
if (FromName == "SYS"){
$("#ChatWindow").append( "<strong class='from-name system-message'>" + message.chatMessage.replace(/(<([^>]+)>)/ig,"") + "</strong>" + '<br />');
}else{
FromName = "<strong class='from-name'>" + escape(FromName) + "</strong>: "
$("#ChatWindow").append(FromName + message.chatMessage.replace(/(<([^>]+)>)/ig,"") + '<br />');
}
//Scroll the window down nicely if we fill it up
$("#ChatWindow").animate({ scrollTop: $("#ChatWindow").prop("scrollHeight") - $("#ChatWindow").height() }, 500);
return;
}
//Check the clients list to see if this name is known, if it's not, spawn that player in
var found = false;
for (var i = 0; this.otherClients.length > i; i++){
if (message.entity.name == this.otherClients[i]){
found = true;
//If the action message is from a player disconnect, remove them from the array
if (message.action == "remove") {
this.otherClients.remove(i);
this.game.disconnectHandler(message);
return;
}
}
}
if (!found && message.entity.pos != undefined){
//console.log("Adding new client named:" + message.entity.name);
this.otherClients.push(message.entity.name);
this.game.spawnOtherPlayer(message);
//Update your position so that the new player sees you
this.game.getEntityByName(this.game.localUser).forceUpdate = true;
}
this.game.updateOtherPlayer(message);
}
});
});