{"size": 1830, "ext": "js", "max_stars_repo_path": "src/classes/GameObject/GameObject.focus.js", "max_stars_repo_name": "dreamirl/dreamengine", "max_stars_repo_stars_event_min_datetime": "2019-02-01T23:10:11.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-05T16:11:24.000Z", "max_issues_repo_path": "src/classes/GameObject/GameObject.focus.js", "max_issues_repo_name": "dreamirl/dreamengine", "max_issues_repo_issues_event_min_datetime": "2019-01-26T15:12:52.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T09:35:35.000Z", "max_forks_repo_path": "src/classes/GameObject/GameObject.focus.js", "max_forks_repo_name": "dreamirl/dreamengine", "max_forks_repo_forks_event_min_datetime": "2019-02-01T16:58:54.000Z", "max_forks_repo_forks_event_max_datetime": "2020-02-01T21:19:06.000Z"}
{"max_stars_count": 12, "max_issues_count": 12, "max_forks_count": 3, "avg_line_length": 25.0684931507, "max_line_length": 90, "alphanum_fraction": 0.6464480874}
import GameObject from 'DE.GameObject';
import Time from 'DE.Time';

/**
 * give a target to this gameObject, then it will focus it until you changed or removed it
 * you can lock independent axes, and set offsets
 * @public
 * @memberOf GameObject
 * @param {GameObject} gameObject is the target to focus on
 * @param {Object} [params] optional parameters, set offsets or lock
 * @example // create a fx for your ship, decal a little on left, and lock y
 * fx.focus( player, { lock: { y: true }, offsets: { x: -200, y: 0 } } );
 */
GameObject.prototype.focus = function(gameObject, params) {
  params = params || {};
  this.target = gameObject;
  this._focusOptions = Object.assign(
    {
      x: true,
      y: true,
      rotation: false,
    },
    params.options,
  );

  // focus default x/y
  this._focusOptions.x = this._focusOptions.x !== false ? true : false;
  this._focusOptions.y = this._focusOptions.y !== false ? true : false;

  this._focusOffsets = Object.assign(
    { x: 0, y: 0 },
    params.offsets || params.offset,
  );

  return this;
};

/**
 * apply focus on target if there is one
 * You shouldn't call or change this method
 * @protected
 * @memberOf Camera
 */
GameObject.prototype.applyFocus = function() {
  if (!this.target) {
    return;
  }

  let pos = this.target;
  if (this.target.getWorldPos) {
    pos = this.target.getWorldPos();
  }

  let parentPos = {};
  if (this.parent.getWorldPos) {
    parentPos = this.parent.getWorldPos();
  } else {
    parentPos = this.parent;
  }

  if (this._focusOptions.x) {
    this.x = pos.x + (this._focusOffsets.x || 0) - parentPos.x;
  }
  if (this._focusOptions.y) {
    this.y = pos.y + (this._focusOffsets.y || 0) - parentPos.y;
  }
  if (this._focusOptions.rotation) {
    this.rotation = this.target.rotation;
  }
};

export default GameObject;
