Permalink
Browse files

added tquery.tween.js

  • Loading branch information...
1 parent 12f008e commit 5e86f5e36edb1ea08b488306470ab8775dadf5a5 @jeromeetienne committed Jun 18, 2012
Showing with 116 additions and 641 deletions.
  1. +23 −23 index.html
  2. +93 −0 tquery.tween.js
  3. +0 −617 vendor/Tween.js
  4. +0 −1 vendor/tquery/Tween.js
View
@@ -35,6 +35,7 @@
<script src="vendor/jsfx/jsfxlib.js"></script>
<script src="vendor/tquery/Tween.js"></script>
+<script src="tquery.tween.js"></script>
<style>
.score {
@@ -99,6 +100,9 @@
world.tRenderer().shadowMapEnabled = true;
world.tRenderer().shadowMapSoft = true;
+ // enable tween updater
+ world.enableTweenUpdater();
+
// create the ground
tQuery.createCube(1,0.2,1, 3,3,3).addTo(world)
.receiveShadow(true)
@@ -129,11 +133,11 @@
var url = "images/plywood.jpg";
var playerColor = playerId === 'right' ? 0xffaacc : 0xccaaff;
var object3d = tQuery.createCube(1,1,1,2,2,2).addTo(world)
- .scale(racketW, 0.2, racketD)
+ .geometry().scaleBy(racketW, 0.2, racketD).back()
.setLambertMaterial().color(playerColor).ambient(0xaaaaaa).map(url).back()
.id(playerId)
.castShadow(true)
- .geometry().smooth(2).back()
+ .geometry().computeAll().smooth(2).back()
object3d.translateX(1.3 * (playerId === "right" ? +1 : -1));
player.object3d = object3d;
@@ -165,8 +169,8 @@
var speedY = 2 * (60*delta);
if( keyboard.pressed('up') ) players['right'].object3d.translateZ(-delta*speedY);
if( keyboard.pressed('down') ) players['right'].object3d.translateZ(+delta*speedY);
- if( keyboard.pressed('s') ) players['left'] .object3d.translateZ(-delta*speedY);
- if( keyboard.pressed('x') ) players['left'] .object3d.translateZ(+delta*speedY);
+ if( keyboard.pressed('q') ) players['left'] .object3d.translateZ(-delta*speedY);
+ if( keyboard.pressed('w') ) players['left'] .object3d.translateZ(+delta*speedY);
// handle racket limit
Object.keys(players).forEach(function(playerId){
var tMesh = players[playerId].object3d.get(0);
@@ -183,6 +187,15 @@
// play a sound
soundRacketBall && soundRacketBall.play();
+ // tween the scale
+ tQuery.createTween({
+ scale : 1
+ }).easing(TWEEN.Easing.Bounce.Out).to({
+ scale : 2
+ }, 250).onUpdate(function(){
+ player.object3d.scale(this.scale, this.scale, this.scale);
+ }).start().thenBounce(200).easing(TWEEN.Easing.Quadratic.InOut).back()
+
// increase velocity
ballVelX *= 1.1;
ballVelZ *= 1.1;
@@ -287,29 +300,16 @@
function wallOnContact(object){
soundWall && soundWall.play();
-
- var tweenvars = {
+
+ // tween the scale
+ tQuery.createTween({
scale : 1
- };
- var tweenFwd = new TWEEN.Tween(tweenvars).to({
+ }).easing(TWEEN.Easing.Elastic.Out).to({
scale : 2
- }, 100).easing(TWEEN.Easing.Elastic.Out).onUpdate(function(){
+ }, 100).onUpdate(function(){
object.scale(1, this.scale, this.scale);
- });
- // setup a backward tween
- var tweenBack = new TWEEN.Tween(tweenvars).to({
- scale : 1
- }, 200).easing(TWEEN.Easing.Linear.None)
- .onUpdate(function(){
- object.scale(1, this.scale, this.scale);
- });
- tweenFwd.chain(tweenBack);
- tweenFwd.start();
+ }).start().thenBounce(200).easing(TWEEN.Easing.Linear.None).back()
}
- // update TWEEN with world.loop
- world.loop().hook(function(){
- TWEEN.update();
- });
</script></body>
View
@@ -0,0 +1,93 @@
+//////////////////////////////////////////////////////////////////////////////////
+// //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.World.register('enableTweenUpdater', function(){
+ // if it is already enabled, do nothing
+ if( this.hasTweenUpdater() ) return this;
+ // init context
+ var context = tQuery.data(this, 'tweenUpdaterCtx', {
+ loopCb : function(){
+ TWEEN.update();
+ }
+ }, true);
+ // hook the loopCb
+ this.loop().hook(context.loopCb);
+ return this; // for chained API
+});
+
+tQuery.World.register('hasTweenUpdater', function(){
+ var context = tQuery.data(this, 'tweenUpdaterCtx');
+ if( !context ) return false;
+ return context.loopCb ? true : false;
+});
+
+tQuery.World.register('disableTweenUpdater', function(){
+ // if it is already enabled, do nothing
+ if( !this.hasTweenUpdater() ) return this;
+ // get context
+ var context = tQuery.data(this, 'tweenUpdaterCtx');
+ // hook the loopCb
+ this.loop().unhook(context.loopCb);
+ // remove the context
+ tQuery.removeData(this, 'tweenUpdaterCtx', true);
+ return this; // for chained API
+});
+
+//////////////////////////////////////////////////////////////////////////////////
+// //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.register('createTween', function(properties, delay){
+ return new tQuery.Tween(properties);
+});
+
+//////////////////////////////////////////////////////////////////////////////////
+// //
+//////////////////////////////////////////////////////////////////////////////////
+
+tQuery.register('Tween', function(properties){
+ // call parent ctor
+ TWEEN.Tween.call(this, properties)
+
+ // initial back value
+ this._back = null;
+ this._onUpdateCallback = null;
+ this._properties = properties;
+ this._origProperties = tQuery.extend({}, properties);
+
+ // overload onUpdate() to cache the value - used in .thenBounce()
+ var $onUpdate = this.onUpdate;
+ this.onUpdate = function(fn){
+ this._onUpdateCallback = fn;
+ $onUpdate(fn)
+ return this;
+ }
+});
+
+/**
+ * Getter/Setter for back value
+*/
+tQuery.Tween.prototype.back = function(value){
+ if( value === undefined ) return this._back;
+ this._back = value;
+ return this;
+}
+
+/**
+ * Getter/Setter for back value
+*/
+tQuery.Tween.prototype.thenBounce = function(delay){
+ //console.log("_properties", JSON.stringify(this._properties));
+ //console.log("_origProperties", JSON.stringify(this._origProperties));
+ //console.log("onUpdateCallbnack", this._onUpdateCallback)
+ // setup a backward tween
+ var tweenBack = tQuery.createTween(this._properties)
+ .to(this._origProperties, delay)
+ .onUpdate(this._onUpdateCallback)
+ .back(this);
+ // make this tween to be chained with the one we just created
+ this.chain(tweenBack);
+ // return the tweenBack. use .back() to come back on the previous tween
+ return tweenBack;
+}
Oops, something went wrong. Retry.

0 comments on commit 5e86f5e

Please sign in to comment.