Browse Source

allow for events to be send on Action loop and finish.

Ben Houston 10 years ago
parent
commit
5b41c70fae
2 changed files with 57 additions and 10 deletions
  1. 46 9
      src/animation/AnimationAction.js
  2. 11 1
      src/animation/AnimationMixer.js

+ 46 - 9
src/animation/AnimationAction.js

@@ -13,6 +13,7 @@ THREE.AnimationAction = function ( clip, startTime, timeScale, weight, loop ) {
 	this.timeScale = timeScale || 1;
 	this.weight = weight || 1;
 	this.loop = loop || clip.loop || false;
+	this.loopCount = 0;
 	this.enabled = true;	// allow for easy disabling of the action.
 
 	this.clipTime = 0;
@@ -24,27 +25,63 @@ THREE.AnimationAction.prototype = {
 
 	updateTime: function( clipDeltaTime ) {
 
-		this.clipTime += clipDeltaTime;
-
+		var newClipTime = this.clipTime + clipDeltaTime;
 		var duration = this.clip.duration;
 
-		if( this.loop ) {
+		if( newClipTime <= 0 ) {
+
+			if( this.loop ) {
+
+				newClipTime -= Math.floor( newClipTime / duration ) * duration;
+		   		this.clipTime = newClipTime % duration;
 
-			if( this.clipTime < 0 ) {
+		   		this.loopCount --;
 
-				this.clipTime -= Math.floor( this.clipTime / duration ) * duration;
+	   			this.mixer.dispatchEvent( { type: 'loop', action: this, direction: -1 } );
 
 			}
+			else {
 
-	   		this.clipTime = this.clipTime % duration;
+		   		if( this.clipTime > 0 ) {
 
-	   	}
-	   	else {
+		   			this.mixer.dispatchEvent( { type: 'finished', action: this, direction: -1 } );
+
+		   		}
 
-	   		this.clipTime = Math.min( this.clipTime, Math.max( duration, 0 ) );
+				this.clipTime = Math.min( newClipTime, Math.max( duration, 0 ) );
+
+			}
+
+		}
+		else if( newClipTime >= duration ) {
+
+			if( this.loop ) {
+	
+				this.clipTime = newClipTime % duration;
+
+		   		this.loopCount ++;
+	
+	 			this.mixer.dispatchEvent( { type: 'loop', action: this, direction: +1 } );
+
+			}
+			else {
+
+		   		if( this.clipTime < duration ) {
+
+		   			this.mixer.dispatchEvent( { type: 'finished', action: this, direction: +1 } );
+
+		   		}
+
+				this.clipTime = Math.min( newClipTime, Math.max( duration, 0 ) );
+
+		   	}
 
 	   	}
+	   	else {
 
+	   		this.clipTime = newClipTime;
+	   		
+	   	}
 	
 	   	return this.clipTime;
 

+ 11 - 1
src/animation/AnimationMixer.js

@@ -25,6 +25,7 @@ THREE.AnimationMixer.prototype = {
 	addAction: function( action ) {
 
 		this.actions.push( action );
+		action.mixer = this;
 
 		var tracks = action.clip.tracks;
 
@@ -51,6 +52,12 @@ THREE.AnimationMixer.prototype = {
 
 	removeAllActions: function() {
 
+		for( var i = 0; i < this.actions.length; i ++ ) {
+
+			this.actions[i].mixer = null;
+			
+		}
+
 		// unbind all property bindings
 		for( var i = 0; i < this.propertyBindingsArray.length; i ++ ) {
 
@@ -71,6 +78,7 @@ THREE.AnimationMixer.prototype = {
 		if ( index !== - 1 ) {
 
 			this.actions.splice( index, 1 );
+			action.mixer = null;
 
 		}
 
@@ -186,4 +194,6 @@ THREE.AnimationMixer.prototype = {
 		}
 	}
 
-};
+};
+
+THREE.EventDispatcher.prototype.apply( THREE.AnimationMixer.prototype );