Socket
Socket
Sign inDemoInstall

@basementuniverse/content-manager

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @basementuniverse/content-manager

A component for loading content assets and providing access to them


Version published
Maintainers
1
Created

Readme

Source

Game Component: Content Manager

A component for loading content assets and providing access to them.

How to use

Initialise the content manager before use:

import ContentManager from '@basementuniverse/content-manager';

ContentManager.initialise();

Load content assets:

ContentManager.load([
  {
    name: 'my-item-1',
    type: 'json',
    args: ['https://some-url.com/test.json'],
  },
]);

Check content manager progress and status:

console.log(ContentManager.progress); // a number between 0 (nothing loaded yet) and 1 (finished loading)
console.log(ContentManager.status); // 0: Idle, 1: Loading, 2: Loaded

Fetch a content asset:

const item = ContentManager.get('my-item-1');

Options

const options = { ... };
ContentManager.initialise(options);
OptionTypeDefaultDescription
loadersContentLoaderMap(see below)A dictionary of content loader functions
simulateSlowLoadingbooleanfalseIf true, simulate long loading times
slowLoadingTimeMinnumber1000Minimum simulated loading time in ms
slowLoadingTimeMaxnumber3000Maximum simulated loading time in ms
throwOnNotFoundbooleanfalseThrow an error when an unknown content item is fetched with get()

Default loaders

The following loaders are registered by default:

{
  "json": JSONLoader,
  "font": FontLoader,
  "image": ImageLoader,
  "audio": AudioLoader,
}

Implementing a custom content loader

Define a function with a signature that matches ContentLoader:

import { ContentLoader } from '@basementuniverse/content-manager';

export const MyCustomLoader: ContentLoader = async <HTMLImageElement>(
  url: string
): Promise<HTMLImageElement> => {
  return new Promise<HTMLImageElement>((resolve, reject) => {
    const image = new Image();
    image.src = url;
    image.addEventListener('load', () => {
      resolve(image as any);
    });
    image.addEventListener('error', () => {
      reject(`Error loading image "${url}"`);
    });
  });
};

Register the loader when initialising the content manager:

ContentManager.initialise({
  loaders: {
    custom: MyCustomLoader,
  },
});

Load content assets using the custom loader:

ContentManager.load([
  {
    name: 'my-custom-item-1',
    type: 'custom',
    args: ['./test.png'],
  },
]);

Other components

  • Camera
  • Content Manager
  • Debug
  • Input Manager
  • Scene Manager

FAQs

Last updated on 29 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc