Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 05-31-2012, 04:46 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
func_tween and func_easing

If you've used jQuery for simple animations, you know it's easy to make simple tweens look nice by using the variety of available easing options.

Whenever I try to do some nice effect in Graal, I end up writing the tweening code from scratch for that particular case, which is ridiculous.

Tweening is pretty easy, but to make it look nice, you need some form of easing. Animations that start and stop abruptly look bad; it's usually better for there to be some kind of slow stop, fast start, or even bouncing .

I ported the wonderful jQuery easing plugin (BSD license) to GScript.

I present to you: func_tween and func_easing.

func_tween is just a couple functions that makes it easy to change the property of an object over time. You can use any object; images, GUIs, players, NPCs, etc.

To use it, all you do is this:

PHP Code:
temp.callback = function() {
  
player.chat "Done!";
};

this.tween(Ball"y"GraalControl.height Ball.height40temp.easingtemp.callback); 
The parameters of tween are:
  • object - the object to tween
  • property - the property to tween
  • targetValue - the target value at the end of the animation
  • frames - the number of frames the animation should last (20 frames per second)
  • easing - the name of the easing function to use (optional, defaults to "swing")
  • callback - a callback function, called when finished (optional)

You can use any easing available from the easing plugin, plus "linear" (normal, abrupt start and stop), and "swing" (the default, soft stop).

You can see all of the available easings, and try them out, on this site (probably requires a non-ancient browser).

Here's what they look like in game. The video is pretty choppy (sorry), but it looks very smooth in game.



My favorite is probably at 1:38.

Here's the two classes you'll need:

func_tween
PHP Code:
function onCreated() {
  
this.join("func_easing");
}

//#CLIENTSIDE
function tween(temp.objtemp.propertytemp.targetValuetemp.framestemp.easingtemp.callback) {
  if (
temp.easing == null) { // default easing
    
temp.easing "swing";
  }
  
  
temp.startValue temp.obj.(@ temp.property) + 0;
  
this.onTweenStep(temp.objtemp.propertytemp.startValuetemp.targetValuetemp.framestemp.framestemp.easingtemp.callback);
}

function 
onTweenStep(temp.objtemp.propertytemp.startValuetemp.targetValuetemp.framesRemainingtemp.totalFramestemp.easingtemp.callback) {
  if (
temp.framesRemaining <= 0) {
    
temp.obj.(@ temp.property) = temp.targetValue;
    
    if (
temp.callback != null) {
      
temp.callback(temp.obj);
    }
    
    return;
  }
  
  
temp.= (temp.totalFrames temp.framesRemaining);
  
temp.state temp.temp.totalFrames;
  
  
temp.pos this.ease(temp.easingtemp.statetemp.n01temp.totalFrames);
  
temp.obj.(@ temp.property) = temp.startValue + ((temp.targetValue temp.startValue) * temp.pos);
  
  
this.scheduleEvent(0.05"onTweenStep"temp.objtemp.propertytemp.startValuetemp.targetValuetemp.framesRemaining 1temp.totalFramestemp.easingtemp.callback);

func_easing
PHP Code:
/*
    This is essentially a port of the jQuery easing plugin to
    GScript
    
    jQuery easing plugin:
    http://gsgd.co.uk/sandbox/jquery/easing/
*/

//#CLIENTSIDE
function easeInQuad(xtbcd) {
  return 
* (/= d) * b;
};

function 
easeOutQuad(xtbcd) {
  return -
* (/= d) * (2) + b;
};

function 
easeInOutQuad(xtbcd) {
  if ((
/= 2) < 1) return b;
  return -
* ((--t) * (2) - 1) + b;
};

function 
easeInCubic(xtbcd) {
  return 
* (/= d) * b;
};

function 
easeOutCubic(xtbcd) {
  return 
* ((1) * 1) + b;
};

function 
easeInOutCubic(xtbcd) {
  if ((
/= 2) < 1) return b;
  return 
* ((-= 2) * 2) + b;
};

function 
easeInQuart(xtbcd) {
  return 
* (/= d) * b;
};

function 
easeOutQuart(xtbcd) {
  return -
* ((1) * 1) + b;
};

function 
easeInOutQuart(xtbcd) {
  if ((
/= 2) < 1) return b;
  return -
* ((-= 2) * 2) + b;
};

function 
easeInQuint(xtbcd) {
  return 
* (/= d) * b;
};

function 
easeOutQuint(xtbcd) {
  return 
* ((1) * 1) + b;
};

function 
easeInOutQuint(xtbcd) {
  if ((
/= 2) < 1) return b;
  return 
* ((-= 2) * 2) + b;
};

function 
easeInSine(xtbcd) {
  return -
cos(* (pi 2)) + b;
};

function 
easeOutSine(xtbcd) {
  return 
sin(* (pi 2)) + b;
};

function 
easeInOutSine(xtbcd) {
  return -
* (cos(pi d) - 1) + b;
};

function 
easeInExpo(xtbcd) {
  return (
== 0) ? ^ (10 * (1)) + b;
};

function 
easeOutExpo(xtbcd) {
  return (
== d) ? * (- (^ (-10 d)) + 1) + b;
};

function 
easeInOutExpo(xtbcd) {
  if (
== 0) return b;
  if (
== d) return c;
  if ((
/= 2) < 1) return ^ (10 * (1)) + b;
  return 
* (- (^ (-10 * --t)) + 2) + b;
};

function 
easeInCirc(xtbcd) {
  return -
* ((- (/= d) * t) ^ 0.5 1) + b;
};

function 
easeOutCirc(xtbcd) {
  return 
* (- (1) * t) ^ 0.5 b;
};

function 
easeInOutCirc(xtbcd) {
  if ((
/= 2) < 1) return -* ((t) ^ 0.5 1) + b;
  return 
* ((- (-= 2) * t) ^ 0.5 1) + b;
};

function 
easeInElastic(xtbcd) {
  
1.70158;
  
0;
  
c;
  if (
== 0) return b;
  if ((
/= d) == 1) return c;
  if (!
p.3;
  if (
abs(c)) {
    
c;
    
4;
  } else 
/ (pi) * arcsin(a);
  return -(
^ (10 * (-= 1)) * sin((s) * (pi) / p)) + b;
};

function 
easeOutElastic(xtbcd) {
  
1.70158;
  
0;
  
c;
  if (
== 0) return b;
  if ((
/= d) == 1) return c;
  if (!
p.3;
  if (
abs(c)) {
    
c;
    
4;
  } else 
/ (pi) * arcsin(a);
  return 
^ (-10 t) * sin((s) * (pi) / p) + b;
};

function 
easeInOutElastic(xtbcd) {
  
1.70158;
  
0;
  
c;
  if (
== 0) return b;
  if ((
/= 2) == 2) return c;
  if (!
p* (.3 1.5);
  if (
abs(c)) {
    
c;
    
4;
  } else 
/ (pi) * arcsin(a);
  if (
1) return -.5 * (^ (10 * (-= 1)) * sin((s) * (pi) / p)) + b;
  return 
^ (-10 * (-= 1)) * sin((s) * (pi) / p) * .5 b;
};

function 
easeInBack(xtbcds) {
  if (
s.type() == (- 1)) 1.70158;
  return 
* (/= d) * * ((1) * s) + b;
};

function 
easeOutBack(xtbcds) {
  if (
s.type() == (- 1)) 1.70158;
  return 
* ((1) * * ((1) * s) + 1) + b;
};

function 
easeInOutBack(xtbcds) {
  if (
s.type() == (- 1)) 1.70158;
  if ((
/= 2) < 1) return * (* (((*= (1.525)) + 1) * s)) + b;
  return 
* ((-= 2) * * (((*= (1.525)) + 1) * s) + 2) + b;
};

function 
easeInBounce(xtbcd) {
  return 
this.easeOutBounce(xt0cd) + b;
};

function 
easeOutBounce(xtbcd) {
  if ((
/= d) < (2.75)) {
    return 
* (7.5625 t) + b;
  } else if (
< (2.75)) {
    return 
* (7.5625 * (-= (1.5 2.75)) * .75) + b;
  } else if (
< (2.5 2.75)) {
    return 
* (7.5625 * (-= (2.25 2.75)) * .9375) + b;
  } else {
    return 
* (7.5625 * (-= (2.625 2.75)) * .984375) + b;
  }
};

function 
easeInOutBounce(xtbcd) {
  if (
2) return this.easeInBounce(x20cd) * .5 b;
  return 
this.easeOutBounce(xd0cd) * .5 .5 b;
};

// basic ones
function easeLinear(x) {
  return 
x;
}

function 
easeSwing(x) {
  return (- 
cos(pi) / 2) + 0.5;
}

// helper
function ease(temp.easingxtbcd) {
  return 
this.(@ "ease" temp.easing)(xtbcd);

Hopefully someone finds use out of this .
__________________

Last edited by cbk1994; 05-31-2012 at 09:28 AM.. Reason: links approved
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:09 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.