From 2fffe574c1a5c482b966d8f42d5e7995eaec05be Mon Sep 17 00:00:00 2001 From: Asraelite Date: Tue, 22 Mar 2016 19:20:50 +0000 Subject: [PATCH] improve multiplayer --- public/js/starbugs/main.js | 2 ++ public/js/starbugs/net.js | 6 +++++- public/js/starbugs/render/ships.js | 5 +++-- public/js/starbugs/world/asteroid.js | 17 ++++++++++++++--- public/js/starbugs/world/ship.js | 14 ++++++++++++++ public/js/starbugs/world/world.js | 18 ++++++++++++------ server/game/player.js | 6 +++++- server/game/room/index.js | 6 +++++- server/game/room/world/index.js | 15 ++++++++++----- server/game/room/world/ship.js | 6 ++++++ 10 files changed, 76 insertions(+), 19 deletions(-) diff --git a/public/js/starbugs/main.js b/public/js/starbugs/main.js index 519b043..f89cc9f 100644 --- a/public/js/starbugs/main.js +++ b/public/js/starbugs/main.js @@ -39,6 +39,8 @@ function Game() { self.input.clear(); + self.world.tick(); + requestAnimationFrame(self.tick); } } diff --git a/public/js/starbugs/net.js b/public/js/starbugs/net.js index 1de6a50..7f61597 100644 --- a/public/js/starbugs/net.js +++ b/public/js/starbugs/net.js @@ -2,7 +2,7 @@ function Net() { this.socket; this.connect = function() { - this.socket = io.connect('http://localhost:8080'); + this.socket = io.connect('/'); this.socket.on('connect', function() { game.connected = true; @@ -27,6 +27,10 @@ function Net() { game.world.add(data.bodies[i]); } }); + + this.socket.on('create', function(data) { + game.world.add(data); + }); }; this.update = function(move) { diff --git a/public/js/starbugs/render/ships.js b/public/js/starbugs/render/ships.js index c1c524e..5ec9533 100644 --- a/public/js/starbugs/render/ships.js +++ b/public/js/starbugs/render/ships.js @@ -1,6 +1,7 @@ function renderShip(pallet, ship) { var img = game.assets.images.ships[ship.hull].hull; - var col = game.assets.images.ships[ship.hull].teamb; + var teama = game.assets.images.ships[ship.hull].teama; + var teamb = game.assets.images.ships[ship.hull].teamb; var thr0 = game.assets.images.ships[ship.hull].thrust0; var thr5 = game.assets.images.ships[ship.hull].thrust5; var thr8 = game.assets.images.ships[ship.hull].thrust8; @@ -12,7 +13,7 @@ function renderShip(pallet, ship) { var vy = -game.world.getCenter().y; pallet.view(x + vx, y + vy, false, ship.r); - pallet.image(col, 0, 0, 0); + pallet.image(ship.team == 'a' ? teama : teamb, 0, 0, 0); pallet.image(img, 0, 0, 0); pallet.image(ship.move[0] ? thr8 : thr0, 0, 0, 0); diff --git a/public/js/starbugs/world/asteroid.js b/public/js/starbugs/world/asteroid.js index 68d1729..028fe74 100644 --- a/public/js/starbugs/world/asteroid.js +++ b/public/js/starbugs/world/asteroid.js @@ -1,8 +1,8 @@ function Asteroid(data) { this.id = data.id; - this.x = data.delta[0]; - this.y = data.delta[1]; - this.r = data.delta[2]; + this.x = data[0]; + this.y = data[1]; + this.r = data[2]; this.bodyType = 'asteroid'; this.frame = data.frame; @@ -16,6 +16,17 @@ function Asteroid(data) { 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.rvel = data[5]; + } + + this.tick = function() { + this.x += this.xvel * 10; + this.y += this.yvel * 10; + this.r += this.rvel * 10; + this.xvel *= 0.98; + this.yvel *= 0.98; } } diff --git a/public/js/starbugs/world/ship.js b/public/js/starbugs/world/ship.js index a9024fe..54d48d5 100644 --- a/public/js/starbugs/world/ship.js +++ b/public/js/starbugs/world/ship.js @@ -1,5 +1,6 @@ function Ship(data) { this.id = data.id; + this.team = data.team; this.x = data.delta[0]; this.y = data.delta[1]; this.r = data.delta[4]; @@ -30,6 +31,8 @@ function Ship(data) { this.xvel = data[2]; this.yvel = data[3]; this.r = data[4]; + this.rvel = data[5]; + if(this != game.world.playerShip) this.move[0] = data[6]; } this.updateMove = function() { @@ -38,4 +41,15 @@ function Ship(data) { this.lastMove = Array.apply(0, this.move); // Bloody Javascript. } } + + this.tick = function() { + this.x += this.xvel * 10; + this.y += this.yvel * 10; + //this.r += this.rvel * 10; + this.xvel *= 0.98; + this.yvel *= 0.98; + + if (this.move[1]) this.rvel -= 0.01; + if (this.move[2]) this.rvel += 0.01; + } } diff --git a/public/js/starbugs/world/world.js b/public/js/starbugs/world/world.js index 0d5bbdc..31c324b 100644 --- a/public/js/starbugs/world/world.js +++ b/public/js/starbugs/world/world.js @@ -20,25 +20,25 @@ function World() { y += Math.sin(a + r) * d; 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; - } + }; this.update = function(data) { this.playerShip = this.bodies[this.playerShipId]; @@ -54,5 +54,11 @@ function World() { if (data[id].destroy) delete this.bodies[id]; } - } + }; + + this.tick = function() { + for (var i in this.bodies) { + this.bodies[i].tick(); + } + }; } diff --git a/server/game/player.js b/server/game/player.js index 69d8627..5aa8dca 100644 --- a/server/game/player.js +++ b/server/game/player.js @@ -25,12 +25,16 @@ class Player { }); } + send(msg, data) { + this.connection.send(msg, data); + } + sendWorld(data) { this.connection.send('world', data); } sendUpdate() { - if (Object.keys(this.delta).length == 0) return; + if (Object.keys(this.delta).length == 0 || Math.random() < 0) return; this.connection.send('update', this.delta); this.delta = {}; } diff --git a/server/game/room/index.js b/server/game/room/index.js index fdac584..c047d6f 100644 --- a/server/game/room/index.js +++ b/server/game/room/index.js @@ -7,7 +7,7 @@ class Room { this.players = new Set(); this.teamA = new Set(); this.teamB = new Set(); - this.world = new World(); + this.world = new World(this); this.name = (Math.random() * 100000 | 0).toString(36); this.start(); @@ -42,6 +42,10 @@ class Room { self.players.forEach(player => player.sendUpdate()); } + broadcast(msg, data) { + this.players.forEach(player => player.send(msg, data)); + } + sendWorld(player) { let data = { playerShipId: player.ship.id, diff --git a/server/game/room/world/index.js b/server/game/room/world/index.js index 66ca51e..e777415 100644 --- a/server/game/room/world/index.js +++ b/server/game/room/world/index.js @@ -5,13 +5,14 @@ const Physics = require('./physics.js'); const Ship = require('./ship.js'); class World { - constructor() { + constructor(room) { this.physics = new Physics(); this.bodies = new Set(); this.structures = new Set(); this.asteroids = new Set(); this.ships = new Map(); this.players = new Set(); + this.room = room; } addPlayer(player) { @@ -27,14 +28,18 @@ class World { addShip(ship) { this.ships.set(ship.player, ship); - this.bodies.add(ship); - this.physics.createBody(ship); + this.addBody(ship); } addAsteroid(asteroid) { this.asteroids.add(asteroid); - this.bodies.add(asteroid); - this.physics.createBody(asteroid); + this.addBody(asteroid); + } + + addBody(body) { + this.bodies.add(body); + this.physics.createBody(body); + this.room.broadcast('create', body.packFull()); } applyDelta(body, data) { diff --git a/server/game/room/world/ship.js b/server/game/room/world/ship.js index 7eb458b..6c9e3db 100644 --- a/server/game/room/world/ship.js +++ b/server/game/room/world/ship.js @@ -47,6 +47,12 @@ class Ship extends Body { if (data.right) { this.applyTorque(this.power.rotation); } + + this.thrust = { + forward: data.forward, + left: data.left, + right: data.right + }; } packTypeDelta() {