Permalink
Browse files

Updated documentation of behavior components

Moved engine options into each behavior
  • Loading branch information...
1 parent cd17b69 commit 912d440f426e72633262b609aed67189b73e70a1 @bfattori committed Nov 6, 2011
@@ -55,25 +55,7 @@ R.Engine.defaultOptions = {
"A": "A",
"B": "B",
"C": "C"
- },
-
- "behaviorLagForceRatio": 5,
- "behaviorDefaultBlending": 1.0,
-
- "seekNearDistance": 10,
-
- "arrivalSlowingDistance": 50,
-
- "fleeMinimumDistance": 350,
-
- "unalignedAvoidanceCollisionRadius": 150,
- "unalignedAvoidanceFutureDistance": 60,
-
- "obstacleAvoidanceRadius": 60,
- "obstacleAvoidanceFutureDistance": 40,
-
- "wanderChange": 1,
- "wanderRadius": 30
+ }
};
@@ -1,3 +1,36 @@
+/**
+ * The Render Engine
+ * ArrivalBehavior
+ *
+ * @fileoverview Arrival behavior, based on Craig Reynolds "Autonomous Steering Behaviors" article.
+ * The arrival behavior is similar to the seek behavior, where a vehicle will navigate
+ * to a target. However, the arrival behavior will slow as it approaches the target.
+ *
+ * @author: Brett Fattori (brettf@renderengine.com)
+ * @author: $Author: bfattori $
+ * @version: $Revision: 1555 $
+ *
+ * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE
+ */
+
// Load all required engine components
R.Engine.define({
"class": "R.components.logic.behaviors.Arrival",
@@ -6,26 +39,39 @@ R.Engine.define({
]
});
+ // Add behavior options
+ if (R.Engine.options.behaviors === undefined) {
+ R.Engine.options.behaviors = {};
+ }
+
+ $.extend(R.Engine.options.behaviors, {
+ "arrivalSlowingDistance": 50
+ });
+
+
/**
* @class The arrival behavior component. Causes the vehicle to seek the destination, but slow as it
* approaches it.
* @param destination The point to seek
* @param [slowingDistance] The distance (in pixels) at which slowing should occur. The velocity
* will be scaled as it nears the destination and stop when it is at, or almost at, the
* destination.
+ * @extends R.components.logic.behaviors.BaseBehavior
+ * @constructor
*/
R.components.logic.behaviors.Arrival = function() {
- return R.components.logic.behaviors.BaseBehavior.extend({
+ return R.components.logic.behaviors.BaseBehavior.extend(/** @scope R.components.logic.behaviors.Arrival.prototype */{
slowDist: 0,
arrived: false,
- destPt: null,
+ target: null,
- constructor: function(destination, slowingDistance) {
+ /** @private */
+ constructor: function(target, slowingDistance) {
this.base("arrival");
this.arrived = false;
- this.destPt = R.math.Vector2D.create(destination);
- this.slowDist = slowingDistance || R.Engine.options.arrivalSlowingDistance;
+ this.setTarget(target);
+ this.slowDist = slowingDistance || R.Engine.options.behaviors.arrivalSlowingDistance;
},
destroy: function() {
@@ -38,9 +84,37 @@ R.components.logic.behaviors.Arrival = function() {
this.base();
},
+ /**
+ * Set the target to seek.
+ * @param target {R.math.Point2D|R.engine.Object2D} The point, or object,
+ * to seek.
+ */
+ setTarget: function(target) {
+ this.target = target;
+ },
+
+ /**
+ * This method is called by the game object to run the component,
+ * updating its state.
+ *
+ * @param renderContext {R.rendercontexts.AbstractRenderContext} The context the component will render within.
+ * @param time {Number} The global engine time
+ * @param dt {Number} The delta between the world time and the last time the world was updated
+ * in milliseconds.
+ */
execute: function(time, dt) {
- var gO = this.getGameObject(), mC = this.getTransformComponent(), pt = R.clone(gO.getPosition()).add(gO.getOrigin()),
- offs = R.clone(this.destPt).sub(pt), distance = offs.len(), steering = R.math.Vector2D.create(0,0);
+ var destPt = R.math.Vector2D.create(0,0);
+ if (this.target.__POINT2D) {
+ destPt.set(this.target);
+ } else if (this.target instanceof R.engine.Object2D && !this.target.isDestroyed()) {
+ destPt.set(this.target.getOriginPosition());
+ } else {
+ // Not a point or object, return zero steering
+ return R.math.Vector2D.ZERO;
+ }
+
+ var gO = this.getGameObject(), mC = this.getTransformComponent(), pt = R.clone(gO.getOriginPosition()),
+ offs = R.clone(destPt).sub(pt), distance = offs.len(), steering = R.math.Vector2D.create(0,0);
if (!this.getGameObject() || this.getGameObject().isDestroyed()) {
return steering;
@@ -50,7 +124,7 @@ R.components.logic.behaviors.Arrival = function() {
offs.normalize();
if (distance < this.slowDist) {
- offs.mul(mC.getMaxSpeed() * (distance / R.Engine.options.arrivalSlowingDistance));
+ offs.mul(mC.getMaxSpeed() * (distance / this.slowDist));
} else {
offs.mul(mC.getMaxSpeed());
}
@@ -62,6 +136,7 @@ R.components.logic.behaviors.Arrival = function() {
offs.destroy();
pt.destroy();
+ destPt.destroy();
return steering;
},
@@ -74,7 +149,7 @@ R.components.logic.behaviors.Arrival = function() {
return this.arrived;
}
- }, {
+ }, /** @scope R.components.logic.behaviors.Arrival.prototype */{
getClassName: function() {
return "R.components.logic.behaviors.Arrival";
}
@@ -1,3 +1,34 @@
+/**
+ * The Render Engine
+ * BaseBehavior
+ *
+ * @fileoverview The base for all behaviors.
+ *
+ * @author: Brett Fattori (brettf@renderengine.com)
+ * @author: $Author: bfattori $
+ * @version: $Revision: 1555 $
+ *
+ * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE
+ */
+
// Load all required engine components
R.Engine.define({
"class": "R.components.logic.behaviors.BaseBehavior",
@@ -7,13 +38,16 @@ R.Engine.define({
});
/**
- * @class The seek behavior component for vehicles. Causes a vehicle to move toward a target.
+ * @class The base behavior component.
+ * @extends R.components.Logic
+ * @constructor
*/
R.components.logic.behaviors.BaseBehavior = function() {
- return R.components.Logic.extend({
+ return R.components.Logic.extend(/** @scope R.components.logic.behaviors.BaseBehavior.prototype */{
transformComponent: null,
+ /** @private */
constructor: function(name) {
this.base(name);
},
@@ -23,15 +57,23 @@ R.components.logic.behaviors.BaseBehavior = function() {
this.base();
},
+ /**
+ * Sets the transformation component which contains this behavior.
+ * @param component {R.components.transform.BehaviorMover2D} The behavior mover component
+ */
setTransformComponent: function(component) {
this.transformComponent = component;
},
+ /**
+ * Gets the transformation component which contains this behavior.
+ * @return {R.components.transform.BehaviorMover2D}
+ */
getTransformComponent: function() {
return this.transformComponent;
}
- }, {
+ }, /** @scope R.components.logic.behaviors.BaseBehavior.prototype */{
getClassName: function() {
return "R.components.logic.behaviors.BaseBehavior";
}
@@ -1,3 +1,36 @@
+/**
+ * The Render Engine
+ * FleeBehavior
+ *
+ * @fileoverview Flee behavior, based on Craig Reynolds "Autonomous Steering Behaviors" article.
+ * The flee behavior is the opposite of the seek behavior, where the vehicle will
+ * move away from the target object.
+ *
+ * @author: Brett Fattori (brettf@renderengine.com)
+ * @author: $Author: bfattori $
+ * @version: $Revision: 1555 $
+ *
+ * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE
+ */
+
// Load all required engine components
R.Engine.define({
"class": "R.components.logic.behaviors.Flee",
@@ -6,23 +39,35 @@ R.Engine.define({
]
});
+ // Add behavior options
+ if (R.Engine.options.behaviors === undefined) {
+ R.Engine.options.behaviors = {};
+ }
+
+ $.extend(R.Engine.options.behaviors, {
+ "fleeMinimumDistance": 350
+ });
+
/**
* @class The flee behavior component. This is essentially opposite of seeking. Fleeing
* needs to be updated dynamically. If the argument to {@link #fleeFrom} is an
* object, it must be a descendant of {@link R.engine.Object2D}.
* @param target The target to flee from or a point to flee from.
* @param [minDist=350] The minimum distance at which the vehicle will be triggered to
* flee.
+ * @extends R.components.logic.behaviors.BaseBehavior
+ * @constructor
*/
R.components.logic.behaviors.Flee = function() {
- return R.components.logic.behaviors.BaseBehavior.extend({
+ return R.components.logic.behaviors.BaseBehavior.extend(/** @scope R.components.logic.behaviors.Flee.prototype */{
minDist: 0,
target: null,
+ /** @private */
constructor: function(target, minDist) {
this.base("flee");
- this.minDist = minDist || R.Engine.options.fleeMinimumDistance;
+ this.minDist = minDist || R.Engine.options.behaviors.fleeMinimumDistance;
this.target = target;
},
@@ -34,6 +79,15 @@ R.components.logic.behaviors.Flee = function() {
this.target = target;
},
+ /**
+ * This method is called by the game object to run the component,
+ * updating its state.
+ *
+ * @param renderContext {R.rendercontexts.AbstractRenderContext} The context the component will render within.
+ * @param time {Number} The global engine time
+ * @param dt {Number} The delta between the world time and the last time the world was updated
+ * in milliseconds.
+ */
execute: function(time, dt) {
if (!this.target || this.target.isDestroyed()) {
@@ -49,10 +103,10 @@ R.components.logic.behaviors.Flee = function() {
if (this.target.__POINT2D) {
flee = R.math.Vector2D.create(this.target);
} else {
- flee = R.math.Vector2D.create(this.target.getPosition()).add(this.target.getOrigin());
+ flee = R.math.Vector2D.create(this.target.getOriginPosition());
}
- var gO = this.getGameObject(), mC = this.getTransformComponent(), pt = R.clone(gO.getPosition()).add(gO.getOrigin()),
+ var gO = this.getGameObject(), mC = this.getTransformComponent(), pt = R.clone(gO.getOriginPosition()),
offs = R.clone(flee).sub(pt), distance = offs.len(), steering = R.math.Vector2D.create(0,0);
offs.normalize();
@@ -67,7 +121,7 @@ R.components.logic.behaviors.Flee = function() {
return steering.neg();
}
- }, {
+ }, /** @scope R.components.logic.behaviors.Flee.prototype */{
getClassName: function() {
return "R.components.logic.behaviors.Flee";
}
Oops, something went wrong.

0 comments on commit 912d440

Please sign in to comment.