add basic missiles
This commit is contained in:
parent
21a30ad212
commit
ac089f3e8e
19 changed files with 215 additions and 85 deletions
|
@ -22,8 +22,8 @@ class Connection {
|
|||
this.player.name = data.name.slice(0, 20) || 'Fish';
|
||||
});
|
||||
|
||||
socket.on('move', data => {
|
||||
this.player.move(data);
|
||||
socket.on('inputs', data => {
|
||||
this.player.updateInputs(data);
|
||||
});
|
||||
|
||||
this.server.assignRoom(this.player);
|
||||
|
|
|
@ -18,12 +18,8 @@ class Player {
|
|||
this.room.remove(this);
|
||||
}
|
||||
|
||||
move(data) {
|
||||
this.ship.move({
|
||||
forward: data.forward || 0,
|
||||
left: data.left || 0,
|
||||
right: data.right || 0
|
||||
});
|
||||
updateInputs(data) {
|
||||
this.ship.updateInputs(data);
|
||||
this.lastAction = Date.now();
|
||||
}
|
||||
|
||||
|
|
|
@ -8,9 +8,10 @@ class Asteroid extends Body {
|
|||
|
||||
this.x = pos.x;
|
||||
this.y = pos.y;
|
||||
this.type = 'asteroid';
|
||||
|
||||
this.size = size;
|
||||
|
||||
this.type = 'asteroid';
|
||||
this.frame = this.randomFrame();
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,11 @@ class Body {
|
|||
if(pos.x > bounds.right) this.applyForce(-0.03, 0);
|
||||
if(pos.y < bounds.top) this.applyForce(0, 0.03);
|
||||
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
|
||||
|
||||
this.tickType();
|
||||
}
|
||||
|
||||
tickType() {
|
||||
}
|
||||
|
||||
packDelta() {
|
||||
|
@ -50,11 +55,24 @@ class Body {
|
|||
}
|
||||
|
||||
packTypeDelta() {
|
||||
throw new Error('Attempt to pack raw body.');
|
||||
}
|
||||
|
||||
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';
|
||||
|
||||
const Asteroid = require('./asteroid.js');
|
||||
const Physics = require('./physics.js');
|
||||
const Ship = require('./ship.js');
|
||||
const Spawner = require('./spawner.js');
|
||||
|
||||
class World {
|
||||
constructor(room) {
|
||||
this.physics = new Physics();
|
||||
this.spawner = new Spawner(this);
|
||||
this.bodies = new Set();
|
||||
this.structures = new Set();
|
||||
this.asteroids = new Set();
|
||||
this.missiles = new Set();
|
||||
this.ships = new Map();
|
||||
this.players = new Set();
|
||||
this.room = room;
|
||||
|
@ -48,6 +50,11 @@ class World {
|
|||
this.addBody(asteroid);
|
||||
}
|
||||
|
||||
addMissile(missile) {
|
||||
this.missiles.add(missile);
|
||||
this.addBody(missile);
|
||||
}
|
||||
|
||||
addBody(body) {
|
||||
this.bodies.add(body);
|
||||
this.physics.createBody(body);
|
||||
|
@ -64,8 +71,7 @@ class World {
|
|||
x: Math.random() * 2000 - 200,
|
||||
y: Math.random() * 500 - 250
|
||||
};
|
||||
let asteroid = new Asteroid(this, pos, Math.random() * 50 + 10);
|
||||
this.addAsteroid(asteroid);
|
||||
this.spawner.spawnAsteroid(pos.x, pos.y,Math.random() * 50 + 10);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,11 +82,12 @@ class World {
|
|||
}
|
||||
|
||||
removeBody(body) {
|
||||
this.physics.toRemove.push(body);
|
||||
this.physics.remove(body);
|
||||
this.bodies.delete(body);
|
||||
this.ships.delete(body);
|
||||
this.structures.delete(body);
|
||||
this.asteroids.delete(body);
|
||||
this.missiles.delete(body);
|
||||
this.room.broadcast('destroy', body.id);
|
||||
}
|
||||
|
||||
|
@ -95,23 +102,23 @@ class World {
|
|||
tick(self) {
|
||||
self.physics.step();
|
||||
|
||||
self.ships.forEach(body => {
|
||||
body.applyDelta(),
|
||||
body.tick();
|
||||
});
|
||||
|
||||
if (this.tickCount % 1 == 0) {
|
||||
self.asteroids.forEach(body => {
|
||||
body.applyDelta(),
|
||||
let tickBodies = (set, interval) => {
|
||||
set.forEach(body => {
|
||||
if (this.tickCount % interval == 0)
|
||||
body.applyDelta();
|
||||
body.tick();
|
||||
});
|
||||
};
|
||||
|
||||
if (Date.now() - this.tpsStart > 5000) {
|
||||
this.tps = this.tpsCount / 5 | 0;
|
||||
this.tpsCount = 0;
|
||||
this.tpsStart = Date.now();
|
||||
//console.log('TPS: ' + this.tps);
|
||||
}
|
||||
tickBodies(self.ships, 1);
|
||||
tickBodies(self.asteroids, 4);
|
||||
tickBodies(self.missiles, 1);
|
||||
|
||||
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++;
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
'use strict';
|
||||
|
||||
const Body = require('./bodies.js');
|
||||
const Body = require('./body.js');
|
||||
|
||||
class Missile extends Body {
|
||||
constructor(world, source) {
|
||||
constructor(world, pos, source) {
|
||||
super(world);
|
||||
|
||||
this.x = pos.x * 32;
|
||||
this.y = pos.y * 32;
|
||||
this.r = pos.r;
|
||||
|
||||
this.source = source;
|
||||
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() {
|
||||
|
@ -17,6 +23,16 @@ class Missile extends Body {
|
|||
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() {
|
||||
return [];
|
||||
}
|
||||
|
@ -26,11 +42,10 @@ class Missile extends Body {
|
|||
type: 'missile',
|
||||
id: this.id,
|
||||
source: this.source.id,
|
||||
team: this.player.team,
|
||||
frame: this.frame,
|
||||
delta: this.packDelta()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Projectile;
|
||||
module.exports = Missile;
|
||||
|
|
|
@ -37,12 +37,14 @@ class Physics {
|
|||
let bodyDef = new Box2D.b2BodyDef();
|
||||
bodyDef.userData = body;
|
||||
bodyDef.position = new b2Vec2(body.x / s || 0, body.y / s || 0);
|
||||
bodyDef.angle = body.r || 0;
|
||||
bodyDef.fixedRotation = false;
|
||||
bodyDef.active = true;
|
||||
bodyDef.linearVelocity = new b2Vec2(body.xvel / s || 0, body.yvel / s || 0);
|
||||
bodyDef.angularVelocity = body.rvel || 0;
|
||||
bodyDef.linearDamping = body.type == 'ship' ? 0.01 : 0.003;
|
||||
bodyDef.angularDamping = body.type == 'ship' ? 0.01 : 0.003;
|
||||
bodyDef.bullet = body.type == 'missile';
|
||||
bodyDef.linearDamping = body.type == 'asteroid' ? 0.003 : 0.01;
|
||||
bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.01;
|
||||
bodyDef.type = body.type == 'structure' ?
|
||||
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
|
||||
if (body.player || true) bodyDef.allowSleep = false;
|
||||
|
@ -64,6 +66,10 @@ class Physics {
|
|||
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
||||
}
|
||||
|
||||
remove(body) {
|
||||
this.toRemove.push(body);
|
||||
}
|
||||
|
||||
step() {
|
||||
this.world.Step(1, 5, 1 / 60);
|
||||
for (var i = 0; i < this.toRemove.length; i++) {
|
||||
|
|
|
@ -19,6 +19,7 @@ class Ship extends Body {
|
|||
this.size = traits.size;
|
||||
this.player = player;
|
||||
this.type = 'ship';
|
||||
this.inputs = {};
|
||||
|
||||
this.thrust = {
|
||||
forward: 0,
|
||||
|
@ -27,35 +28,40 @@ class Ship extends Body {
|
|||
}
|
||||
}
|
||||
|
||||
move(data) {
|
||||
let b = this.b2body;
|
||||
updateInputs(data) {
|
||||
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(typeof b[i] == 'function') console.log(i);
|
||||
}
|
||||
if (this.inputs.missile) this.launchMissile();
|
||||
}
|
||||
|
||||
if (data.forward) {
|
||||
launchMissile() {
|
||||
this.world.spawner.spawnMissile(this);
|
||||
}
|
||||
|
||||
tickType() {
|
||||
if (this.thrust.forward) {
|
||||
let power = this.power.forward;
|
||||
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
||||
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
||||
this.applyForce(x, y);
|
||||
}
|
||||
|
||||
if (data.left) {
|
||||
if (this.thrust.left) {
|
||||
this.applyTorque(-this.power.rotation);
|
||||
}
|
||||
|
||||
if (data.right) {
|
||||
if (this.thrust.right) {
|
||||
this.applyTorque(this.power.rotation);
|
||||
}
|
||||
|
||||
this.thrust = {
|
||||
forward: data.forward,
|
||||
left: data.left,
|
||||
right: data.right
|
||||
};
|
||||
}
|
||||
|
||||
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