Renamed get to getById, map improvement

This commit is contained in:
Dennis Postma 2025-02-09 17:33:10 +01:00
parent 49dcd92a9e
commit a9cedba4e0
13 changed files with 44 additions and 26 deletions

View File

@ -7,7 +7,7 @@ export function uuidv4() {
} }
export function unduplicateArray(array: any[]) { export function unduplicateArray(array: any[]) {
const arrayToProcess = typeof array.flat === 'function' ? array.flat() : array; const arrayToProcess = typeof array.flat === 'function' ? array.flat() : array
return [...new Set(arrayToProcess)] return [...new Set(arrayToProcess)]
} }
@ -24,7 +24,7 @@ export async function downloadCache<T extends { id: string; updatedAt: Date }>(e
for (const item of items) { for (const item of items) {
let overwrite = false let overwrite = false
const existingItem = await storage.get(item.id) const existingItem = await storage.getById(item.id)
if (!existingItem || item.updatedAt > existingItem.updatedAt) { if (!existingItem || item.updatedAt > existingItem.updatedAt) {
overwrite = true overwrite = true

View File

@ -48,7 +48,7 @@ onMounted(async () => {
hairSpriteId.value = spriteId hairSpriteId.value = spriteId
const spriteStorage = new SpriteStorage() const spriteStorage = new SpriteStorage()
sprite.value = await spriteStorage.get(spriteId) sprite.value = await spriteStorage.getById(spriteId)
await loadSpriteTextures(scene, spriteId) await loadSpriteTextures(scene, spriteId)
}) })
</script> </script>

View File

@ -36,7 +36,7 @@ gameStore.connection?.on('map:character:teleport', async (data: mapLoadData) =>
async function initialize() { async function initialize() {
if (!mapStore.mapId) return if (!mapStore.mapId) return
const map = await mapStorage.get(mapStore.mapId) const map = await mapStorage.getById(mapStore.mapId)
if (!map) return if (!map) return
await loadTileTexturesFromMapTileArray(mapStore.mapId, scene) await loadTileTexturesFromMapTileArray(mapStore.mapId, scene)

View File

@ -22,7 +22,7 @@ const props = defineProps<{
onMounted(async () => { onMounted(async () => {
if (!mapStore.mapId) return if (!mapStore.mapId) return
const map = await mapStorage.get(mapStore.mapId) const map = await mapStorage.getById(mapStore.mapId)
if (!map) return if (!map) return
await loadTileTexturesFromMapTileArray(mapStore.mapId, scene) await loadTileTexturesFromMapTileArray(mapStore.mapId, scene)

View File

@ -23,7 +23,7 @@ const items = ref<PlacedMapObjectT[]>([])
onMounted(async () => { onMounted(async () => {
if (!mapStore.mapId) return if (!mapStore.mapId) return
const map = await mapStorage.get(mapStore.mapId) const map = await mapStorage.getById(mapStore.mapId)
if (!map) return if (!map) return
items.value = map.placedMapObjects items.value = map.placedMapObjects

View File

@ -41,7 +41,7 @@ async function initialize() {
} }
const mapObjectStorage = new MapObjectStorage() const mapObjectStorage = new MapObjectStorage()
const _mapObject = await mapObjectStorage.get(props.placedMapObject.mapObject as string) const _mapObject = await mapObjectStorage.getById(props.placedMapObject.mapObject as string)
if (!_mapObject) return if (!_mapObject) return
mapObject.value = _mapObject mapObject.value = _mapObject

View File

@ -103,7 +103,7 @@ async function handleUpdate() {
onMounted(async () => { onMounted(async () => {
if (!props.placedMapObject.mapObject) return if (!props.placedMapObject.mapObject) return
mapObject.value = await mapObjectStorage.get(props.placedMapObject.mapObject as string) mapObject.value = await mapObjectStorage.getById(props.placedMapObject.mapObject as string)
if (!mapObject.value) return if (!mapObject.value) return
mapObjectName.value = mapObject.value.name mapObjectName.value = mapObject.value.name

View File

@ -44,7 +44,7 @@ export function useSoundComposable() {
} }
let audio: HTMLAudioElement let audio: HTMLAudioElement
const cachedSound = await soundStorage.get(soundUrl) const cachedSound = await soundStorage.getById(soundUrl)
if (cachedSound) { if (cachedSound) {
audio = new Audio(`data:audio/mpeg;base64,${cachedSound.base64}`) audio = new Audio(`data:audio/mpeg;base64,${cachedSound.base64}`)

View File

@ -71,23 +71,41 @@ export const calculateIsometricDepth = (positionX: number, positionY: number, wi
} }
async function loadTileTextures(tiles: TileT[], scene: Phaser.Scene) { async function loadTileTextures(tiles: TileT[], scene: Phaser.Scene) {
// Load each tile into the scene const tileStorage = new TileStorage()
for (const tile of tiles) {
const textureData = { // Process all tiles in parallel using Promise.all
await Promise.all(
tiles.map(async (tileData) => {
try {
// Get complete tile data if needed
const tile = !tileData.id || !tileData.updatedAt ? await tileStorage.getById(tileData.id) : tileData
// Skip if tile data couldn't be retrieved
if (!tile?.id) {
console.warn(`Failed to load tile data for ID: ${tileData.id}`)
return
}
const textureData: TextureData = {
key: tile.id, key: tile.id,
data: '/textures/tiles/' + tile.id + '.png', data: `/textures/tiles/${tile.id}.png`,
group: 'tiles', group: 'tiles',
updatedAt: tile.updatedAt updatedAt: tile.updatedAt
} as TextureData
await loadTexture(scene, textureData)
} }
await loadTexture(scene, textureData)
} catch (error) {
console.error(`Error loading texture for tile ${tileData.id}:`, error)
}
})
)
} }
export async function loadTileTexturesFromMapTileArray(map_id: string, scene: Phaser.Scene) { export async function loadTileTexturesFromMapTileArray(map_id: string, scene: Phaser.Scene) {
const mapStorage = new MapStorage() const mapStorage = new MapStorage()
const tileStorage = new TileStorage() const tileStorage = new TileStorage()
const map = await mapStorage.get(map_id) const map = await mapStorage.getById(map_id)
if (!map) return if (!map) return
const tileArray = unduplicateArray(map.tiles) const tileArray = unduplicateArray(map.tiles)

View File

@ -60,7 +60,7 @@ export async function loadSpriteTextures(scene: Phaser.Scene, sprite_id: string)
if (!sprite_id) return false if (!sprite_id) return false
const spriteStorage = new SpriteStorage() const spriteStorage = new SpriteStorage()
const sprite = await spriteStorage.get(sprite_id) const sprite = await spriteStorage.getById(sprite_id)
if (!sprite) { if (!sprite) {
console.error('Failed to load sprite:', sprite_id) console.error('Failed to load sprite:', sprite_id)

View File

@ -14,7 +14,7 @@ export class BaseStorage<T extends { id: string }> {
async add(item: T, overwrite = false) { async add(item: T, overwrite = false) {
try { try {
const existing = await this.get(item.id) const existing = await this.getById(item.id)
if (existing && !overwrite) return if (existing && !overwrite) return
await this.dexie.table(this.tableName).put({ ...item }) await this.dexie.table(this.tableName).put({ ...item })
@ -39,7 +39,7 @@ export class BaseStorage<T extends { id: string }> {
} }
} }
async get(id: string): Promise<T | null> { async getById(id: string): Promise<T | null> {
try { try {
const item = await this.dexie.table(this.tableName).get(id) const item = await this.dexie.table(this.tableName).get(id)
return item || null return item || null

View File

@ -31,7 +31,7 @@ export class CharacterTypeStorage extends BaseStorage<any> {
} }
async getSpriteId(characterTypeId: string) { async getSpriteId(characterTypeId: string) {
const characterType = await this.get(characterTypeId) const characterType = await this.getById(characterTypeId)
return characterType?.sprite return characterType?.sprite
} }
} }
@ -42,7 +42,7 @@ export class CharacterHairStorage extends BaseStorage<any> {
} }
async getSpriteId(characterTypeId: string) { async getSpriteId(characterTypeId: string) {
const characterType = await this.get(characterTypeId) const characterType = await this.getById(characterTypeId)
return characterType?.sprite return characterType?.sprite
} }
} }