forked from noxious/client
npm run format, moved some files for improved file structure, removed redundant logic
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import config from '@/application/config'
|
||||
import { getTile, tileToWorldXY } from '@/composables/mapComposable'
|
||||
import { getTile, tileToWorldXY } from '@/services/mapService'
|
||||
import { useGameStore } from '@/stores/gameStore'
|
||||
import { ref, type Ref } from 'vue'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { getTile } from '@/composables/mapComposable'
|
||||
import { getTile } from '@/services/mapService'
|
||||
import { useGameStore } from '@/stores/gameStore'
|
||||
import type { Ref } from 'vue'
|
||||
import { useBaseControlsComposable } from './useBaseControlsComposable'
|
||||
|
@ -1,101 +0,0 @@
|
||||
import type { TextureData } from '@/application/types'
|
||||
import { SpriteStorage } from '@/storage/storages'
|
||||
import { TextureStorage } from '@/storage/textureStorage'
|
||||
import { useGameStore } from '@/stores/gameStore'
|
||||
|
||||
const textureLoadingPromises = new Map<string, Promise<boolean>>()
|
||||
|
||||
export async function loadTexture(scene: Phaser.Scene, textureData: TextureData): Promise<boolean> {
|
||||
const gameStore = useGameStore()
|
||||
const textureStorage = new TextureStorage()
|
||||
|
||||
// Check if the texture is already loaded in Phaser
|
||||
if (gameStore.game.loadedTextures.find((texture) => texture === textureData.key)) {
|
||||
return true
|
||||
}
|
||||
|
||||
// If there's already a loading promise for this texture, return it
|
||||
if (textureLoadingPromises.has(textureData.key)) {
|
||||
return await textureLoadingPromises.get(textureData.key)!
|
||||
}
|
||||
|
||||
// Create new loading promise
|
||||
const loadingPromise = (async () => {
|
||||
// Check if the asset is already cached
|
||||
let texture = await textureStorage.get(textureData.key)
|
||||
|
||||
// If asset is not found, download it
|
||||
if (!texture) {
|
||||
await textureStorage.download(textureData)
|
||||
texture = await textureStorage.get(textureData.key)
|
||||
}
|
||||
|
||||
// If asset is found, add it to the scene
|
||||
if (texture) {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
// Remove existing texture if it exists
|
||||
if (scene.textures.exists(texture.key)) {
|
||||
scene.textures.remove(texture.key)
|
||||
}
|
||||
|
||||
scene.textures.addBase64(texture.key, texture.data)
|
||||
scene.textures.once(`addtexture-${texture.key}`, () => {
|
||||
gameStore.game.loadedTextures.push(textureData.key)
|
||||
textureLoadingPromises.delete(textureData.key) // Clean up the promise
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
textureLoadingPromises.delete(textureData.key) // Clean up the promise
|
||||
return false
|
||||
})()
|
||||
|
||||
// Store the loading promise
|
||||
textureLoadingPromises.set(textureData.key, loadingPromise)
|
||||
return loadingPromise
|
||||
}
|
||||
|
||||
export async function loadSpriteTextures(scene: Phaser.Scene, sprite_id: string) {
|
||||
if (!sprite_id) return false
|
||||
|
||||
const spriteStorage = new SpriteStorage()
|
||||
const sprite = await spriteStorage.get(sprite_id)
|
||||
|
||||
if (!sprite) {
|
||||
console.error('Failed to load sprite:', sprite_id)
|
||||
return false
|
||||
}
|
||||
|
||||
for await (const sprite_action of sprite.spriteActions) {
|
||||
const key = sprite.id + '-' + sprite_action.action
|
||||
await loadTexture(scene, {
|
||||
key,
|
||||
data: '/textures/sprites/' + sprite.id + '/' + sprite_action.action + '.png',
|
||||
group: sprite_action.frameCount > 1 ? 'sprite_animations' : 'sprites',
|
||||
updatedAt: sprite_action.updatedAt,
|
||||
originX: sprite_action.originX,
|
||||
originY: sprite_action.originY,
|
||||
frameWidth: sprite_action.frameWidth,
|
||||
frameHeight: sprite_action.frameHeight,
|
||||
frameRate: sprite_action.frameRate
|
||||
} as TextureData)
|
||||
|
||||
// If the sprite has no more than one frame, skip
|
||||
if (sprite_action.frameCount <= 1) continue
|
||||
|
||||
// Check if animation already exists
|
||||
if (scene.anims.get(key)) continue
|
||||
|
||||
// Add the animation to the scene
|
||||
const anim = scene.textures.get(key)
|
||||
scene.textures.addSpriteSheet(key, anim, { frameWidth: sprite_action.frameWidth ?? 0, frameHeight: sprite_action.frameHeight ?? 0 })
|
||||
scene.anims.create({
|
||||
key: key,
|
||||
frameRate: sprite_action.frameRate,
|
||||
frames: scene.anims.generateFrameNumbers(key, { start: 0, end: sprite_action.frameCount! - 1 }),
|
||||
repeat: -1
|
||||
})
|
||||
}
|
||||
return true
|
||||
}
|
@ -1,146 +0,0 @@
|
||||
import config from '@/application/config';
|
||||
import type { Map as MapT, TextureData, Tile as TileT, UUID } from '@/application/types';
|
||||
import { unduplicateArray } from '@/application/utilities';
|
||||
import { loadTexture } from '@/composables/gameComposable';
|
||||
import { MapStorage, TileStorage } from '@/storage/storages'
|
||||
|
||||
import Tilemap = Phaser.Tilemaps.Tilemap
|
||||
import TilemapLayer = Phaser.Tilemaps.TilemapLayer
|
||||
import Tileset = Phaser.Tilemaps.Tileset
|
||||
import Tile = Phaser.Tilemaps.Tile
|
||||
|
||||
export function getTile(layer: TilemapLayer | Tilemap, positionX: number, positionY: number): Tile | null {
|
||||
return layer.getTileAtWorldXY(positionX, positionY)
|
||||
}
|
||||
|
||||
export function tileToWorldXY(layer: TilemapLayer | Tilemap, positionX: number, positionY: number) {
|
||||
const worldPoint = layer.tileToWorldXY(positionX, positionY)
|
||||
if (!worldPoint) return { worldPositionX: 0, worldPositionY: 0 }
|
||||
|
||||
const worldPositionX = worldPoint.x + config.tile_size.height
|
||||
const worldPositionY = worldPoint.y
|
||||
|
||||
return { worldPositionX, worldPositionY }
|
||||
}
|
||||
|
||||
export function tileToWorldX(layer: TilemapLayer | Tilemap, positionX: number, positionY: number): number {
|
||||
const worldPoint = layer.tileToWorldXY(positionX, positionY)
|
||||
if (!worldPoint) return 0
|
||||
|
||||
return worldPoint.x + config.tile_size.width / 2
|
||||
}
|
||||
|
||||
export function tileToWorldY(layer: TilemapLayer | Tilemap, positionX: number, positionY: number): number {
|
||||
const worldPoint = layer.tileToWorldXY(positionX, positionY)
|
||||
if (!worldPoint) return 0
|
||||
|
||||
return worldPoint.y + config.tile_size.height * 1.5
|
||||
}
|
||||
|
||||
/**
|
||||
* Can also be used to replace tiles
|
||||
* @param map
|
||||
* @param layer
|
||||
* @param positionX
|
||||
* @param positionY
|
||||
* @param tileName
|
||||
*/
|
||||
export function placeTile(map: Tilemap, layer: TilemapLayer, positionX: number, positionY: number, tileName: string) {
|
||||
let tileImg = map.getTileset(tileName) as Tileset
|
||||
if (!tileImg) {
|
||||
tileImg = map.getTileset('blank_tile') as Tileset
|
||||
}
|
||||
layer.putTileAt(tileImg.firstgid, positionX, positionY)
|
||||
}
|
||||
|
||||
export function setLayerTiles(map: Tilemap, layer: TilemapLayer, tiles: string[][]) {
|
||||
if (!tiles) return
|
||||
|
||||
tiles.forEach((row: string[], y: number) => {
|
||||
row.forEach((tile: string, x: number) => {
|
||||
placeTile(map, layer, x, y, tile)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function createTileArray(width: number, height: number, tile: string = 'blank_tile') {
|
||||
return Array.from({ length: height }, () => Array.from({ length: width }, () => tile))
|
||||
}
|
||||
|
||||
export const calculateIsometricDepth = (positionX: number, positionY: number, width: number = 0, height: number = 0, isCharacter: boolean = false) => {
|
||||
const baseDepth = (positionX + positionY) * 1000
|
||||
if (isCharacter) {
|
||||
return baseDepth + 500 // Characters should always be above objects at the same position
|
||||
}
|
||||
return baseDepth + (width + height) / 4
|
||||
}
|
||||
|
||||
async function getTiles(tiles: TileT[], scene: Phaser.Scene) {
|
||||
// Load each tile into the scene
|
||||
for (const tile of tiles) {
|
||||
if (!tile) continue
|
||||
const textureData = {
|
||||
key: tile.id,
|
||||
data: '/textures/tiles/' + tile.id + '.png',
|
||||
group: 'tiles',
|
||||
updatedAt: tile.updatedAt
|
||||
} as TextureData
|
||||
await loadTexture(scene, textureData)
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadMapTilesIntoScene(map_id: UUID, scene: Phaser.Scene) {
|
||||
const tileStorage = new TileStorage()
|
||||
const mapStorage = new MapStorage()
|
||||
const map = await mapStorage.get(map_id)
|
||||
if (!map) return
|
||||
|
||||
const tileArray = unduplicateArray(map.tiles)
|
||||
const tiles = await tileStorage.getByIds(tileArray)
|
||||
|
||||
await getTiles(tiles, scene)
|
||||
}
|
||||
|
||||
export async function loadTilesIntoScene(tileIds: string[], scene: Phaser.Scene) {
|
||||
const tileStorage = new TileStorage()
|
||||
const tiles = await tileStorage.getByIds(tileIds)
|
||||
|
||||
await getTiles(tiles, scene)
|
||||
}
|
||||
|
||||
export async function loadAllTilesIntoScene(scene: Phaser.Scene) {
|
||||
const tileStorage = new TileStorage()
|
||||
const tiles = await tileStorage.getAll()
|
||||
|
||||
await getTiles(tiles, scene)
|
||||
}
|
||||
|
||||
export function createTileMap(scene: Phaser.Scene, map: MapT) {
|
||||
const mapConfig = new Phaser.Tilemaps.MapData({
|
||||
width: map.width,
|
||||
height: map.height,
|
||||
tileWidth: config.tile_size.width,
|
||||
tileHeight: config.tile_size.height,
|
||||
orientation: Phaser.Tilemaps.Orientation.ISOMETRIC,
|
||||
format: Phaser.Tilemaps.Formats.ARRAY_2D
|
||||
})
|
||||
|
||||
return new Phaser.Tilemaps.Tilemap(scene, mapConfig)
|
||||
}
|
||||
|
||||
export function createTileLayer(currentTileMap: Phaser.Tilemaps.Tilemap, mapData: any) {
|
||||
const tilesArray = unduplicateArray(mapData?.tiles.flat())
|
||||
|
||||
const tilesetImages = tilesArray.map((tile: string, index: number) => {
|
||||
return currentTileMap.addTilesetImage(tile, tile, config.tile_size.width, config.tile_size.height, 1, 2, index + 1, { x: 0, y: -config.tile_size.height })
|
||||
})
|
||||
|
||||
// Add blank tile
|
||||
tilesetImages.push(currentTileMap.addTilesetImage('blank_tile', 'blank_tile', config.tile_size.width, config.tile_size.height, 1, 2, 0, { x: 0, y: -config.tile_size.height }))
|
||||
|
||||
const layer = currentTileMap.createBlankLayer('tiles', tilesetImages as Tileset[], 0, config.tile_size.height) as Phaser.Tilemaps.TilemapLayer
|
||||
|
||||
layer.setDepth(0)
|
||||
layer.setCullPadding(2, 2)
|
||||
return layer
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
import { Direction } from '@/application/enums'
|
||||
import { type MapCharacter } from '@/application/types'
|
||||
import { loadSpriteTextures } from '@/composables/gameComposable'
|
||||
import { calculateIsometricDepth, tileToWorldX, tileToWorldY } from '@/composables/mapComposable'
|
||||
import { calculateIsometricDepth, tileToWorldX, tileToWorldY } from '@/services/mapService'
|
||||
import { loadSpriteTextures } from '@/services/textureService'
|
||||
import { CharacterTypeStorage } from '@/storage/storages'
|
||||
import { refObj } from 'phavuer'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
export function useCharacterSprite(scene: Phaser.Scene, tilemap: Phaser.Tilemaps.Tilemap, mapCharacter: MapCharacter) {
|
||||
export function useCharacterSpriteComposable(scene: Phaser.Scene, tilemap: Phaser.Tilemaps.Tilemap, mapCharacter: MapCharacter) {
|
||||
const characterContainer = refObj<Phaser.GameObjects.Container>()
|
||||
const characterSpriteId = ref('')
|
||||
const characterSprite = refObj<Phaser.GameObjects.Sprite>()
|
||||
|
Reference in New Issue
Block a user