feat: character/NPC management system with spritesheet support
Add drag & drop spritesheet upload in editor, character registry in sprites.js, character selector for NPCs, sprite rendering on editor canvas, server API for character persistence, and game-side character loading via characterLoader.js. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
26
js/world/characterLoader.js
Normal file
26
js/world/characterLoader.js
Normal file
@@ -0,0 +1,26 @@
|
||||
// characterLoader.js - Loads character spritesheets from server and registers them
|
||||
import { registerCharacter } from './sprites.js';
|
||||
|
||||
/**
|
||||
* Fetch character data from the server and register all characters
|
||||
* with the sprite system so NPCs can use them.
|
||||
* @returns {Promise<number>} number of characters loaded
|
||||
*/
|
||||
export async function loadCharacters() {
|
||||
try {
|
||||
const res = await fetch('/api/characters');
|
||||
const data = await res.json();
|
||||
if (!data.characters) return 0;
|
||||
|
||||
const entries = Object.entries(data.characters);
|
||||
const promises = entries.map(([id, c]) =>
|
||||
registerCharacter(id, c.name, c.spritesheet, c.frameW, c.frameH)
|
||||
);
|
||||
await Promise.all(promises);
|
||||
console.log(`[characterLoader] loaded ${entries.length} character(s)`);
|
||||
return entries.length;
|
||||
} catch (e) {
|
||||
console.warn('[characterLoader] failed to load characters:', e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user