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

@ -0,0 +1,47 @@
'use strict';
const Body = require('./body.js');
class Asteroid extends Body {
constructor(world, pos, size) {
super(world);
this.x = pos.x;
this.y = pos.y;
this.debug = 0;
this.size = size;
this.type = 'asteroid';
this.frame = this.randomFrame();
}
randomFrame() {
let s = this.size;
let l = (Math.random() * 4 + 4) | 0;
let build = Array(l).fill().map(_ => Math.random() * Math.PI * 2);
build = build.sort().map(a => [Math.cos(a) * s, Math.sin(a) * s]);
return [build];
}
tickType() {
if (this.debug > 0)
this.debug--;
}
packTypeDelta() {
return [this.debug];
}
packFull() {
return {
type: 'asteroid',
id: this.id,
frame: this.frame,
delta: this.packDelta()
}
}
}
module.exports = Asteroid;

View file

@ -0,0 +1,96 @@
'use strict';
const uuid = require('uuid');
const b2Vec2 = require('box2d-html5').b2Vec2;
class Body {
constructor(world) {
this.x = 0;
this.y = 0;
this.r = 0;
this.b2body = false;
this.type = 'asteroid';
this.health = 1;
this.world = world;
this.id = uuid.v4().slice(0, 8);
}
applyDelta() {
this.world.applyDelta(this.id, this.packDelta());
}
applyForce(x, y, center) {
let b = this.b2body;
let c = center ? new b2Vec2(center.x, center.y) : b.GetWorldCenter();
b.ApplyForce(new b2Vec2(x, y), c);
}
applyImpulse(x, y, center) {
let b = this.b2body;
let c = center ? new b2Vec2(center.x, center.y) : b.GetWorldCenter();
b.ApplyLinearImpulse(new b2Vec2(x, y), c);
}
applyTorque(f) {
this.b2body.ApplyTorque(f);
}
contact() {
}
tick() {
let pos = this.b2body.GetPosition();
let bounds = this.world.bounds;
if(pos.x < bounds.left) 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.bottom) this.applyForce(-0, -0.03);
this.tickType();
}
tickType() {
}
packDelta() {
let pos = this.b2body.GetPosition();
let vel = this.b2body.GetLinearVelocity();
let rot = this.b2body.GetAngleRadians();
let rvel = this.b2body.GetAngularVelocity();
// Simple array to save bandwidth.
return [pos.x, pos.y, vel.x, vel.y, rot, rvel].concat(this.packTypeDelta());
}
packTypeDelta() {
}
packFull() {
}
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()
};
}
get vel() {
return {
x: this.b2body.GetLinearVelocity().x,
y: this.b2body.GetLinearVelocity().y
}
}
}
module.exports = Body;

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

@ -0,0 +1,65 @@
'use strict';
const Projectile = require('./projectile.js');
class Missile 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.fuel = 200;
this.type = 'missile';
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
}
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

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

View file

View file

@ -0,0 +1,92 @@
'use strict';
const defaults = require('../traits/defaults.json');
const shipTraits = require('../traits/ships.json');
const Body = require('./body.js');
class Ship extends Body {
constructor(world, pos, player, build) {
super(world);
this.x = pos.x || 0;
this.y = pos.y || 0;
build = build || {};
this.class = build.ship || defaults.spawnShip.ship;
this.turrets = build.turrets || defaults.spawnShip.turrets;
let traits = shipTraits[this.class];
this.frame = traits.hull;
this.power = traits.power;
this.mounts = traits.mounts;
this.size = traits.size;
this.player = player;
this.type = 'ship';
this.inputs = {};
this.thrust = {
forward: 0,
left: 0,
right: 0
}
}
updateInputs(data) {
this.inputs = {
forward: data[0],
left: data[1],
right: data[2],
missile: data[3]
};
this.thrust.forward = this.inputs.forward;
this.thrust.left = this.inputs.left;
this.thrust.right = this.inputs.right;
if (this.inputs.missile) this.launchMissile();
}
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 (this.thrust.left) {
this.applyTorque(-this.power.rotation);
}
if (this.thrust.right) {
this.applyTorque(this.power.rotation);
}
}
packTypeDelta() {
let t = this.thrust;
return [t.forward, t.left, t.right];
}
packFull() {
return {
type: 'ship',
id: this.id,
team: this.player.team,
name: this.player.name,
frame: this.frame,
power: this.power,
mounts: this.mounts,
turrets: this.turrets,
size: this.size,
delta: this.packDelta()
};
}
}
module.exports = Ship;

View file

@ -0,0 +1,13 @@
'use strict';
const Body = require('./body.js');
class Fixture extends Body {
constructor(player) {
super();
this.type = 'static';
}
}
module.exports = Ship;