Improvements to spritesheet gen.

This commit is contained in:
Dennis Postma 2024-12-22 02:32:06 +01:00
parent 5af2e399c9
commit 1facd2d641

View File

@ -76,9 +76,45 @@ export default class SpriteUpdateEvent {
} }
const parsedActions = this.validateSpriteActions(payload.spriteActions) const parsedActions = this.validateSpriteActions(payload.spriteActions)
const processedActions = await this.processSprites(parsedActions)
await Promise.all([this.updateDatabase(payload.id, payload.name, processedActions), this.saveSpritesToDisk(payload.id, processedActions)]) // Get existing sprite actions from database
const existingSprite = await prisma.sprite.findUnique({
where: { id: payload.id },
include: { spriteActions: true }
})
// Process only actions with changed sprites
const processedActions = await Promise.all(parsedActions.map(async (action) => {
const existing = existingSprite?.spriteActions.find(sa => sa.action === action.action)
// If sprites array matches existing, return existing data
if (existing && JSON.stringify(existing.sprites) === JSON.stringify(action.sprites)) {
return {
...action,
frameWidth: existing.frameWidth,
frameHeight: existing.frameHeight,
buffersWithDimensions: [] // No need to process frames
}
}
// Otherwise process the new sprites
const spriteBuffers = await this.convertBase64ToBuffers(action.sprites)
const frameWidth = ISOMETRIC_CONFIG.tileWidth
const frameHeight = await this.calculateOptimalHeight(spriteBuffers)
const processedFrames = await this.normalizeFrames(spriteBuffers, frameWidth, frameHeight)
return {
...action,
frameWidth,
frameHeight,
buffersWithDimensions: processedFrames
}
}))
await Promise.all([
this.updateDatabase(payload.id, payload.name, processedActions),
this.saveSpritesToDisk(payload.id, processedActions.filter(a => a.buffersWithDimensions.length > 0))
])
callback(true) callback(true)
} catch (error) { } catch (error) {
@ -103,24 +139,6 @@ export default class SpriteUpdateEvent {
} }
} }
private async processSprites(actions: SpriteActionInput[]): Promise<ProcessedSpriteAction[]> {
return Promise.all(
actions.map(async (action) => {
const spriteBuffers = await this.convertBase64ToBuffers(action.sprites)
const frameWidth = ISOMETRIC_CONFIG.tileWidth
const frameHeight = await this.calculateOptimalHeight(spriteBuffers)
const processedFrames = await this.normalizeFrames(spriteBuffers, frameWidth, frameHeight)
return {
...action,
frameWidth,
frameHeight,
buffersWithDimensions: processedFrames
}
})
)
}
private async convertBase64ToBuffers(sprites: string[]): Promise<Buffer[]> { private async convertBase64ToBuffers(sprites: string[]): Promise<Buffer[]> {
return sprites.map((sprite) => Buffer.from(sprite.split(',')[1], 'base64')) return sprites.map((sprite) => Buffer.from(sprite.split(',')[1], 'base64'))
} }
@ -139,6 +157,8 @@ export default class SpriteUpdateEvent {
} }
private async calculateOptimalHeight(buffers: Buffer[]): Promise<number> { private async calculateOptimalHeight(buffers: Buffer[]): Promise<number> {
if (!buffers.length) return ISOMETRIC_CONFIG.tileHeight // Return default height if no buffers
const heights = await Promise.all( const heights = await Promise.all(
buffers.map(async (buffer) => { buffers.map(async (buffer) => {
const bounds = await this.findContentBounds(buffer) const bounds = await this.findContentBounds(buffer)