Worked on sprite logic

This commit is contained in:
Dennis Postma 2024-07-26 04:40:46 +02:00
parent bc626c57ad
commit 71f5330b9b
4 changed files with 45 additions and 32 deletions

View File

@ -24,6 +24,7 @@ CREATE TABLE `SpriteAction` (
`id` VARCHAR(191) NOT NULL,
`spriteId` VARCHAR(191) NOT NULL,
`action` VARCHAR(191) NOT NULL,
`sprites` JSON NULL,
`origin_x` DECIMAL(65, 30) NOT NULL DEFAULT 0,
`origin_y` DECIMAL(65, 30) NOT NULL DEFAULT 0,
`isAnimated` BOOLEAN NOT NULL DEFAULT false,

View File

@ -12,6 +12,7 @@ model SpriteAction {
spriteId String
sprite Sprite @relation(fields: [spriteId], references: [id], onDelete: Cascade)
action String
sprites Json?
origin_x Decimal @default(0)
origin_y Decimal @default(0)
isAnimated Boolean @default(false)
@ -19,12 +20,4 @@ model SpriteAction {
frameWidth Int @default(0)
frameHeight Int @default(0)
frameSpeed Int @default(0)
images SpriteActionImage[]
}
model SpriteActionImage {
id String @id @default(uuid())
spriteActionId String
spriteAction SpriteAction @relation(fields: [spriteActionId], references: [id], onDelete: Cascade)
order Int
}

View File

@ -2,22 +2,17 @@ import { Server } from 'socket.io'
import { TSocket } from '../../../utilities/Types'
import prisma from '../../../utilities/Prisma'
import type { SpriteAction } from '@prisma/client'
type uploadSpriteAction = SpriteAction & {
base64?: string
}
import path from 'path'
import { writeFile } from 'node:fs/promises'
import fs from 'fs/promises'
import spriteRepository from '../../../repositories/SpriteRepository'
type Payload = {
id: string
name: string
spriteActions: uploadSpriteAction[]
spriteActions: SpriteAction[]
}
/**
* Handle game master sprite update event
* @param socket
* @param io
*/
export default function (socket: TSocket, io: Server) {
socket.on('gm:sprite:update', async (data: Payload, callback: (success: boolean) => void) => {
if (socket.character?.role !== 'gm') {
@ -25,7 +20,7 @@ export default function (socket: TSocket, io: Server) {
}
try {
const sprite = await prisma.sprite.update({
await prisma.sprite.update({
where: {
id: data.id
},
@ -35,12 +30,24 @@ export default function (socket: TSocket, io: Server) {
deleteMany: {
spriteId: data.id
},
create: data.spriteActions
create: data.spriteActions.map((spriteAction) => ({
action: spriteAction.action,
origin_x: spriteAction.origin_x,
origin_y: spriteAction.origin_y,
isAnimated: spriteAction.isAnimated,
isLooping: spriteAction.isLooping,
sprites: spriteAction.sprites as string[],
}))
}
}
})
// store the sprite image
const public_folder = path.join(process.cwd(), 'public', 'sprites', data.id)
// Ensure the folder exists
await fs.mkdir(public_folder, { recursive: true })
const sprite = await spriteRepository.getById(data.id)
callback(true)
} catch (error) {

View File

@ -12,7 +12,19 @@ class SpriteRepository {
}
async getAll(): Promise<Sprite[]> {
return prisma.sprite.findMany()
return prisma.sprite.findMany({
include: {
spriteActions: true
}
})
}
async getSpriteActions(spriteId: string) {
return prisma.spriteAction.findMany({
where: {
spriteId
}
})
}
}