<commit_before><commit_msg>Add EnergyChunkPathMover.js<commit_after>// Copyright 2016, University of Colorado Boulder

/**
 * Class that is used to move an energy chunk along a path.
 *
 * @author <NAME>
 * @author <NAME>
 */
define( function( require ) {
  'use strict';

  // Modules
  var energyFormsAndChanges = require( 'ENERGY_FORMS_AND_CHANGES/energyFormsAndChanges' );
  var inherit = require( 'PHET_CORE/inherit' );
  var Vector2 = require( 'DOT/Vector2' );

  /**
   * @param {EnergyChunk} energyChunk [description]
   * @param {Array<Vector2>} path        [description]
   * @param {Number} velocity    [description]
   * @constructor
   */
  function EnergyChunkPathMover( energyChunk, path, velocity ) {
    this.energyChunk = energyChunk;
    this.path = path;
    this.velocity = velocity;
    this.pathFullyTraversed = false;

    assert && assert( path.length === 0, 'Path must have at least one point' );

    this.nextPoint = path[ 0 ];

  }

  energyFormsAndChanges.register( 'EnergyChunkPathMover', EnergyChunkPathMover );

  return inherit( Object, EnergyChunkPathMover, {
    /**
     * Advance chunk position on path
     * @param  {Number} dt timestep
     */
    moveAlongPath: function( dt ) {
      var distanceToTravel = dt * this.velocity;
      while ( this.distanceToTravel > 0 && !this.pathFullyTraversed ) {
        if ( this.distanceToTravel < this.energyChunk.positionProperty.get().distance( this.nextPoint ) ) {
          // Not arriving at destination next point yet, so just move towards it.
          var phi = this.nextPoint.minus( this.energyChunk.positionProperty.get() ).angle();
          var velocity = new Vector2( this.distanceToTravel, 0 ).rotated( phi );
          this.energyChunk.position.set( this.energyChunk.position.get().plus( velocity ) );
          distanceToTravel = 0; // No remaining distance.
        } else {
          // Arrived at next destination point.
          distanceToTravel -= this.energyChunk.position.get().distance( this.nextPoint );
          this.energyChunk.position.set( this.nextPoint );
          if ( this.nextPoint === this.path[ this.path.length - 1 ] ) {
            // At the end.
            this.pathFullyTraversed = true;
          } else {
            // Set the next destination point.
            this.nextPoint = this.path[ this.path.indexOf( this.nextPoint ) + 1 ];
          }
        }
      }

    },

    getFinalDestination: function() {
      return this.path.get[ this.path.length - 1 ];
    }

  } );
} );

{'fraction_non_alphanumeric': 0.11947431302270012, 'fraction_numerical': 0.006371963361210673, 'mean_word_length': 2.614388489208633, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5928690', 'n_tokens_mistral': 759, 'n_tokens_neox': 720, 'n_words': 184}