Skip to content

Commit

Permalink
added horizontal following of moving destination, see #214
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Feb 15, 2019
1 parent 95fe806 commit 14af36b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
56 changes: 47 additions & 9 deletions js/common/model/EnergyChunkWanderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define( require => {

// modules
const energyFormsAndChanges = require( 'ENERGY_FORMS_AND_CHANGES/energyFormsAndChanges' );
const Range = require( 'DOT/Range' );
const Vector2 = require( 'DOT/Vector2' );

// constants
Expand All @@ -32,6 +33,8 @@ define( require => {
*/
constructor( energyChunk, destinationProperty, options ) {

const self = this;

options = _.extend( {

// {Range} - bounding range in the X direction within which the energy chunk's motion should be constrained
Expand All @@ -40,8 +43,10 @@ define( require => {
// {number} - range of angle variations, higher means more wandering, in radians from Math.PI to zero
wanderAngleVariation: DEFAULT_ANGLE_VARIATION,

// {boolean} - follow the destination if it changes by moving the EC and wander constraints too
followDestination: true
// {boolean} - Translate the EC position and wander constraints horizonally if the destination changes. This
// was found to be useful to help prevent "chase scenes" when an energy was heading towards an object and the
// user started dragging that object.
translateXWithDestination: true

}, options );

Expand Down Expand Up @@ -74,20 +79,53 @@ define( require => {
this.changeVelocityVector();

let speedIncreased = false;
this.destinationProperty.lazyLink( newDestination => {

const distanceToDestination = newDestination.distance( this.energyChunk.positionProperty.value );
function handleDestinationChanged( newDestination, oldDestination ) {

const distanceToDestination = newDestination.distance( self.energyChunk.positionProperty.value );

// if the destination changes, speed up and go directly to the destination
if ( distanceToDestination <= GO_STRAIGHT_HOME_DISTANCE && !speedIncreased ) {
const increaseFactor = 8;
this.minSpeed = DEFAULT_MIN_SPEED * increaseFactor;
this.maxSpeed = DEFAULT_MAX_SPEED * increaseFactor;
self.minSpeed = DEFAULT_MIN_SPEED * increaseFactor;
self.maxSpeed = DEFAULT_MAX_SPEED * increaseFactor;
speedIncreased = true;
this.wandering = false;
self.wandering = false;
}
self.changeVelocityVector();

if ( options.translateXWithDestination ) {

const translation = newDestination.minus( oldDestination );

// adjust the current EC position
self.energyChunk.positionProperty.set(
self.energyChunk.positionProperty.value.plusXY( translation.x, 0 )
);

// adjust the wander constraints if present
if ( self.horizontalWanderConstraint ) {
self.setHorizontalWanderConstraint( new Range(
self.horizontalWanderConstraint.min + translation.x,
self.horizontalWanderConstraint.max + translation.x
) );
}
}
this.changeVelocityVector();
} );
}

this.destinationProperty.lazyLink( handleDestinationChanged );

this.disposeEnergyChunkWanderController = () => {
this.destinationProperty.unlink( handleDestinationChanged );
};
}

/**
* dispose function
* @public
*/
dispose() {
this.disposeEnergyChunkWanderController();
}

/**
Expand Down
13 changes: 11 additions & 2 deletions js/intro/model/RectangularThermalMovableModelElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,18 @@ define( function( require ) {

// when an approaching energy chunk is removed from the list, make sure its wander controller goes away too
this.approachingEnergyChunks.addItemRemovedListener( function( removedEC ) {
self.energyChunkWanderControllers = self.energyChunkWanderControllers.filter( function( wanderController ) {
return wanderController.energyChunk !== removedEC;

// find the wander controller that is controlling the motion of this energy chunk
const wanderController = _.find( self.energyChunkWanderControllers, wanderController => {
return wanderController.energyChunk === removedEC;
} );

assert && assert( wanderController, 'there should always be a wander controller for each approaching EC' );

self.energyChunkWanderControllers = _.without( self.energyChunkWanderControllers, wanderController );

// dispose the wander controller
wanderController.dispose();
} );

// @private {number} - minimum amount of energy that this is allowed to have
Expand Down

0 comments on commit 14af36b

Please sign in to comment.