Add start of ship rendering

This commit is contained in:
asraelite 2018-03-03 00:04:17 +00:00
parent 4a253b0184
commit 704c82838a
15 changed files with 199 additions and 13 deletions

26
js/world/body.mjs Normal file
View file

@ -0,0 +1,26 @@
import {GRAVITATIONAL_CONSTANT as G} from '../consts.mjs';
export default class Body {
constructor(x, y, mass) {
this.x = x;
this.y = y;
this.r = 0;
this.xvel = 0;
this.yvel = 0;
this.rvel = 0;
this.mass = mass;
}
tickGravity(bodies) {
bodies.forEach(b => {
let force = b.mass / this.mass / (distanceTo(b) ** 2) * G;
let angle = Math.atan2(b.y - this.y, b.x - this.x);
this.xvel += Math.cos(angle) * force;
this.yvel += Math.sin(angle) * force;
});
}
distanceTo(body) {
return Math.sqrt(((body.x - this.x) ** 2) + ((body.y - this.y) ** 2));
}
}

10
js/world/celestial.mjs Normal file
View file

@ -0,0 +1,10 @@
import Body from './body.mjs';
export default class Celestial extends Body {
constructor(x, y, radius, {
density = 1,
mass = (radius ** 2) * density
}) {
super(x, y, mass);
}
}

View file

@ -1,3 +1,26 @@
import * as sector from './sector.mjs';
import * as spawn from './spawn.mjs';
export {getSectorFromWorld, getContainedSectors} from './sector.mjs';
export const entities = new Set();
export const celestials = new Set();
export const ships = new Set();
export let playerShip = null;
export function setPlayerShip(ship) {
playerShip = ship;
}
export function init() {
entities.clear();
celestials.clear();
spawn.player();
}
export function tick() {
celestials.forEach(c => c.tick());
entities.forEach(e => e.tick());
ships.forEach(s => s.tick());
}

15
js/world/module.mjs Normal file
View file

@ -0,0 +1,15 @@
export default class Module {
constructor(x, y, {
name = 'Unnamed Module',
type = 'block',
mass = 1,
...properties
}) {
this.x = x;
this.y = y;
this.name = name;
this.type = type;
this.mass = mass;
}
}

35
js/world/ship.mjs Normal file
View file

@ -0,0 +1,35 @@
import Module from './module.mjs';
import Body from './body.mjs';
export default class Ship extends Body {
constructor(x, y) {
super(x, y, 0);
this.com = [0, 0];
this.modules = new Set();
}
tick() {
}
addModule(x, y, properties, options) {
let module = new Module(x, y, {...properties, ...options});
this.modules.add(module);
this.refreshShape();
}
deleteModule(module) {
this.modules.delete(module);
this.refreshShape();
}
refreshShape() {
let points = [];
this.modules.forEach(m => points.push([m.x, m.y, m.mass]));
this.mass = points.reduce((a, [,,b]) => a + b, 0);
this.com = points.reduce(([ax, ay], b) =>
[ax + b.x * b.mass, ay + b.y * b.mass], [0, 0])
.map(x => x / this.mass);
}
}

13
js/world/spawn.mjs Normal file
View file

@ -0,0 +1,13 @@
import Ship from './ship.mjs';
import Module from './module.mjs';
import {modules} from '../data.mjs';
import * as world from './index.mjs';
export function player() {
let ship = new Ship(0, 0);
ship.addModule(0, 0, modules.capsule.small);
ship.addModule(0, 1, modules.fuel.small, { filled: true });
ship.addModule(0, 2, modules.thruster.light);
world.ships.add(ship);
world.setPlayerShip(ship);
}