Fixes and improvements for dynamic zone asset loading
This commit is contained in:
@ -52,42 +52,45 @@ async function addHttpRoutes(app: Application) {
|
||||
})
|
||||
|
||||
app.get('/assets/:zoneId', async (req: Request, res: Response) => {
|
||||
const zoneId = req.params.zoneId
|
||||
if(zoneId && parseInt(zoneId)) {
|
||||
const zone = await zoneRepository.getById(parseInt(zoneId))
|
||||
if(zone) {
|
||||
const assets = await zoneManager.getNeededAssets(zone);
|
||||
const sprites = await spriteRepository.getAll()
|
||||
const spritesArray: TAsset[] = [];
|
||||
sprites.forEach((sprite) => {
|
||||
sprite.spriteActions.forEach((spriteAction) => {
|
||||
spritesArray.push({
|
||||
key: sprite.id + '-' + spriteAction.action,
|
||||
url: '/assets/sprites/' + sprite.id + '/' + spriteAction.action + '.png',
|
||||
group: spriteAction.isAnimated ? 'sprite_animations' : 'sprites',
|
||||
frameWidth: spriteAction.frameWidth,
|
||||
frameHeight: spriteAction.frameHeight
|
||||
})
|
||||
})
|
||||
})
|
||||
const zoneId = parseInt(req.params.zoneId);
|
||||
|
||||
res.json([
|
||||
...spritesArray,
|
||||
...assets.tiles.map(x => { return {
|
||||
key: x,
|
||||
url: '/assets/tiles/' + x + '.png',
|
||||
group: 'tiles'
|
||||
}}),
|
||||
...assets.objects.map(x => { return {
|
||||
key: x,
|
||||
url: '/assets/objects/' + x + '.png',
|
||||
group: 'objects'
|
||||
}})
|
||||
]);
|
||||
// res.json( );
|
||||
}
|
||||
if (isNaN(zoneId) || zoneId === 0) {
|
||||
return res.status(400).json({ message: 'Invalid zone ID' });
|
||||
}
|
||||
})
|
||||
|
||||
const zone = await zoneRepository.getById(zoneId);
|
||||
|
||||
if (!zone) {
|
||||
return res.status(404).json({ message: 'Zone not found' });
|
||||
}
|
||||
|
||||
const assets = await zoneManager.getZoneAssets(zone);
|
||||
const sprites = await spriteRepository.getAll();
|
||||
|
||||
const spritesAssets = sprites.flatMap(sprite =>
|
||||
sprite.spriteActions.map(action => ({
|
||||
key: `${sprite.id}-${action.action}`,
|
||||
url: `/assets/sprites/${sprite.id}/${action.action}.png`,
|
||||
group: action.isAnimated ? 'sprite_animations' : 'sprites',
|
||||
frameWidth: action.frameWidth,
|
||||
frameHeight: action.frameHeight
|
||||
}))
|
||||
);
|
||||
|
||||
const tilesAssets = assets.tiles.map(tile => ({
|
||||
key: tile,
|
||||
url: `/assets/tiles/${tile}.png`,
|
||||
group: 'tiles'
|
||||
}));
|
||||
|
||||
const objectsAssets = assets.objects.map(object => ({
|
||||
key: object,
|
||||
url: `/assets/objects/${object}.png`,
|
||||
group: 'objects'
|
||||
}));
|
||||
|
||||
res.json([...spritesAssets, ...tilesAssets, ...objectsAssets]);
|
||||
});
|
||||
|
||||
app.get('/assets/:type/:spriteId?/:file', (req: Request, res: Response) => {
|
||||
const assetType = req.params.type
|
||||
|
Reference in New Issue
Block a user