Created
July 5, 2011 15:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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