Skip to content

Instantly share code, notes, and snippets.

@jpsarda
Last active December 23, 2015 06:29
Show Gist options
  • Save jpsarda/6594716 to your computer and use it in GitHub Desktop.
Save jpsarda/6594716 to your computer and use it in GitHub Desktop.
A shake action for Futile 2G Game engine for Unity3D.
/*
Usage :
ShakeUtil.Go(myFNode,myDurationInSeconds,myAmplitudeInPixels);
*/
public class ShakeUtil {
static Dictionary<FNode, ShakeUtil> _pendings = new Dictionary<FNode, ShakeUtil>();
public Vector2 oPosition;
public float duration,amplitude;
public FNode node=null;
public float curDuration,curAmplitude;
public ShakeUtil () {
}
public void go(FNode node_,float duration_,float amplitude_) {
Cancel(node_);
oPosition=node_.GetPosition();
curDuration=duration=duration_;
curAmplitude=amplitude=amplitude_;
_pendings.Add(node_,this);
if (node==null) {
Futile.instance.SignalUpdate+=HandleUpdate;
}
node=node_;
}
protected void HandleUpdate() {
curDuration-=Time.deltaTime;
if (curDuration<0) {
Stop();
} else {
curAmplitude=amplitude*curDuration/duration;
node.x=oPosition.x+RXRandom.Range(-curAmplitude,curAmplitude);
node.y=oPosition.y+RXRandom.Range(-curAmplitude,curAmplitude);
}
}
protected void Stop() {
if (node!=null) {
Futile.instance.SignalUpdate-=HandleUpdate;
node.SetPosition(oPosition);
_pendings.Remove(node);
node=null;
}
}
static public void Go(FNode node_,float duration_,float amplitude_) {
(new ShakeUtil()).go (node_,duration_,amplitude_);
}
static public void Cancel(FNode node) {
ShakeUtil obj=null;
_pendings.TryGetValue(node, out obj);
if (obj!=null) {
obj.Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment