Skip to content

Commit

Permalink
Fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjohnward committed May 5, 2016
1 parent ea42388 commit 643bd2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion client/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script type="text/javascript" src="./js/phaser.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body onresize="resize()">
<body>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<div class="chat">
<ul id="messages"></ul>
Expand Down
28 changes: 15 additions & 13 deletions client/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ $('form').submit(function(){
});
server.socket.on('joined', function(user) {
$('#messages').append($('<li>').text(user + " just joined the chat."));
users[user.name] = user;
console.log(user.name + " just joined the chat");
httpGetAsync(server.ip + '/user/' + user, function(data) {
users[user] = data;
});
console.log(user + " just joined the chat");
})
server.socket.on('left', function(user) {
$('#messages').append($('<li>').text(user + " just left the chat."));
Expand All @@ -93,7 +95,7 @@ server.socket.on('left', function(user) {
server.socket.on('message', function(msg){
$('#messages').append($('<li>').text(msg));
});
server.socket.on('move', function(move) {
server.socket.on('user_move', function(move) {
users[move.user].x = move.x;
users[move.user].y = move.y;
users[move.user].rotation = move.rotation;
Expand Down Expand Up @@ -306,42 +308,42 @@ function controls() {
if (game.input.keyboard.isDown(Phaser.Keyboard.W)) {
player_sprite.move("up");
var pos = {
user: player_sprite.name,
user: player,
x: player_sprite.x,
y: player_sprite.y,
rotation: player_sprite.rotation
}
server.socket.emit('move', pos);
server.socket.emit('user_move', pos);
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.A)) {
player_sprite.move("left");
var pos = {
user: player_sprite.name,
user: player,
x: player_sprite.x,
y: player_sprite.y,
rotation: player_sprite.rotation
}
server.socket.emit('move', pos);
server.socket.emit('user_move', pos);
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.S)) {
player_sprite.move("down");
var pos = {
user: player_sprite.name,
user: player,
x: player_sprite.x,
y: player_sprite.y,
rotation: player_sprite.rotation
}
server.socket.emit('move', pos);
server.socket.emit('user_move', pos);
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.D)) {
player_sprite.move("right");
var pos = {
user: player_sprite.name,
user: player,
x: player_sprite.x,
y: player_sprite.y,
rotation: player_sprite.rotation
}
server.socket.emit('move', pos);
server.socket.emit('user_move', pos);
}
}

Expand All @@ -357,8 +359,8 @@ function renderConnectedUsers(data) {
if(data != undefined) {
users = data;
}
if(player_sprite == undefined) {
player_sprite = new Sprite(users[player].model.name, data[player].model.url, 2, 2);
if(player_sprite == undefined && users[player] != undefined) {
player_sprite = new Sprite(users[player].model.name, users[player].model.url, 2, 2);
}
for (i in users) {
//console.log(users[i]);
Expand Down
24 changes: 18 additions & 6 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var CHARACTER_MODELS = [
}
];

var ROTATIONS = ["left", "right", "up", "down"];

var users = {};
var messages = [];
var world = {};
Expand Down Expand Up @@ -75,10 +77,15 @@ app.get('/world', function (req, res) {

app.get('/users', function (req, res) {
//res.send('Got a GET request at /user');
console.log('Got a GET request at /user');
console.log('Got a GET request at /users');
res.send(users);
});

app.get('/user/:username', function (req, res) {
console.log('Got a GET request at /user');
res.send(users[req.params.username]);
});

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
Expand All @@ -97,7 +104,6 @@ app.get('/login', function(req, res) {
}
else {
if(users[req.query.username].password == req.query.password) {
users[req.query.username].connect();
res.send("yes");
}
else {
Expand Down Expand Up @@ -128,7 +134,8 @@ app.get('/assets/player2.png', function(req, res) {
io.on('connection', function(socket){
var username = socket.handshake.query.username;
console.log("{connection} " + username);
io.emit('joined', users[username]);
users[username].connect();
io.emit('joined', username);
socket.on('disconnect', function(){
users[username].disconnect();
console.log("{disconnection} " + username);
Expand All @@ -141,9 +148,14 @@ io.on('connection', function(socket){
io.emit('message', fmsg);
});
socket.on('user_move', function(move) {
users[move.user].x = move.x;
users[move.user].y = move.y;
users[move.user].rotation = move.rotation;
//console.log(users);
//console.log(move);
if(users[move.user] != undefined) {
users[move.user].x = move.x;
users[move.user].y = move.y;
users[move.user].rotation = move.rotation;
console.log("{move} " + move.user + ": " + move.x + ", " + move.y + " (" + ROTATIONS[move.rotation] + ")")
}
io.emit('user_move', move);
});

Expand Down

0 comments on commit 643bd2f

Please sign in to comment.