Skip to content

Commit

Permalink
update for tween system
Browse files Browse the repository at this point in the history
  • Loading branch information
Beeblerox committed Aug 6, 2012
1 parent 503a957 commit 382915d
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 46 deletions.
10 changes: 5 additions & 5 deletions src/org/flixel/FlxBasic.hx
Expand Up @@ -66,7 +66,6 @@ class FlxBasic
alive = true;
ignoreDrawDebug = false;

//autoClear = false;
autoClear = true;
}

Expand All @@ -79,7 +78,7 @@ class FlxBasic
{
if (autoClear && hasTween)
{
clearTweens();
clearTweens(true);
_tween = null;
}
}
Expand Down Expand Up @@ -187,7 +186,7 @@ class FlxBasic
return t;
}

public function removeTween(t:FlxTween):FlxTween
public function removeTween(t:FlxTween, ?destroy:Bool = false):FlxTween
{
var ft:FriendTween = t;
if (ft._parent != this)
Expand All @@ -211,19 +210,20 @@ class FlxBasic
}
ft._next = ft._prev = null;
ft._parent = null;
if (destroy) t.destroy();
t.active = false;
return t;
}

public function clearTweens():Void
public function clearTweens(?destroy:Bool = false):Void
{
var t:FlxTween;
var ft:FriendTween = _tween;
var fn:FriendTween;
while (ft != null)
{
fn = ft._next;
removeTween(cast(ft, FlxTween));
removeTween(cast(ft, FlxTween), destroy);
ft = fn;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/FlxG.hx
Expand Up @@ -1751,7 +1751,7 @@ class FlxG
*/
public static function tween(object:Dynamic, values:Dynamic, duration:Float, ?options:Dynamic = null):MultiVarTween
{
var type:TweenType = TweenType.OneShot,
var type:Int = FlxTween.ONESHOT,
complete:CompleteCallback = null,
ease:EaseFunction = null,
tweener:FlxBasic = FlxG.tweener;
Expand Down
99 changes: 81 additions & 18 deletions src/org/flixel/tweens/FlxTween.hx
Expand Up @@ -3,13 +3,6 @@ package org.flixel.tweens;
import org.flixel.tweens.util.Ease;
import org.flixel.FlxBasic;

enum TweenType
{
Persist;
Looping;
OneShot;
}

typedef CompleteCallback = Void->Void;

/**
Expand All @@ -26,6 +19,31 @@ typedef FriendTween = {

class FlxTween
{
/**
* Persistent Tween type, will stop when it finishes.
*/
public static inline var PERSIST:Int = 1;

/**
* Looping Tween type, will restart immediately when it finishes.
*/
public static inline var LOOPING:Int = 2;

/**
* "To and from" Tween type, will play tween hither and thither
*/
public static inline var TOANDFRO:Int = 4;

/**
* Oneshot Tween type, will stop and remove itself from its core container when it finishes.
*/
public static inline var ONESHOT:Int = 8;

/**
* Backward Tween type, will play tween in reverse direction
*/
public static inline var BACKWARD:Int = 16;

public var active:Bool;
public var complete:CompleteCallback;

Expand All @@ -36,17 +54,30 @@ class FlxTween
* @param complete Optional callback for when the Tween completes.
* @param ease Optional easer function to apply to the Tweened value.
*/
public function new(duration:Float, ?type:TweenType, ?complete:CompleteCallback, ?ease:EaseFunction)
public function new(duration:Float, ?type:Int = 0, ?complete:CompleteCallback, ?ease:EaseFunction)
{
_target = duration;
if (type == null)
if (type == 0)
{
type = FlxTween.PERSIST;
}
else if (type == FlxTween.BACKWARD)
{
type = TweenType.Persist;
type = FlxTween.PERSIST | FlxTween.BACKWARD;
}
_type = type;
this.complete = complete;
_ease = ease;
_t = 0;

_backward = (_type & BACKWARD) > 0;
}

public function destroy():Void
{
complete = null;
_parent = null;
_ease = null;
}

/**
Expand All @@ -56,13 +87,24 @@ class FlxTween
{
_time += FlxG.elapsed;
_t = _time / _target;
if (_ease != null && _t > 0 && _t < 1)
if (_ease != null)
{
_t = _ease(_t);
}
if (_backward)
{
_t = 1 - _t;
}
if (_time >= _target)
{
_t = 1;
if (!_backward)
{
_t = 1;
}
else
{
_t = 0;
}
_finish = true;
}
}
Expand All @@ -80,24 +122,43 @@ class FlxTween
}
active = true;
}

/**
* Immediately stops the Tween and removes it from its Tweener without calling the complete callback.
*/
public function cancel():Void
{
active = false;
if (_parent != null)
{
_parent.removeTween(this);
}
}

/** @private Called when the Tween completes. */
private function finish():Void
{
switch (_type)
switch ((_type & ~ FlxTween.BACKWARD))
{
case Persist:
case FlxTween.PERSIST:
_time = _target;
active = false;
case Looping:
case FlxTween.LOOPING:
_time %= _target;
_t = _time / _target;
if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
start();
case FlxTween.TOANDFRO:
_time %= _target;
_t = _time / _target;
if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
if (_backward) _t = 1 - _t;
_backward = !_backward;
start();
case OneShot:
case FlxTween.ONESHOT:
_time = _target;
active = false;
_parent.removeTween(this);
_parent.removeTween(this, true);
}
_finish = false;
if (complete != null) complete();
Expand All @@ -110,7 +171,7 @@ class FlxTween
public var scale(getScale, null):Float;
private function getScale():Float { return _t; }

private var _type:TweenType;
private var _type:Int;
private var _ease:EaseFunction;
private var _t:Float;

Expand All @@ -121,4 +182,6 @@ class FlxTween
private var _parent:FlxBasic;
private var _prev:FriendTween;
private var _next:FriendTween;

private var _backward:Bool;
}
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/misc/Alarm.hx
Expand Up @@ -13,7 +13,7 @@ class Alarm extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(duration:Float, ?complete:CompleteCallback, type:TweenType)
public function new(duration:Float, ?complete:CompleteCallback, ?type:Int = 0)
{
super(duration, type, complete, null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/misc/AngleTween.hx
Expand Up @@ -19,7 +19,7 @@ class AngleTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
angle = 0;
super(0, type, complete);
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/misc/ColorTween.hx
Expand Up @@ -24,7 +24,7 @@ class ColorTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
alpha = 1;
super(0, type, complete);
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/misc/MultiVarTween.hx
Expand Up @@ -14,7 +14,7 @@ class MultiVarTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_vars = new Array<String>();
_start = new Array<Float>();
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/misc/NumTween.hx
Expand Up @@ -18,7 +18,7 @@ class NumTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
value = 0;
super(0, type, complete);
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/misc/VarTween.hx
Expand Up @@ -13,7 +13,7 @@ class VarTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
super(0, type, complete);
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/motion/CircularMotion.hx
Expand Up @@ -14,7 +14,7 @@ class CircularMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_centerX = _centerY = 0;
_radius = angle = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/motion/CubicMotion.hx
Expand Up @@ -14,7 +14,7 @@ class CubicMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_fromX = _fromY = _toX = _toY = 0;
_aX = _aY = _bX = _bY = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/motion/LinearMotion.hx
Expand Up @@ -14,7 +14,7 @@ class LinearMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_fromX = _fromY = _moveX = _moveY = 0;
_distance = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/motion/LinearPath.hx
Expand Up @@ -14,7 +14,7 @@ class LinearPath extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_points = new Array<Point>();
_pointD = new Array<Float>();
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/motion/Motion.hx
Expand Up @@ -25,7 +25,7 @@ class Motion extends FlxTween
* @param type Tween type.
* @param ease Optional easer function.
*/
public function new(duration:Float, ?complete:CompleteCallback, ?type:TweenType, ?ease:EaseFunction = null)
public function new(duration:Float, ?complete:CompleteCallback, ?type:Int = 0, ?ease:EaseFunction = null)
{
x = y = 0;
super(duration, type, complete, ease);
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/motion/QuadMotion.hx
Expand Up @@ -18,7 +18,7 @@ class QuadMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_distance = -1;
_fromX = _fromY = _toX = _toY = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/motion/QuadPath.hx
Expand Up @@ -15,7 +15,7 @@ class QuadPath extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_points = new Array<Point>();
_curve = new Array<Point>();
Expand Down
2 changes: 1 addition & 1 deletion src/org/flixel/tweens/sound/Fader.hx
Expand Up @@ -14,7 +14,7 @@ class Fader extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(?complete:CompleteCallback, ?type:TweenType)
public function new(?complete:CompleteCallback, ?type:Int = 0)
{
super(0, type, complete);
}
Expand Down
12 changes: 4 additions & 8 deletions src/org/flixel/tweens/sound/SfxFader.hx
@@ -1,5 +1,6 @@
package org.flixel.tweens.sound;

import org.flixel.FlxG;
import org.flixel.FlxSound;
import org.flixel.tweens.FlxTween;
import org.flixel.tweens.util.Ease;
Expand All @@ -15,7 +16,7 @@ class SfxFader extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
public function new(sfx:FlxSound, ?complete:CompleteCallback, type:TweenType)
public function new(sfx:FlxSound, ?complete:CompleteCallback, ?type:Int = 0)
{
super(0, type, finish);
_complete = complete;
Expand Down Expand Up @@ -44,24 +45,19 @@ class SfxFader extends FlxTween
/**
* Fades out the Sfx, while also playing and fading in a replacement Sfx.
* @param play The Sfx to play and fade in.
* @param loop If the new Sfx should loop.
* @param duration Duration of the crossfade.
* @param volume The volume to fade in the new Sfx to.
* @param ease Optional easer function.
*/
public function crossFade(play:FlxSound, loop:Bool, duration:Float, ?volume:Float = 1, ?ease:EaseFunction = null):Void
public function crossFade(play:FlxSound, duration:Float, ?volume:Float = 1, ?ease:EaseFunction = null):Void
{
_crossSfx = play;
_crossRange = volume;
_start = _sfx.volume;
_range = -_start;
_target = duration;
_ease = ease;
if (loop)
{
_crossSfx.loop(0);
}
else _crossSfx.play(0);
_crossSfx.play(true);
start();
}

Expand Down

0 comments on commit 382915d

Please sign in to comment.