This commit is contained in:
Dennis Postma 2025-02-21 01:46:53 +01:00
parent d6681f9af7
commit c59b391a6a
4 changed files with 60 additions and 40 deletions

Binary file not shown.

View File

@ -489,7 +489,7 @@ export default class InitCommand extends BaseCommand {
private async createCharacterHair(): Promise<void> {
const hairSprite = new Sprite()
hairSprite.setId('922ee95f-1500-49c0-8ead-f8cc46dad136').setName('Hair 1')
hairSprite.setId('922ee95f-1500-49c0-8ead-f8cc46dad136').setName('Hair 1').setWidth(30).setHeight(40)
await hairSprite.save()
const frontAction = new SpriteAction()

View File

@ -65,9 +65,12 @@ export class AvatarController extends BaseController {
return this.sendError(res, 'Body sprite file not found', 404)
}
// Get body sprite metadata
const bodyMetadata = await sharp(bodySpritePath).metadata()
let avatar = sharp(bodySpritePath).extend({
top: 2,
bottom: 2,
top: 0,
bottom: 0,
background: { r: 0, g: 0, b: 0, alpha: 0 }
})
@ -76,7 +79,21 @@ export class AvatarController extends BaseController {
if (characterHair?.sprite?.id) {
const hairSpritePath = Storage.getPublicPath('sprites', characterHair.sprite.id, 'front.png')
if (fs.existsSync(hairSpritePath)) {
avatar = avatar.composite([{ input: hairSpritePath, gravity: 'north' }])
// Resize hair sprite to match body dimensions
const resizedHair = await sharp(hairSpritePath)
.resize(bodyMetadata.width, bodyMetadata.height, {
fit: 'contain',
background: { r: 0, g: 0, b: 0, alpha: 0 }
})
.toBuffer()
avatar = avatar.composite([
{
input: resizedHair,
left: 0,
top: -27 // Apply vertical offset
}
])
}
}
}
@ -84,6 +101,7 @@ export class AvatarController extends BaseController {
res.setHeader('Content-Type', 'image/png')
return avatar.pipe(res)
} catch (error) {
console.error('Avatar generation error:', error)
return this.sendError(res, 'Error generating avatar', 500)
}
}

View File

@ -31,6 +31,8 @@ interface EffectiveDimensions {
type Payload = {
id: UUID
name: string
width: number
height: number
spriteActions: Array<{
action: string
sprites: SpriteImage[]
@ -56,7 +58,7 @@ export default class SpriteUpdateEvent extends BaseEvent {
await spriteRepository.getEntityManager().populate(sprite, ['spriteActions'])
// Update sprite in database
await sprite.setName(data.name).setUpdatedAt(new Date()).save()
await sprite.setName(data.name).setWidth(data.width).setHeight(data.height).setUpdatedAt(new Date()).save()
// First verify all sprite sheets can be generated
for (const actionData of data.spriteActions) {