File: gameplay\spell-casting\spell-invoking\Components.js
/**
A component class which allows an entity to combine colors to cast spells.
@class SpellInvoking
@constructor
@param invokingPatterns {Object} An object holding patterns and corresponding spells. See example for more.
@param colorQueueLength {Number} Number of non-unique colors per pattern.
@param canUseCyan {Boolean} Specifies whether the entity can use cyan when combining colors.
@param canUseMagenta {Boolean} Specifies whether the entity can use magenta when combining colors.
@param canUseYellow {Boolean} Specifies whether the entity can use yellow when combining colors.
@example
//Invoking patterns should be in this form:
var invokingPatterns = {
ccc : 'cyanMissile',
cym : 'flare'
};
//The property names on the object describe color combinations.
//Each letter corresponds to a different color, c for cyan, y for yellow and m for magenta.
//The order of colors on the property name doesn't matter, only the count of each individual color.
//The values are strings holding names of spells that correspond to the pattern on the property name.
*/
function SpellInvoking(invokingPatterns, colorQueueLength, canUseCyan, canUseMagenta, canUseYellow){
this._invokingPatterns = invokingPatterns || null;
this._colorQueue = new Array(colorQueueLength || 3);
this._canUseCyan = canUseCyan || false;
this._canUseMagenta = canUseMagenta || false;
this._canUseYellow = canUseYellow || false;
/*These flags and the supporting logic make sure that the user can't, intentionally or not,
*queue a color multiple times just by holding the use button.
*
*What will happen is, if the player uses a color, then it'll set the _inputWasUsed flag to true.
*If the _inputWasUsed flag is true, then, in the update function, the _inputCanUse flag is set to false and the
*_inputWasUsed flag is reset to false. The _inputCanUse flag will be set to true only if during one frame,
*the color isn't used and thus the _inputWasUsed flag remains false when the update function is called.
*/
this._inputWasCyanUsed = false;
this._inputCanUseCyan = true;
this._inputWasMagentaUsed = false;
this._inputCanUseMagenta = true;
this._inputWasYellowUsed = false;
this._inputCanUseYellow = true;
}
SpellInvoking.prototype = {
constructor : SpellInvoking,
_componentIdentifier : 0,
/**
@method getInvokingPatterns
@return {Object|Null} Direct reference to the pattern object. If the object wasn't set, returns null.
*/
getInvokingPatterns : function(){
return this._invokingPatterns;
},
/**
@method setInvokingPatterns
@param invokingPatterns {Object} An object holding patterns and corresponding spells. See constructor example for more.
*/
setInvokingPatterns : function(invokingPatterns){
this._invokingPatterns = invokingPatterns;
},
/**
@method getColorQueueLength
@return {Number}
*/
getColorQueueLength : function(){
return this._colorQueue.length;
},
/**
@method setColorQueueLength
@param colorQueueLength {Number} Number of non-unique colors per pattern.
*/
setColorQueueLength : function(colorQueueLength){
this._colorQueue.length = colorQueueLength;
},
/**
Sets all queued colors to null.
@method clearColorQueue
*/
clearColorQueue : function(){
for(var i=0; i<this.colorQueue.length; ++i){
this.colorQueue[i] = null;
}
},
/**
Returns a color at a given index in the color queue.
@method colorAt
@param index {Number} Index of the color in the queue. If this index is out of bounds, returns null.
@return {String|Null} Returns 'c' for cyan, 'y' for yellow and 'm' for magenta. Returns null if the color was not set or if the provided index is out of bounds.
*/
colorAt : function(index){
if(index < 0 || index >= this._colorQueue.length){
return null;
}else{
return this._colorQueue[index];
}
},
/**
@method getCanUseCyan
@return {Boolean}
*/
getCanUseCyan : function(){
return this._canUseCyan;
},
/**
@method setCanUseCyan
@param canUseCyan {Boolean} Specifies whether the entity can use cyan when combining colors.
*/
setCanUseCyan : function(canUseCyan){
this._canUseCyan = canUseCyan;
},
/**
@method getCanUseMagenta
@return {Boolean}
*/
getCanUseMagenta : function(){
return this._canUseMagenta;
},
/**
@method setCanUseMagenta
@param canUseMagenta {Boolean} Specifies whether the entity can use magenta when combining colors.
*/
setCanUseMagenta : function(canUseMagenta){
this._canUseMagenta = canUseMagenta;
},
/**
@method getCanUseYellow
@return {Boolean}
*/
getCanUseYellow : function(){
return this._canUseYellow;
},
/**
@method setCanUseYellow
@param canUseYellow {Boolean} Specifies whether the entity can use yellow when combining colors.
*/
setCanUseYellow : function(canUseYellow){
this._canUseYellow = canUseYellow;
}
};