303 lines
9.5 KiB
JavaScript
303 lines
9.5 KiB
JavaScript
const Item = require("./Item");
|
|
const Cannon = require("./Cannon");
|
|
const Harpooner = require("./Harpooner");
|
|
const InventoryList = require("../Game/InventoryList");
|
|
|
|
const schemeCannons = require("../Data/cannons");
|
|
const schemeHarpooners = require("../Data/harpooners");
|
|
const ammunitions = require("../Data/ammunitions");
|
|
const harpoons = require("../Data/harpoons");
|
|
const itemTable = require("../Data/items");
|
|
const constants = require("../Data/constants");
|
|
|
|
const { CATEGORIES, AFFECTS } = constants;
|
|
const { CATEGORY_ITEM, CATEGORY_AMMUNITION, CATEGORY_HARPOON, CATEGORY_DESIGN, CATEGORY_ECONOMY, CATEGORY_STATUS, CATEGORY_CANNON, CATEGORY_HARPOONER } = CATEGORIES;
|
|
const { AFFECT_CANNON_DAMAGE, AFFECT_HARPOONER_DAMAGE, AFFECT_PROTECTION, AFFECT_SAILING_SPEED, AFFECT_HITPOINT } = AFFECTS;
|
|
|
|
const AFFECT_TABLE = {
|
|
1: [1, 2, 3, 4],
|
|
[AFFECT_PROTECTION]: [5, 6, 7, 8]
|
|
};
|
|
|
|
class Inventory {
|
|
constructor(info) {
|
|
this.selectedAmmunition = 0;
|
|
this.selectedHarpoon = 0;
|
|
|
|
this.resourceSharedLimiter = info.resourceSharedLimiter;
|
|
|
|
this[CATEGORY_AMMUNITION] = new InventoryList();
|
|
this[CATEGORY_HARPOON] = new InventoryList();
|
|
this[CATEGORY_ITEM] = new InventoryList();
|
|
this[CATEGORY_STATUS] = new InventoryList();
|
|
this[CATEGORY_ECONOMY] = new InventoryList();
|
|
this[CATEGORY_DESIGN] = new InventoryList();
|
|
this[CATEGORY_CANNON] = new InventoryList();
|
|
this[CATEGORY_HARPOONER] = new InventoryList();
|
|
|
|
this[CATEGORY_DESIGN].addItem(1, {
|
|
categoryID: CATEGORY_DESIGN,
|
|
id: 1,
|
|
amount: 1
|
|
}); // default design
|
|
}
|
|
|
|
refreshSingle(element) {
|
|
switch (element.categoryID) {
|
|
case CATEGORY_ECONOMY:
|
|
|
|
this[CATEGORY_ECONOMY].accumulateItem(element.id, element);
|
|
break;
|
|
case CATEGORY_ITEM:
|
|
const inventoryItem = this[CATEGORY_ITEM].getElement(element.id);
|
|
if (!inventoryItem) return this.addItem(element);
|
|
|
|
inventoryItem.amount += element.amount;
|
|
break;
|
|
case CATEGORY_AMMUNITION:
|
|
if (!element.amount) return;
|
|
|
|
this[CATEGORY_AMMUNITION].accumulateItem(element.id, element);
|
|
|
|
if (!this.selectedAmmunition) this.setSelectedAmmunition(element.id);
|
|
break;
|
|
case CATEGORY_HARPOON:
|
|
if (!element.amount) return;
|
|
|
|
this[CATEGORY_HARPOON].accumulateItem(element.id, element);
|
|
|
|
if (!this.selectedHarpoon) this.setSelectedHarpoon(element.id);
|
|
break;
|
|
case CATEGORY_DESIGN:
|
|
this[CATEGORY_DESIGN].accumulateItem(element.id, element);
|
|
break;
|
|
case CATEGORY_STATUS:
|
|
this[CATEGORY_STATUS].accumulateItem(element.id, element);
|
|
break;
|
|
case CATEGORY_CANNON:
|
|
let cannon = this[CATEGORY_CANNON].getElement(element.id);
|
|
if (!cannon) {
|
|
const schemeCannon = schemeCannons[element.id];
|
|
if (!schemeCannon) return;
|
|
|
|
cannon = new Cannon({
|
|
id: element.id,
|
|
damage: schemeCannon.damage,
|
|
range: schemeCannon.range,
|
|
reload: schemeCannon.reload,
|
|
scatter: schemeCannon.scatter,
|
|
amount: element.amount,
|
|
amountEquipped: element.amountEquipped,
|
|
resourceSharedLimiter: this.resourceSharedLimiter.cannon
|
|
});
|
|
|
|
this[CATEGORY_CANNON].accumulateItem(cannon.id, cannon);
|
|
} else this[CATEGORY_CANNON].accumulateItem(cannon.id, element);
|
|
break;
|
|
case CATEGORY_HARPOONER:
|
|
let harpooner = this[CATEGORY_HARPOONER].getElement(element.id);
|
|
if (!harpooner) {
|
|
const schemeHarpooner = schemeHarpooners[element.id];
|
|
if (!schemeHarpooner) return;
|
|
|
|
harpooner = new Harpooner({
|
|
id: element.id,
|
|
damage: schemeHarpooner.damage,
|
|
range: schemeHarpooner.range,
|
|
reload: schemeHarpooner.reload,
|
|
scatter: schemeHarpooner.scatter,
|
|
amount: element.amount,
|
|
amountEquipped: element.amountEquipped,
|
|
resourceSharedLimiter: this.resourceSharedLimiter.harpooner
|
|
});
|
|
|
|
this[CATEGORY_HARPOONER].accumulateItem(harpooner.id, harpooner);
|
|
} else this[CATEGORY_HARPOONER].accumulateItem(element.id, element);
|
|
|
|
break;
|
|
};
|
|
}
|
|
|
|
use(categoryID, id, amount) {
|
|
const category = this[categoryID];
|
|
if (!category) return
|
|
|
|
const item = category[id];
|
|
if (!item || !item.amount) return
|
|
|
|
const itemScheme = itemTable[id];
|
|
if (!itemScheme && categoryID === CATEGORY_ITEM) return
|
|
|
|
let removedCount = -1;
|
|
|
|
if (itemScheme.isDurable) item.isActive = true;
|
|
else {
|
|
if (categoryID === CATEGORY_ITEM) item.nextUseAt = performance.now() + itemScheme.cooldown;
|
|
removedCount = itemScheme.useOnImpact || categoryID !== CATEGORY_ITEM ? Math.min(amount, item.amount) : 0;
|
|
|
|
item.amount -= removedCount;
|
|
item.usedAmount = removedCount;
|
|
};
|
|
|
|
return item;
|
|
}
|
|
|
|
disable(id) {
|
|
const item = this[CATEGORY_ITEM].getElement(id);
|
|
if (!item) return;
|
|
|
|
item.isActive = false;
|
|
}
|
|
|
|
alterAmount(categoryID, id, amount) {
|
|
const category = this[categoryID];
|
|
if (!category) return
|
|
|
|
const element = category.getElement(id);
|
|
if (!element || !element.amount) return
|
|
|
|
const usedAmount = Math.min(amount, element.amount);
|
|
element.amount -= usedAmount;
|
|
|
|
return usedAmount;
|
|
}
|
|
|
|
setSelectedAmmunition(id) {
|
|
if (!id) return this.selectedAmmunition = 0;
|
|
|
|
if (!ammunitions[id]) return;
|
|
|
|
const ammunition = this[CATEGORY_AMMUNITION].hasElement(id);
|
|
if (!ammunition) return this.selectedAmmunition = 0;
|
|
|
|
console.info(ammunition)
|
|
|
|
return this.selectedAmmunition = id;
|
|
}
|
|
|
|
getSelectedAmmunition() {
|
|
const selectedAmmunition = this[CATEGORY_AMMUNITION].getElement(this.selectedAmmunition);
|
|
if (!selectedAmmunition) return;
|
|
|
|
return selectedAmmunition;
|
|
}
|
|
|
|
setSelectedHarpoon(id) {
|
|
if (!id) return this.selectedHarpoon = 0;
|
|
|
|
if (!harpoons[id]) return;
|
|
|
|
const harpoon = this[CATEGORY_HARPOON].hasElement(id);
|
|
if (!harpoon) return this.selectedHarpoon = 0;
|
|
|
|
return this.selectedHarpoon = id;
|
|
}
|
|
|
|
getSeletedHarpoon() {
|
|
const selectedHarpoon = this[CATEGORY_HARPOON].getElement(this.selectedHarpoon);
|
|
if (!selectedHarpoon) return;
|
|
|
|
return selectedHarpoon;
|
|
}
|
|
|
|
addItem(entry) {
|
|
const itemScheme = itemTable[entry.id];
|
|
if (!itemScheme) return;
|
|
if (!entry.amount) return;
|
|
|
|
const { time, scale, affects } = itemScheme;
|
|
|
|
const item = new Item({
|
|
id: entry.id,
|
|
amount: entry.amount,
|
|
time,
|
|
scale,
|
|
affects
|
|
});
|
|
|
|
this[CATEGORY_ITEM].addItem(item.id, item);
|
|
}
|
|
|
|
getItemsByAffect(affect) {
|
|
const items = Object.values(this[CATEGORY_ITEM]);
|
|
return items.filter(item => item.affects.includes(affect));
|
|
}
|
|
|
|
useAvailableItemsByAffect(affect, cb) {
|
|
AFFECT_TABLE[affect]
|
|
.forEach(itemID => {
|
|
const item = this[CATEGORY_ITEM].getElement(itemID);
|
|
if (item && item.isActive && item.amount) {
|
|
const use = item.use();
|
|
cb(use.scale, itemID, use.statusID, use.isStatusActive);
|
|
};
|
|
});
|
|
}
|
|
|
|
getPosessions() {
|
|
return {
|
|
[CATEGORY_AMMUNITION]: this[CATEGORY_AMMUNITION],
|
|
[CATEGORY_HARPOON]: this[CATEGORY_HARPOON],
|
|
[CATEGORY_DESIGN]: this[CATEGORY_DESIGN]
|
|
};
|
|
}
|
|
|
|
getDesignIDs() {
|
|
const designIDs = [];
|
|
this[CATEGORY_DESIGN].each(key => designIDs.push(key));
|
|
|
|
return designIDs;
|
|
}
|
|
|
|
getEconomy() {
|
|
const economy = [];
|
|
this[CATEGORY_ECONOMY].each((key, value) => economy.push(value));
|
|
|
|
return economy;
|
|
}
|
|
|
|
getAmmunitions() {
|
|
const ammunitions = [];
|
|
this[CATEGORY_AMMUNITION].each((key, value) => ammunitions.push(value));
|
|
|
|
return ammunitions;
|
|
}
|
|
|
|
getHarpoons() {
|
|
const harpoons = [];
|
|
this[CATEGORY_HARPOON].each((key, value) => harpoons.push(value));
|
|
|
|
return harpoons;
|
|
}
|
|
|
|
getItems() {
|
|
const items = [];
|
|
this[CATEGORY_ITEM].each((key, value) => items.push(value));
|
|
|
|
return items;
|
|
}
|
|
|
|
getCannons() {
|
|
const cannons = [];
|
|
this[CATEGORY_CANNON].each((key, value) => cannons.push(value));
|
|
|
|
return cannons;
|
|
}
|
|
|
|
getHarpooners() {
|
|
const harpooners = [];
|
|
this[CATEGORY_HARPOONER].each((key, value) => harpooners.push(value));
|
|
|
|
return harpooners;
|
|
}
|
|
|
|
hasDesign(id) {
|
|
return this[CATEGORY_DESIGN].hasElement(id);
|
|
}
|
|
|
|
getItem(id) {
|
|
return this[CATEGORY_ITEM].getElement(id);
|
|
}
|
|
};
|
|
|
|
module.exports = Inventory; |