Add inventory space limit

This commit is contained in:
asraelite 2018-03-07 17:41:51 +00:00
parent a11dfb9352
commit 0c63cb075b
5 changed files with 44 additions and 9 deletions

View file

@ -97,7 +97,7 @@ function getAttributes() {
'Cargo capacity: ' + cargo; 'Cargo capacity: ' + cargo;
} }
function validate() { export function validate() {
let capsulesFound = 0; let capsulesFound = 0;
let thrustersFound = 0; let thrustersFound = 0;
let fuelFound = 0; let fuelFound = 0;
@ -136,6 +136,8 @@ function validate() {
reason = 'no thruster' reason = 'no thruster'
} else if (fuelFound === 0) { } else if (fuelFound === 0) {
reason = 'no fuel tank' reason = 'no fuel tank'
} else if (inventory.usedSpace > inventory.capacity) {
reason = 'inventory too full';
} else { } else {
reason = false; reason = false;
} }

View file

@ -182,6 +182,10 @@ export function collectItem(type, id, name) {
notify('Collected fuel: +10'); notify('Collected fuel: +10');
return true; return true;
} else { } else {
if (inventory.usedSpace > inventory.capacity) {
notify('No space left in inventory', 60);
return false;
}
inventory.addItem(type, id); inventory.addItem(type, id);
audio.play('itemPickup'); audio.play('itemPickup');
score += 20; score += 20;

View file

@ -1,10 +1,14 @@
import {state} from './index.mjs'; import {state} from './index.mjs';
import {modules} from '../data.mjs'; import {modules} from '../data.mjs';
import {playerShip} from '../world/index.mjs';
import {images as assets} from '../assets.mjs'; import {images as assets} from '../assets.mjs';
import * as edit from '../game/edit.mjs';
import * as events from './events.mjs'; import * as events from './events.mjs';
export const items = new Map(); export const items = new Map();
export let currentItem = null; export let currentItem = null;
export let capacity = 0;
export let usedSpace = 0;
let onupdate = () => {}; let onupdate = () => {};
@ -16,9 +20,15 @@ export function init() {
addItem('connector', 'xheavy'); addItem('connector', 'xheavy');
} }
export function canToss() {
return !state.editing || edit.message === 'inventory too full'
|| edit.message === '';
}
export function getTiles() { export function getTiles() {
let list = Array.from(items.values()); let list = Array.from(items.values());
list.sort((a, b) => toId(...a.ident) < toId(...b.ident)); list.sort((a, b) => toId(...a.ident) < toId(...b.ident));
usedSpace = list.reduce((a, b) => a + b.quantity, 0);
return list; return list;
} }
@ -39,9 +49,10 @@ export function removeItem(type, id) {
items.delete(mapId); items.delete(mapId);
currentItem = null; currentItem = null;
} }
if (!state.editing) if (canToss())
events.tossItem(); events.tossItem();
update(); update();
edit.validate();
} }
export function selectItem(type, id) { export function selectItem(type, id) {
@ -54,6 +65,7 @@ export function setOnupdate(func) {
} }
function update() { function update() {
capacity = playerShip.cargoCapacity;
onupdate(); onupdate();
} }

View file

@ -85,9 +85,11 @@ export default class GuiInventory extends GuiElement {
tileClicked(type, id, button) { tileClicked(type, id, button) {
if (button == 'left') inventory.selectItem(type, id); if (button == 'left') inventory.selectItem(type, id);
if (!state.editing && button == 'right') { if (button == 'right') {
if (inventory.canToss()) {
inventory.removeItem(type, id); inventory.removeItem(type, id);
} }
}
this.updateTiles(); this.updateTiles();
} }

View file

@ -8,6 +8,7 @@ import GuiButton from './button.mjs';
import GuiEdit from './edit.mjs'; import GuiEdit from './edit.mjs';
import GuiText from './text.mjs'; import GuiText from './text.mjs';
import GuiInventory from './inventory.mjs'; import GuiInventory from './inventory.mjs';
import * as inventory from '../game/inventory.mjs';
import * as events from '../game/events.mjs'; import * as events from '../game/events.mjs';
import {state} from '../game/index.mjs'; import {state} from '../game/index.mjs';
import * as world from '../world/index.mjs'; import * as world from '../world/index.mjs';
@ -183,9 +184,23 @@ export function game() {
invShadow.x += 10; invShadow.x += 10;
invShadow.y += 10; invShadow.y += 10;
let inventory = new GuiInventory(0, 0, 0, 0); let invElement = new GuiInventory(0, 0, 0, 0);
invShadow.append(inventory); invShadow.append(invElement);
inventory.posRelative({w: 1, h: 1}); invElement.posRelative({w: 1, h: 0.8});
let capacityInfo = new GuiText('', 0, 0, 0, 0, {
size: 12,
align: 'left',
valign: 'bottom'
});
invShadow.append(capacityInfo);
capacityInfo.posRelative({x: 0, y: 1});
capacityInfo.y -= 5;
capacityInfo.x += 5;
capacityInfo.tick = () => {
capacityInfo.text = 'Space used: ' + inventory.usedSpace + ' / ' +
inventory.capacity;
};
let moduleInfo = new GuiText('', 0, 0, 0, 0, { let moduleInfo = new GuiText('', 0, 0, 0, 0, {
size: 12, size: 12,
@ -196,9 +211,9 @@ export function game() {
moduleInfo.posRelative({x: 0, y: 1, w: 1}); moduleInfo.posRelative({x: 0, y: 1, w: 1});
moduleInfo.splitLines(); moduleInfo.splitLines();
moduleInfo.y += 5; moduleInfo.y += 5;
inventory.guiInfo = moduleInfo; invElement.guiInfo = moduleInfo;
edit.guiInventory = inventory; edit.guiInventory = invElement;
let notification = new GuiText('', 0, 0, 0, 0, { let notification = new GuiText('', 0, 0, 0, 0, {