Add collectable fuel cans

This commit is contained in:
asraelite 2018-03-07 02:19:06 +00:00
parent 4f8fd6e1af
commit b27bd7bba2
9 changed files with 141 additions and 27 deletions

View file

@ -110,13 +110,13 @@ export default class Body {
this.rvel += r / this.mass;
}
orbit(cel, altitude) {
orbit(cel, altitude, angle = 0) {
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;
let [dx, dy] = this.rotateVector(0, -(altitude + cel.radius), angle);
[this.xvel, this.yvel] = this.rotateVector(speed, 0, angle);
this.x = cx + dx;
this.y = cy + dy;
}
}

View file

@ -14,7 +14,6 @@ export default class Entity extends Body {
gravity = false
} = {}) {
super(x, y, 0.1);
this.xvel = xvel;
this.yvel = yvel;
this.width = 1;
@ -22,7 +21,13 @@ export default class Entity extends Body {
this.radius = (this.width + this.height) / 2;
this.type = type;
this.id = id;
this.image = assets.modules[type][id];
if (this.type === 'fuelcan') {
this.image = assets.modules.fuelcan;
} else {
this.image = assets.modules[type][id];
if (this.type === 'thruster')
this.image = this.image.off;
}
this.gravity = gravity;
this.touched = false;
}
@ -36,6 +41,8 @@ export default class Entity extends Body {
}
tick() {
if (Math.abs(playerShip.x - this.x) > 500 ||
Math.abs(playerShip.y - this.y) > 500) return;
this.r += consts.ENTITY_ROTATION_RATE;
this.tickMotion();
if (this.gravity) this.tickGravity(celestials);
@ -47,6 +54,7 @@ export default class Entity extends Body {
if (playerShip.colliding(this.com, this.radius)) {
if (this.touched) return;
this.touched = true;
let success = events.collectItem(this.type, this.id);
if (!success) return;
particle.createPickupBurst(playerShip, this.com);

View file

@ -7,8 +7,6 @@ export default class Module {
type = 'block',
id = 'unknown',
mass = 1,
// Fuel
filled = false,
fuelCapacity = 0,
...properties
}) {
@ -21,10 +19,8 @@ export default class Module {
this.id = id;
this.images = assets.modules[this.type][this.id];
this.data = modules[this.type][this.id];
// Fuel
if (this.type == 'fuel') {
this.fuel = filled ? fuelCapacity : 0;
} else if (this.type == 'thruster') {
if (this.type == 'thruster') {
this.power = 0;
}
}

View file

@ -74,6 +74,10 @@ export default class Ship extends Body {
this.modules.clear();
}
addFuel(amount) {
this.fuel = Math.min(this.fuel + amount, this.maxFuel);
}
addModule(x, y, properties, options) {
let module = new Module(x, y, this, {...properties, ...options});
this.modules.add(module);
@ -188,6 +192,7 @@ export default class Ship extends Body {
let thrustForce = -forward * consts.THRUST_POWER * this.thrust;
let turnForce = (turnRight - turnLeft) * consts.TURN_POWER;
turnForce *= this.rotationPower;
this.fuel -= Math.abs(thrustForce) * consts.FUEL_BURN_RATE;
this.applyDirectionalForce(0, thrustForce, turnForce);

View file

@ -41,11 +41,11 @@ function nearest(x, y, set) {
function spawnSector(x, y) {
let area = SS ** 2;
for (let i = 0; i < area / 10000; i++) {
for (let i = 0; i < area / 1000; i++) {
let [px, py] = [(x + Math.random()) * SS, (y + Math.random()) * SS];
if (Math.random() > consts.PLANET_SPAWN_RATE) {
if (Math.random() < consts.PLANET_SPAWN_RATE / 1000) {
randomPlanet(px, py);
} else if (Math.random() > 0.01 ){
} else if (Math.random() < consts.ENTITY_SPAWN_RATE / 1000){
randomEntity(px, py);
}
}
@ -68,13 +68,23 @@ function randomPlanet(x, y) {
for (let i = 1.5; i < 8; i += 1) {
if (Math.random() > consts.ENTITY_SPAWN_RATE) {
let e = randomEntity();
e.orbit(planet, i * rad);
e.orbit(planet, i * rad, Math.random() * Math.PI * 2);
}
}
}
function randomEntity(x, y) {
let entity = new Entity(x, y);
let entity, type, id;
if (Math.random() > 0.3) {
entity = new Entity(x, y, 'fuelcan');
} else {
let arr = Object.entries(modules);
[type, arr] = arr[Math.random() * arr.length | 0];
arr = Object.keys(arr);
entity = new Entity(x, y, type, arr[Math.random() * arr.length | 0]);
}
world.entities.add(entity);
return entity;
}
@ -88,6 +98,7 @@ export function player() {
//ship.addModule(-1, 2, modules.thruster.light);
world.ships.add(ship);
world.setPlayerShip(ship);
ship.addFuel(ship.maxFuel);
let tracer = new Tracer(ship);
world.tracers.add(tracer);