/**
A component class responsible for attacking.
@class Attacking
@constructor
@param attack {MeleeAttack|RangedAttack} An object describing the type of attack the entity will perform.
@param rightAttackPointX {Number} The x component of the attack point. The attack point is a position relative to the entity's position from which an attack will be performed. For melee attacks, it's the center point for the scan shape, for ranged attacks it's the point from which projectiles will be fired. This point is for when an entity attacks while facing to the right, the left variant is calculated automatically.
@param rightAttackPointY {Number} The y component of the attack point. The attack point is a position relative to the entity's position from which an attack will be performed. For melee attacks, it's the center point for the scan shape, for ranged attacks it's the point from which projectiles will be fired. This point is for when an entity attacks while facing to the right, the left variant is calculated automatically.
@param rightAttackDirectionX {Number} The x component of the direction vector. The vector should be pointing in the attack direction when an entity is facing to the right, the left variant is calculated automatically. This vector needs to be normalized. A direction is only required if the attack type is ranged.
@param rightAttackDirectionY {Number} The y component of the direction vector. The vector should be pointing in the attack direction when an entity is facing to the right, the left variant is calculated automatically. This vector needs to be normalized. A direction is only required if the attack type is ranged.
@param attackCooldown {Number} Attack cooldown needs to be in seconds.
@param foeCollideWith {Number} A bit mask used when querying the physics engine for enemy entities.
*/
function Attacking(attack, rightAttackPointX, rightAttackPointY, rightAttackDirectionX, rightAttackDirectionY, attackCooldown, foeCollideWith){
this._attack = attack || null;
this._rightAttackPoint = new Vector2D(rightAttackPointX, rightAttackPointY);
this._rightAttackDirection = new Vector2D(rightAttackDirectionX, rightAttackDirectionY);
this._attackCooldown = attackCooldown || 0;
this._attackCooldownRemaining = 0;
this._foeCollideWith = (foeCollideWith !== undefined) ? foeCollideWith : 0xFFFFFFFF;
}
Attacking.prototype = {
constructor : Attacking,
_componentIdentifier : 0,
/**
Returns the attack object.
@method getAttack
@return {MeleeAttack|RangedAttack}
*/
getAttack : function(){
return this._attack;
},
/*
Sets a new type of attack.
@method setAttack
@param attack {MeleeAttack|RangedAttack} An object describing the type of attack the entity will perform.
*/
setAttack : function(attack){
this._attack = attack;
},
/**
@method getRightAttackPoint
@param vector {Vector2D} Vector to which the attack point will be copied.
*/
getRightAttackPoint : function(vector){
vector.x = this._rightAttackPoint.x;
vector.y = this._rightAttackPoint.y;
},
/**
@method setRightAttackPoint
@param x {Number} The x component of the attack point. The attack point is a position relative to the entity's position from which an attack will be performed. For melee attacks, it's the center point for the scan shape, for ranged attacks it's the point from which projectiles will be fired. This point is for when an entity attacks while facing to the right, the left variant is calculated automatically.
@param y {Number} The y component of the attack point. The attack point is a position relative to the entity's position from which an attack will be performed. For melee attacks, it's the center point for the scan shape, for ranged attacks it's the point from which projectiles will be fired. This point is for when an entity attacks while facing to the right, the left variant is calculated automatically.
*/
setRightAttackPoint : function(x, y){
this._rightAttackPoint.x = x;
this._rightAttackPoint.y = y;
},
/**
@method getRightAttackDirection
@param vector {Vector2D} Vector to which the attack direction will be copied.
*/
getRightAttackDirection : function(vector){
vector.x = this._rightAttackDirection.x;
vector.y = this._rightAttackDirection.y;
},
/**
@method setRightAttackDirection
@param x {Number} The x component of the direction vector. The vector should be pointing in the attack direction when an entity is facing to the right, the left variant is calculated automatically. This vector needs to be normalized. A direction is only required if the attack type is ranged.
@param y {Number} The y component of the direction vector. The vector should be pointing in the attack direction when an entity is facing to the right, the left variant is calculated automatically. This vector needs to be normalized. A direction is only required if the attack type is ranged.
*/
setRightAttackDirection : function(x, y){
this._rightAttackDirection.x = x;
this._rightAttackDirection.y = y;
},
/**
Returns the attack cooldown.
@method getAttackCooldown
@return {Number} Attack cooldown is in seconds.
*/
getAttackCooldown : function(){
return this._attackCooldown;
},
/*
@method setAttackCooldown
@param attackCooldown {Number} Attack cooldown needs to be in seconds.
*/
setAttackCooldown : function(attackCooldown){
this._attackCooldown = attackCooldown;
},
/**
Returns the remaining attack cooldown.
@method getAttackCooldownRemaining
@return {Number} Number of seconds remaining before the entity can attack again.
*/
getAttackCooldownRemaining : function(){
//The remaining attack cooldown can be negative due to the underlying logic. If it's negative, then just return 0.
return (this._attackCooldownRemaining > 0) ? this._attackCooldownRemaining : 0;
},
/**
Check whether the entity is ready to attack.
@method canAttack
@return {Boolean}
*/
canAttack : function(){
return this._attackCooldownRemaining <= 0;
},
/**
@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;
}
};
/**
An object describing a melee attack.
@class MeleeAttack
@constructor
@param scanShape {Box|Circle|Polygon} Object representing the area affected by the melee attack.
@param damageAmount {Number} The amount of damage dealt by the melee attack.
@param damageType {String} The type of damage dealt by the melee attack. This type can be 'c' for cyan, 'm' for magenta, 'y' for yellow and 'w' for white.
*/
function MeleeAttack(scanShape, damageAmount, damageType){
this._scanShape = new Shape(scanShape);
this._damageAmount = damageAmount || 0;
this._damageType = damageType || null;
}
MeleeAttack.prototype = {
constructor : MeleeAttack,
/**
@method getScanShape
@return {Box|Circle|Polygon}
*/
getScanShape : function(){
return this._scanShape.getShape();
},
/**
@method setScanShape
@param scanShape {Box|Circle|Polygon} Object representing the area affected by the melee attack.
*/
setScanShape : function(shape){
this._scanShape.setShape(shape);
},
/**
@method getDamageAmount
@return {Number}
*/
getDamageAmount : function(){
return this._damageAmount;
},
/**
@method setDamageAmount
@param damageAmount {Number} The amount of damage dealt by the melee attack.
*/
setDamageAmount : function(damageAmount){
this._damageAmount = damageAmount;
},
/**
@method getDamageType
@return {String|Null} Returns 'c', 'm', 'y' or 'w' if a correct damage type was set. If no damage type was set, it'll return null.
*/
getDamageType : function(){
return this._damageType;
},
/**
@method setDamageType
@param damageType {String} The type of damage dealt by the melee attack. This type can be 'c' for cyan, 'm' for magenta, 'y' for yellow and 'w' for white.
*/
setDamageType : function(damageType){
this._damageType = damageType;
}
}
/**
An object describing a ranged attack. A ranged attack can spawn multiple projectiles at once.
@class RangedAttack
@constructor
*/
function RangedAttack(){
this._projectiles = [];
}
RangedAttack.prototype = {
constructor : RangedAttack,
/**
@method addProjectile
@param damageAmount {Number} The amount of damage dealt by the projectile.
@param damageType {String} The type of damage dealt by the projectile. This type can be 'c' for cyan, 'm' for magenta, 'y' for yellow and 'w' for white.
@param shape {Box|Circle|Polygon} Object representing the physical shape of the projectile.
@param speed {Number} The speed of the projectile.
@param angleOffset {Number} This value needs to be in degrees. It describes the difference between the direction of attack and the direction a projectile is fired. For example, if this value is 30, the projectile will be fired slightly higher than the attack direction, if it's -30, the projectile will be fired slightly lower.
@param flyingAnimationHandle {AssetHandle} A handle to the animation to be played while the projectile is flying.
@param [impactAnimationHandle] {AssetHandle} A handle to the animation to be played once the projectile hits something.
@param [forceGenerator] {Object} A force generator that will affect this projectile. This can be for example a GravityGenerator.
*/
addProjectile : function(damageAmount, damageType, shape, speed, angleOffset, flyingAnimationHandle, impactAnimationHandle, forceGenerator){
this._projectiles.push({
damageAmount : damageAmount,
damageType : damageType,
shape : shape,
speed : speed,
//The accepted angle is in degrees, transform that value to radians.
angleOffset : angleOffset * Math.PI / 180,
flyingAnimationHandle : flyingAnimationHandle,
impactAnimationHandle : impactAnimationHandle,
forceGenerator : forceGenerator,
});
}
}
function Projectile(damageAmount, damageType, forceGenerator){
this._damageAmount = damageAmount || 0;
this._damageType = damageType || null;
this._forceGenerator = forceGenerator || false;
this._destroy = false;
}
Projectile.prototype._componentIdentifier = 0;