Skip to content

Asset Loading

Erik Watson edited this page Apr 3, 2021 · 8 revisions

Assets can be loaded in via Bramble.assets, each type of asset is loaded in via a dedicated function. These loading functions are essentially just Promises with some conveniences. The Image loader returns the image as a HTML Image Element, the Terrain loader builds a usable Terrain object, etc.

You will notice quite quickly from the examples that the way the assets are loaded is very similar regardless of what type of asset it is you are loading.

Assumed Import

const { assets } = require('@erikwatson/bramble')

Load a Text File

Single File

assets
  .loadText('./path/to/text.txt')
  .then(txt => console.log(txt))
  .catch(err => console.error(err))

Multiple Files

const filePaths = [
  './path/to/a.txt', 
  './path/to/b.txt', 
  './path/to/c.txt'
]

assets
  .loadAllText(filePaths)
  .then(files => {
    files.forEach(txt => {
      console.log(txt)
    })
  })
  .catch(err => console.error(err))

Load an Image

Single Image

assets
  .loadImage('./path/to/img.png')
  .then(img => console.log(img))
  .catch(err => console.error(err))

Multiple Images

const filePaths = [
  './path/to/a.png', 
  './path/to/b.png', 
  './path/to/c.png'
]

assets
  .loadAllImages(filePaths)
  .then(files => {
    files.forEach(img => {
      console.log(img)
    })
  })
  .catch(err => console.error(err))

Load Music

TODO

Load Sound Effects

TODO

Load Terrain

Terrain in Bramble is the name for the auto-tiling tile maps, they are loaded in via a JSON file that describes what image they use, what tiles exist within that image, etc.

Single File

assets
  .loadTerrain('./path/to/terrain.json')
  .then(terrain => console.log(terrain))
  .catch(err => console.error(err))

Multiple Files

const filePaths = [
  './path/to/a.json', 
  './path/to/b.json', 
  './path/to/c.json'
]

assets
  .loadAllTerrain(filePaths)
  .then(allTerrain => {
    allTerrain.forEach(terrain => {
      console.log(terrain)
    })
  })
  .catch(err => console.error(err))

Load Level

TODO