General improvements

I forgot what I actually changed. It may not even be playable, I just want to get this up there.
This commit is contained in:
Asraelite 2023-03-31 11:43:56 +02:00
parent 8a0bf0ada9
commit c73130e3ff
25 changed files with 584 additions and 274 deletions

View file

@ -72,13 +72,15 @@ export default class Body {
}
tickGravity(bodies, speed = 1) {
bodies.forEach(b => {
let force = b.mass / (this.distanceTo(b) ** 2) * G;
let [[ax, ay], [bx, by]] = [this.com, b.com];
for (let body of bodies) {
const distanceSquared = this.distanceToSquared(body);
if (distanceSquared > (1000 ** 2)) continue;
let force = body.mass / distanceSquared * G;
let [[ax, ay], [bx, by]] = [this.com, body.com];
let angle = Math.atan2(by - ay, bx - ax);
this.xvel += Math.cos(angle) * force * speed;
this.yvel += Math.sin(angle) * force * speed;
});
}
}
distanceTo(body) {
@ -87,6 +89,12 @@ export default class Body {
((by - ay) ** 2)), 1);
}
distanceToSquared(body) {
let [[ax, ay], [bx, by]] = [this.com, body.com];
return Math.max(((bx - ax) ** 2) +
((by - ay) ** 2), 1);
}
angleTo(ax, ay, bx, by) {
return Math.atan2(by - ay, bx - ax);
}

View file

@ -1,5 +1,7 @@
import {tempCanvas, tempContext} from '../graphics/index.mjs';
import {images as assets} from '../assets.mjs';
import Body from './body.mjs';
import { PLANET_IMAGE_SIZE } from '../consts.mjs';
export default class Celestial extends Body {
constructor(x, y, radius, {
@ -11,8 +13,15 @@ export default class Celestial extends Body {
this.radius = radius;
this.type = type;
let imageArr = Object.values(assets.celestials[this.type]);
this.image = imageArr[Math.random() * imageArr.length | 0];
const imageArr = Object.values(assets.celestials[this.type]);
const svgImage = imageArr[Math.random() * imageArr.length | 0];
tempCanvas.width = PLANET_IMAGE_SIZE;
tempCanvas.height = PLANET_IMAGE_SIZE;
tempContext.clearRect(0, 0, PLANET_IMAGE_SIZE, PLANET_IMAGE_SIZE);
tempContext.drawImage(svgImage, 0, 0, PLANET_IMAGE_SIZE, PLANET_IMAGE_SIZE);
this.image = new Image();
this.image.src = tempCanvas.toDataURL();
// this.image = tempContext.getImageData(0, 0, PLANET_IMAGE_SIZE, PLANET_IMAGE_SIZE);
}
get com() {

View file

@ -9,6 +9,8 @@ export const tracers = new Set();
export let playerShip = null;
export let speed = 1;
export function setPlayerShip(ship) {
playerShip = ship;
}
@ -33,11 +35,22 @@ export function remove(object) {
celestials.delete(object);
}
export function tick() {
particles.forEach(p => p.tick());
celestials.forEach(c => c.tick());
entities.forEach(e => e.tick());
ships.forEach(s => s.tick());
if (graphics.trace) tracers.forEach(t => t.tick());
spawn.tick();
export function increaseSpeed() {
if (speed < 5) speed += 1;
}
export function decreaseSpeed() {
if (speed > 1) speed -= 1;
}
export function tick() {
for (let i = 0; i < speed; i++) {
particles.forEach(p => p.tick());
celestials.forEach(c => c.tick());
entities.forEach(e => e.tick());
ships.forEach(s => s.tick());
}
spawn.tick();
if (graphics.trace) tracers.forEach(t => t.tick());
}

View file

@ -22,6 +22,7 @@ export default class Ship extends Body {
this.rotationPower = 0;
this.cargoCapacity = 0;
this.thrust = 0;
this.computation = 0;
this.crashed = false;
this.timeWithoutFuel = 0;
}
@ -113,6 +114,7 @@ export default class Ship extends Body {
this.rotationPower = 0;
this.cargoCapacity = 0;
this.thrust = 0;
this.computation = 0;
this.modules.forEach(m => {
if (m.type === 'fuel') {
@ -120,12 +122,15 @@ export default class Ship extends Body {
} else if (m.type === 'capsule') {
this.rotationPower += m.data.rotation;
this.cargoCapacity += m.data.capacity;
this.computation += m.data.computation;
} else if (m.type === 'thruster') {
this.thrust += m.data.thrust;
} else if (m.type === 'gyroscope') {
this.rotationPower += m.data.rotation;
} else if (m.type === 'cargo') {
this.cargoCapacity += m.data.capacity;
} else if (m.type === 'navigation') {
this.computation += m.data.computation;
}
});
}

View file

@ -40,7 +40,7 @@ export default class Tracer extends Body {
}
tick() {
this.run(100);
this.run(this.ship.computation);
}
tickPath(speed) {