Files
novafarma/old_logic/src_backup_1768938138/utils/GlobalInventoryHelper.js
2026-01-21 01:08:21 +01:00

87 lines
2.5 KiB
JavaScript

/**
* GLOBAL INVENTORY HELPER
* Provides simple object-based inventory access for compatibility
* with simple crafting system format
*
* Automatically syncs with InventorySystem
*/
window.inventory = new Proxy({}, {
/**
* GET - Read inventory count
* Usage: inventory.wood → returns wood count
*/
get(target, prop) {
if (typeof prop === 'symbol' || prop === 'toJSON' || prop === 'toString') {
return target[prop];
}
const gameScene = window.gameState?.gameScene;
if (!gameScene || !gameScene.inventorySystem) {
console.warn('⚠️ InventorySystem not ready yet');
return 0;
}
return gameScene.inventorySystem.getItemCount(prop);
},
/**
* SET - Modify inventory count
* Usage: inventory.wood = 10 → sets wood to 10
* Usage: inventory.wood += 5 → adds 5 wood
* Usage: inventory.wood -= 2 → removes 2 wood
*/
set(target, prop, value) {
if (typeof prop === 'symbol') {
target[prop] = value;
return true;
}
const gameScene = window.gameState?.gameScene;
if (!gameScene || !gameScene.inventorySystem) {
console.warn('⚠️ InventorySystem not ready yet');
return false;
}
// Get current count
const currentCount = gameScene.inventorySystem.getItemCount(prop);
const difference = value - currentCount;
if (difference > 0) {
// Add items
gameScene.inventorySystem.addItem(prop, difference);
} else if (difference < 0) {
// Remove items
gameScene.inventorySystem.removeItem(prop, Math.abs(difference));
}
// ✨ AUTO UI UPDATE
gameScene.inventorySystem.updateUI();
return true;
}
});
/**
* Global helper function for simple crafting system
*/
window.addItemToInventory = function (itemKey, quantity) {
const gameScene = window.gameState?.gameScene;
if (!gameScene || !gameScene.inventorySystem) {
console.warn('⚠️ InventorySystem not ready yet');
return false;
}
const result = gameScene.inventorySystem.addItem(itemKey, quantity);
// ✨ AUTO UI UPDATE
gameScene.inventorySystem.updateUI();
return result;
};
console.log('✅ Global inventory helper initialized');
console.log('💡 Usage: inventory.wood, inventory.stone, etc.');
console.log('💡 Usage: inventory.wood = 10, inventory.wood += 5, etc.');
console.log('✨ UI auto-updates on inventory change!');