File: gameplay\spell-casting\spells\Components.js
function Spell(spellEntity, spellData, onUpdateScript, onCollisionScript){
this._spellEntity = spellEntity || null;
this._spellData = spellData || null;
this._onUpdateScript = onUpdateScript || null;
this._onCollisionScript = onCollisionScript || null;
this._destroy = false;
}
Spell.prototype._componentIdentifier = 0;
/**
A component class which allows an entity to cast spells.
@class SpellCasting
@constructor
@param rightCastPointX {Number} The x component of the cast point. The cast point is a position relative to the entity's position from which a spell will be fired. This point is for when an entity casts while facing to the right, the left variant is calculated automatically.
@param rightCastPointY {Number} The y component of the cast point. The cast point is a position relative to the entity's position from which a spell will be fired. This point is for when an entity casts while facing to the right, the left variant is calculated automatically.
@param friendCollideWith {Number} A bit mask used when querying the physics engine for friendly entities.
@param foeCollideWith {Number} A bit mask used when querying the physics engine for enemy entities.
*/
function SpellCasting(rightCastPointX, rightCastPointY, friendCollideWith, foeCollideWith){
this._rightCastPoint = new Vector2D(rightCastPointX, rightCastPointY);
this._friendCollideWith = (friendCollideWith !== undefined) ? friendCollideWith : 0xFFFFFFFF;
this._foeCollideWith = (foeCollideWith !== undefined) ? foeCollideWith : 0xFFFFFFFF;
this._spellCooldowns = {};
}
SpellCasting.prototype = {
constructor : SpellCasting,
_componentIdentifier : 0,
/**
@method getRightCastPoint
@param vector {Vector2D} Vector to which the cast point will be copied.
*/
getRightCastPoint : function(vector){
vector.x = this._rightCastPoint.x;
vector.y = this._rightCastPoint.y;
},
/**
@method setRightCastPoint
@param rightCastPointX {Number} The x component of the cast point. The cast point is a position relative to the entity's position from which a spell will be fired. This point is for when an entity casts while facing to the right, the left variant is calculated automatically.
@param rightCastPointY {Number} The y component of the cast point. The cast point is a position relative to the entity's position from which a spell will be fired. This point is for when an entity casts while facing to the right, the left variant is calculated automatically.
*/
setRightCastPoint : function(x, y){
this._rightCastPoint.x = x;
this._rightCastPoint.y = y;
},
/**
@method getFriendCollideWith
@return {Number}
*/
getFriendCollideWith : function(){
return this._friendCollideWith;
},
/**
@method setFriendCollideWith
@param friendCollideWith {Number} A bit mask used when querying the physics engine for friendly entities.
*/
setFriendCollideWith : function(friendCollideWith){
this._friendCollideWith = friendCollideWith;
},
/**
@method getFoeCollideWith
@return {Number}
*/
getFoeCollideWith : function(){
return this._foeCollideWith;
},
/**
@method setFoeCollideWith
@param foeCollideWith {Number} A bit mask used when querying the physics engine for enemy entities.
*/
setFoeCollideWith : function(foeCollideWith){
this._foeCollideWith = foeCollideWith;
},
/**
@method getSpellCooldown
@param spellName {String}
@return {Number} Number of seconds before the spell can be used.
*/
getSpellCooldown : function(spellName){
var cooldown = this._spellCooldowns[spellName];
//If cooldown is undefined or negative, return 0, otherwise return the cooldown.
return (cooldown > 0) ? cooldown : 0;
},
/**
@method setSpellCooldown
@param spellName {String}
@param cooldown {Number} This value needs to be in seconds.
*/
setSpellCooldown : function(spellName, cooldown){
this._spellCooldowns[spellName] = cooldown;
},
/**
Checks whether the spell is ready to be cast again.
@method canCastSpell
@param spellName {String}
@return {Boolean}
*/
canCastSpell : function(spellName){
var cooldown = this._spellCooldowns[spellName];
//This is going to return true if cooldown is undefined or if the cooldown is less than or equal to 0.
return !(cooldown > 0);
}
}