diff --git a/src/socketEvents/gameMaster/assetManager/sprite/update.ts b/src/socketEvents/gameMaster/assetManager/sprite/update.ts index 24c4113..ee3feaa 100644 --- a/src/socketEvents/gameMaster/assetManager/sprite/update.ts +++ b/src/socketEvents/gameMaster/assetManager/sprite/update.ts @@ -129,41 +129,32 @@ export default class SpriteUpdateEvent { await Promise.all( processedActions.map(async ({ action, buffersWithDimensions, frameWidth, frameHeight }) => { - // First prepare each frame with exact dimensions - const centeredFrames = await Promise.all( + // Get and validate all frame dimensions first + const frames = await Promise.all( buffersWithDimensions.map(async ({ buffer }) => { - // Get the current dimensions const metadata = await sharp(buffer).metadata() - const width = metadata.width! - - // Calculate padding to center the sprite - const leftPadding = ((frameWidth - width) >> 1) - - return await sharp(buffer) - .extend({ - top: 0, - bottom: 0, - left: leftPadding, - right: frameWidth - width - leftPadding, - background: { r: 0, g: 0, b: 0, alpha: 0 } - }) - .toBuffer() + return { + buffer, + width: metadata.width!, + height: metadata.height! + } }) ) - // Combine the frames const combinedImage = await sharp({ create: { - width: frameWidth * centeredFrames.length, + width: frameWidth * frames.length, height: frameHeight, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } } }) .composite( - centeredFrames.map((buffer, index) => ({ + frames.map(({ buffer, width, height }, index) => ({ input: buffer, - left: index * frameWidth, + // Center horizontally based on the exact middle of each frame + left: (index * frameWidth) + ((frameWidth - width) >> 1), + // Top position is always 0 top: 0 })) )