87 lines
3.6 KiB
JavaScript
87 lines
3.6 KiB
JavaScript
// ============================================================
|
||
// DATA-LOADER.JS — Загрузка игровых данных из JSON-файлов
|
||
// ============================================================
|
||
// Требует локальный сервер (Live Server в VS Code, или
|
||
// python -m http.server / npx serve).
|
||
// При запуске через file:// будет показана подсказка.
|
||
// ============================================================
|
||
|
||
const DataLoader = {
|
||
_loaded: false,
|
||
|
||
async load() {
|
||
if (this._loaded) return;
|
||
|
||
const FILES = [
|
||
'enemies', 'quests', 'recipes', 'shop', 'loot',
|
||
'lore', 'sets', 'enchants', 'classes', 'world',
|
||
];
|
||
|
||
let results;
|
||
try {
|
||
results = await Promise.all(
|
||
FILES.map(f =>
|
||
fetch('data/' + f + '.json')
|
||
.then(r => {
|
||
if (!r.ok) throw new Error('HTTP ' + r.status + ' for ' + f + '.json');
|
||
return r.json();
|
||
})
|
||
)
|
||
);
|
||
} catch (e) {
|
||
console.error('DataLoader.load() failed:', e);
|
||
this._showError(e);
|
||
throw e;
|
||
}
|
||
|
||
const [enemies, quests, recipes, shop, loot,
|
||
lore, sets, enchants, classes, world] = results;
|
||
|
||
// ── Заполнение RPG ──────────────────────────────────
|
||
RPG.ENEMY_DB = enemies;
|
||
RPG.QUEST_DB = quests;
|
||
RPG.CRAFT_RECIPES = recipes;
|
||
RPG.SHOP_ITEMS = shop;
|
||
RPG.LOOT_DB = loot;
|
||
RPG.LORE_NOTES = lore;
|
||
RPG.EQUIPMENT_SETS = sets;
|
||
RPG.ENCHANTS = enchants;
|
||
RPG.CLASSES = classes.classes;
|
||
RPG.SPELLS = classes.spells;
|
||
RPG.SKILLS = classes.skills;
|
||
RPG.PERK_TREE = classes.perkTree;
|
||
|
||
// ── Заполнение Game ─────────────────────────────────
|
||
Game.LOCATIONS = world.locations;
|
||
Game.NPC_DIALOGS = world.dialogs;
|
||
Game._WORLD = world; // spawns, npcs, decos, weather
|
||
|
||
this._loaded = true;
|
||
console.log('[DataLoader] Все данные загружены ✓');
|
||
},
|
||
|
||
_showError(e) {
|
||
// Удалим старые баннеры если есть
|
||
const old = document.getElementById('dl-error');
|
||
if (old) old.remove();
|
||
|
||
const isFileProtocol = location.protocol === 'file:';
|
||
const msg = isFileProtocol
|
||
? '⚠️ Игра требует локальный сервер для загрузки JSON-данных.<br>' +
|
||
'Откройте через <b>Live Server</b> (VS Code) или запустите:<br>' +
|
||
'<code>python -m http.server</code> и перейдите на <b>http://localhost:8000</b>'
|
||
: '⚠️ Ошибка загрузки данных: ' + e.message + '<br>Откройте консоль (F12) для подробностей.';
|
||
|
||
const banner = document.createElement('div');
|
||
banner.id = 'dl-error';
|
||
banner.style.cssText = [
|
||
'position:fixed', 'top:0', 'left:0', 'right:0',
|
||
'background:#8b0000', 'color:#fff', 'padding:14px 20px',
|
||
'font-size:13px', 'text-align:center', 'z-index:99999',
|
||
'line-height:1.7', 'font-family:monospace',
|
||
].join(';');
|
||
banner.innerHTML = msg;
|
||
document.body.prepend(banner);
|
||
},
|
||
};
|