add asteroids

This commit is contained in:
Asraelite 2016-03-22 18:34:11 +00:00
parent 0ceea5f4af
commit 0aa259b874
17 changed files with 246 additions and 37 deletions

View file

@ -16,11 +16,16 @@ function Net() {
this.socket.on('update', function(data) {
game.world.update(data);
window.q = data;
});
this.socket.on('world', function(data) {
game.world.clear();
game.world.playerShipId = data;
console.log(data);
game.world.playerShipId = data.playerShipId;
for (var i in data.bodies) {
game.world.add(data.bodies[i]);
}
});
};
@ -31,4 +36,8 @@ function Net() {
right: move[2]
});
}
this.send = function(msg, data) {
this.socket.emit(msg, data);
}
}

View file

@ -0,0 +1,22 @@
function renderAsteroid(pallet, body) {
var pos = body.getPos();
var x = pos.x * SCALE;
var y = pos.y * SCALE;
var vx = -game.world.getCenter().x;
var vy = -game.world.getCenter().y;
pallet.view(x + vx, y + vy, false, body.r);
var context = pallet.context;
var points = body.frame[0];
context.beginPath();
context.moveTo(points[0][0], points[0][1]);
for (var i = 1; i < points.length; i++) {
context.lineTo(points[i][0], points[i][1]);
}
context.closePath();
context.strokeStyle = '#fff';
context.stroke();
pallet.restore();
}

View file

@ -51,6 +51,8 @@ function Renderer() {
if (body.bodyType == 'ship') {
renderShip(pallet, body);
} else if (body.bodyType == 'asteroid') {
renderAsteroid(pallet, body);
} else {
pallet.rect('#338', body.x, body.y, 10, 10);
}

View file

@ -6,8 +6,8 @@ function renderShip(pallet, ship) {
var thr8 = game.assets.images.ships[ship.hull].thrust8;
//pallet.view(ship.x, ship.y, false, ship.r);
var pos = ship.getPos();
var x = pos.x + 16;
var y = pos.y + 16;
var x = pos.x;
var y = pos.y;
var vx = -game.world.getCenter().x;
var vy = -game.world.getCenter().y;

View file

@ -1,3 +0,0 @@
function Body() {
}

View file

@ -0,0 +1,21 @@
function Asteroid(data) {
this.id = data.id;
this.x = data.delta[0];
this.y = data.delta[1];
this.r = data.delta[2];
this.bodyType = 'asteroid';
this.frame = data.frame;
this.getPos = function() {
return {
x: this.x,
y: this.y
};
}
this.update = function(data) {
this.x = data[0];
this.y = data[1];
this.r = data[4];
}
}

View file

@ -1,8 +1,11 @@
function Ship(id) {
this.id = id;
this.x = 0;
this.y = 0;
this.r = 0;
function Ship(data) {
this.id = data.id;
this.x = data.delta[0];
this.y = data.delta[1];
this.r = data.delta[4];
this.xvel = data.delta[2];
this.yvel = data.delta[3];
this.rvel = data.delta[5];
this.hull = '01';
this.move = [];
this.lastMove = [];
@ -21,6 +24,14 @@ function Ship(id) {
}
}
this.update = function (data) {
this.x = data[0];
this.y = data[1];
this.xvel = data[2];
this.yvel = data[3];
this.r = data[4];
}
this.updateMove = function() {
if (JSON.stringify(this.move) != JSON.stringify(this.lastMove) || true) {
game.net.update(this.move);

View file

@ -22,6 +22,19 @@ function World() {
return { x: x, y: y };
}
this.add = function(data) {
var body;
if (data.type == 'asteroid') body = new Asteroid(data);
if (data.type == 'ship') body = new Ship(data);
if (data.type == 'structure') body = new Structure(data);
this.bodies[body.id] = body;
}
this.remove = function(id) {
delete this.bodies[id];
}
this.clear = function() {
this.bodies = {};
this.playerShip = false;
@ -32,15 +45,14 @@ function World() {
for (var id in data) {
if (!this.bodies[id]) {
this.bodies[id] = new Ship(id);
game.net.send('requestBodyData', id);
continue;
}
var body = this.bodies[id];
body.x = data[id][0];
body.y = data[id][1];
body.r = data[id][2];
body.update(data[id]);
if (data[id].destroy) delete this.bodies[id];
}
}
}