add basic updating to client on physics

This commit is contained in:
Asraelite 2016-03-21 23:45:27 +00:00
parent 95e0f6b710
commit 1027d980c1
15 changed files with 160 additions and 13 deletions

View file

@ -19,6 +19,7 @@ function Game() {
this.state = 'connecting';
this.net = new Net();
this.world = new World();
this.renderer = new Renderer();
this.tick = function() {

View file

@ -13,5 +13,9 @@ function Net() {
game.connected = false;
game.state = 'disconnected';
});
this.socket.on('update', function(data) {
game.world.update(data);
});
};
}

View file

@ -17,6 +17,26 @@ function Renderer() {
pallet.clear();
pallet.fill('#000');
this.renderGrid();
for (var id in game.world.bodies) {
var body = game.world.bodies[id];
pallet.rect('#338', body.x, body.y, 10, 10);
}
}
this.renderGrid = function() {
var ox = (game.world.playerShip.x || 0) % 50;
var oy = (game.world.playerShip.y || 0) % 50;
for (var x = -50 + ox; x < canvas.width + 50; x += 50) {
for (var y = -50 + oy; y < canvas.height + 50; y += 50) {
pallet.outline('#0a0a0a', x, y, 51, 51, 1);
}
}
}
pallet.fillScreen();

View file

@ -0,0 +1,5 @@
function Ship(id) {
this.id = id;
this.x = 0;
this.y = 0;
}

View file

@ -0,0 +1,20 @@
function World() {
this.bodies = {};
this.playerShip = false;
this.update = function(data) {
for (var id in data) {
if (!this.bodies[id]) {
this.bodies[id] = new Ship(id);
this.playerShip = this.bodies[id];
}
var body = this.bodies[id];
body.x = data[id][0];
body.y = data[id][1];
body.r = data[id][2];
}
}
}