Improve inventory

This commit is contained in:
asraelite 2018-03-07 00:03:53 +00:00
parent ee5ab45cfb
commit 27c6a8bcd0
12 changed files with 99 additions and 17 deletions

View file

@ -4,9 +4,8 @@ export const modules = {
capsule: { capsule: {
small: { small: {
name: 'Small Capsule', name: 'Small Capsule',
tooltip: 'A small, simple capsule. Provides just enough ' + tooltip: 'A small, simple capsule. Provides a small amount ' +
'rotational power for a small rocket and has a small ' + 'of rotational power and storage space.',
'amount of storage space.',
type: 'capsule', type: 'capsule',
id: 'small', id: 'small',
mass: 2, mass: 2,

View file

@ -73,6 +73,7 @@ export function endEditing() {
if (valid) { if (valid) {
audio.play('endEdit'); audio.play('endEdit');
particle.createEndEditBurst(world.playerShip);
graphics.changePerspective('universe'); graphics.changePerspective('universe');
game.state.editing = false; game.state.editing = false;
game.state.inventory = false; game.state.inventory = false;

View file

@ -65,9 +65,26 @@ class Tile {
this.mapId = toId(type, id); this.mapId = toId(type, id);
this.quantity = q; this.quantity = q;
this.image = assets.modules[type][id]; this.image = assets.modules[type][id];
this.data = modules[type][id];
if (type === 'thruster') this.image = this.image.off; if (type === 'thruster') this.image = this.image.off;
} }
get textInfo() {
let text = this.data.name + '\n\n' + this.data.tooltip + '\n\n';
text += 'Mass: ' + this.data.mass + '\n';
if (this.type === 'thruster')
text += 'Power: ' + this.data.thrust + '\n';
if (this.type === 'fuel')
text += 'Fuel capacity: ' + this.data.fuelCapacity + '\n';
if (this.type === 'capsule') {
text += 'Rotational power: ' + this.data.rotation + '\n';
text += 'Cargo space: ' + this.data.capacity + '\n';
}
return text;
}
get ident() { get ident() {
return [this.type, this.id]; return [this.type, this.id];
} }

View file

@ -36,7 +36,9 @@ function renderText(element) {
context.textAlign = element.align; context.textAlign = element.align;
context.textBaseline = element.valign; context.textBaseline = element.valign;
context.fillStyle = element.color; context.fillStyle = element.color;
context.fillText(element.text, element.x, element.y); element.text.split('\n').forEach((line, i) =>
context.fillText(line, element.x, element.y + i * element.spacing)
);
} }
function renderButton(element) { function renderButton(element) {

View file

@ -10,7 +10,7 @@ export default class GuiButton extends GuiElement {
} }
click() { click() {
if (this.options.draw && !this.options.disabled) if (this.drawn && !this.options.disabled)
this.onclick(); this.onclick();
} }
} }

View file

@ -26,6 +26,12 @@ export default class GuiElement extends Rect {
} }
get drawn() {
if (!this.options.drawChildren) return false;
if (!this.parent) return true;
return this.parent.drawn;
}
append(element) { append(element) {
this.children.add(element); this.children.add(element);
element.parent = this; element.parent = this;

View file

@ -12,6 +12,7 @@ export default class GuiInventory extends GuiElement {
this.tileHeight = 5; this.tileHeight = 5;
this.currentPage = 0; this.currentPage = 0;
inventory.setOnupdate(this.updateTiles.bind(this)); inventory.setOnupdate(this.updateTiles.bind(this));
this.guiInfo = null;
} }
updateTiles() { updateTiles() {
@ -35,14 +36,15 @@ export default class GuiInventory extends GuiElement {
let offset = pageSize * this.currentPage; let offset = pageSize * this.currentPage;
let tiles = inventory.getTiles().slice(offset); let tiles = inventory.getTiles().slice(offset);
let tile; let tile;
let cur = inventory.currentItem;
for (let y = 0; y < this.tileHeight; y++) for (let y = 0; y < this.tileHeight; y++)
for (let x = 0; x < this.tileWidth && tiles.length; x++) { for (let x = 0; x < this.tileWidth && tiles.length; x++) {
let i = y * this.tileWidth + (x % this.tileWidth) + offset; let i = y * this.tileWidth + (x % this.tileWidth) + offset;
tile = tiles.shift(); tile = tiles.shift();
let ex = x * tileSize + spacing / 2 + ox + this.x; let ex = x * tileSize + spacing / 2 + ox;
let ey = y * tileSize + spacing / 2 + oy + this.y; let ey = y * tileSize + spacing / 2 + oy;
let [ew, eh] = [tileSize - spacing, tileSize - spacing]; let [ew, eh] = [tileSize - spacing, tileSize - spacing];
let ident = tile.ident; let ident = tile.ident;
@ -51,7 +53,6 @@ export default class GuiInventory extends GuiElement {
this.tileClicked(...ident, button); this.tileClicked(...ident, button);
}; };
let cur = inventory.currentItem;
let selected = cur !== null && tile.type === cur.type let selected = cur !== null && tile.type === cur.type
&& tile.id === cur.id; && tile.id === cur.id;
@ -63,13 +64,18 @@ export default class GuiInventory extends GuiElement {
this.append(el); this.append(el);
} }
this.guiInfo.text = cur === null ? '' : cur.textInfo;
this.guiInfo.splitLines();
} }
tick() { tick() {
if (state.inventory && !this.active) this.updateTiles(); if (state.inventory && !this.active) this.updateTiles();
this.active = state.inventory; this.active = state.inventory;
this.options.draw = this.options.drawChildren = this.active; this.parent.options.drawChildren = this.active;
if (!this.active) return; if (!this.active) return;
this.children
} }
getTile(x, y) { getTile(x, y) {

View file

@ -17,10 +17,12 @@ export default class GuiItemButton extends GuiButton {
} }
click() { click() {
if (this.drawn)
this.onclick('left'); this.onclick('left');
} }
rightClick() { rightClick() {
if (this.drawn)
this.onclick('right'); this.onclick('right');
} }
} }

View file

@ -67,7 +67,7 @@ export function game() {
edit.posRelative({w: 1, h: 1}); edit.posRelative({w: 1, h: 1});
let editInfoText = new GuiText('', 0, 0, 0, 0, { let editInfoText = new GuiText('', 0, 0, 0, 0, {
size: 10, size: 12,
align: 'center' align: 'center'
}); });
editShadow.append(editInfoText); editShadow.append(editInfoText);
@ -75,7 +75,7 @@ export function game() {
editInfoText.y += 5; editInfoText.y += 5;
let editWarnText = new GuiText('', 0, 0, 0, 0, { let editWarnText = new GuiText('', 0, 0, 0, 0, {
size: 10, size: 12,
align: 'center' align: 'center'
}); });
editShadow.append(editWarnText); editShadow.append(editWarnText);
@ -93,18 +93,27 @@ export function game() {
invShadow.posRelative({x: 0, w: 0.4, h: 0.6}); invShadow.posRelative({x: 0, w: 0.4, h: 0.6});
invShadow.x += 10; invShadow.x += 10;
invShadow.y += 10; invShadow.y += 10;
invShadow.h += 60;
let inventory = new GuiInventory(0, 0, 0, 0); let inventory = new GuiInventory(0, 0, 0, 0);
invShadow.append(inventory); invShadow.append(inventory);
inventory.posRelative({w: 1, h: 1}); inventory.posRelative({w: 1, h: 1});
inventory.h -= 60;
let moduleInfo = new GuiText('test\nline\n', 0, 0, 0, 0, {
size: 12,
align: 'left',
valign: 'top'
});
invShadow.append(moduleInfo);
moduleInfo.posRelative({x: 0, y: 1, w: 1});
moduleInfo.splitLines();
moduleInfo.y += 5;
inventory.guiInfo = moduleInfo;
edit.guiInventory = inventory; edit.guiInventory = inventory;
let notification = new GuiText('', 0, 0, 0, 0, { let notification = new GuiText('', 0, 0, 0, 0, {
size: 12, size: 14,
align: 'center', align: 'center',
valign: 'top' valign: 'top'
}); });

View file

@ -1,5 +1,6 @@
import * as gui from './index.mjs'; import * as gui from './index.mjs';
import GuiElement from './element.mjs'; import GuiElement from './element.mjs';
import {context} from '../graphics/index.mjs';
export default class GuiText extends GuiElement { export default class GuiText extends GuiElement {
constructor(text = '', x, y, w, h, { constructor(text = '', x, y, w, h, {
@ -14,8 +15,33 @@ export default class GuiText extends GuiElement {
this.type = 'text'; this.type = 'text';
this.color = color; this.color = color;
this.text = text; this.text = text;
this.font = size + 'pt Consolas'; this.spacing = size * 1.2;
this.font = size + 'px Consolas';
this.align = align; this.align = align;
this.valign = valign; this.valign = valign;
} }
splitLines() {
// Not very robust, but good enough for now. Mebe fix later?
context.font = this.font;
let maxLineLength = (this.w / context.measureText('A').width) | 0;
maxLineLength = Math.max(maxLineLength, 1);
let lines = [];
let currentLine = '';
this.text.split('\n').forEach(l => {
currentLine = '';
l.split(' ').forEach(word => {
if (word.length + currentLine.length > maxLineLength) {
lines.push(currentLine.slice(0, -1));
currentLine = '';
}
currentLine += word + ' ';
});
lines.push(currentLine.slice(0, -1));
});
this.text = lines.join('\n');
}
} }

View file

@ -15,6 +15,18 @@ export function createThrustExhaust(thruster) {
})); }));
} }
export function createEndEditBurst(ship) {
for (let i = 0; i < 20; i++) {
particles.add(new Particle(...ship.poc, {
color: '#ccc',
lifetime: Math.random() * 30 + 25,
size: Math.random() * 0.3 + 0.05,
spray: 0.3,
gravity: true
}));
}
}
export function createPickupBurst(ship, point) { export function createPickupBurst(ship, point) {
for (let i = 0; i < 20; i++) { for (let i = 0; i < 20; i++) {
particles.add(new Particle(...point, { particles.add(new Particle(...point, {

View file

@ -16,6 +16,7 @@ export default class Ship extends Body {
this.maxRadius = 0; this.maxRadius = 0;
this.landed = false; this.landed = false;
this.lastContactModule = null; this.lastContactModule = null;
this.poc = this.com;
} }
get com() { get com() {
@ -121,6 +122,7 @@ export default class Ship extends Body {
this.halt(); this.halt();
this.resolveCelestialCollision(p, body, module); this.resolveCelestialCollision(p, body, module);
this.lastContactModule = module; this.lastContactModule = module;
this.poc = p;
} }
} }