Excalibur icon indicating copy to clipboard operation
Excalibur copied to clipboard

Add a resource document type loadable to excalibur

Open eonarheim opened this issue 9 years ago • 2 comments

Context

This would crystallize the common pattern that we tend to use every time we build a game.

Proposal

Create a Resource file resource.js that is a dictionary of resource keys and loadables, then in main.js we loop through those keys and add them to the ex loader.

// resource.js
var Resources = {   
   AxeSwing: new ex.Sound('sounds/axe-swing.mp3', 'sounds/axe-swing.wav'),
   AxeSwingHit: new ex.Sound('sounds/axe-swing-hit-2.mp3', 'sounds/axe-swing-hit-2.wav'),
}
// main.js
var loader = new ex.Loader();

// load up all resources in dictionary
_.forIn(Resources, (resource) => {
   loader.addResource(resource);
});

Instead we should just let people define a json file of their resources and load that into ex.

// resource.json
{
    "axeswing": ["sounds/axe-swing.mp3", "sounds/axe-swing.wav"],
    "axeimage": "img/axe-sprite.png"
}
loader.loadResourceFile('res/resource.json');

Resources could then hang off of a global in excalibur

ex.Resources.axeswing.play()

eonarheim avatar Jan 21 '16 03:01 eonarheim

Some feedback:

  1. Would we rely on file extension to know what class to load (Sound, Texture, etc.)
  2. One of the things I like about the file that's compiled is it's strongly typed for intellisense. In a loadable, I'd need to pass in an interface... and at that point I might as well use the old pattern

Instead I'd propose an overload to loader that accepts a JSON hash of loadables and also a constructor that allows passing in a dictionary (or array of dictionaries ...rest).

public addResources(dictionary: {[key: string]: ILoadable}): void {
  _.forIn(dictionary, (resource) => {
   this.addResource(resource);
  });
}

var GlobalTextures = {
  AxeSwing: new ex.Texture("foo")
}
var GlobalSounds = {
  AxeSwing: new ex.Sound("foo")
}

var loader = new ex.Loader(GlobalTextures, GlobalSounds, ...);

// or
loader.addResources(GlobalResources);

I think having external dictionary support is also okay (to load dynamic stuff or via AJAX or whatever), but it wouldn't be my default.

kamranayub avatar Jan 21 '16 14:01 kamranayub

This issue hasn't had any recent activity lately and is being marked as stale automatically.

github-actions[bot] avatar Dec 27 '20 00:12 github-actions[bot]

Closing for now, not feeling necessary for v1

eonarheim avatar Apr 25 '24 13:04 eonarheim