Add rotation view and path prediction toggles
This commit is contained in:
parent
ce6a707526
commit
5b861cc341
12 changed files with 195 additions and 19 deletions
|
@ -55,20 +55,20 @@ export default class Body {
|
|||
return this.rotateVector(x, y, this.r);
|
||||
}
|
||||
|
||||
tickMotion() {
|
||||
this.x += this.xvel;
|
||||
this.y += this.yvel;
|
||||
this.r += this.rvel;
|
||||
this.rvel *= this.rfriction;
|
||||
tickMotion(speed = 1) {
|
||||
this.x += this.xvel * speed;
|
||||
this.y += this.yvel * speed;
|
||||
this.r += this.rvel * speed;
|
||||
this.rvel *= this.rfriction * speed;
|
||||
}
|
||||
|
||||
tickGravity(bodies) {
|
||||
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];
|
||||
let angle = Math.atan2(by - ay, bx - ax);
|
||||
this.xvel += Math.cos(angle) * force;
|
||||
this.yvel += Math.sin(angle) * force;
|
||||
this.xvel += Math.cos(angle) * force * speed;
|
||||
this.yvel += Math.sin(angle) * force * speed;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -100,4 +100,14 @@ export default class Body {
|
|||
this.yvel += vy;
|
||||
this.rvel += r / this.mass;
|
||||
}
|
||||
|
||||
orbit(cel, altitude) {
|
||||
this.gravity = true;
|
||||
let speed = Math.sqrt(G * cel.mass / (altitude + cel.radius));
|
||||
let [cx, cy] = cel.com;
|
||||
this.x = cx;
|
||||
this.y = cy - (altitude + cel.radius);
|
||||
this.yvel = 0;
|
||||
this.xvel = speed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ export default class Entity extends Body {
|
|||
yvel = 0,
|
||||
gravity = false
|
||||
} = {}) {
|
||||
super(x, y, 100);
|
||||
super(x, y, 0.1);
|
||||
|
||||
this.xvel = xvel;
|
||||
this.yvel = yvel;
|
||||
|
@ -31,10 +31,6 @@ export default class Entity extends Body {
|
|||
return [this.x + this.width / 2, this.y + this.height / 2];
|
||||
}
|
||||
|
||||
orbit(celestial, radius) {
|
||||
this.gravity = true;
|
||||
}
|
||||
|
||||
remove() {
|
||||
entities.delete(this);
|
||||
}
|
||||
|
@ -50,10 +46,13 @@ export default class Entity extends Body {
|
|||
}
|
||||
|
||||
if (playerShip.colliding(this.com, this.radius)) {
|
||||
if (this.touched) return;
|
||||
let success = events.collectItem(this.type, this.id);
|
||||
if (!success) return;
|
||||
particle.createPickupBurst(playerShip, this.com);
|
||||
this.remove();
|
||||
} else {
|
||||
this.touched = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ export const entities = new Set();
|
|||
export const celestials = new Set();
|
||||
export const ships = new Set();
|
||||
export const particles = new Set();
|
||||
export const tracers = new Set();
|
||||
|
||||
export let playerShip = null;
|
||||
|
||||
|
@ -19,9 +20,10 @@ export function init() {
|
|||
celestials.clear();
|
||||
ships.clear();
|
||||
particles.clear();
|
||||
tracers.clear();
|
||||
spawn.player();
|
||||
spawn.startPlanet();
|
||||
spawn.testEntity();
|
||||
let p = spawn.startPlanet();
|
||||
spawn.testEntity(p);
|
||||
}
|
||||
|
||||
export function tick() {
|
||||
|
@ -29,4 +31,5 @@ export function tick() {
|
|||
celestials.forEach(c => c.tick());
|
||||
entities.forEach(e => e.tick());
|
||||
ships.forEach(s => s.tick());
|
||||
tracers.forEach(t => t.tick());
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import * as world from './index.mjs';
|
|||
import * as consts from '../consts.mjs';
|
||||
import * as particle from './particle.mjs';
|
||||
import * as events from '../game/events.mjs';
|
||||
import Tracer from './tracer.mjs';
|
||||
import {state} from '../game/index.mjs';
|
||||
|
||||
export default class Ship extends Body {
|
||||
|
|
|
@ -2,6 +2,7 @@ import Ship from './ship.mjs';
|
|||
import Module from './module.mjs';
|
||||
import Celestial from './celestial.mjs';
|
||||
import Entity from './entity.mjs';
|
||||
import Tracer from './tracer.mjs';
|
||||
import {modules} from '../data.mjs';
|
||||
import * as world from './index.mjs';
|
||||
|
||||
|
@ -12,6 +13,10 @@ export function player() {
|
|||
ship.addModule(0, 2, modules.thruster.light);
|
||||
world.ships.add(ship);
|
||||
world.setPlayerShip(ship);
|
||||
|
||||
let tracer = new Tracer(ship);
|
||||
world.tracers.add(tracer);
|
||||
|
||||
return ship;
|
||||
}
|
||||
|
||||
|
@ -22,9 +27,10 @@ export function startPlanet() {
|
|||
});
|
||||
}
|
||||
|
||||
export function testEntity() {
|
||||
export function testEntity(parent) {
|
||||
let entity = new Entity(0, -50);
|
||||
world.entities.add(entity);
|
||||
entity.orbit(parent, 10);
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
|
51
js/world/tracer.mjs
Normal file
51
js/world/tracer.mjs
Normal file
|
@ -0,0 +1,51 @@
|
|||
import Body from './body.mjs';
|
||||
import {modules} from '../data.mjs';
|
||||
import {playerShip} from './index.mjs';
|
||||
import {images as assets} from '../assets.mjs';
|
||||
import {celestials, particles, entities} from './index.mjs';
|
||||
import * as particle from './particle.mjs';
|
||||
import * as consts from '../consts.mjs';
|
||||
import * as events from '../game/events.mjs';
|
||||
|
||||
export default class Tracer extends Body {
|
||||
constructor(ship) {
|
||||
super(...ship.pos, 0.1);
|
||||
|
||||
this.ship = ship;
|
||||
this.path = [];
|
||||
}
|
||||
|
||||
run(distance) {
|
||||
this.path = [];
|
||||
[this.x, this.y] = this.ship.com;
|
||||
[this.xvel, this.yvel] = [this.ship.xvel, this.ship.yvel];
|
||||
let dis = 0;
|
||||
let last = this.pos;
|
||||
let factor = 5;
|
||||
|
||||
for (let i = 0; dis < distance; i++) {
|
||||
if (this.tickPath(factor)) break;
|
||||
this.path.push(this.pos);
|
||||
|
||||
if (i % 10 === 0) {
|
||||
let [lx, ly] = last;
|
||||
dis += Math.sqrt((this.x - lx) ** 2 + (this.y - ly) ** 2);
|
||||
last = this.pos;
|
||||
}
|
||||
|
||||
if (i > distance * 5) break;
|
||||
}
|
||||
|
||||
[this.x, this.y] = this.ship.com;
|
||||
}
|
||||
|
||||
tick() {
|
||||
this.run(100);
|
||||
}
|
||||
|
||||
tickPath(speed) {
|
||||
this.tickMotion(speed);
|
||||
this.tickGravity(celestials, speed);
|
||||
return !!this.getCelestialCollision(celestials);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue