add basic missiles
This commit is contained in:
parent
21a30ad212
commit
ac089f3e8e
19 changed files with 215 additions and 85 deletions
|
@ -18,11 +18,13 @@ class Game {
|
||||||
|
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.state = 'connecting';
|
this.state = 'connecting';
|
||||||
|
this.pingMode = 'fast';
|
||||||
|
|
||||||
this.input = new Input();
|
this.input = new Input();
|
||||||
this.net = new Net();
|
this.net = new Net();
|
||||||
this.world = new World();
|
this.world = new World();
|
||||||
this.renderer = new Renderer();
|
this.renderer = new Renderer();
|
||||||
|
this.player = new Player();
|
||||||
}
|
}
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
|
@ -30,9 +32,10 @@ class Game {
|
||||||
|
|
||||||
var ship = this.world ? this.world.playerShip : false;
|
var ship = this.world ? this.world.playerShip : false;
|
||||||
|
|
||||||
if(ship) {
|
if(this.player.ship) {
|
||||||
ship.move = [87, 65, 68].map(k => this.input.keys.held[k] || false);
|
let delta = this.player.packDelta();
|
||||||
ship.updateMove();
|
if (delta)
|
||||||
|
game.net.sendUpdate(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.input.clear();
|
this.input.clear();
|
||||||
|
|
|
@ -23,15 +23,14 @@ class Net {
|
||||||
|
|
||||||
this.socket.on('world', function(data) {
|
this.socket.on('world', function(data) {
|
||||||
game.world.clear();
|
game.world.clear();
|
||||||
game.world.playerShipId = data.playerShipId;
|
|
||||||
game.world.bounds = data.bounds;
|
game.world.bounds = data.bounds;
|
||||||
for (var i in data.bodies) {
|
for (var i in data.bodies) {
|
||||||
game.world.add(data.bodies[i]);
|
game.world.add(data.bodies[i]);
|
||||||
}
|
}
|
||||||
|
game.world.setPlayerShip(data.playerShipId);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('create', function(data) {
|
this.socket.on('create', function(data) {
|
||||||
console.log(data.id);
|
|
||||||
game.world.add(data);
|
game.world.add(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -40,12 +39,8 @@ class Net {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
update(move) {
|
sendUpdate(inputs) {
|
||||||
this.socket.emit('move', {
|
this.socket.emit('inputs', inputs);
|
||||||
forward: move[0],
|
|
||||||
left: move[1],
|
|
||||||
right: move[2]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send(msg, data) {
|
send(msg, data) {
|
||||||
|
|
19
public/js/starbugs/player.js
Normal file
19
public/js/starbugs/player.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
class Player {
|
||||||
|
constructor(name, team, ship) {
|
||||||
|
this.name = name;
|
||||||
|
this.team = team;
|
||||||
|
this.ship = ship;
|
||||||
|
|
||||||
|
this.lastInputs = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
packDelta() {
|
||||||
|
// W, A, D, Space
|
||||||
|
let inputs = [87, 65, 68];
|
||||||
|
inputs = inputs.map(k => game.input.keys.held[k] || false);
|
||||||
|
inputs[3] = game.input.keys.pressed[32] || false;
|
||||||
|
let delta = this.lastInputs == inputs ? false : inputs;
|
||||||
|
this.lastInputs = inputs;
|
||||||
|
return delta;
|
||||||
|
}
|
||||||
|
}
|
25
public/js/starbugs/render/body.js
Normal file
25
public/js/starbugs/render/body.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
Renderer.prototype.renderBody = (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, pos.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.lineWidth = 0.5;
|
||||||
|
context.strokeStyle = '#fff';
|
||||||
|
context.fillStyle = '#200';
|
||||||
|
context.fill();
|
||||||
|
context.stroke();
|
||||||
|
|
||||||
|
pallet.restore();
|
||||||
|
};
|
|
@ -66,6 +66,7 @@ class Renderer {
|
||||||
} else if (body.bodyType == 'asteroid') {
|
} else if (body.bodyType == 'asteroid') {
|
||||||
this.renderAsteroid(pallet, body);
|
this.renderAsteroid(pallet, body);
|
||||||
} else {
|
} else {
|
||||||
|
this.renderBody(pallet, body);
|
||||||
// Render structures, projectiles etc..
|
// Render structures, projectiles etc..
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
public/js/starbugs/world/missile.js
Normal file
14
public/js/starbugs/world/missile.js
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
class Missile extends Body {
|
||||||
|
constructor(data) {
|
||||||
|
super(data);
|
||||||
|
this.bodyType = 'missile';
|
||||||
|
}
|
||||||
|
|
||||||
|
updateType() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,12 +19,14 @@ class Physics {
|
||||||
let bodyDef = new b2BodyDef();
|
let bodyDef = new b2BodyDef();
|
||||||
bodyDef.userData = body;
|
bodyDef.userData = body;
|
||||||
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
|
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
|
||||||
|
bodyDef.angle = body.r || 0;
|
||||||
bodyDef.fixedRotation = false;
|
bodyDef.fixedRotation = false;
|
||||||
bodyDef.active = true;
|
bodyDef.active = true;
|
||||||
bodyDef.linearVelocity = new b2Vec2(0, 0);
|
bodyDef.linearVelocity = new b2Vec2(0, 0);
|
||||||
bodyDef.angularVelocity = 0;
|
bodyDef.angularVelocity = 0;
|
||||||
bodyDef.linearDamping = body.bodyType == 'ship' ? 0.01 : 0.003;
|
bodyDef.bullet = body.type == 'missile';
|
||||||
bodyDef.angularDamping = body.bodyType == 'ship' ? 0.01 : 0.003;
|
bodyDef.linearDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01;
|
||||||
|
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01;
|
||||||
bodyDef.type = body.bodyType == 'structure' ?
|
bodyDef.type = body.bodyType == 'structure' ?
|
||||||
b2Body.b2_staticBody : b2Body.b2_dynamicBody;
|
b2Body.b2_staticBody : b2Body.b2_dynamicBody;
|
||||||
bodyDef.allowSleep = false;
|
bodyDef.allowSleep = false;
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
class Player {
|
|
||||||
constructor(name, team, ship) {
|
|
||||||
this.name = name;
|
|
||||||
this.team = team;
|
|
||||||
this.ship = ship;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ class Ship extends Body {
|
||||||
this.team = data.team;
|
this.team = data.team;
|
||||||
this.name = data.name;
|
this.name = data.name;
|
||||||
this.hull = '01';
|
this.hull = '01';
|
||||||
this.move = [];
|
|
||||||
this.thrust = {};
|
this.thrust = {};
|
||||||
this.power = data.power;
|
this.power = data.power;
|
||||||
this.mounts = data.mounts;
|
this.mounts = data.mounts;
|
||||||
|
@ -19,13 +18,6 @@ class Ship extends Body {
|
||||||
this.bodyType = 'ship';
|
this.bodyType = 'ship';
|
||||||
}
|
}
|
||||||
|
|
||||||
updateMove() {
|
|
||||||
if (JSON.stringify(this.move) != JSON.stringify(this.lastMove) || true) {
|
|
||||||
game.net.update(this.move);
|
|
||||||
this.lastMove = Array.apply(0, this.move); // Bloody Javascript.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
updateType(data) {
|
updateType(data) {
|
||||||
this.thrust = {
|
this.thrust = {
|
||||||
forward: data[6]
|
forward: data[6]
|
||||||
|
@ -33,18 +25,18 @@ class Ship extends Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
if (this.move[0]) {
|
if (this.thrust.forward) {
|
||||||
var power = this.power.forward;
|
var power = this.power.forward;
|
||||||
var x = Math.cos(this.getPos().r) * power;
|
var x = Math.cos(this.getPos().r) * power;
|
||||||
var y = Math.sin(this.getPos().r) * power;
|
var y = Math.sin(this.getPos().r) * power;
|
||||||
this.applyForce(x, y);
|
this.applyForce(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.move[1]) {
|
if (this.thrust.left) {
|
||||||
this.applyTorque(-this.power.rotation);
|
this.applyTorque(-this.power.rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.move[2]) {
|
if (this.thrust.right) {
|
||||||
this.applyTorque(this.power.rotation);
|
this.applyTorque(this.power.rotation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ class World {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.bodies = {};
|
this.bodies = {};
|
||||||
this.playerShip = false;
|
this.playerShip = false;
|
||||||
this.playerShipId = false;
|
|
||||||
this.physics = new Physics();
|
this.physics = new Physics();
|
||||||
|
|
||||||
this.bounds = {
|
this.bounds = {
|
||||||
|
@ -37,6 +36,7 @@ class World {
|
||||||
if (data.type == 'asteroid') body = new Asteroid(data);
|
if (data.type == 'asteroid') body = new Asteroid(data);
|
||||||
if (data.type == 'ship') body = new Ship(data);
|
if (data.type == 'ship') body = new Ship(data);
|
||||||
if (data.type == 'structure') body = new Structure(data);
|
if (data.type == 'structure') body = new Structure(data);
|
||||||
|
if (data.type == 'missile') body = new Missile(data);
|
||||||
|
|
||||||
//if(data.type == 'ship') console.log(body);
|
//if(data.type == 'ship') console.log(body);
|
||||||
|
|
||||||
|
@ -59,8 +59,6 @@ class World {
|
||||||
};
|
};
|
||||||
|
|
||||||
update(data) {
|
update(data) {
|
||||||
this.playerShip = this.bodies[this.playerShipId];
|
|
||||||
|
|
||||||
for (var id in data) {
|
for (var id in data) {
|
||||||
if (!this.bodies[id]) {
|
if (!this.bodies[id]) {
|
||||||
game.net.send('requestBodyData', id);
|
game.net.send('requestBodyData', id);
|
||||||
|
@ -74,6 +72,11 @@ class World {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
setPlayerShip(id) {
|
||||||
|
this.playerShip = this.bodies[id];
|
||||||
|
game.player.ship = this.playerShip;
|
||||||
|
}
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
this.physics.step();
|
this.physics.step();
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,8 @@ class Connection {
|
||||||
this.player.name = data.name.slice(0, 20) || 'Fish';
|
this.player.name = data.name.slice(0, 20) || 'Fish';
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('move', data => {
|
socket.on('inputs', data => {
|
||||||
this.player.move(data);
|
this.player.updateInputs(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.server.assignRoom(this.player);
|
this.server.assignRoom(this.player);
|
||||||
|
|
|
@ -18,12 +18,8 @@ class Player {
|
||||||
this.room.remove(this);
|
this.room.remove(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
move(data) {
|
updateInputs(data) {
|
||||||
this.ship.move({
|
this.ship.updateInputs(data);
|
||||||
forward: data.forward || 0,
|
|
||||||
left: data.left || 0,
|
|
||||||
right: data.right || 0
|
|
||||||
});
|
|
||||||
this.lastAction = Date.now();
|
this.lastAction = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,10 @@ class Asteroid extends Body {
|
||||||
|
|
||||||
this.x = pos.x;
|
this.x = pos.x;
|
||||||
this.y = pos.y;
|
this.y = pos.y;
|
||||||
this.type = 'asteroid';
|
|
||||||
|
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
|
||||||
|
this.type = 'asteroid';
|
||||||
this.frame = this.randomFrame();
|
this.frame = this.randomFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,11 @@ class Body {
|
||||||
if(pos.x > bounds.right) this.applyForce(-0.03, 0);
|
if(pos.x > bounds.right) this.applyForce(-0.03, 0);
|
||||||
if(pos.y < bounds.top) this.applyForce(0, 0.03);
|
if(pos.y < bounds.top) this.applyForce(0, 0.03);
|
||||||
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
|
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
|
||||||
|
|
||||||
|
this.tickType();
|
||||||
|
}
|
||||||
|
|
||||||
|
tickType() {
|
||||||
}
|
}
|
||||||
|
|
||||||
packDelta() {
|
packDelta() {
|
||||||
|
@ -50,11 +55,24 @@ class Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
packTypeDelta() {
|
packTypeDelta() {
|
||||||
throw new Error('Attempt to pack raw body.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
packFull() {
|
packFull() {
|
||||||
throw new Error('Attempt to pack raw body.');
|
}
|
||||||
|
|
||||||
|
get center() {
|
||||||
|
return {
|
||||||
|
x: this.b2body.GetWorldCenter().x,
|
||||||
|
y: this.b2body.GetWorldCenter().y
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
get pos() {
|
||||||
|
return {
|
||||||
|
x: this.b2body.GetPosition().x,
|
||||||
|
y: this.b2body.GetPosition().y,
|
||||||
|
r: this.b2body.GetAngleRadians()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Asteroid = require('./asteroid.js');
|
|
||||||
const Physics = require('./physics.js');
|
const Physics = require('./physics.js');
|
||||||
const Ship = require('./ship.js');
|
const Ship = require('./ship.js');
|
||||||
|
const Spawner = require('./spawner.js');
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
constructor(room) {
|
constructor(room) {
|
||||||
this.physics = new Physics();
|
this.physics = new Physics();
|
||||||
|
this.spawner = new Spawner(this);
|
||||||
this.bodies = new Set();
|
this.bodies = new Set();
|
||||||
this.structures = new Set();
|
this.structures = new Set();
|
||||||
this.asteroids = new Set();
|
this.asteroids = new Set();
|
||||||
|
this.missiles = new Set();
|
||||||
this.ships = new Map();
|
this.ships = new Map();
|
||||||
this.players = new Set();
|
this.players = new Set();
|
||||||
this.room = room;
|
this.room = room;
|
||||||
|
@ -48,6 +50,11 @@ class World {
|
||||||
this.addBody(asteroid);
|
this.addBody(asteroid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addMissile(missile) {
|
||||||
|
this.missiles.add(missile);
|
||||||
|
this.addBody(missile);
|
||||||
|
}
|
||||||
|
|
||||||
addBody(body) {
|
addBody(body) {
|
||||||
this.bodies.add(body);
|
this.bodies.add(body);
|
||||||
this.physics.createBody(body);
|
this.physics.createBody(body);
|
||||||
|
@ -64,8 +71,7 @@ class World {
|
||||||
x: Math.random() * 2000 - 200,
|
x: Math.random() * 2000 - 200,
|
||||||
y: Math.random() * 500 - 250
|
y: Math.random() * 500 - 250
|
||||||
};
|
};
|
||||||
let asteroid = new Asteroid(this, pos, Math.random() * 50 + 10);
|
this.spawner.spawnAsteroid(pos.x, pos.y,Math.random() * 50 + 10);
|
||||||
this.addAsteroid(asteroid);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,11 +82,12 @@ class World {
|
||||||
}
|
}
|
||||||
|
|
||||||
removeBody(body) {
|
removeBody(body) {
|
||||||
this.physics.toRemove.push(body);
|
this.physics.remove(body);
|
||||||
this.bodies.delete(body);
|
this.bodies.delete(body);
|
||||||
this.ships.delete(body);
|
this.ships.delete(body);
|
||||||
this.structures.delete(body);
|
this.structures.delete(body);
|
||||||
this.asteroids.delete(body);
|
this.asteroids.delete(body);
|
||||||
|
this.missiles.delete(body);
|
||||||
this.room.broadcast('destroy', body.id);
|
this.room.broadcast('destroy', body.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,23 +102,23 @@ class World {
|
||||||
tick(self) {
|
tick(self) {
|
||||||
self.physics.step();
|
self.physics.step();
|
||||||
|
|
||||||
self.ships.forEach(body => {
|
let tickBodies = (set, interval) => {
|
||||||
body.applyDelta(),
|
set.forEach(body => {
|
||||||
body.tick();
|
if (this.tickCount % interval == 0)
|
||||||
});
|
body.applyDelta();
|
||||||
|
|
||||||
if (this.tickCount % 1 == 0) {
|
|
||||||
self.asteroids.forEach(body => {
|
|
||||||
body.applyDelta(),
|
|
||||||
body.tick();
|
body.tick();
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if (Date.now() - this.tpsStart > 5000) {
|
tickBodies(self.ships, 1);
|
||||||
this.tps = this.tpsCount / 5 | 0;
|
tickBodies(self.asteroids, 4);
|
||||||
this.tpsCount = 0;
|
tickBodies(self.missiles, 1);
|
||||||
this.tpsStart = Date.now();
|
|
||||||
//console.log('TPS: ' + this.tps);
|
if (Date.now() - this.tpsStart > 5000) {
|
||||||
}
|
this.tps = this.tpsCount / 5 | 0;
|
||||||
|
this.tpsCount = 0;
|
||||||
|
this.tpsStart = Date.now();
|
||||||
|
//console.log('TPS: ' + this.tps);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tpsCount++;
|
this.tpsCount++;
|
||||||
|
|
|
@ -1,15 +1,21 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Body = require('./bodies.js');
|
const Body = require('./body.js');
|
||||||
|
|
||||||
class Missile extends Body {
|
class Missile extends Body {
|
||||||
constructor(world, source) {
|
constructor(world, pos, source) {
|
||||||
super(world);
|
super(world);
|
||||||
|
|
||||||
|
this.x = pos.x * 32;
|
||||||
|
this.y = pos.y * 32;
|
||||||
|
this.r = pos.r;
|
||||||
|
|
||||||
this.source = source;
|
this.source = source;
|
||||||
this.player = source.player;
|
this.player = source.player;
|
||||||
|
this.fuel = 100;
|
||||||
|
|
||||||
this.frame = [[[0, 0], [0, 10], [3, 10], [3, 0]]];
|
this.type = 'missile';
|
||||||
|
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
|
||||||
}
|
}
|
||||||
|
|
||||||
detonate() {
|
detonate() {
|
||||||
|
@ -17,6 +23,16 @@ class Missile extends Body {
|
||||||
this.world.removeBody(this);
|
this.world.removeBody(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tickType() {
|
||||||
|
let power = 0.005;
|
||||||
|
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
||||||
|
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
||||||
|
this.applyForce(x, y);
|
||||||
|
|
||||||
|
if(this.fuel-- <= 0)
|
||||||
|
this.detonate();
|
||||||
|
}
|
||||||
|
|
||||||
packTypeDelta() {
|
packTypeDelta() {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -26,11 +42,10 @@ class Missile extends Body {
|
||||||
type: 'missile',
|
type: 'missile',
|
||||||
id: this.id,
|
id: this.id,
|
||||||
source: this.source.id,
|
source: this.source.id,
|
||||||
team: this.player.team,
|
|
||||||
frame: this.frame,
|
frame: this.frame,
|
||||||
delta: this.packDelta()
|
delta: this.packDelta()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Projectile;
|
module.exports = Missile;
|
||||||
|
|
|
@ -37,12 +37,14 @@ class Physics {
|
||||||
let bodyDef = new Box2D.b2BodyDef();
|
let bodyDef = new Box2D.b2BodyDef();
|
||||||
bodyDef.userData = body;
|
bodyDef.userData = body;
|
||||||
bodyDef.position = new b2Vec2(body.x / s || 0, body.y / s || 0);
|
bodyDef.position = new b2Vec2(body.x / s || 0, body.y / s || 0);
|
||||||
|
bodyDef.angle = body.r || 0;
|
||||||
bodyDef.fixedRotation = false;
|
bodyDef.fixedRotation = false;
|
||||||
bodyDef.active = true;
|
bodyDef.active = true;
|
||||||
bodyDef.linearVelocity = new b2Vec2(body.xvel / s || 0, body.yvel / s || 0);
|
bodyDef.linearVelocity = new b2Vec2(body.xvel / s || 0, body.yvel / s || 0);
|
||||||
bodyDef.angularVelocity = body.rvel || 0;
|
bodyDef.angularVelocity = body.rvel || 0;
|
||||||
bodyDef.linearDamping = body.type == 'ship' ? 0.01 : 0.003;
|
bodyDef.bullet = body.type == 'missile';
|
||||||
bodyDef.angularDamping = body.type == 'ship' ? 0.01 : 0.003;
|
bodyDef.linearDamping = body.type == 'asteroid' ? 0.003 : 0.01;
|
||||||
|
bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.01;
|
||||||
bodyDef.type = body.type == 'structure' ?
|
bodyDef.type = body.type == 'structure' ?
|
||||||
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
|
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
|
||||||
if (body.player || true) bodyDef.allowSleep = false;
|
if (body.player || true) bodyDef.allowSleep = false;
|
||||||
|
@ -64,6 +66,10 @@ class Physics {
|
||||||
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remove(body) {
|
||||||
|
this.toRemove.push(body);
|
||||||
|
}
|
||||||
|
|
||||||
step() {
|
step() {
|
||||||
this.world.Step(1, 5, 1 / 60);
|
this.world.Step(1, 5, 1 / 60);
|
||||||
for (var i = 0; i < this.toRemove.length; i++) {
|
for (var i = 0; i < this.toRemove.length; i++) {
|
||||||
|
|
|
@ -19,6 +19,7 @@ class Ship extends Body {
|
||||||
this.size = traits.size;
|
this.size = traits.size;
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.type = 'ship';
|
this.type = 'ship';
|
||||||
|
this.inputs = {};
|
||||||
|
|
||||||
this.thrust = {
|
this.thrust = {
|
||||||
forward: 0,
|
forward: 0,
|
||||||
|
@ -27,35 +28,40 @@ class Ship extends Body {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
move(data) {
|
updateInputs(data) {
|
||||||
let b = this.b2body;
|
this.inputs = {
|
||||||
|
forward: data[0],
|
||||||
|
left: data[1],
|
||||||
|
right: data[2],
|
||||||
|
missile: data[3]
|
||||||
|
};
|
||||||
|
|
||||||
//console.log(b.GetLocalCenter());
|
this.thrust.forward = this.inputs.forward;
|
||||||
|
this.thrust.left = this.inputs.left;
|
||||||
|
this.thrust.right = this.inputs.right;
|
||||||
|
|
||||||
for(var i in b) {
|
if (this.inputs.missile) this.launchMissile();
|
||||||
//if(typeof b[i] == 'function') console.log(i);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (data.forward) {
|
launchMissile() {
|
||||||
|
this.world.spawner.spawnMissile(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
tickType() {
|
||||||
|
if (this.thrust.forward) {
|
||||||
let power = this.power.forward;
|
let power = this.power.forward;
|
||||||
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
||||||
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
||||||
this.applyForce(x, y);
|
this.applyForce(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.left) {
|
if (this.thrust.left) {
|
||||||
this.applyTorque(-this.power.rotation);
|
this.applyTorque(-this.power.rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.right) {
|
if (this.thrust.right) {
|
||||||
this.applyTorque(this.power.rotation);
|
this.applyTorque(this.power.rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.thrust = {
|
|
||||||
forward: data.forward,
|
|
||||||
left: data.left,
|
|
||||||
right: data.right
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
packTypeDelta() {
|
packTypeDelta() {
|
||||||
|
|
34
server/game/room/world/spawner.js
Normal file
34
server/game/room/world/spawner.js
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Asteroid = require('./asteroid.js');
|
||||||
|
const Missile = require('./missile.js');
|
||||||
|
|
||||||
|
class Spawner {
|
||||||
|
constructor(world) {
|
||||||
|
this.world = world;
|
||||||
|
}
|
||||||
|
|
||||||
|
spawnAsteroid(x, y, size) {
|
||||||
|
let pos = {
|
||||||
|
x: x,
|
||||||
|
y: y
|
||||||
|
};
|
||||||
|
let asteroid = new Asteroid(this.world, pos, size);
|
||||||
|
this.world.addAsteroid(asteroid);
|
||||||
|
}
|
||||||
|
|
||||||
|
spawnMissile(ship) {
|
||||||
|
let r = ship.pos.r;
|
||||||
|
let ox = Math.cos(r) * 0.7;
|
||||||
|
let oy = Math.sin(r) * 0.7;
|
||||||
|
let pos = {
|
||||||
|
x: ship.center.x + ox,
|
||||||
|
y: ship.center.y + oy,
|
||||||
|
r: r
|
||||||
|
};
|
||||||
|
let missile = new Missile(this.world, pos, ship);
|
||||||
|
this.world.addMissile(missile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Spawner;
|
Loading…
Add table
Add a link
Reference in a new issue