package game.scenes;
import engine.geometry.Rectangle;
import engine.hierarchy.DefaultScene;
import engine.hierarchy.Scene;
import engine.classes.Colour;
import engine.interfaces.RenderTarget;
public final class FadeThroughBlackScene extends DefaultScene {
private static final Rectangle SCREEN = new Rectangle(0, 0, 640, 480);
private static final int SPEED = 15;
private int alpha;
private boolean fadeIn;
private Scene toScene;
public FadeThroughBlackScene(final Scene toScene) {
this.toScene = toScene;
fadeIn = false;
alpha = 0;
}
@Override
protected void onAdd() {
getStage().getSceneBelow(this).setActive(false);
}
public void onAfterMove() {
if (!fadeIn) {
if (alpha + SPEED < 256) {
// Fading out
alpha += SPEED;
} else {
// Halfway
alpha = 255;
fadeIn = true;
getStage().removeSceneBelow(this);
getStage().insertSceneBelow(toScene, this).setActive(false);
}
} else {
if (alpha - SPEED > 0) {
// Fading in
alpha -= SPEED;
} else {
// Done
alpha = 0;
getStage().getSceneBelow(this).setActive(true);
getStage().removeScene(this);
}
}
}
@Override
public void onDraw(final RenderTarget target) {
target.transformPush();
target.transformIdentity();
target.fillRectangle(SCREEN, new Colour(0, 0, 0, alpha));
target.transformPop();
}
}