API Docs for:
Show:

File: physics\ForceGeneratorRegistry.js

/**
 This registry provides a universal interface for dealing with force generators and entities those generators affect. The registry requires that the force generators provide a function named "applyForce". This function should accept an entity and, if required, a second parameter, usually the time delta (see the update method). For examples of the implementation see the provided force generators.
 @class ForceGeneratorRegistry
 @constructor
 */

function ForceGeneratorRegistry(){
    
    var registry = new LinkedList();
    
    /**
     Adds an entry into the registry. From now on, each time the registry is updated, the force generator will be used to apply a force to the entity.
     @method add
     @param forceGenerator {} The force generator object.
     @param entity {Entity} The entity this generator should apply force to.
     */
    this.add = function(forceGenerator, entity){        
        registry.insert(0, [forceGenerator, entity]);
    };
    
    /**
     Removes a single entry from the registry.
     @method remove
     @param forceGenerator {} The force generator object used when creating the entry.
     @param entity {Entity} The entity used when creating the entry.
     */
    this.remove = function(forceGenerator, entity){            
        registry.removeIf(function(nodeValue){
            return nodeValue[0] === forceGenerator && nodeValue[1] === entity;
        });
    };
    
    /**
     Removes all entries from the registry that have the specified force generator.
     @method removeForceGenerator
     @param forceGenerator {} The force generator object.
     */
    this.removeForceGenerator = function(forceGenerator){
        registry.removeIf(function(nodeValue){
            return nodeValue[0] === forceGenerator;
        }, true);
    };
    
    /**
     Removes all entries from the registry that have the specified entity.
     @method removeEntity
     @param entity {Entity}
     */
    this.removeEntity = function(entity){            
        registry.removeIf(function(nodeValue){
            return nodeValue[1] === entity;
        }, true);
    };
    
    /**
     Removes all entries from the registry.
     @method clear
     */
    this.clear = function(){
        registry.clear();
    };
    
    /**
     @method update
     @param deltaTime {Number} This is the second paramterer sent to each applyForce call. It's meant to be used for sending the frame time, since some force generators may require it, but since this is JavaScript, it can be anything.
     */
    this.update = function(deltaTime){        
        for(var node=registry.getFirst(); node; node=node.next){
            node.value[0].applyForce(node.value[1], deltaTime);
        }
    };
}