restructure world files for copulae

This commit is contained in:
Asraelite 2016-03-26 14:23:51 +00:00
parent 41a3a295c6
commit ccc77490ea
18 changed files with 163 additions and 29 deletions

View file

@ -1,11 +0,0 @@
'use strict';
const Discharge = require('./discharge.js');
class Beam extends Discharge {
constructor() {
super();
}
}
module.exports = Beam;

View file

@ -0,0 +1,69 @@
'use strict';
const Projectile = require('./projectile.js');
const Rope = require('../copula/rope.js');
class Grapple extends Projectile {
constructor(world, pos, source) {
super(world);
this.x = pos.x * 32;
this.y = pos.y * 32;
this.xvel = pos.xvel;
this.yvel = pos.yvel;
this.r = pos.r;
this.xvel += Math.cos(this.r) * 0.2;
this.yvel += Math.sin(this.r) * 0.2;
this.source = source;
this.player = source.player;
this.rope = new Rope(this, this);
this.type = 'grapple';
this.frame = [
[[0, -8], [5, -12], [4, 0], [0, 0]],
[[0, 0], [4, 0], [5, 12], [0, 8]]
];
}
contact(body) {
this.detonate();
}
detonate() {
this.world.explosion(this.center, 10);
this.world.room.broadcast('effect', {
type: 'explosion',
size: 10,
pos: this.center
});
this.world.removeBody(this);
}
tickType() {
let power = 0.004;
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 [];
}
packFull() {
return {
type: 'missile',
id: this.id,
source: this.source.id,
frame: this.frame,
delta: this.packDelta()
};
}
}
module.exports = Missile;

View file

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const Body = require('./body.js'); const Projectile = require('./projectile.js');
class Missile extends Body { class Missile extends Projectile {
constructor(world, pos, source) { constructor(world, pos, source) {
super(world); super(world);

View file

@ -0,0 +1,11 @@
'use strict';
const Body = require('../body.js');
class Projectile extends Body {
constructor(world) {
super(world);
}
}
module.exports = Projectile;

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const defaults = require('./traits/defaults.json'); const defaults = require('../traits/defaults.json');
const shipTraits = require('./traits/ships.json'); const shipTraits = require('../traits/ships.json');
const Body = require('./body.js'); const Body = require('./body.js');

View file

@ -0,0 +1,10 @@
'use strict';
class Copula {
constructor(b1, b2) {
this.b1 = b1;
this.b2 = b2;
}
}
module.exports = Copula;

View file

@ -0,0 +1,11 @@
'use strict';
const Copula = require('./copula.js');
class Rope extends Copula {
constructor(p1, p2) {
super();
}
}
module.exports = Rope;

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Physics = require('./physics.js'); const Physics = require('./physics.js');
const Ship = require('./ship.js'); const Ship = require('./body/ship.js');
const Spawner = require('./spawner.js'); const Spawner = require('./spawner.js');
const b2Vec2 = require('box2d-html5').b2Vec2; const b2Vec2 = require('box2d-html5').b2Vec2;
@ -11,6 +11,7 @@ class World {
this.physics = new Physics(); this.physics = new Physics();
this.spawner = new Spawner(this); this.spawner = new Spawner(this);
this.bodies = new Set(); this.bodies = new Set();
this.copulae = new Set();
this.structures = new Set(); this.structures = new Set();
this.asteroids = new Set(); this.asteroids = new Set();
this.missiles = new Set(); this.missiles = new Set();
@ -63,6 +64,12 @@ class World {
this.room.broadcast('create', body.packFull()); this.room.broadcast('create', body.packFull());
} }
addCopula(copula) {
this.copulae.add(copula);
this.physics.createCopula(copula);
this.room.broadcast('create', copula.packFull());
}
applyDelta(body, data) { applyDelta(body, data) {
this.players.forEach(player => player.delta[body] = data); this.players.forEach(player => player.delta[body] = data);
} }
@ -93,6 +100,14 @@ class World {
}; };
this.spawner.spawnAsteroid(pos.x, pos.y,Math.random() * 50 + 10); this.spawner.spawnAsteroid(pos.x, pos.y,Math.random() * 50 + 10);
} }
let a1 = Array.from(this.asteroids)[Math.random() * this.asteroids.size];
let a2 = Array.from(this.asteroids)[Math.random() * this.asteroids.size];
let copula = new (require('./copula/copula.js'))(a1, a2);
if (a1 != a2) {
this.physics.createCopula(copula);
}
} }
removePlayer(player) { removePlayer(player) {
@ -111,6 +126,11 @@ class World {
this.room.broadcast('destroy', body.id); this.room.broadcast('destroy', body.id);
} }
removeCopula(copula) {
this.copulae.delete(body);
this.room.broadcast('destroy', copula.id);
}
start() { start() {
this.interval = setInterval(_ => this.tick(this), 1000 / 60); this.interval = setInterval(_ => this.tick(this), 1000 / 60);
} }

View file

@ -69,6 +69,10 @@ class Physics {
//if (body.type == 'ship') console.log(b2body.GetLocalCenter()); //if (body.type == 'ship') console.log(b2body.GetLocalCenter());
} }
createCopula(copula) {
}
raycast(start, end) { raycast(start, end) {
let p1 = new b2Vec2(start.x, start.y); let p1 = new b2Vec2(start.x, start.y);
let p2 = new b2Vec2(end.x, end.y); let p2 = new b2Vec2(end.x, end.y);

View file

@ -1,11 +0,0 @@
'use strict';
const Discharge = require('./discharge.js');
class Projectile extends Discharge {
constructor() {
super();
}
}
module.exports = Projectile;

View file

@ -0,0 +1,11 @@
'use strict';
const Shot = require('./shot.js');
class Beam extends Shot {
constructor() {
super();
}
}
module.exports = Beam;

View file

@ -0,0 +1,11 @@
'use strict';
const Shot = require('./shot.js');
class Bullet extends Shot {
constructor() {
super();
}
}
module.exports = Bullet;

View file

@ -0,0 +1,9 @@
'use strict';
class Shot {
constructor() {
}
}
module.exports = Beam;

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Asteroid = require('./asteroid.js'); const Asteroid = require('./body/asteroid.js');
const Missile = require('./missile.js'); const Missile = require('./body/projectile/missile.js');
class Spawner { class Spawner {
constructor(world) { constructor(world) {