API Docs for:
Show:

File: gameplay\movement\dashing\System.js

/**
 The system uses the 'moveLeft' and 'moveRight' events triggered by the input system to trigger dashing. The system uses a
 double click appraoch for that. Dashing is achieved by usage of a MovementSpeedSpellEffect and it requires a Dashing, GroundedMovement
 and SpellEffects components to be present on an entity.
 @class DashingSystem
 @constructor
 @param entitySystemManager {Manager} The entity system manager whose entities this system will be working on.
 @param inputSystem {KeyboardInputSystem}
 */
function DashingSystem(entitySystemManager, inputSystem){
    
    //System variables.
    //Maximum time between clicks to register a double click.
    var DOUBLE_CLICK_TIME = 0.1;
    
    var moveLeftEventCallback = function(entity){        
        var dashing = entity.get(Dashing),
            groundedMovement = entity.get(GroundedMovement),
            spellEffects = entity.get(SpellEffects);
        
        if(!(dashing && groundedMovement && spellEffects)){
            return;
        }
        
        dashing._movedLeftThisFrame = true;
        
        if(dashing._timeSinceMovedLeft > 0 &&
           dashing._timeSinceMovedLeft <= DOUBLE_CLICK_TIME &&
           //Dashing can only be activated if the entity is on the ground.
           groundedMovement.isOnGround()){
            
            dashing._dashingSpellEffect = new MovementSpeedSpellEffect(64/1000, dashing._dashingSpeedMultiplier);
            spellEffects.applySpellEffect(dashing._dashingSpellEffect);
            dashing._isDashingLeft = true;
        }
    };
    
    var moveRightEventCallback = function(entity){
        var dashing = entity.get(Dashing),
            groundedMovement = entity.get(GroundedMovement),
            spellEffects = entity.get(SpellEffects);
        
        if(!(dashing && groundedMovement && spellEffects)){
            return;
        }
        
        dashing._movedRightThisFrame = true;
        
        if(dashing._timeSinceMovedRight > 0 &&
           dashing._timeSinceMovedRight <= DOUBLE_CLICK_TIME &&
           //Dashing can only be activated if the entity is on the ground.
           groundedMovement.isOnGround()){
            
            dashing._dashingSpellEffect = new MovementSpeedSpellEffect(64/1000, dashing._dashingSpeedMultiplier);
            spellEffects.applySpellEffect(dashing._dashingSpellEffect);
            dashing._isDashingRight = true;
        }
    };
    
    inputSystem.subscribe('moveLeft', moveLeftEventCallback);
    inputSystem.subscribe('moveRight', moveRightEventCallback);
    
    var dashingEntities = entitySystemManager.createAspect([Dashing]);
    
    /**
     @method update
     @param deltaTime {Number}
     */
    this.update = function(deltaTime){
        dashingEntities.iterate(function(entity){
            var dashing = entity.get(Dashing);
            
            if(dashing._movedLeftThisFrame){
                dashing._timeSinceMovedLeft = 0;
            }else{
                dashing._isDashingLeft = false;
                dashing._timeSinceMovedLeft += deltaTime;
            }
            dashing._movedLeftThisFrame = false;
            
            if(dashing._movedRightThisFrame){
                dashing._timeSinceMovedRight = 0;
            }else{
                dashing._isDashingRight = false;
                dashing._timeSinceMovedRight += deltaTime;
            }
            dashing._movedRightThisFrame = false;
            
            if(dashing.isDashing()){
                dashing._dashingSpellEffect._durationRemaining = 64/1000;
            }
        });
    };
    
    /**
     @method destroy
     */
    this.destroy = function(){
        inputSystem.unsubscribe('moveLeft', moveLeftEventCallback);
        inputSystem.unsubscribe('moveRight', moveRightEventCallback);
        
        dashingEntities.destroy();
    }
}