1
0
forked from noxious/server

Finish sprite gen. update

This commit is contained in:
2025-02-21 02:01:51 +01:00
parent c59b391a6a
commit 5b06386a39
6 changed files with 87 additions and 64 deletions

View File

@ -31,8 +31,8 @@ interface EffectiveDimensions {
type Payload = {
id: UUID
name: string
width: number
height: number
width: number | null
height: number | null
spriteActions: Array<{
action: string
sprites: SpriteImage[]
@ -57,12 +57,17 @@ export default class SpriteUpdateEvent extends BaseEvent {
await spriteRepository.getEntityManager().populate(sprite, ['spriteActions'])
// Update sprite in database
await sprite.setName(data.name).setWidth(data.width).setHeight(data.height).setUpdatedAt(new Date()).save()
// Update sprite in database with width/height if provided
await sprite
.setName(data.name)
.setWidth(data.width ?? sprite.getWidth())
.setHeight(data.height ?? sprite.getHeight())
.setUpdatedAt(new Date())
.save()
// First verify all sprite sheets can be generated
for (const actionData of data.spriteActions) {
if (!(await this.generateSpriteSheet(actionData.sprites, sprite.getId(), actionData.action))) {
if (!(await this.generateSpriteSheet(actionData.sprites, sprite.getId(), actionData.action, data.width ?? 0, data.height ?? 0))) {
return callback(false)
}
}
@ -80,24 +85,25 @@ export default class SpriteUpdateEvent extends BaseEvent {
const imageData = await Promise.all(actionData.sprites.map((sprite) => this.processImage(sprite)))
const effectiveDimensions = imageData.map((dimensions) => this.calculateEffectiveDimensions(dimensions))
// Calculate total height needed for the sprite sheet
const maxHeight = Math.max(...effectiveDimensions.map((d) => d.height))
// Calculate maximum dimensions
const maxWidth = data.width ?? Math.max(...effectiveDimensions.map((d) => d.width))
const maxHeight = data.height ?? Math.max(...effectiveDimensions.map((d) => d.height))
const maxTop = Math.max(...effectiveDimensions.map((d) => d.top))
const maxBottom = Math.max(...effectiveDimensions.map((d) => d.bottom))
const totalHeight = maxHeight + maxTop + maxBottom
const totalHeight = data.height ?? maxHeight + maxTop + maxBottom
const spriteAction = new SpriteAction()
spriteAction.setSprite(sprite)
sprite.getSpriteActions().add(spriteAction)
spriteAction
.setAction(actionData.action)
.setSprites(actionData.sprites)
.setOriginX(actionData.originX)
.setOriginY(actionData.originY)
.setFrameWidth(await this.calculateMaxWidth(actionData.sprites))
.setFrameHeight(totalHeight)
.setFrameRate(actionData.frameRate)
.setAction(actionData.action)
.setSprites(actionData.sprites)
.setOriginX(actionData.originX)
.setOriginY(actionData.originY)
.setFrameWidth(maxWidth)
.setFrameHeight(totalHeight)
.setFrameRate(actionData.frameRate)
await spriteRepository.getEntityManager().persistAndFlush(spriteAction)
}
@ -109,7 +115,7 @@ export default class SpriteUpdateEvent extends BaseEvent {
}
}
private async generateSpriteSheet(sprites: SpriteImage[], spriteId: string, action: string): Promise<boolean> {
private async generateSpriteSheet(sprites: SpriteImage[], spriteId: string, action: string, containerWidth: number, containerHeight: number): Promise<boolean> {
try {
if (!sprites.length) return true
@ -118,37 +124,56 @@ export default class SpriteUpdateEvent extends BaseEvent {
const effectiveDimensions = imageData.map((dimensions) => this.calculateEffectiveDimensions(dimensions))
// Calculate maximum dimensions
const maxWidth = Math.max(...effectiveDimensions.map((d) => d.width))
const maxHeight = Math.max(...effectiveDimensions.map((d) => d.height))
const maxWidth = containerWidth > 0 ? containerWidth : Math.max(...effectiveDimensions.map((d) => d.width))
const maxHeight = containerHeight > 0 ? containerHeight : Math.max(...effectiveDimensions.map((d) => d.height))
const maxTop = Math.max(...effectiveDimensions.map((d) => d.top))
const maxBottom = Math.max(...effectiveDimensions.map((d) => d.bottom))
// Calculate total height needed
const totalHeight = maxHeight + maxTop + maxBottom
const totalHeight = containerHeight > 0 ? containerHeight : maxHeight + maxTop + maxBottom
// Process images and create sprite sheet
const processedImages = await Promise.all(
sprites.map(async (sprite, index) => {
const { width, height, offsetX, offsetY } = await this.processImage(sprite)
const uri = sprite.url.split(';base64,').pop()
if (!uri) throw new Error('Invalid base64 image')
const buffer = Buffer.from(uri, 'base64')
sprites.map(async (sprite) => {
const { width, height, offsetX, offsetY } = await this.processImage(sprite)
const uri = sprite.url.split(';base64,').pop()
if (!uri) throw new Error('Invalid base64 image')
const buffer = Buffer.from(uri, 'base64')
// Create individual frame
const left = offsetX >= 0 ? offsetX : 0
const verticalOffset = totalHeight - height - (offsetY >= 0 ? offsetY : 0)
return sharp({
create: {
width: maxWidth,
height: totalHeight,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 0 }
}
})
.composite([{ input: buffer, left, top: verticalOffset }])
.png()
.toBuffer()
// Calculate position based on container or offset
// If container dimensions are set, position at top center
const left = containerWidth > 0 ? Math.floor((maxWidth - width) / 2) : offsetX >= 0 ? offsetX : 0
const top =
containerHeight > 0
? 0 // Place at top when container dimensions are set
: totalHeight - height - (offsetY >= 0 ? offsetY : 0)
return sharp({
create: {
width: maxWidth,
height: totalHeight,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 0 }
}
})
.composite([
{
input: buffer,
left,
top,
...(containerWidth > 0 || containerHeight > 0
? {
width: containerWidth || undefined,
height: containerHeight || undefined,
fit: 'contain'
}
: {})
}
])
.png()
.toBuffer()
})
)
// Combine frames into sprite sheet
@ -160,15 +185,15 @@ export default class SpriteUpdateEvent extends BaseEvent {
background: { r: 0, g: 0, b: 0, alpha: 0 }
}
})
.composite(
processedImages.map((buffer, index) => ({
input: buffer,
left: index * maxWidth,
top: 0
}))
)
.png()
.toBuffer()
.composite(
processedImages.map((buffer, index) => ({
input: buffer,
left: index * maxWidth,
top: 0
}))
)
.png()
.toBuffer()
// Ensure directory exists
const dir = `public/sprites/${spriteId}`