Add inventory space limit
This commit is contained in:
parent
a11dfb9352
commit
0c63cb075b
5 changed files with 44 additions and 9 deletions
|
@ -97,7 +97,7 @@ function getAttributes() {
|
|||
'Cargo capacity: ' + cargo;
|
||||
}
|
||||
|
||||
function validate() {
|
||||
export function validate() {
|
||||
let capsulesFound = 0;
|
||||
let thrustersFound = 0;
|
||||
let fuelFound = 0;
|
||||
|
@ -136,6 +136,8 @@ function validate() {
|
|||
reason = 'no thruster'
|
||||
} else if (fuelFound === 0) {
|
||||
reason = 'no fuel tank'
|
||||
} else if (inventory.usedSpace > inventory.capacity) {
|
||||
reason = 'inventory too full';
|
||||
} else {
|
||||
reason = false;
|
||||
}
|
||||
|
|
|
@ -182,6 +182,10 @@ export function collectItem(type, id, name) {
|
|||
notify('Collected fuel: +10');
|
||||
return true;
|
||||
} else {
|
||||
if (inventory.usedSpace > inventory.capacity) {
|
||||
notify('No space left in inventory', 60);
|
||||
return false;
|
||||
}
|
||||
inventory.addItem(type, id);
|
||||
audio.play('itemPickup');
|
||||
score += 20;
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
import {state} from './index.mjs';
|
||||
import {modules} from '../data.mjs';
|
||||
import {playerShip} from '../world/index.mjs';
|
||||
import {images as assets} from '../assets.mjs';
|
||||
import * as edit from '../game/edit.mjs';
|
||||
import * as events from './events.mjs';
|
||||
|
||||
export const items = new Map();
|
||||
export let currentItem = null;
|
||||
export let capacity = 0;
|
||||
export let usedSpace = 0;
|
||||
|
||||
let onupdate = () => {};
|
||||
|
||||
|
@ -16,9 +20,15 @@ export function init() {
|
|||
addItem('connector', 'xheavy');
|
||||
}
|
||||
|
||||
export function canToss() {
|
||||
return !state.editing || edit.message === 'inventory too full'
|
||||
|| edit.message === '';
|
||||
}
|
||||
|
||||
export function getTiles() {
|
||||
let list = Array.from(items.values());
|
||||
list.sort((a, b) => toId(...a.ident) < toId(...b.ident));
|
||||
usedSpace = list.reduce((a, b) => a + b.quantity, 0);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
@ -39,9 +49,10 @@ export function removeItem(type, id) {
|
|||
items.delete(mapId);
|
||||
currentItem = null;
|
||||
}
|
||||
if (!state.editing)
|
||||
if (canToss())
|
||||
events.tossItem();
|
||||
update();
|
||||
edit.validate();
|
||||
}
|
||||
|
||||
export function selectItem(type, id) {
|
||||
|
@ -54,6 +65,7 @@ export function setOnupdate(func) {
|
|||
}
|
||||
|
||||
function update() {
|
||||
capacity = playerShip.cargoCapacity;
|
||||
onupdate();
|
||||
}
|
||||
|
||||
|
|
|
@ -85,8 +85,10 @@ export default class GuiInventory extends GuiElement {
|
|||
tileClicked(type, id, button) {
|
||||
if (button == 'left') inventory.selectItem(type, id);
|
||||
|
||||
if (!state.editing && button == 'right') {
|
||||
inventory.removeItem(type, id);
|
||||
if (button == 'right') {
|
||||
if (inventory.canToss()) {
|
||||
inventory.removeItem(type, id);
|
||||
}
|
||||
}
|
||||
|
||||
this.updateTiles();
|
||||
|
|
|
@ -8,6 +8,7 @@ import GuiButton from './button.mjs';
|
|||
import GuiEdit from './edit.mjs';
|
||||
import GuiText from './text.mjs';
|
||||
import GuiInventory from './inventory.mjs';
|
||||
import * as inventory from '../game/inventory.mjs';
|
||||
import * as events from '../game/events.mjs';
|
||||
import {state} from '../game/index.mjs';
|
||||
import * as world from '../world/index.mjs';
|
||||
|
@ -183,9 +184,23 @@ export function game() {
|
|||
invShadow.x += 10;
|
||||
invShadow.y += 10;
|
||||
|
||||
let inventory = new GuiInventory(0, 0, 0, 0);
|
||||
invShadow.append(inventory);
|
||||
inventory.posRelative({w: 1, h: 1});
|
||||
let invElement = new GuiInventory(0, 0, 0, 0);
|
||||
invShadow.append(invElement);
|
||||
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, {
|
||||
size: 12,
|
||||
|
@ -196,9 +211,9 @@ export function game() {
|
|||
moduleInfo.posRelative({x: 0, y: 1, w: 1});
|
||||
moduleInfo.splitLines();
|
||||
moduleInfo.y += 5;
|
||||
inventory.guiInfo = moduleInfo;
|
||||
invElement.guiInfo = moduleInfo;
|
||||
|
||||
edit.guiInventory = inventory;
|
||||
edit.guiInventory = invElement;
|
||||
|
||||
|
||||
let notification = new GuiText('', 0, 0, 0, 0, {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue