HTML and CSS Reference
In-Depth Information
player.del('sword');
// Should cause an error
player.attack();
The component system in Quintus needs to register components, add components, remove components, and
let the base sprite be extended with additional methods.
Implementing the Component System
The actual implementation for the component system has three separate parts. The first is the register method to
register components. The register functionality is handled by the Q.register method, and under the hood it
creates a new class for the component and stores it in Q.components , indexed by name.
Add the code in Listing 9-14 in the usual spot to the bottom of quintus.js , before the final return
statement.
Listing 9-14: The base Quintus components functionality
Q.components = {};
Q.register = function(name,methods) {
methods.name = name;
Q.components[name] = Q.Component.extend(methods);
};
All this code does is register the component into a Q.components object and extend the Q.Component
class with the passed-in method. Next is the creation of the Q.Component class that handles the heavy lifting
of adding itself and removing itself from an object.
Add the code in Listing 9-15 below the code you just added in to create the Q.Component class.
Listing 9-15: The Q.Component class
Q.Component = Q.Evented.extend({
init: function(entity) {
this.entity = entity;
if(this.extend) _.extend(entity,this.extend);
entity[this.name] = this;
entity.activeComponents.push(this.name);
if(this.added) this.added();
},
destroy: function() {
if(this.extend) {
var extensions = _.keys(this.extend);
for(var i=0,len=extensions.length;i<len;i++) {
delete this.entity[extensions[i]];
}
}
delete this.entity[this.name];
var idx = this.entity.activeComponents.indexOf(this.name);
 
 
 
 
Search WWH ::




Custom Search