2025-06-04 01:16:48 -04:00
|
|
|
import Room from "./room.js"
|
|
|
|
import Tileset from "./tileset.js"
|
|
|
|
|
|
|
|
const IMAGE_ASSETS = {
|
|
|
|
}
|
|
|
|
|
|
|
|
const TILESET_ASSETS = {
|
|
|
|
}
|
|
|
|
|
|
|
|
const ROOM_ASSETS = {
|
|
|
|
sampleRoom: "./rooms/sample.tmj"
|
|
|
|
}
|
|
|
|
|
2025-06-03 21:33:59 -04:00
|
|
|
export default class Assets {
|
2025-06-07 08:28:54 -04:00
|
|
|
constructor(game) {
|
|
|
|
this.game = game
|
2025-06-03 21:33:59 -04:00
|
|
|
this.assetMap = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(assetName) {
|
|
|
|
return this.assetMap[assetName]
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2025-06-04 01:16:48 -04:00
|
|
|
await Promise.all([
|
|
|
|
this.loadImages(IMAGE_ASSETS),
|
|
|
|
this.loadTilesets(TILESET_ASSETS),
|
|
|
|
this.loadRooms(ROOM_ASSETS)
|
2025-06-03 21:41:46 -04:00
|
|
|
])
|
2025-06-04 01:16:48 -04:00
|
|
|
Object.keys(ROOM_ASSETS).forEach(roomName => this.get(roomName).populateTilesets(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
loadImages(images) {
|
|
|
|
return Promise.all(Object.entries(images).map(entry => this.loadImage(...entry)))
|
|
|
|
}
|
|
|
|
|
|
|
|
loadTilesets(tilesets) {
|
|
|
|
return Promise.all(Object.entries(tilesets).map(entry => this.loadTileset(...entry)))
|
|
|
|
}
|
|
|
|
|
|
|
|
loadRooms(rooms) {
|
|
|
|
return Promise.all(Object.entries(rooms).map(entry => this.loadRoom(...entry)))
|
2025-06-03 21:41:46 -04:00
|
|
|
}
|
2025-06-03 21:33:59 -04:00
|
|
|
|
2025-06-03 21:41:46 -04:00
|
|
|
loadImage(name, path) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const img = new Image()
|
|
|
|
this.assetMap[name] = img
|
|
|
|
img.src = path
|
|
|
|
img.addEventListener("load", () => resolve(img))
|
2025-06-03 21:33:59 -04:00
|
|
|
})
|
|
|
|
}
|
2025-06-04 01:16:48 -04:00
|
|
|
|
|
|
|
loadTileset(name, path) {
|
|
|
|
return fetch(path).then(rsp => rsp.json()).then(json => {
|
2025-06-07 08:28:54 -04:00
|
|
|
return this.assetMap[name] = new Tileset(this.game, json, name)
|
2025-06-04 01:16:48 -04:00
|
|
|
}).then(tileset => this.loadImages(tileset.imagesToLoad))
|
|
|
|
}
|
|
|
|
|
|
|
|
loadRoom(name, path) {
|
|
|
|
return fetch(path).then(rsp => rsp.json()).then(json => {
|
2025-06-07 08:28:54 -04:00
|
|
|
return this.assetMap[name] = new Room(this.game, json, name)
|
2025-06-04 01:16:48 -04:00
|
|
|
}).then(room => this.loadTilesets(room.tilesetsToLoad))
|
|
|
|
}
|
2025-06-03 21:33:59 -04:00
|
|
|
}
|