Skip to content

Instantly share code, notes, and snippets.

@iaincarsberg
Created July 5, 2011 15:11
Show Gist options
  • Save iaincarsberg/1065026 to your computer and use it in GitHub Desktop.
Save iaincarsberg/1065026 to your computer and use it in GitHub Desktop.
Example of a possible solution when adding components that require an asynchronous operation to attach to an entity, for example when loading a level.
require('./thorny/base')('./config/default.json', function ($) {
// Register a component so we can use it below.
$.es().registerComponent('load-level', function () {
return {
// Used to enable the isReady function.
asynchronousAttachEvent: 'world-loaded',
// Called in response to .addComponent()
attach: function (entity, filename) {
$.ajax({
url: '...',
data: {filename: filename},
success: function (data) {
// ...
// Mark the end of this request.
entity.asynchronousTaskComplete();
}
});
}
};});
// Create the world, and load in two level files.
$.es().makeEntity()
.addTag('world')
.addComponent('load-level', './content/level/fileOne.json')
.addComponent('load-level', './content/level/fileTwo.json');
$.event().bind('world-loaded', function () {
// Called once all the asynchronous operations have completed.
//
// One possible use would be to spawn monsters/items into the
// loaded level.
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment