Add ship movement

This commit is contained in:
asraelite 2018-03-03 15:58:51 +00:00
parent 09b3df649c
commit 4959519f39
15 changed files with 278 additions and 77 deletions

View file

@ -8,12 +8,15 @@ export default class Body {
this.xvel = 0;
this.yvel = 0;
this.rvel = 0;
this.rfriction = 0.9;
this.mass = mass;
}
tickMotion() {
this.x += this.xvel;
this.y += this.yvel;
this.r += this.rvel;
this.rvel *= this.rfriction;
}
tickGravity(bodies) {
@ -40,4 +43,10 @@ export default class Body {
this.xvel = 0;
this.yvel = 0;
}
applyDirectionalForce(x, y, r) {
this.xvel += (x * Math.cos(this.r) - y * Math.sin(this.r)) / this.mass;
this.yvel += (y * Math.cos(this.r) - x * Math.sin(this.r)) / this.mass;
this.rvel += r / this.mass;
}
}

View file

@ -1,7 +1,10 @@
import {images as assets} from '../assets.mjs';
export default class Module {
constructor(x, y, {
name = 'Unnamed Module',
type = 'block',
id = 'unknown',
mass = 1,
// Fuel
filled = false,
@ -13,7 +16,31 @@ export default class Module {
this.name = name;
this.type = type;
this.mass = mass;
this.id = id;
this.images = assets.modules[this.type][this.id];
// Fuel
this.fuel = filled ? fuelCapacity : 0;
if (this.type == 'fuel') {
this.fuel = filled ? fuelCapacity : 0;
} else if (this.type == 'thruster') {
this.power = 0;
}
}
reset() {
if (this.type == 'thruster') {
this.power = 0;
}
}
get currentImage() {
if (this.type == 'thruster') {
return this.power > 0 ? this.images.on : this.images.off;
} else {
return this.images;
}
}
get com() {
return [this.x + 0.5, this.y + 0.5];
}
}

View file

@ -14,6 +14,7 @@ export default class Ship extends Body {
this.tickMotion();
this.tickGravity(world.celestials);
this.resolveCollisions();
this.modules.forEach(m => m.reset());
}
addModule(x, y, properties, options) {
@ -29,11 +30,12 @@ export default class Ship extends Body {
refreshShape() {
let points = [];
this.modules.forEach(m => points.push([m.x, m.y, m.mass]));
this.modules.forEach(m => points.push([...m.com, 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])
this.com = points.reduce(([ax, ay], [bx, by, bm]) =>
[ax + bx * bm, ay + by * bm], [0, 0])
.map(x => x / this.mass);
window.q = points;
}
resolveCollisions() {
@ -45,4 +47,16 @@ export default class Ship extends Body {
}
})
}
applyThrust({ forward = 0, left = 0, right = 0, back = 0,
turnLeft = 0, turnRight = 0}) {
let turnForce = (turnRight - turnLeft) / 20;
this.applyDirectionalForce(0, -forward / 30, turnForce);
this.modules.forEach(m => {
if (m.type !== 'thruster') return;
m.power = forward;
});
}
}

View file

@ -5,7 +5,7 @@ import {modules} from '../data.mjs';
import * as world from './index.mjs';
export function player() {
let ship = new Ship(0, 0);
let ship = new Ship(0, -45);
ship.addModule(0, 0, modules.capsule.small);
ship.addModule(0, 1, modules.fuel.small, { filled: true });
ship.addModule(0, 2, modules.thruster.light);
@ -15,7 +15,7 @@ export function player() {
}
export function startPlanet() {
return celestial(-40, 10, 40, {
return celestial(0, 0, 40, {
density: 1,
type: 'green'
});