<commit_before>/*
  Project: Golden
  Author:  Copyright (C) 2015, <NAME>

  License: BSD license, see LICENSE for more details.

  http://www.atanaslaskov.com/golden/
*/

/// <reference path="vector.ts" />
/// <reference path="matrix.ts" />

/// <reference path="renderer.ts" />
/// <reference path="game_object.ts" />

// Dragon controlled by the player
//
class GameObjectDragon implements GameObject {
  private position: Vector;
  private velocity: Vector;

  private clickTarget: Vector;
  private clickTargetVelocity: Vector;

  private renderer: Renderer;
  private id: number;
  private idClickTarget: number;

  private rotMatrix: Matrix;
  private wiggle: number;

  constructor(renderer: Renderer, position: Vector = new Vector()) {
    this.position = position;
    this.velocity = new Vector(1.0, 0.0);
    this.clickTargetVelocity = new Vector(1.0, 0.0);

    this.rotMatrix = new Matrix();
    this.wiggle = 0;

    this.renderer = renderer;
    this.id = this.renderer.add( RendererObjectType.SPRITE, "arrow.png", this.position );
    this.idClickTarget = this.renderer.add( RendererObjectType.SPRITE, "circle.png", new Vector() );

    this.target( new Vector(0.0, -0.3) );

    window.addEventListener('click', (e) => {
      var clickPosition: Vector = new Vector(e.clientX, e.clientY);
      this.target( renderer.unproject(clickPosition) );
    });
  }

  // Target waypoint
  //
  private target(v: Vector) {
    this.clickTarget = v;
    this.renderer.position( this.idClickTarget, this.clickTarget );
  }

  // Animate the dragon
  //
  animate(dt: number): void {
    // Compute velocity to reach the target
    this.clickTargetVelocity = Vector.norm( Vector.minus(this.clickTarget, this.position) );

    // As the dragon moves it wiggles
    var wiggleAmount = Vector.minus(this.position, this.clickTarget).distance()*0.8;
    this.rotMatrix.rotation( Math.sin(this.wiggle)*wiggleAmount );
    this.rotMatrix.transform( this.clickTargetVelocity );
    this.wiggle += dt*1.0;

    var angleDelta = this.clickTargetVelocity.angle() - this.velocity.angle();

    // Interpolate velocity vector (Note: lerp would cancel out speed)
    if( Math.abs(angleDelta) > dt ) {
      var direction = angleDelta >= 0 ? +1:-1;
      if( direction*angleDelta >= Math.PI ) direction *= -1;

      this.rotMatrix.rotation(direction*dt);
      this.rotMatrix.transform(this.velocity);
    }
    else {
      this.velocity = this.clickTargetVelocity.copy();
    }

    // Update position and rotation
    this.position = Vector.plus( this.position, Vector.scale(this.velocity, dt*0.1) );
    this.renderer.position( this.id, this.position );
    this.renderer.rotation( this.id, this.velocity.angle() );
  }
}<commit_msg>Alternate turn direction when target reached.<commit_after>/*
  Project: Golden
  Author:  Copyright (C) 2015, <NAME>

  License: BSD license, see LICENSE for more details.

  http://www.atanaslaskov.com/golden/
*/

/// <reference path="vector.ts" />
/// <reference path="matrix.ts" />

/// <reference path="renderer.ts" />
/// <reference path="game_object.ts" />

// Dragon controlled by the player
//
class GameObjectDragon implements GameObject {
  private position: Vector;
  private velocity: Vector;

  private clickTarget: Vector;
  private clickTargetVelocity: Vector;

  private renderer: Renderer;
  private id: number;
  private idClickTarget: number;

  private rotMatrix: Matrix;

  private wiggle: number;
  private turn: number;
  private isTurning: boolean;

  constructor(renderer: Renderer, position: Vector = new Vector()) {
    this.position = position;
    this.velocity = new Vector(1.0, 0.0);
    this.clickTargetVelocity = new Vector(1.0, 0.0);

    this.rotMatrix = new Matrix();

    this.wiggle = 0.0;
    this.turn = 1.0;
    this.isTurning = false;

    this.renderer = renderer;
    this.id = this.renderer.add( RendererObjectType.SPRITE, "arrow.png", this.position );
    this.idClickTarget = this.renderer.add( RendererObjectType.SPRITE, "circle.png", new Vector() );

    this.target( new Vector(0.0, -0.2) );

    window.addEventListener('click', (e) => {
      var clickPosition: Vector = new Vector(e.clientX, e.clientY);
      this.target( renderer.unproject(clickPosition) );
    });
  }

  // Target waypoint
  //
  private target(v: Vector) {
    this.clickTarget = v;
    this.renderer.position( this.idClickTarget, this.clickTarget );
  }

  // Animate the dragon
  //
  animate(dt: number): void {
    // Compute velocity to reach the target
    this.clickTargetVelocity = Vector.norm( Vector.minus(this.clickTarget, this.position) );

    // Target approached, choose rotation direction +1 or -1 each time
    var targetDistance = Vector.minus(this.position, this.clickTarget).distance();
    var turn = 0.0;

    if( targetDistance < 0.05 ) {
      if( !this.isTurning ) {
        this.isTurning = true;
        this.turn *= -1.0;
      }
      turn = this.turn;
    }
    else {
      this.isTurning = false;
    }

    // Wiggle and turn
    this.rotMatrix.rotation( Math.sin(this.wiggle)*targetDistance*0.8 + turn*dt );
    this.rotMatrix.transform( this.clickTargetVelocity );
    this.wiggle += dt*1.0;

    var angleDelta = this.clickTargetVelocity.angle() - this.velocity.angle();

    // Interpolate velocity vector (Note: lerp would cancel out speed)
    if( Math.abs(angleDelta) > dt ) {
      var angleDeltaRandomized = angleDelta;

      var direction = angleDeltaRandomized >= 0 ? +1:-1;
      if( direction*angleDeltaRandomized >= Math.PI ) direction *= -1;

      this.rotMatrix.rotation(direction*dt);
      this.rotMatrix.transform(this.velocity);
    }
    else {
      this.velocity = this.clickTargetVelocity.copy();
    }

    // Update position and rotation
    this.position = Vector.plus( this.position, Vector.scale(this.velocity, dt*0.1) );
    this.renderer.position( this.id, this.position );
    this.renderer.rotation( this.id, this.velocity.angle() );
  }
}
{'fraction_non_alphanumeric': 0.1348540758134854, 'fraction_numerical': 0.01107011070110701, 'mean_word_length': 3.1728481455563333, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6783171', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1849, 'n_words': 437}