Browse files

server now exchange player positions. Init now is just for one player.

  • Loading branch information...
1 parent 794dd27 commit bc6d5dca34a368e23395a34ca2b3c80f8a9066d0 @alkavan committed Apr 6, 2012
Showing with 77 additions and 15 deletions.
  1. +1 −1 index.html
  2. +42 −5 js/script.js
  3. +34 −9 js/server.js
View
2 index.html
@@ -6,7 +6,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <title>MFG 0.2.2</title>
+ <title>MFG 0.3</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
View
47 js/script.js
@@ -30,6 +30,7 @@ var mfg = function () {
_scene_padding: 5,
_grid: {w: 60, h: 30, p: 16, active: null},
_socket: null,
+ _conn_id: undefined,
grid: null,
scene: null,
sprites: null,
@@ -42,8 +43,9 @@ var mfg = function () {
events: null,
eventsq: null,
player: null,
- players: new Array(),
+ players: [],
player_index: 0,
+ player_conn: {},
shots: null,
mousecross: null,
resouces: {
@@ -268,11 +270,11 @@ var mfg = function () {
this.initTicker();
- // Local Player Index
- var pos_data = mfg.player.sprite.x+','+mfg.player.sprite.y;
var package1 = new mfg.Package({type: 'LPI', data: mfg.player_index, length: mfg.player_index.length});
+ // Local Player Index
// First position
+ var pos_data = JSON.stringify({x: mfg.player.sprite.x, y: mfg.player.sprite.y});
var package2 = new mfg.Package({type: 'POS', data: pos_data, length: pos_data.length});
setTimeout(function () {
@@ -1008,6 +1010,41 @@ var mfg = function () {
var package = new mfg.Package(JSON.parse(event.data));
log('[WS] -> ['+package.type+'] '+package.data);
+
+ if (package.type == "CONNID")
+ {
+ var data = JSON.parse(package.data);
+ mfg._conn_id = data.id;
+ mfg.player_conn[mfg._conn_id] = mfg.player_index;
+ }
+
+ if(package.type == "HELO")
+ {
+ var data = JSON.parse(package.data);
+
+ if(data.id != mfg._conn_id)
+ {
+ mfg.players.push(new mfg.ent.Player());
+ mfg.player_conn[data.id] = mfg.players.length-1;
+
+ }
+ }
+
+ if (package.type == "POS")
+ {
+ var data = JSON.parse(package.data);
+
+ if (data.id != mfg._conn_id)
+ {
+ var player_index = mfg.player_conn[data.c];
+// log(mfg.players[player_index]);
+ if (typeof mfg.players[player_index] !== "undefined")
+ {
+ var player = mfg.players[player_index];
+ player.sprite.position(data.x, data.y);
+ }
+ }
+ }
};
socket.onclose = function(){
@@ -1052,7 +1089,7 @@ var mfg = function () {
// add player to sprite list
mfg.players[0] = new mfg.ent.Player();
- mfg.players[1] = new mfg.ent.Player();
+// mfg.players[1] = new mfg.ent.Player();
mfg.player = mfg.players[0];
var mousecross = mfg.scene.layers.ground_player
@@ -1281,7 +1318,7 @@ var mfg = function () {
if(moved)
{
- var pos_data = player.sprite.x+','+player.sprite.y;
+ var pos_data = JSON.stringify({x: player.sprite.x, y: player.sprite.y});
var package = new mfg.Package({type: 'POS', data: pos_data, length: pos_data.length});
mfg._socket.send(JSON.stringify(package));
}
View
43 js/server.js
@@ -5,6 +5,7 @@ var port = 9998;
var host = 'localhost';
var data = [];
+var player_conn_idx = [];
/**
* Network Data Package
@@ -64,7 +65,7 @@ server.addListener("clientError", function(){
server.addListener("connection", function(conn){
- console.log(conn);
+// console.log(conn);
// conn.send("** Connected as: user_"+conn.id);
// conn.broadcast("** "+conn.id+" connected");
@@ -78,28 +79,52 @@ server.addListener("connection", function(conn){
switch(package.type)
{
case "HELO":
- out = new mfg.Package({type: 'HELO', data: conn.id, length: conn.id.length});
+ var conn_id_str = JSON.stringify({id: conn.id});
+ out = new mfg.Package({type: 'CONNID', data: conn_id_str, length: conn_id_str.length});
+ server.send(conn.id, JSON.stringify(out));
+
+ out = new mfg.Package({type: 'HELO', data: conn_id_str, length: conn_id_str.length});
+ server.broadcast(JSON.stringify(out));
+
+ server.manager.forEach(function (c) {
+ if(conn.id != c.id)
+ {
+ var conn_id_str = JSON.stringify({id: c.id});
+ out = new mfg.Package({type: 'HELO', data: conn_id_str, length: conn_id_str.length});
+ server.send(conn.id, JSON.stringify(out));
+ }
+
+ });
+
break;
case "LPI":
tmp = '[C:'+conn.id+'] Local Player Index: '+package.data;
out = new mfg.Package({type: 'GOT', data: tmp.toString(), length: tmp.length});
+// server.send(conn.id, JSON.stringify(out));
break;
case "POS":
- tmp = '[C:'+conn.id+'] Player Position: '+package.data;
- out = new mfg.Package({type: 'GOT', data: tmp.toString(), length: tmp.length});
+ tmp = new String(package.data);
+// out = new mfg.Package({type: 'GOT', data: tmp.toString(), length: tmp.length});
+ var pos = JSON.parse(package.data);
+ pos.c = conn.id;
+
+ package.data = JSON.stringify(pos);
+ package.length = package.data.length;
+// server.send(conn.id, JSON.stringify(out));
+ server.broadcast(JSON.stringify(package));
break;
default:
tmp = '[C:'+conn.id+'] UNKNOWN COMMAND: '+package.type;
out = new mfg.Package({type: 'ERROR', data: tmp.toString(), length: tmp.length});
+ server.send(conn.id, JSON.stringify(out));
break;
}
- if(out.type == 'ERROR')
- {
- conn.emit("error", "-> "+tmp);
- }
- server.send(conn.id, JSON.stringify(out));
+// if(out.type == "ERROR")
+// {
+// conn.emit("error", "-> "+tmp);
+// }
});
conn.addListener("rejected", function(msg){

0 comments on commit bc6d5dc

Please sign in to comment.