import THREE from 'three';
import Entity from './entity';
import traverse from './traverse';

const TorusKnot = THREE.Curve.create(
  function TorusKnot( scale = 10 ) {
    this.scale = scale;
  },

  function getPoint( t ) {
    const p = 3;
    const q = 4;

    t *= 2 * Math.PI;

    const tx = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
    const ty = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
    const tz = Math.sin( q * t );

    return new THREE.Vector3( tx, ty, tz )
      .multiplyScalar( this.scale );
  }
);

const geometry = new THREE.IcosahedronGeometry( 0.2 );
const material = new THREE.MeshPhongMaterial({
  shading: THREE.FlatShading
});

const vector = new THREE.Vector3();

export default class Drone extends Entity {
  constructor() {
    super( geometry, material.clone() );
    this.geometry.computeBoundingSphere();

    this.path = new TorusKnot( 2 );

    this.length = this.path.getLength();
    this.speed = 2;
    this.duration = this.length / this.speed;

    this.clock = new THREE.Clock();
  }

  update( dt, scene ) {
    const t = ( this.clock.getElapsedTime() / this.duration ) % 1;
    this.position.copy( this.path.getPointAt( t ) );

    this.material.color.setRGB( 1, 1, 1 );
    traverse( scene, object => {
      if ( object.type === 'bullet' ) {
        this.worldToLocal( object.getWorldPosition( vector ) );
        if ( this.geometry.boundingSphere.containsPoint( vector ) ) {
          this.material.color.setRGB( 1, 0, 0 );
          return false;
        }
      }
    });
  }
}

@@ -20,16 +20,46 @@
 three';%0A
+import TWEEN from 'tween.js';%0A
 import E
@@ -1055,16 +1055,405 @@
 lock();%0A
+%0A    const %7B color %7D = this.material;%0A    this.colorArray = material.color.toArray();%0A%0A    this.tween = new TWEEN.Tween( this.colorArray )%0A      .onUpdate(function() %7B%0A        color.setRGB( ...this );%0A      %7D)%0A      .start();%0A  %7D%0A%0A  hit() %7B%0A    this.material.color.setRGB( 1, 0, 0 )%0A      .toArray( this.colorArray );%0A%0A    this.tween.stop()%0A      .to( %5B 1, 1, 1 %5D, 100 )%0A      .start();%0A  
   %7D%0A%0A  u
@@ -1597,52 +1597,29 @@
  );%0A
-%0A
     
-this.material.color.setRGB( 1, 1, 1 
+TWEEN.update(
 );%0A
+%0A
     
@@ -1840,39 +1840,12 @@
 his.
-material.color.setRGB( 1, 0, 0 
+hit(
 );%0A 

Add tween to Drone hit effect.