Skip to content

Commit

Permalink
Add "flipX / flipY" property to animations, make it interact properly…
Browse files Browse the repository at this point in the history
… with FlxSprite's own flipX/flipY
  • Loading branch information
larsiusprime committed Dec 4, 2015
1 parent 2cff1f0 commit 916475f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
33 changes: 29 additions & 4 deletions flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ class FlxSprite extends FlxObject
}
else
{
_frame.prepareMatrix(_matrix, FlxFrameAngle.ANGLE_0, flipX, flipY);
_frame.prepareMatrix(_matrix, FlxFrameAngle.ANGLE_0, checkFlipX(), checkFlipY());
_matrix.translate( -origin.x, -origin.y);
_matrix.scale(scale.x, scale.y);

Expand Down Expand Up @@ -934,15 +934,16 @@ class FlxSprite extends FlxObject
}
#end

var doFlipX = flipX != _frame.flipX;
var doFlipY = flipY != _frame.flipY;
var doFlipX:Bool = checkFlipX();
var doFlipY:Bool = checkFlipY();

if (!doFlipX && !doFlipY && _frame.type == FlxFrameType.REGULAR)
{
framePixels = _frame.paint(framePixels, _flashPointZero, false, true);
}
else
{
framePixels = _frame.paintRotatedAndFlipped(framePixels, _flashPointZero, FlxFrameAngle.ANGLE_0, flipX, flipY, false, true);
framePixels = _frame.paintRotatedAndFlipped(framePixels, _flashPointZero, FlxFrameAngle.ANGLE_0, checkFlipX(), checkFlipY(), false, true);
}

if (useColorTransform)
Expand Down Expand Up @@ -1357,6 +1358,30 @@ class FlxSprite extends FlxObject
{
return antialiasing = value;
}

private inline function checkFlipX():Bool
{
var doFlipX:Bool = FlxMath.boolXOR(flipX, _frame.flipX);

if (animation.curAnim != null)
{
doFlipX = FlxMath.boolXOR(flipX, animation.curAnim.flipX);
}

return doFlipX;
}

private inline function checkFlipY():Bool
{
var doFlipY:Bool = FlxMath.boolXOR(flipY, _frame.flipY);

if (animation.curAnim != null)
{
doFlipY = FlxMath.boolXOR(flipY, animation.curAnim.flipY);
}

return doFlipY;
}
}

interface IFlxSprite extends IFlxBasic
Expand Down
18 changes: 16 additions & 2 deletions flixel/animation/FlxAnimation.hx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ class FlxAnimation extends FlxBaseAnimation
*/
public var reversed(default, null):Bool = false;

/**
* Whether or not the frames of this animation are horizontally flipped
*/
public var flipX(default, null):Bool = false;

/**
* Whether or not the frames of this animation are vertically flipped
*/
public var flipY(default, null):Bool = false;

/**
* A list of frames stored as int objects
*/
Expand All @@ -64,14 +74,18 @@ class FlxAnimation extends FlxBaseAnimation
* @param Frames An array of numbers indicating what frames to play in what order (e.g. 1, 2, 3)
* @param FrameRate The speed in frames per second that the animation should play at (e.g. 40)
* @param Looped Whether or not the animation is looped or just plays once
* @param FlipX Whether or not the frames of this animation are horizontally flipped
* @param FlipY Whether or not the frames of this animation are vertically flipped
*/
public function new(Parent:FlxAnimationController, Name:String, Frames:Array<Int>, FrameRate:Int = 0, Looped:Bool = true)
public function new(Parent:FlxAnimationController, Name:String, Frames:Array<Int>, FrameRate:Int = 0, Looped:Bool = true, FlipX:Bool = false, FlipY:Bool = false)
{
super(Parent, Name);

frameRate = FrameRate;
_frames = Frames;
looped = Looped;
flipX = FlipX;
flipY = FlipY;
}

/**
Expand Down Expand Up @@ -212,7 +226,7 @@ class FlxAnimation extends FlxBaseAnimation

override public function clone(Parent:FlxAnimationController):FlxAnimation
{
return new FlxAnimation(Parent, name, _frames, frameRate, looped);
return new FlxAnimation(Parent, name, _frames, frameRate, looped, flipX, flipY);
}

private function set_frameRate(value:Int):Int
Expand Down
20 changes: 17 additions & 3 deletions flixel/animation/FlxAnimationController.hx
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ class FlxAnimationController implements IFlxDestroyable
* @param Frames An array of numbers indicating what frames to play in what order (e.g. 1, 2, 3).
* @param FrameRate The speed in frames per second that the animation should play at (e.g. 40 fps).
* @param Looped Whether or not the animation is looped or just plays once.
* @param FlipX Whether the frames should be horizontally flipped
* @param FlipY Whether the frames should be vertically flipped
*/
public function add(Name:String, Frames:Array<Int>, FrameRate:Int = 30, Looped:Bool = true):Void
public function add(Name:String, Frames:Array<Int>, FrameRate:Int = 30, Looped:Bool = true, FlipX:Bool = false, FlipY:Bool = false):Void
{
// Check _animations frames
var framesToAdd:Array<Int> = Frames;
Expand Down Expand Up @@ -255,8 +257,10 @@ class FlxAnimationController implements IFlxDestroyable
* @param FrameNames An array of image names from atlas indicating what frames to play in what order.
* @param FrameRate The speed in frames per second that the animation should play at (e.g. 40 fps).
* @param Looped Whether or not the animation is looped or just plays once.
* @param FlipX Whether the frames should be horizontally flipped
* @param FlipY Whether the frames should be vertically flipped
*/
public function addByNames(Name:String, FrameNames:Array<String>, FrameRate:Int = 30, Looped:Bool = true):Void
public function addByNames(Name:String, FrameNames:Array<String>, FrameRate:Int = 30, Looped:Bool = true, FlipX:Bool = false, FlipY:Bool = false):Void
{
if (_sprite.frames != null)
{
Expand All @@ -265,7 +269,7 @@ class FlxAnimationController implements IFlxDestroyable

if (indices.length > 0)
{
var anim = new FlxAnimation(this, Name, indices, FrameRate, Looped);
var anim = new FlxAnimation(this, Name, indices, FrameRate, Looped, FlipX, FlipY);
_animations.set(Name, anim);
}
}
Expand Down Expand Up @@ -502,12 +506,22 @@ class FlxAnimationController implements IFlxDestroyable
return;
}

var oldFlipX:Bool = false;
var oldFlipY:Bool = false;

if (_curAnim != null && AnimName != _curAnim.name)
{
oldFlipX = _curAnim.flipX;
oldFlipY = _curAnim.flipY;
_curAnim.stop();
}
_curAnim = _animations.get(AnimName);
_curAnim.play(Force, Reversed, Frame);

if (oldFlipX != _curAnim.flipX || oldFlipY != _curAnim.flipY)
{
_sprite.dirty = true;
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions flixel/math/FlxMath.hx
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,15 @@ class FlxMath
{
return (a > 0) ? a : -a;
}

/**
* Returns the result of a "boolean exclusive or" comparison
* @param a
* @param b
* @return
*/
public static inline function boolXOR(a:Bool, b:Bool):Bool
{
return (a && !b) || (b && !a);
}
}

0 comments on commit 916475f

Please sign in to comment.