2025-06-07 08:28:54 -04:00
|
|
|
import RoomObject from "./roomObject.js"
|
|
|
|
|
2025-06-04 01:16:48 -04:00
|
|
|
export default class Room {
|
2025-06-07 08:28:54 -04:00
|
|
|
constructor(game, json, name) {
|
|
|
|
this.game = game
|
2025-06-04 01:16:48 -04:00
|
|
|
this.json = json
|
|
|
|
this.name = name
|
2025-06-07 08:28:54 -04:00
|
|
|
const objectJson = this.json.layers.find(layer => layer.type == "objectgroup")?.objects || []
|
|
|
|
this.objects = objectJson.map(RoomObject.fromJson.bind(null, this.game))
|
2025-06-04 01:16:48 -04:00
|
|
|
}
|
2025-06-03 21:33:59 -04:00
|
|
|
|
2025-06-07 08:28:54 -04:00
|
|
|
tick(dt) {
|
|
|
|
this.objects.forEach(object => object.tick(dt))
|
2025-06-04 21:29:23 -04:00
|
|
|
}
|
|
|
|
|
2025-06-04 01:16:48 -04:00
|
|
|
get tilesetsToLoad() {
|
|
|
|
const ts = {}
|
|
|
|
this.json.tilesets.forEach((tileset, index) => {
|
|
|
|
ts[`${this.name}-${index}`] = tileset.source
|
|
|
|
})
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
populateTilesets(assets) {
|
|
|
|
this.tilesets = this.json.tilesets.map((tileset, index) => {
|
|
|
|
const ts = assets.get(`${this.name}-${index}`)
|
|
|
|
ts.populateImage(assets)
|
|
|
|
return ts
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
draw(ctx) {
|
2025-06-04 21:29:23 -04:00
|
|
|
this.json.layers.forEach(this.drawLayer.bind(this, ctx))
|
2025-06-07 08:45:01 -04:00
|
|
|
this.objects.forEach(object => object.draw(ctx))
|
2025-06-04 21:29:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
drawLayer(ctx, layer) {
|
|
|
|
if (layer.type == "tilelayer") this.drawTileLayer(ctx, layer)
|
|
|
|
}
|
|
|
|
|
|
|
|
drawTileLayer(ctx, layer) {
|
|
|
|
for (let y = 0; y < layer.height; y++) {
|
|
|
|
for (let x = 0; x < layer.width; x++) {
|
|
|
|
const index = x + (y * layer.width)
|
|
|
|
const tileIndex = layer.data[index] - 1
|
|
|
|
const tileset = this.tilesets[0]
|
|
|
|
const [sx, sy] = tileset.tileOffset(tileIndex)
|
|
|
|
ctx.drawImage(
|
|
|
|
tileset.image,
|
|
|
|
sx,
|
|
|
|
sy,
|
|
|
|
tileset.tileWidth,
|
|
|
|
tileset.tileHeight,
|
|
|
|
x * this.json.tilewidth,
|
|
|
|
y * this.json.tileheight,
|
|
|
|
this.json.tilewidth,
|
|
|
|
this.json.tileheight
|
|
|
|
)
|
2025-06-04 01:16:48 -04:00
|
|
|
}
|
2025-06-04 21:29:23 -04:00
|
|
|
}
|
|
|
|
}
|
2025-06-03 21:33:59 -04:00
|
|
|
}
|