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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue