Make RoomObjects into real things
This commit is contained in:
parent
f99494c42b
commit
299f1dfcca
@ -12,13 +12,12 @@ const ROOM_ASSETS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default class Assets {
|
export default class Assets {
|
||||||
constructor() {
|
constructor(game) {
|
||||||
|
this.game = game
|
||||||
this.assetMap = {}
|
this.assetMap = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
get(assetName) {
|
get(assetName) {
|
||||||
console.log("getting", assetName)
|
|
||||||
console.log("from", this.assetMap)
|
|
||||||
return this.assetMap[assetName]
|
return this.assetMap[assetName]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +43,6 @@ export default class Assets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadImage(name, path) {
|
loadImage(name, path) {
|
||||||
console.log(name, path)
|
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const img = new Image()
|
const img = new Image()
|
||||||
this.assetMap[name] = img
|
this.assetMap[name] = img
|
||||||
@ -55,13 +53,13 @@ export default class Assets {
|
|||||||
|
|
||||||
loadTileset(name, path) {
|
loadTileset(name, path) {
|
||||||
return fetch(path).then(rsp => rsp.json()).then(json => {
|
return fetch(path).then(rsp => rsp.json()).then(json => {
|
||||||
return this.assetMap[name] = new Tileset(json, name)
|
return this.assetMap[name] = new Tileset(this.game, json, name)
|
||||||
}).then(tileset => this.loadImages(tileset.imagesToLoad))
|
}).then(tileset => this.loadImages(tileset.imagesToLoad))
|
||||||
}
|
}
|
||||||
|
|
||||||
loadRoom(name, path) {
|
loadRoom(name, path) {
|
||||||
return fetch(path).then(rsp => rsp.json()).then(json => {
|
return fetch(path).then(rsp => rsp.json()).then(json => {
|
||||||
return this.assetMap[name] = new Room(json, name)
|
return this.assetMap[name] = new Room(this.game, json, name)
|
||||||
}).then(room => this.loadTilesets(room.tilesetsToLoad))
|
}).then(room => this.loadTilesets(room.tilesetsToLoad))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ export default class Game {
|
|||||||
|
|
||||||
tick(dt) {
|
tick(dt) {
|
||||||
this.actors.forEach(actor => actor.tick(dt))
|
this.actors.forEach(actor => actor.tick(dt))
|
||||||
this.currentRoom.tick(this, dt)
|
this.currentRoom.tick(dt)
|
||||||
Object.values(this.events).forEach(e => e.nextFrame())
|
Object.values(this.events).forEach(e => e.nextFrame())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ document.addEventListener("DOMContentLoaded", async e => {
|
|||||||
const canvas = document.getElementById("game-canvas")
|
const canvas = document.getElementById("game-canvas")
|
||||||
const game = new Game(canvas)
|
const game = new Game(canvas)
|
||||||
|
|
||||||
game.assets = new Assets()
|
game.assets = new Assets(game)
|
||||||
await game.assets.load()
|
await game.assets.load()
|
||||||
|
|
||||||
game.start()
|
game.start()
|
||||||
|
29
src/room.js
29
src/room.js
@ -1,28 +1,16 @@
|
|||||||
import { doRectanglesOverlap } from "./util.js"
|
import RoomObject from "./roomObject.js"
|
||||||
|
|
||||||
export default class Room {
|
export default class Room {
|
||||||
constructor(json, name) {
|
constructor(game, json, name) {
|
||||||
|
this.game = game
|
||||||
this.json = json
|
this.json = json
|
||||||
this.name = name
|
this.name = name
|
||||||
|
const objectJson = this.json.layers.find(layer => layer.type == "objectgroup")?.objects || []
|
||||||
|
this.objects = objectJson.map(RoomObject.fromJson.bind(null, this.game))
|
||||||
}
|
}
|
||||||
|
|
||||||
tick(game, dt) {
|
tick(dt) {
|
||||||
this.json.layers.forEach(layer => {
|
this.objects.forEach(object => object.tick(dt))
|
||||||
if (layer.type == "objectgroup") this.tickObjectGroup(game, layer)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
tickObjectGroup(game, layer) {
|
|
||||||
const { player } = game
|
|
||||||
layer.objects.forEach(object => {
|
|
||||||
if (doRectanglesOverlap(player, object)) {
|
|
||||||
const eventName = object.properties.find(property => property.name == "event")?.value
|
|
||||||
if (eventName) game.triggerEvent(eventName, object)
|
|
||||||
}
|
|
||||||
if (player.interactHitbox && doRectanglesOverlap(player.interactHitbox, object)) {
|
|
||||||
const eventName = object.properties.find(property => property.name == "interactEvent")?.value
|
|
||||||
if (eventName) game.triggerEvent(eventName, object)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get tilesetsToLoad() {
|
get tilesetsToLoad() {
|
||||||
@ -34,7 +22,6 @@ export default class Room {
|
|||||||
}
|
}
|
||||||
|
|
||||||
populateTilesets(assets) {
|
populateTilesets(assets) {
|
||||||
console.log(this.json)
|
|
||||||
this.tilesets = this.json.tilesets.map((tileset, index) => {
|
this.tilesets = this.json.tilesets.map((tileset, index) => {
|
||||||
const ts = assets.get(`${this.name}-${index}`)
|
const ts = assets.get(`${this.name}-${index}`)
|
||||||
ts.populateImage(assets)
|
ts.populateImage(assets)
|
||||||
|
34
src/roomObject.js
Normal file
34
src/roomObject.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { doRectanglesOverlap } from "./util.js"
|
||||||
|
|
||||||
|
export default class RoomObject {
|
||||||
|
constructor(game) {
|
||||||
|
this.game = game
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(game, json) {
|
||||||
|
const roomObject = new RoomObject(game)
|
||||||
|
Object.entries(json).forEach(([key, value]) => roomObject[key] = value)
|
||||||
|
return roomObject
|
||||||
|
}
|
||||||
|
|
||||||
|
getProperty(name) {
|
||||||
|
const property = this.properties.find(p => p.name == name)
|
||||||
|
if (!property) {
|
||||||
|
// console.error(`Unknown property ${name} on ${this.name}`)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return property.value
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(dt) {
|
||||||
|
const { player } = this.game
|
||||||
|
if (doRectanglesOverlap(player, this)) {
|
||||||
|
const eventName = this.getProperty.call(this, "event")
|
||||||
|
if (eventName) this.game.triggerEvent(eventName, this)
|
||||||
|
}
|
||||||
|
if (player.interactHitbox && doRectanglesOverlap(player.interactHitbox, this)) {
|
||||||
|
const eventName = this.getProperty("interactEvent")
|
||||||
|
if (eventName) this.game.triggerEvent(eventName, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
export default class Tileset {
|
export default class Tileset {
|
||||||
constructor(json, name) {
|
constructor(game, json, name) {
|
||||||
|
this.game = game
|
||||||
this.json = json
|
this.json = json
|
||||||
this.name = name
|
this.name = name
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user