Add score and fuel overlay

This commit is contained in:
asraelite 2018-03-07 01:23:35 +00:00
parent 27c6a8bcd0
commit 4f8fd6e1af
13 changed files with 130 additions and 37 deletions

View file

@ -24,8 +24,8 @@ export const TIP_ANGLE = 0.25;
export const TIP_SPEED = 0.03;
// Ship flight mechanics. Speed measured in units per tick.
export const FUEL_BURN_RATE = 0.01;
export const THRUST_POWER = 0.007;
export const TURN_POWER = 0.05;
export const THRUST_POWER = 0.004;
export const TURN_POWER = 0.07;
// Distance at which an orbited planet will not be considered a parent body.
export const MAX_PARENT_CELESTIAL_DISTANCE = 120;
// Ship editing.

View file

@ -11,7 +11,7 @@ export const modules = {
mass: 2,
connectivity: [false, false, true, false],
capacity: 2,
rotation: 0.1
rotation: 1
}
},
fuel: {
@ -29,13 +29,12 @@ export const modules = {
light: {
name: 'Light Main Thruster',
tooltip: 'Powerful enough to lift a small ship, but not much ' +
'more. Not very efficient.',
'more.',
type: 'thruster',
id: 'light',
mass: 2,
connectivity: [true, false, false, false],
thrust: 10,
isp: 200
thrust: 10
}
},
connector: {

View file

@ -1,8 +1,8 @@
import * as input from '../input.mjs';
import * as events from './events.mjs';
import * as player from './player.mjs';
import * as graphics from '../graphics/index.mjs';
import * as inventory from './inventory.mjs';
import {playerShip} from '../world/index.mjs';
import {state} from './index.mjs';
export const mapping = {
@ -36,15 +36,15 @@ export function tick() {
function tickPlaying() {
if (held[mapping.thrust]) {
player.ship.applyThrust({ forward: 1 });
playerShip.applyThrust({ forward: 1 });
}
if (held[mapping.left]) {
player.ship.applyThrust({ turnLeft: 1 });
playerShip.applyThrust({ turnLeft: 1 });
}
if (held[mapping.right]) {
player.ship.applyThrust({ turnRight: 1 });
playerShip.applyThrust({ turnRight: 1 });
}
if (pressed[mapping.inventory]) {
@ -67,4 +67,6 @@ function tickEditing() {
if (pressed[mapping.exitEdit]) {
events.endEditing();
}
if (pressed['KeyX']) throw new Error();
}

View file

@ -13,6 +13,7 @@ export let height = 0;
export let position = [0, 0];
export let bounds = [0, 0, 0, 0];
export let message = '';
export let info = '';
export function init() {
let ship = world.playerShip;
@ -38,6 +39,7 @@ function adjustSize() {
let [sx, ex, sy, ey] = getBoundaries();
[width, height] = [ex - sx + margin * 2 + 1, ey - sy + margin * 2 + 1];
position = [sx - margin, sy - margin];
getAttributes();
}
export function end() {
@ -65,6 +67,35 @@ export function end() {
return result;
}
function getAttributes() {
let cargo = 0;
let fuel = 0;
let rotation = 0;
let mass = 0;
let thrust = 0;
tiles.forEach(t => {
if (t.type === null) return;
if (t.type === 'fuel') {
fuel += t.module.fuelCapacity;
} else if (t.type === 'capsule') {
rotation += t.module.rotation;
cargo += t.module.capacity;
} else if (t.type === 'thruster') {
thrust += t.module.thrust;
} else if (t.type === 'gyroscope') {
rotation += t.module.rotation;
}
mass += t.module.mass;
});
info = 'Mass: ' + mass + '\n' +
'Fuel capacity: ' + fuel + '\n' +
'Thrust/mass ratio: ' + (thrust / mass).toFixed(1) + '\n' +
'Rotation speed: ' + (rotation / mass * 100).toFixed(1) + '\n' +
'Cargo capacity: ' + cargo;
}
function validate() {
let capsulesFound = 0;
let thrustersFound = 0;

View file

@ -1,21 +1,23 @@
import * as game from './index.mjs';
import * as graphics from '../graphics/index.mjs';
import * as world from '../world/index.mjs';
import * as player from './player.mjs';
import * as inventory from './inventory.mjs';
import * as particle from '../world/particle.mjs';
import * as edit from './edit.mjs';
import * as audio from './audio.mjs';
export let shipLanded = false;
export let score = 0;
let notification = null;
let notLife = 0;
let landedPlanets = new Set();
function notify(message) {
if (notification === null) return;
notification.text = message;
notLife = 60;
notLife = 80;
}
export function tick() {
@ -33,11 +35,21 @@ export function startGame() {
graphics.perspective.focusPlayer();
}
export function landShip() {
export function landShip(planet) {
shipLanded = true;
if (!landedPlanets.has(planet)) {
newPlanet(planet);
}
game.state.landed = true;
}
function newPlanet(planet) {
let value = (planet.radius + 30) | 0;
landedPlanets.add(planet);
score += value;
notify('Landed on new planet: +' + value);
}
export function launchShip() {
shipLanded = false;
game.state.landed = false;
@ -95,5 +107,7 @@ export function tossItem() {
export function collectItem(type, id) {
inventory.addItem(type, id);
audio.play('itemPickup');
score += 20;
notify('Collected module: +20');
return true;
}

View file

@ -6,7 +6,6 @@ import * as inventory from './inventory.mjs';
import * as world from '../world/index.mjs';
import * as events from './events.mjs';
import * as control from './control.mjs';
import * as player from './player.mjs';
import * as edit from './edit.mjs';
export let state;
@ -45,7 +44,6 @@ export function changeView(view) {
state.editing = false;
state.paused = false;
world.init();
player.init();
inventory.init();
}
}

View file

@ -1,8 +0,0 @@
import * as world from '../world/index.mjs';
import * as inventory from './inventory.mjs';
export let ship;
export function init() {
ship = world.playerShip;
}

View file

@ -7,7 +7,6 @@ export function render() {
}
function renderElement(element) {
//console.log(element.options);
if (element.options.draw) {
if (element.type === 'frame') renderFrame(element);
if (element.type === 'image') renderImage(element);
@ -93,7 +92,7 @@ function renderItemButton(element) {
context.drawImage(element.image, ox, oy, dw, dh);
}
if (element.quantity > 1) { // CHANGE TO 1
if (element.quantity > 1) {
context.textAlign = 'right';
context.textBaseline = 'bottom';
context.fillStyle = '#fff';
@ -110,6 +109,6 @@ function renderEdit(element) {
function renderInventory(element) {
context.globalAlpha = 0.1;
context.fillStyle = '#541';
context.fillRect(...element.shape);
context.fillRect(...element.parent.shape);
context.globalAlpha = 1;
}

View file

@ -57,7 +57,7 @@ export default class GuiEdit extends GuiElement {
this.parent.options.drawChildren = this.active;
if (!this.active) return;
this.textElements.info.text = edit.message;
this.textElements.info.text = edit.info;
[this.tileWidth, this.tileHeight] = [edit.width, edit.height];
}

View file

@ -10,6 +10,7 @@ import GuiText from './text.mjs';
import GuiInventory from './inventory.mjs';
import * as events from '../game/events.mjs';
import {state} from '../game/index.mjs';
import * as world from '../world/index.mjs';
export function root() {
return new GuiFrame(0, 0, canvas.width, canvas.height, {
@ -51,11 +52,41 @@ export function game() {
editButton.options.disabled = state.editing && editMessage !== '';
if (state.editing) {
editButton.text = 'Finish';
if (editMessage !== '') editButton.text = '(' + editMessage + ')';
} else {
editButton.text = 'Edit rocket';
}
}
let fuel = new GuiText('', 0, 0, 0, 0, {
size: 14,
align: 'right',
valign: 'bottom'
});
shadow.append(fuel);
fuel.posRelative({x: 1, y: 1});
fuel.y -= 10;
fuel.x -= 10;
fuel.tick = () => {
let ship = world.playerShip;
fuel.text = 'Fuel: ' + ship.fuel.toFixed(1) + '/' +
ship.maxFuel.toFixed(1);
};
let score = new GuiText('', 0, 0, 0, 0, {
size: 14,
align: 'left',
valign: 'bottom'
});
shadow.append(score);
score.posRelative({x: 0, y: 1});
score.y -= 10;
score.x += 10;
score.tick = () => {
score.text = 'Score: ' + events.score
};
let editShadow = root();
shadow.append(editShadow);
editShadow.posRelative({x: 0.45, y: 0, w: 0.55, h: 0.6});
@ -68,11 +99,12 @@ export function game() {
let editInfoText = new GuiText('', 0, 0, 0, 0, {
size: 12,
align: 'center'
align: 'right'
});
editShadow.append(editInfoText);
editInfoText.posRelative({x: 0.5, y: 1});
editInfoText.posRelative({x: 1, y: 1});
editInfoText.y += 5;
editInfoText.x -= 20;
let editWarnText = new GuiText('', 0, 0, 0, 0, {
size: 12,

View file

@ -23,19 +23,17 @@ export function init() {
});
window.addEventListener('keyup', event => {
keyCode.pressed[event.code] = false;
keyCode.held[event.code] = false;
key.pressed[event.key] = false;
key.held[event.key] = false;
});
// Ṕ͕͖ẖ̨̖̺͓̪̹n̼͇͔̯̝̖g̙̩̭͕ͅͅl̻̰͘u͎̥͍̗ͅi̼̞̪̩͚̜͖ ̫̝̻͚͟m͎̳̙̭̩̩̕g̟̤̬̮l̫̕w̶͚͈͚̟͔͖n͏̝͖̞̺ͅa͏̹͓̬̺f̗̬̬̬̖̫͜h͙̘̝̱̬̗͜ ̼͎͖C̱͔̱͖ṭ̬̱͖h̵̰̼̘̩ùlh̙́u̪̫ ̪̺̹̙̯R̞͓̹̞͍͎͉͎̦͙ͅl͇̠̮y̙̪̰̪͙̖e̢̩͉͙̼h̗͔̹̳ ̶w̨̼͍̝̭̣̣ͅg̶̳̦̳a̴͉̹͙̭̟ͅh͈͎̞̜͉̼̜̠́͞n̲a̯g̮͚͓̝l̠ ̹̹̱͙̝f̧̝͖̱h̪̟̻͖̖t͎͘aͅg̤̘͜n̶͈̻̻̝̳
window.addEventListener('mousedown', event => {
mouse.pressed[event.button] = !mouse.held[event.button];
mouse.held[event.button] = true;
tickAfterMouse = false;
});
window.addEventListener('mouseup', event => {
mouse.pressed[event.button] = false;
mouse.held[event.button] = false;
});

View file

@ -105,8 +105,8 @@ export default class Body {
applyDirectionalForce(x, y, r) {
let [vx, vy] = this.rotateVector(x, y);
this.xvel += vx;
this.yvel += vy;
this.xvel += vx / this.mass;
this.yvel += vy / this.mass;
this.rvel += r / this.mass;
}

View file

@ -17,6 +17,11 @@ export default class Ship extends Body {
this.landed = false;
this.lastContactModule = null;
this.poc = this.com;
this.maxFuel = 0;
this.fuel = 0;
this.rotationPower = 0;
this.cargoCapacity = 0;
this.thrust = 0;
}
get com() {
@ -56,8 +61,13 @@ export default class Ship extends Body {
this.modules.forEach(m => m.reset());
if (events.shipLanded != this.landed)
this.landed ? events.landShip() : events.launchShip();
if (events.shipLanded != this.landed) {
if (this.landed) {
events.landShip(this.parentCelestial)
} else {
events.launchShip()
}
}
}
clearModules() {
@ -85,6 +95,23 @@ export default class Ship extends Body {
let [lx, ly] = this.localCom;
this.maxRadius = points.reduce((a, [bx, by]) =>
Math.max(Math.sqrt((bx - lx) ** 2 + (by - ly) ** 2), a), 0) + 1;
this.maxFuel = 0;
this.rotationPower = 0;
this.cargoCapacity = 0;
this.modules.forEach(m => {
if (m.type === 'fuel') {
this.maxFuel += m.data.fuelCapacity;
} else if (m.type === 'capsule') {
this.rotationPower += m.data.rotation;
this.cargoCapacity += m.data.capacity;
} else if (m.type === 'thruster') {
this.thrust += m.data.thrust;
} else if (m.type === 'gyroscope') {
this.rotationPower += m.data.rotation;
}
});
}
colliding(point, radius) {
@ -158,8 +185,9 @@ export default class Ship extends Body {
applyThrust({ forward = 0, left = 0, right = 0, back = 0,
turnLeft = 0, turnRight = 0}) {
let thrustForce = -forward * consts.THRUST_POWER;
let thrustForce = -forward * consts.THRUST_POWER * this.thrust;
let turnForce = (turnRight - turnLeft) * consts.TURN_POWER;
turnForce *= this.rotationPower;
this.applyDirectionalForce(0, thrustForce, turnForce);