package com.gebauz.Bauzoid.graphics.sprite;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.graphics.Graphics;
/** Animated Sprite class using the features of AtlasSprite. */
public class AnimatedSprite
extends AtlasSprite
{
// Scale factors
public float scaleX = 1.0f
;
public float scaleY = 1.0f
;
public static abstract class AnimationListener
{
public abstract void onAnimationStarted
(AnimatedSprite sprite,
int anim
);
public abstract void onAnimationFinished
(AnimatedSprite sprite,
int anim
);
}
private Animation
[] mAnimations =
null;
private int mCurrentAnimation = -
1;
private AnimationListener mAnimationListener =
null;
/** Constructor. */
public AnimatedSprite
(Graphics graphics, Texture texture, SpriteRegion
[] regions
)
{
super(graphics, texture, regions
);
}
/** Destructor. */
public void dispose
()
{
super.
dispose();
if (mAnimations
!=
null)
{
for (int i =
0; i
< mAnimations.
length; i++
)
{
mAnimations
[i
].
dispose();
mAnimations
[i
] =
null;
}
mAnimations =
null;
}
mAnimationListener =
null;
}
/** Play an animation. */
public void playAnimation
(int anim
)
{
if ((mAnimations ==
null) ||
(anim
>= mAnimations.
length))
return;
mCurrentAnimation = anim
;
mAnimations
[mCurrentAnimation
].
start();
if (mAnimationListener
!=
null)
mAnimationListener.
onAnimationStarted(this, anim
);
}
/** Update animated sprite. */
public void update
(float deltaTime
)
{
if ((mAnimations ==
null) ||
(mCurrentAnimation == -
1) ||
(mCurrentAnimation
>= mAnimations.
length))
return;
Animation anim = mAnimations
[mCurrentAnimation
];
boolean alreadyFinished = anim.
isFinished();
anim.
update(deltaTime
);
if ((mAnimationListener
!=
null) && !alreadyFinished
&& anim.
isFinished())
mAnimationListener.
onAnimationFinished(this, mCurrentAnimation
);
}
/** Render the animated sprite. */
public void render
()
{
if ((mAnimations ==
null) ||
(mCurrentAnimation == -
1) ||
(mCurrentAnimation
>= mAnimations.
length))
return;
AnimationFrame frame = mAnimations
[mCurrentAnimation
].
getCurrentFrame();
float prevAngle = angle
;
w = frame.
w * scaleX
;
h = frame.
h * scaleY
;
pivotX = frame.
pivotX * scaleX
;
pivotY = frame.
pivotY * scaleY
;
mirrorX = frame.
mirrorX;
mirrorY = frame.
mirrorY;
angle = angle + frame.
angle;
super.
render(frame.
getRegionIndex(), x + frame.
x, y + frame.
y);
angle = prevAngle
;
}
/** Get currently playing animation. */
public final Animation getCurrentAnimation
()
{
return mAnimations
[mCurrentAnimation
];
}
/** Set an animation listener. */
// TODO: maybe allow multiple?
public void setAnimationListener
(AnimationListener listener
)
{
mAnimationListener = listener
;
}
/** Set animations. */
public final void setAnimations
(Animation
[] anims
)
{
mAnimations = anims
;
}
}