Skip to content

Commit 6fa759b

Browse files
committed
Fix render order bug
1 parent dc97077 commit 6fa759b

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

client/js/game.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var user_sprites = {}
77
var player = "";
88
var world = {};
99
var synced = {};
10+
var sprites;
1011

1112
var CHARACTER_MODELS = [
1213
//"assets/white-character.png",
@@ -93,22 +94,27 @@ server.socket.on('joined', function(user) {
9394
});
9495
console.log(user + " just joined the chat");
9596
})
97+
98+
// Array Remove - By John Resig (MIT Licensed)
9699
server.socket.on('left', function(user) {
97100
$('#messages').append($('<li>').text(user + " just left the chat."));
98-
delete users[user];
99-
console.log(user + " just left the chat");
101+
if(user_sprites[user] != undefined) {
102+
user_sprites[user].sprite.kill();
103+
delete user_sprites[user];
104+
console.log(user + " just left the chat");
105+
}
100106
})
101107
server.socket.on('message', function(msg){
102108
$('#messages').append($('<li>').text(msg));
103109
});
104110
server.socket.on('user_move', function(move) {
105111
console.log("{move} " + move.user + ": " + move.x + ", " + move.y + " (" + ROTATIONS[move.rotation] + ")");
106-
107112
if(users[move.user] != undefined) {
108113
users[move.user].x = move.x;
109114
users[move.user].y = move.y;
110115
users[move.user].rotation = move.rotation;
111116
}
117+
playerSync();
112118
renderConnectedUsers();
113119
});
114120
server.socket.on('place', function(cmd) {
@@ -203,6 +209,7 @@ function coord_to_pixels(c) {
203209
}
204210

205211
function Sprite(name, url, x, y) {
212+
206213
this.move = function(direction) {
207214
if((this.moveTime + this.moveThreshold) < game.time.now) {
208215
if(ROTATIONS[this.rotation] != direction) {
@@ -263,25 +270,13 @@ function Sprite(name, url, x, y) {
263270
return this.y * ZOOM_FACTOR * TILE_SIZE + (TILE_SIZE*ZOOM_FACTOR)/2;
264271
}
265272
this.setX = function(x) {
266-
this.x = x; // ADD LERP
273+
this.x = x;
267274
}
268275
this.setY = function(y) {
269-
this.y = y; // ADD LERP
276+
this.y = y;
270277
}
271278
this.setRotation = function(rotation) {
272-
if(rotation == "up") {
273-
this.sprite.angle = 90;
274-
this.sprite.scale.x = Math.abs(this.sprite.scale.x);
275-
} else if(rotation == "down"){
276-
this.sprite.angle = -90;
277-
this.sprite.scale.x = Math.abs(this.sprite.scale.x);
278-
} else if(rotation == "left") {
279-
this.sprite.angle = 0;
280-
this.sprite.scale.x = -Math.abs(this.sprite.scale.x);
281-
} else if(rotation == "right") {
282-
this.sprite.angle = 0;
283-
this.sprite.scale.x = Math.abs(this.sprite.scale.x);
284-
}
279+
//this.rotation = rotation;
285280
}
286281

287282
this.name = name;
@@ -440,20 +435,23 @@ function renderConnectedUsers(data) {
440435
}
441436
for (i in users) {
442437
//console.log(users[i]);
443-
if(users[i].username != player) {
438+
if(users[i].username != player && users[i].connected) {
444439
if(user_sprites[users[i].username] == undefined) {
445440
console.log("Creating: \n" +
446441
"\tuser_sprites[" + users[i].username + "] = new Sprite(" + users[i].model.name + ", " + users[i].model.url + ", " + users[i].x + ", " + users[i].y + ");");
447442
user_sprites[users[i].username] = new Sprite(users[i].model.name, users[i].model.url, users[i].x, users[i].y);
448443
} else {
449444
console.log("Updating: \n" +
450445
"\tuser_sprites[" + users[i].username + "].setX(" + users[i].x + ");\n" +
451-
"\tuser_sprites[" + users[i].username + "].setY(" + users[i].y + ");\n"
446+
"\tuser_sprites[" + users[i].username + "].setY(" + users[i].y + ");\n" +
447+
"\tuser_sprites[" + users[i].username + "].setRotation(" + ROTATIONS[users[i].rotation] + ");\n"
452448
);
453449
user_sprites[users[i].username].setX(users[i].x);
454450
user_sprites[users[i].username].setY(users[i].y);
455-
user_sprites[users[i].username].setRotation(users[i].rotation);
451+
user_sprites[users[i].username].setRotation(ROTATIONS[users[i].rotation]);
456452
user_sprites[users[i].username].updateLocation();
453+
sprites.add(user_sprites[users[i].username].sprite);
454+
game.world.bringToTop(sprites);
457455
}
458456
}
459457
}
@@ -484,6 +482,7 @@ function preload() {
484482
}
485483

486484
function create() {
485+
sprites = game.add.group();
487486
httpGetAsync(server.ip + "/world", buildWorld);
488487
httpGetAsync(server.ip + "/users", renderConnectedUsers);
489488
cursors = game.input.keyboard.createCursorKeys();
@@ -521,7 +520,6 @@ function update() {
521520
}
522521

523522
function render() {
524-
playerSync();
525523
//playerSync();
526524
//renderConnectedUsers();
527525
//game.debug.cameraInfo(game.camera, 32, 32);

server/app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var tileset = [
3333
var ROTATIONS = ["left", "right", "up", "down"];
3434

3535
var users = {};
36+
var connected_users = {};
3637
var messages = [];
3738
var server = {};
3839

@@ -54,9 +55,11 @@ var user = function(_username, _password, _ip) {
5455
this.connect = function(_ip) {
5556
ip = _ip;
5657
connected = true;
58+
connected_users[this.username] = this;
5759
}
5860
this.disconnect = function() {
5961
connected = false;
62+
delete connected_users[this.username];
6063
}
6164
}
6265

@@ -91,6 +94,12 @@ app.get('/users', function (req, res) {
9194
res.send(users);
9295
});
9396

97+
app.get('/connected_users', function (req, res) {
98+
//res.send('Got a GET request at /user');
99+
console.log('Got a GET request at /connected_users');
100+
res.send(connected_users);
101+
});
102+
94103
app.get('/user/:username', function (req, res) {
95104
console.log('Got a GET request at /user');
96105
res.send(users[req.params.username]);

0 commit comments

Comments
 (0)