File: gameplay\AI\AIScripts.js
/*
*Objects that hold data for the AI scripts. They're also used to identify which scripts are attached to an AI component,
*allowing the AI system to call apropriate functions for the AI entities.
*
*The script identifiers on the prototypes for each object need to be unique sequential integers as they're used as
*indices into an array within the AI component.
*/
//=====Walking script=====
/**
An AI script object. Causes the entity to walk left to right and back based on the facing direction. If it detects a wall, it causes the entity to turn around. Requires a GroundedMovement component, all the dynamic physics components and a FacingDirection component to be present.
@class AIScriptWalk
@constructor
@param wallDetectorRightRay {Ray} A ray that will be used to detect walls. The beginning position of the ray should be defined relatively to the position of the entity. The ray is for detecting walls on the right side, the left variant is calculated automatically.
*/
function AIScriptWalk(wallDetectorRightRay){
this._wallDetectorRightRay = wallDetectorRightRay;
}
AIScriptWalk.prototype = {
_scriptIdentifier : 0,
constructor : AIScriptWalk,
/**
@method getWallDetectorRightRay
@return {Ray} The ray object.
*/
getWallDetectorRightRay : function(){
return this._wallDetectorRightRay;
},
/**
@method setWallDetectorRightRay
@param wallDetectorRightRay {Ray} A ray that will be used to detect walls. The beginning position of the ray should be defined relatively to the position of the entity. The ray is for detecting walls on the right side, the left variant is calculated automatically.
*/
setWallDetectorRightRay : function(wallDetectorRightRay){
this._wallDetectorRightRay = wallDetectorRightRay;
}
};
//=====Gap detection script=====
/**
An AI script object. Causes the entity to turn around if an open space is detected by the ray. Requires a GroundedMovement component, a Position and a FacingDirection component to be present. Doesn't move the entity, just changes the facing direction. Doesn't work if the entity isn't on ground.
@class AIScriptAvoidGaps
@constructor
@param gapDetectorRightRay {Ray} A ray that will be used to detect gaps. The ray should be defined in relation to the position of the entity. The ray is for detecting gaps on the right side, a left variant is calculated automatically. The ray should be defined in a way so that it can't be contained within a world shape, because that will cause the shape to not be detected and thus the entity will turn around. This is especially apparent if you define the beginning of the ray at the "foot" level of an entity and point it straight down, then when climbing a hill, this might cause the hill to not be detected. To avoid this, define the ray beginning at the "head" level, extended slightly to the right and pointing a long way down.
*/
function AIScriptAvoidGaps(gapDetectorRightRay){
this._gapDetectorRightRay = gapDetectorRightRay;
}
AIScriptAvoidGaps.prototype = {
_scriptIdentifier : 1,
constructor : AIScriptAvoidGaps,
/**
@method getGapDetectorRightRay
@return {Ray} The ray object.
*/
getGapDetectorRightRay : function(){
return this._gapDetectorRightRay;
},
/**
@method setGapDetectorRightRay
@param gapDetectorRightRay {Ray} A ray that will be used to detect gaps. The ray should be defined in relation to the position of the entity. The ray is for detecting gaps on the right side, a left variant is calculated automatically. The ray should be defined in a way so that it can't be contained within a world shape, because that will cause the shape to not be detected and thus the entity will turn around. This is especially apparent if you define the beginning of the ray at the "foot" level of an entity and point it straight down, then when climbing a hill, this might cause the hill to not be detected. To avoid this, define the ray beginning at the "head" level, extended slightly to the right and pointing a long way down.
*/
setGapDetectorRightRay : function(gapDetectorRightRay){
this._gapDetectorRightRay = gapDetectorRightRay;
}
};
//=====Charging enemies script=====
/**
An AI script object. Causes the entity to speed up if an enemy entity is detected in front of it. The speed increase is handled by a spell effect, so the corresponding component needs to be present on the entity, additionally the GroundedMovement component, a Position and a FacingDirection are required. This script doesn't cause the entity to move, it should be paired with the walk script.
@class AIScriptChargeEnemies
@constructor
@param chargeTargetDetectorRange {Number} The enemy detection range. The script uses a ray to query for enemies. The ray is constructed automatically by using a ground tangent stored on the GroundedMovement component. Thanks to this, the ray always runs along the ground and the entity can charge enemies up and down hill.
@param chargingSpeedMultiplier {Number} The speed multiplier that will be used to construct the MovementSpeedSpellEffect.
*/
function AIScriptChargeEnemies(chargeTargetDetectorRange, chargingSpeedMultiplier){
this._chargeTargetDetectorRange = chargeTargetDetectorRange || 0;
this._chargingSpeedMultiplier = chargingSpeedMultiplier || 1;
this._chargingSpellEffect = null;
this._isCharging = false;
}
AIScriptChargeEnemies.prototype = {
_scriptIdentifier : 2,
constructor : AIScriptChargeEnemies,
/**
@method getChargeTargetDetectorRange
@return {Number}
*/
getChargeTargetDetectorRange : function(){
return this._chargeTargetDetectorRange;
},
/**
@method setChargeTargetDetectorRange
@param chargeTargetDetectorRange {Number} The enemy detection range. The script uses a ray to query for enemies. The ray is constructed automatically by using a ground tangent stored on the GroundedMovement component. Thanks to this, the ray always runs along the ground and the entity can charge enemies up and down hill.
*/
setChargeTargetDetectorRange : function(chargeTargetDetectorRange){
this._chargeTargetDetectorRange = chargeTargetDetectorRange;
},
/**
@method getChargingSpeedMultiplier
@return {Number}
*/
getChargingSpeedMultiplier : function(){
return this._chargingSpeedMultiplier;
},
/**
@method setChargingSpeedMultiplier
@param chargingSpeedMultiplier {Number} The speed multiplier that will be used to construct the MovementSpeedSpellEffect.
*/
setChargingSpeedMultiplier : function(chargingSpeedMultiplier){
this._chargingSpeedMultiplier = chargingSpeedMultiplier;
},
/**
@method isCharging
@return {Boolean} Returns true if the entity is currently charging.
*/
isCharging : function(){
return this._isCharging;
}
};